在Sphinx中,如果我有以下标题声明:
.. _somestuff:
``this is code``, this is not!
==============================
Run Code Online (Sandbox Code Playgroud)
它渲染,像这样:
this is code, 这不是!哪个好,但是,如果我使用参考,例如:
Have a look at :ref:`somestuff`
Run Code Online (Sandbox Code Playgroud)
它丢失了代码格式并呈现如下:
代替:
是否可以在引用中保留代码格式?我该怎么办呢?
documentation restructuredtext documentation-generation python-sphinx
我知道我可以打开多个文件,比如
with open('a', 'rb') as a, open('b', 'rb') as b:
Run Code Online (Sandbox Code Playgroud)
但我有一个情况,我有一个文件列表打开,我想知道当文件数量提前未知时,首选方法是做什么的.就像是,
with [ open(f, 'rb') for f in files ] as fs:
Run Code Online (Sandbox Code Playgroud)
(但是AttributeError因为列表没有实现而失败了__exit__)
我不介意使用像
try:
fs = [ open(f, 'rb') for f in files ]
....
finally:
for f in fs:
f.close()
Run Code Online (Sandbox Code Playgroud)
但是我不确定如果在尝试打开它们时抛出一些文件会发生什么.是否会fs在块中正确定义管理打开的文件finally?
在下面,setattr第一次调用成功,但在第二次调用失败,有:
AttributeError: 'method' object has no attribute 'i'
Run Code Online (Sandbox Code Playgroud)
为什么会这样,有没有办法在方法上设置属性,使其只存在于一个实例上,而不是存在于每个类的实例中?
class c:
def m(self):
print(type(c.m))
setattr(c.m, 'i', 0)
print(type(self.m))
setattr(self.m, 'i', 0)
Run Code Online (Sandbox Code Playgroud)
Python 3.2.2
我正在尝试将Django项目安装到我的OSX机器上,这需要PyCrypto.我收到以下错误:
running install
running build
running build_py
running build_ext
running build_configure
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/Users/home/Documents/tmp/dlitz-pycrypto-d2170a4':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details
Traceback (most recent call last):
File "setup.py", line 486, in <module>
core.setup(**kw)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/core.py", line 152, in setup
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/dist.py", line 953, in run_commands
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/dist.py", line 972, in run_command
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/command/install.py", line 573, in run
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/cmd.py", line …Run Code Online (Sandbox Code Playgroud) 我有一个类有一些相当大的方法.在它的基本和最常见的状态中,大多数功能都不是必需的,所以我想知道是否有一种延迟加载只是部分类的方法.该方法需要能够访问私有/受保护的成员,使这将是理想的,如果该方法是原产于类,但是在寻找其他的解决方案,我碰上了这其中讨论在回调中使用私有成员这将是一个可行的解决方案(我将使用包含调用回调和延迟加载类的函数的单独类.那是2009年,以及这个功能是否已经在PHP的更高版本中删除了我不知道,但它似乎不适用于5.3.5
有没有办法做到这一点,或者你对我应该看的其他模式有什么建议吗?
谢谢.
哇!感谢所有的答案.我认为你们中有很多人认为这是一个可能的过早优化,或者更糟糕的是,根本不是优化是非常有效的,我将进行分析以检查我所解决的任何解决方案实际上是否有助于不伤害....现在正确阅读和消化你的所有想法.再次感谢.
使用真正的闭包,我们可以做到,
function foo(&$ref)
{
$inFn = function() use (&$ref)
{
$ref = 42;
};
$inFn();
}
Run Code Online (Sandbox Code Playgroud)
因此修改引用而不必在调用中传递它$inFn.
如果我们更换,
$inFn = function(...
Run Code Online (Sandbox Code Playgroud)
同
$inFn = create_function(...
Run Code Online (Sandbox Code Playgroud)
是否有任何(简单而干净)的方式来做同样的事情; 通过引用引用包含范围的变量而不明确地将其传递给$inFn?
任何人都可以解释为什么我收到以下错误?
在代码中,如果echo $gz;被注释掉,我没有收到任何错误(但也没有输出!),如果不是我得到的(来自Firefox),
内容编码错误
您尝试查看的页面无法显示,因为它使用无效或不受支持的压缩形式.
感谢您的帮助,以下是代码:
ob_start('ob_gzhandler') OR ob_start();
echo 'eh?';
$gz = ob_get_clean();
echo $gz;
Run Code Online (Sandbox Code Playgroud) 在下面的程序中,当我将进程附加到列表(看似毫无意义的事情)时,它会按预期运行.但是如果我删除了追加,那么进程析构函数在它运行之前会被多次调用.只有n结构,但(n)(n+1)/2(n进程数)在哪里析构.这使我相信每个进程都被复制到每个新进程中,然后立即被销毁.也许这就是多处理模块的工作原理.这是有道理的,因为每个进程都是当前进程的一个分支.但是,附加到列表的重要性是什么?为什么简单地这样做会阻止这种行为?
这是测试和示例输出:
import multiprocessing
class _ProcSTOP:
pass
class Proc(multiprocessing.Process):
def __init__(self, q, s):
s.i += 1
self._i = s.i
self._q = q
print('constructing:', s.i)
super().__init__()
def run(self):
dat = self._q.get()
while not dat is _ProcSTOP:
self._q.task_done()
dat = self._q.get()
self._q.task_done()
print('returning: ', self._i)
def __del__(self):
print('destroying: ', self._i)
if __name__ == '__main__':
q = multiprocessing.JoinableQueue()
s = multiprocessing.Manager().Namespace()
s.i = 0
pool = []
for i in range(4):
p = Proc(q, s)
p.start()
pool.append(p) …Run Code Online (Sandbox Code Playgroud) 我正在尝试转换迭代器类,我必须与stl兼容,以便它可以与stl算法一起使用.在下面的简单(和坦率无用)示例中,应该打印0到5的值,我收到以下错误,
ISO C++禁止递增类型'Iterator(*)()'的指针
和,
从'Iterator(*)()'到'int'的无效转换
我究竟做错了什么?
谢谢.
#include <iterator>
#include <algorithm>
#include <iostream>
class Iterator : public std::iterator<std::bidirectional_iterator_tag, int> {
public:
Iterator(int i = 0) : val(i) {
if(val<0 || val>5) throw;
}
bool operator==(Iterator const& rhs) const {
return (val==rhs.val);
}
bool operator!=(Iterator const& rhs) const {
return !(*this==rhs);
}
Iterator& operator++() {
if(val!=6)
++val;
return *this;
}
Iterator operator++(int) {
Iterator tmp (*this);
++(*this);
return tmp;
}
Iterator& operator--() {
if(val!=-1)
--val;
return *this;
}
Iterator operator--(int) { …Run Code Online (Sandbox Code Playgroud)