我想在Python文档字符串的其他地方引用先前记录的函数参数.考虑以下(当然是完全人为的)示例:
def foo(bar):
"""Perform foo action
:param bar: The bar parameter
"""
def nested():
"""Some nested function that depends on enclosing scope's bar parameter.
I'd like to reference function foo's bar parameter here
with a link, is that possible?"""
return bar * bar
# ...
return nested()
Run Code Online (Sandbox Code Playgroud)
是否有一种使用Sphinx标记嵌入参数引用的简单方法,还是会自动发生?
(我是一个完整的Sphinx新手.我一直在扫描Sphinx文档,但没有找到这个问题的答案,或者是一个展示正确标记的例子.)
在Python 2(2.7,更准确地说)中,我想以递减计数顺序迭代collections.Counter实例.
>>> import collections
>>> c = collections.Counter()
>>> c['a'] = 1
>>> c['b'] = 999
>>> c
Counter({'b': 999, 'a': 1})
>>> for x in c:
print x
a
b
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,似乎元素按照它们添加到Counter实例的顺序进行迭代.
我想从最高到最低迭代列表.我看到Counter的字符串表示就是这样,只是想知道是否有推荐的方法来做到这一点.
我想检测Jinja2模板块内容是否为空.像这样的东西:
{% block foo %}{% endblock foo %}{% if foo %} - {% endif %}Blah Blah Blah
Run Code Online (Sandbox Code Playgroud)
我想要的是块定义本身之外的条件文本.在设计的示例中,我希望能够在块-
之后插入条件字符串,当且仅当块被覆盖且不为空时.
这可能吗?
在一个旨在从shell运行的简单Python脚本中,我是否可以可靠地确定sys.stdin是否已从实际文件重定向而不是从另一个进程传输?
我想根据stdin是来自数据文件还是来自另一个进程通过管道流来改变运行时行为.
正如预期的那样,isatty()
在两种情况下都返回False.这是一个快速isatty()
测试:
# test.py
import os
import sys
print sys.stdin.isatty()
print os.isatty(sys.stdin.fileno())
Run Code Online (Sandbox Code Playgroud)
测试:
python test.py < file.txt
Run Code Online (Sandbox Code Playgroud)
生产:
False
False
Run Code Online (Sandbox Code Playgroud)
和:
ls -al | python test.py
Run Code Online (Sandbox Code Playgroud)
生产:
False
False
Run Code Online (Sandbox Code Playgroud)
有这样做的pythonic方法吗?
Unix/Linux特定的很好,但知道是否可以以可移植的方式执行此操作会很好.
编辑:请注意评论者:我为什么关心?好吧,在我的情况下,我想处理从另一个进程传输时不规则间隔的时间戳数据; 当我从文件中播放预先录制的数据时,我想使用固定或可变延迟重放它.
我同意使用更干净的方法可能是有利的(我可以想到几个,包括在回放流中插入延迟的中间脚本),但我最终很好奇.
我想解析一个包含以逗号分隔的整数列表的必需的位置参数.如果第一个整数包含一个前导减号(' - '),argparse会抱怨:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('positional')
parser.add_argument('-t', '--test', action='store_true')
opts = parser.parse_args()
print opts
$ python example.py --test 1,2,3,4
Namespace(positional='1,2,3,4', test=True)
$ python example.py --test -1,2,3,4
usage: example.py [-h] [-t] positional
example.py: error: too few arguments
$ python example.py --test "-1,2,3,4"
usage: example.py [-h] [-t] positional
example.py: error: too few arguments
Run Code Online (Sandbox Code Playgroud)
我见过人们建议除了-
作为旗帜角色之外还使用其他一些角色,但我宁愿不这样做.是否有另一种方法来配置argparse以允许两个--test
和-1,2,3,4
作为有效参数?
使用格式字符串+ args列表与格式内联调用日志记录函数是否有利?
我已经看过(并写过)使用内联字符串格式的日志代码:
logging.warn("%s %s %s" % (arg1, arg2, arg3))
Run Code Online (Sandbox Code Playgroud)
但我认为使用它更好(性能方面,更具惯用性):
logging.warn("%s %s %s", arg1, arg2, arg3)
Run Code Online (Sandbox Code Playgroud)
因为第二种形式在调用日志记录功能之前避免了字符串格式化操作.如果当前日志记录级别将过滤掉日志消息,则不需要格式化,从而减少了计算时间和内存分配.
我在这里是正确的轨道,还是我错过了什么?
我想在非Django项目中使用类似Django信号的东西.我以为我过去曾经见过像这样的图书馆,但我一直无法通过常规搜索找到一个.
Python的信号库不提供相同的功能.
我正在使用此答案中显示的技术将网页的选择扩展到单词边界:
function snapSelectionToWord() {
var sel;
// Check for existence of window.getSelection() and that it has a
// modify() method. IE 9 has both selection APIs but no modify() method.
if (window.getSelection && (sel = window.getSelection()).modify) {
sel = window.getSelection();
if (!sel.isCollapsed) {
// Detect if selection is backwards
var range = document.createRange();
range.setStart(sel.anchorNode, sel.anchorOffset);
range.setEnd(sel.focusNode, sel.focusOffset);
var backwards = range.collapsed;
range.detach();
// modify() works on the focus of the selection
var endNode = sel.focusNode, endOffset = sel.focusOffset; …
Run Code Online (Sandbox Code Playgroud) 这里有一个关于实例变量的新手Python问题.
考虑以下Python 2.7类定义:
class Foo(object):
a = 1
def __init__(self):
self.b = 2
def __repr__(self):
return "%s" % self.__dict__
Run Code Online (Sandbox Code Playgroud)
现在,当我创建一个实例Foo
,Foo.__dict__
包含b
但不是a
.
>>> x=Foo()
>>> x
{'b': 2}
>>> dir(x)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__',
'__getattribute__', '__hash__', '__init__', '__module__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__',
'__str__', '__subclasshook__', '__weakref__', 'a', 'b']
>>> x.__dict__
{'b': 2}
Run Code Online (Sandbox Code Playgroud)
在这里,我认为我对Python的方式有了很好的把握.
x.a
和之间有什么区别x.b
?据我所知,它们都是实例变量.
编辑:好的,重新阅读Python文档,我看到这Foo.a
是一个类属性而不是实例变量.嗯...我想混淆来自于我可以为其分配一个新值,x.a
而新值只影响x
实例 - 我想我现在正在Foo.a …