Python2.7 argparse只接受互斥组中的可选参数(前缀):
parser = argparse.ArgumentParser(prog='mydaemon')
action = parser.add_mutually_exclusive_group(required=True)
action.add_argument('--start', action='store_true', help='Starts %(prog)s daemon')
action.add_argument('--stop', action='store_true', help='Stops %(prog)s daemon')
action.add_argument('--restart', action='store_true', help='Restarts %(prog)s daemon')
Run Code Online (Sandbox Code Playgroud)
$ mydaemon -h
usage: mydaemon [-h] (--start | --stop | --restart)
optional arguments:
-h, --help show this help message and exit
--start Starts mydaemon daemon
--stop Stops mydaemon daemon
--restart Restarts mydaemon daemon
Run Code Online (Sandbox Code Playgroud)
有没有办法让argparse参数表现得像传统的unix守护进程控件:
(start | stop | restart) and not (--start | --stop | --restart) ?
Run Code Online (Sandbox Code Playgroud) 我发现Python的断言语句是捕获永远不会发生的情况的好方法.当代码被认为是正确的时,可以通过Python优化删除它.
它似乎是在调试模式下运行Python应用程序的完美机制.但是看看django,twisted和zope等几个Python项目,assert几乎从未使用过.那么,为什么会这样呢?
为什么在Python社区中不经常使用断言语句?
Python 3有float('inf'),Decimal('Infinity')但没有int('inf').那么,为什么语言中缺少表示无限整数集的数字?是int('inf')不合理的?
有没有人成功使用过ZmqSocket.js?我想知道它如何用于在浏览器和zeromq服务器应用程序之间建立安全通道.这种用例还有其他/更好的选择吗?
Python 文档声明uuid1使用当前时间来形成uuid值.但我找不到确保UUID1顺序的引用.
>>> import uuid
>>> u1 = uuid.uuid1()
>>> u2 = uuid.uuid1()
>>> u1 < u2
True
>>>
Run Code Online (Sandbox Code Playgroud) 即使在读取SQLite限制之后,我也找不到SQLite数据库文件可以容纳的最大表数.所以,我想知道是否
我想将微秒分辨率的日期时间保存为时间戳。但似乎 Python 3 日期时间模块在加载它们时丢失了一微秒。为了测试这一点,让我们创建一个脚本:
test_datetime.py:
from random import randint
from datetime import datetime
now = datetime.now()
for n in range(1000):
d = datetime(year=now.year, month=now.month, day=now.day,
hour=now.hour, minute=now.minute, second=now.second,
microsecond=randint(0,999999))
ts = d.timestamp()
d2 = datetime.fromtimestamp(ts)
assert d == d2, 'failed in pass {}: {} != {}'.format(n, d, d2)
Run Code Online (Sandbox Code Playgroud)
python3 test_datetime.py总是会失败一微秒:
Traceback (most recent call last):
File "test_datetime.py", line 14, in <module>
assert d == d2, 'failed in pass {}: {} != {}'.format(n, d, d2)
AssertionError: failed in pass …Run Code Online (Sandbox Code Playgroud) 如果我用pyzmq连接到一个不存在的套接字,我需要按CTRL_C来停止该程序.有人可以解释为什么会这样吗?
import zmq
INVALID_ADDR = 'ipc:///tmp/idontexist.socket'
context = zmq.Context()
socket = context.socket(zmq.REQ)
socket.connect(INVALID_ADDR)
socket.send('hello')
poller = zmq.Poller()
poller.register(socket, zmq.POLLIN)
conn = dict(poller.poll(1000))
if conn:
if conn.get(socket) == zmq.POLLIN:
print "got result: ", socket.recv(zmq.NOBLOCK)
else:
print 'got no result'
Run Code Online (Sandbox Code Playgroud)