怎么if __name__ == "__main__":办?
# Threading example
import time, thread
def myfunction(string, sleeptime, lock, *args):
while True:
lock.acquire()
time.sleep(sleeptime)
lock.release()
time.sleep(sleeptime)
if __name__ == "__main__":
lock = thread.allocate_lock()
thread.start_new_thread(myfunction, ("Thread #: 1", 2, lock))
thread.start_new_thread(myfunction, ("Thread #: 2", 2, lock))
Run Code Online (Sandbox Code Playgroud) 为了交互式地测试我的python脚本,我想创建一个Namespace对象,类似于返回的对象argparse.parse_args().显而易见的方式,
>>> import argparse
>>> parser = argparse.ArgumentParser()
>>> parser.parse_args()
Namespace()
>>> parser.parse_args("-a")
usage: [-h]
: error: unrecognized arguments: - a
Process Python exited abnormally with code 2
Run Code Online (Sandbox Code Playgroud)
可能导致Python repl退出(如上所述)一个愚蠢的错误.
那么,使用给定属性集创建Python命名空间的最简单方法是什么?
例如,我可以动态创建dict(dict([("a",1),("b","c")]))但我不能将其用作Namespace:
AttributeError: 'dict' object has no attribute 'a'
Run Code Online (Sandbox Code Playgroud) 我正在构建一个Python包,并使用Sphinx来创建文档.除了我的包代码,我还包括很多使用argparse的命令行Python脚本.我想知道是否有办法让Sphinx自动记录这些脚本?最终目标将是一个漂亮的脚本列表,以及相关的帮助打印,参数和选项.要清楚,我正在寻找一种预先存在的方法来做到这一点,而不是自己实现这一点的方法.
这并不像我通常所问的那样具体,如果有更合适的SE网站发布这个问题,请告诉我.谢谢.
我正在尝试在sphinx中为我的项目编写文档,每当sphinx在我的模块中遇到OptionParser时,它就会给我:
sphinx-build: error: no such option: -b
Run Code Online (Sandbox Code Playgroud)
我认为这是不可能的,所以我写了一个简单的模块来检查:
from optparse import OptionParser
"""some comment here"""
parser = OptionParser(conflict_handler='resolve')
parser.add_option('', '--force', action='store_true', dest='force', default=False, help='gqdel will skip asking questions, and delete them all.');
parser.add_option('', '--verbose', action='store_true', dest='verbose', default=False, help='Report additional information from gqdel')
(options, args) = parser.parse_args()
"""and here"""
print "foo"
Run Code Online (Sandbox Code Playgroud)
它给了我同样的错误.我的第一个看起来像这样:
some title
==========
.. automodule:: test
:members:
Run Code Online (Sandbox Code Playgroud)