Python的setuptool有两种方法可以将命令行脚本添加到Python包中:script和entry_point.
本教程概述了以下方法:
scripts将Python脚本(funniest-joke)添加到包树,并将其路径添加到setup.py:
setup(
...
scripts=['bin/funniest-joke'],
...
)
Run Code Online (Sandbox Code Playgroud)
将Python脚本(funniest-joke)添加到包树.添加一个main()函数,并添加command_line.py运行最有趣的子模块main():
command_line.py:import funniest
def main():
print funniest.joke()
Run Code Online (Sandbox Code Playgroud)
setup.pysetup(
...
entry_points = {
'console_scripts': ['funniest-joke=funniest.command_line:main'],
}
...
)
Run Code Online (Sandbox Code Playgroud)
每种方法有哪些优缺点?
我有一个烧瓶应用程序,调用期望JSON有效负载.在处理每个调用之前,我有一个两步错误检查过程:
以下列方式实现:
@app.route('/activate', methods=['POST'])
def activate():
request_id = request.__hash__()
# Assert that the payload is a valid JSON
try:
input = request.json
except BadRequest, e:
msg = "payload must be a valid json"
return jsonify({"error": msg}), 400
# JSON Schema Validation
try:
validate(request.json, app.config['activate_schema'])
except ValidationError, e:
return jsonify({"error": e.message}), 400
Run Code Online (Sandbox Code Playgroud)
由于此代码在许多调用中都是重复的,我想知道如果我可以优雅地将它移动到装饰器,那么形式为:
@validate_json
@validate_schema(schema=app.config['activate_schema'])
@app.route('/activate', methods=['POST'])
def activate():
....
Run Code Online (Sandbox Code Playgroud)
问题是request参数是隐式的:我可以在函数中引用它,但它不是它的参数.因此,我不知道如何在装饰器中使用它.
如何使用Python装饰器实现验证检查?
我正在尝试将一个timedelta对象与另一个对象分开以计算服务器正常运行时间:
>>> import datetime
>>> installation_date=datetime.datetime(2010,8,01)
>>> down_time=datetime.timedelta(seconds=1400)
>>> server_life_period=datetime.datetime.now()-installation_date
>>> down_time_percentage=down_time/server_life_period
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for /: 'datetime.timedelta'
and 'datetime.timedelta'
Run Code Online (Sandbox Code Playgroud)
我知道这已经在Python 3.2中得到了解决,但除了计算微秒,秒和天数以及除以?之外,还有一种方便的方法可以在Python的早期版本中处理它吗?
谢谢,
亚当
以下AWK格式:
/REGEX/ {Action}
Run Code Online (Sandbox Code Playgroud)
将Action在当前行匹配时执行REGEX.
有没有办法添加一个else子句,如果当前行与正则表达式不匹配将执行,而不使用if-then-else显式,如下所示:
/REGEX/ {Action-if-matches} {Action-if-does-not-match}
Run Code Online (Sandbox Code Playgroud) 我cURL用来测试一些RESTful API.其中一些API由Apache机器提供,并使用简单.httaccess文件通过用户/密码组合进行保护.
有没有办法为cURL提供用户名/密码组合作为参数?
当我git diff在OSX命令行上运行时,输出显示在一个less或vim接口内.界面让我可以上下滚动,然后使用q键退出.
这非常烦人,特别是当没有diff和git打开一个空白屏幕时.
我可以在不进入交互模式的情况下在屏幕上编写diff(颜色)输出吗?
我创建了一个virtualenv并在其中安装了SQLAlchemy:
$ virtualenv alchemy
$ source alchemy/bin/activate
$ pip install sqlalchemy
Run Code Online (Sandbox Code Playgroud)
import 在python中工作:
$ python
Python 2.7.5 (default, Mar 9 2014, 22:15:05)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlalchemy
>>> print sqlalchemy.__version__
0.9.7
Run Code Online (Sandbox Code Playgroud)
>>> import sqlalchemy
Traceback (most recent call last):
File "<input>", line 1, in <module>
ImportError: No module named sqlalchemy
Run Code Online (Sandbox Code Playgroud)
为什么bpython找不到virtualenv中安装的软件包,即使它source alchemy/bin/activate被调用后执行?
有没有办法忽略tage名称中的XML命名空间elementtree.ElementTree?
我尝试打印所有technicalContact标签:
for item in root.getiterator(tag='{http://www.example.com}technicalContact'):
print item.tag, item.text
Run Code Online (Sandbox Code Playgroud)
我得到类似的东西:
{http://www.example.com}technicalContact blah@example.com
Run Code Online (Sandbox Code Playgroud)
但我真正想要的是:
technicalContact blah@example.com
Run Code Online (Sandbox Code Playgroud)
有没有办法只显示后缀(sans xmlns),或更好 - 迭代元素而不明确说明xmlns?
我正在为会议建立一个网站,具有opengraph用于 Facebook 和 Twitter 共享的属性。
该验证说,og:type字段是强制性的:
所以,我添加了一个字段:
<meta property="og:type" content="..." />
Run Code Online (Sandbox Code Playgroud)
我不确定我的内容类型是什么。该手册指出:
为了在图形中表示您的对象,您需要指定其类型。这是使用 og:type 属性完成的:
Run Code Online (Sandbox Code Playgroud)<meta property="og:type" content="website" />当社区就一种类型的模式达成一致时,它会被添加到全局类型列表中。
但是,我找不到“全局类型”列表,我不确定我的会议网页的类型是什么。
在哪里可以找到开放图形对象的全局对象类型列表?