如果我click.group()有多个子命令,有没有办法可以将命令行参数传递给组内的子命令?
我知道你可以从组中向下通过context,我知道我可以使用callback在命令之前执行的函数,但我不知道是否有更好的方法来执行此操作而不是使用callback.
一个例子:
@click.group()
def cli():
pass
@cli.command()
@click.argument('task')
@click.argument('task_id')
def sync(task, task_id):
click.echo('Synching: {}'.format(task))
Run Code Online (Sandbox Code Playgroud)
在这个例子中,有没有办法获得task或task_id在组方法?
我在本地模块中使用click并且我想调整帮助的显示方式:
当前输出--help:
Usage: __main__.py [OPTIONS] COMMAND [ARGS]...
Options:
--help Show this message and exit.
Commands:
foo Foo is a program very nice and pretty...
Run Code Online (Sandbox Code Playgroud)
默认情况下,prog名称为__main__.py并且文本被修剪为 78 个字符。
我发现这可以使用HelpFormatter类进行调整。但我不知道如何在这种情况下使用它。
当前代码:
import click
@click.group()
def main(ctx):
pass
@main.command()
def foo():
pass
click.CommandCollection(sources=[main])()
Run Code Online (Sandbox Code Playgroud)
预期输出:
Usage: my_module_name [OPTIONS] COMMAND [ARGS]...
Options:
--help Show this message and exit.
Commands:
foo Foo is a program very nice and pretty and this sentence is very long.
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Python库Click,但是很难使示例工作。我定义了两组,其中一组(group2)用于处理该组命令的通用参数。我想要实现的是那些通用参数由组函数(group2)处理并分配给上下文变量,因此它们可以由实际命令使用。
一个用例是许多需要用户名和密码的命令,而有些则不需要(甚至不是可选的)。
这是代码
import click
@click.group()
@click.pass_context
def group1(ctx):
pass
@click.group()
@click.option('--optparam', default=None, type=str)
@click.option('--optparam2', default=None, type=str)
@click.pass_context
def group2(ctx, optparam):
print 'in group2', optparam
ctx['foo'] = create_foo_by_processing_params(optparam, optparam2)
@group2.command()
@click.pass_context
def command2a(ctx):
print 'command2a', ctx['foo']
@group2.command()
@click.option('--another-param', default=None, type=str)
@click.pass_context
def command2b(ctx, another_param):
print 'command2b', ctx['foo'], another_param
# many more more commands here...
# @group2.command()
# def command2x():
# ...
@group1.command()
@click.argument('argument1')
@click.option('--option1')
def command1(argument1, option1):
print 'In command2', argument1, option1
cli = …Run Code Online (Sandbox Code Playgroud) 我正在尝试pandas.DataFrame直接写入Google云端硬盘,而无需先在本地写入文件.我找不到解决方案,也不确定它是否可行.我已经尝试了下面的代码,但我得到了一个AttributeError.
import pandas as pd
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
gauth = GoogleAuth()
gauth.LoadCredentialsFile(mycreds)
drive = GoogleDrive(gauth)
df = pd.DataFrame({'a':[1,2],'b':[2,3]})
f = drive.CreateFile({'id': '0B_6_uVX9biFuX0FJWFkt'}) #test.xlsx file
f.SetContentString(df)
f.Upload()
Run Code Online (Sandbox Code Playgroud)
AttributeError:'DataFrame'对象没有属性'encode'
IIS 8.5中可以执行多少个并发请求?
我找不到在IIS 8.5中可以执行多少并发请求的正确值
正如我在下面发现的2个不同的值:
默认情况下,IIS 8.5可以根据MaxConcurrentRequestsPerCPUaspnet.config中的设置处理5000个并发请求
在machine.config中,默认情况下,每个CPU的maxconnection为2.因此,如果有6个核心CPU,那么默认情况下可以有12个并发请求.
所以我想知道Point 1是正确的,或者Point 2适用于IIS 8.5的并发请求.
我正在使用 click 在 Python 中编写一个 cli 程序,我需要编写如下内容:
import click
@click.group()
def root():
"""root"""
pass
@root.group()
def cli():
"""test"""
pass
@root.group()
def cli2():
"""test"""
pass
@cli.command('test1')
@cli2.command('test1')
def test1():
"""test2"""
print 1234
return
root()
Run Code Online (Sandbox Code Playgroud)
但这将失败:
类型错误:尝试将回调转换为命令两次。
如何在多个组之间共享命令?
我想使用数组作为函数的参数,该函数将用于解决ODE函数。
def ode(x, t, read_tau, tau_arr):
q_ib = x[0:4]
omega = x[4:7]
dq_ib = 0.5 * np.dot(gen_omega(omega), q_ib) + read_tau(tau_arr)
return dq_ib
dq_ib = odeint(rhs, x0, t, args=(b_I, read_tau, tau_arr))
Run Code Online (Sandbox Code Playgroud)
tau_arr是一个(1000,3)数组。我能想到的唯一解决方案是,首先使tau_arr成为迭代器,并使其成为read_tau()。
def read_tau(tau_arr):
return next(tau_arr)
Run Code Online (Sandbox Code Playgroud)
read_tau函数的返回值将是一个1x3数组,该数组将用于求解ODE。
我的问题是如何将二维数组转换为迭代器,并在使用next()调用迭代器时,它将逐行返回数组。
a = np.array([[1,2,3], [4,5,6]])
convert_to_iter(a)
next(a)
[1,2,3]
next[a]
[4,5,6]
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建具有以下行为的应用程序:
myapp- 将启动应用程序并执行操作 A
myapp "some argument"- 会做事情 B。事情 B 受到参数“某些参数”的影响。
myapp command- 将启动具有功能 C 的“命令”(由装饰器表示@cli.command)。这将受到 click 必须提供的所有内容的影响,例如@click.option。
请注意,在我的应用程序中会有更多像 C 这样的命令。
我尝试使用以下代码来实现这一点:
import click
class GroupWithOption(click.Group):
def list_commands(self, ctx):
return ['command']
def get_command(self, ctx, cmd_name):
if cmd_name == 'command':
return command
else:
return do_b
@click.group(cls=GroupWithOption, invoke_without_command=True)
def main():
print("Does A")
@main.command()
def command():
print("Does C")
@main.command()
def do_b():
print("Does B")
if __name__ == '__main__':
main()
Run Code Online (Sandbox Code Playgroud)
结果好坏参半。其一,我可以非常轻松地调用 3 种不同的行为(或更多),但我无法弄清楚如何将参数传递给 B 命令。我不喜欢这个解决方案。 …
我有以下包结构:
.
??? my_app
? ??? app.py
? ??? cli.py
? ??? db.py
? ??? __init__.py
??? setup.py
??? tests
? ??? data
? ? ??? foobar.gz
? ??? test_app.py
? ??? test_database.py
??? tox.ini
Run Code Online (Sandbox Code Playgroud)
在 内test_app.py,我这样做:
import pkg_resources
path = '../tests/data/foobar.gz'
full_path = pkg_resources.resource_filename(__name__, path)
Run Code Online (Sandbox Code Playgroud)
现在我看到了这条消息:
Run Code Online (Sandbox Code Playgroud)/usr/local/lib/python3.7/site-packages/pkg_resources/__init__.py:1145: DeprecationWarning: Use of .. or absolute path in a resource path is not allowed and will raise exceptions in a future release. self, resource_name
我应该怎么做?
(我也可以更改包的结构,但我想将测试数据保留在测试中 - 运行应用程序不需要)
os.path.getmtime()我正在尝试使用并将时间转换为人类可读的格式来获取文件的修改日期;建议使用time.ctime()方法,但它也给出了日期和月份的名称,这是我不想要的。
可以格式化吗%d %m %Y, %H:%M?
目前我有这样的代码:
import os.path, time
path = #path of the file
print time.ctime(os.path.getmtime(path))
Run Code Online (Sandbox Code Playgroud) python ×9
python-click ×5
python-3.x ×2
arrays ×1
concurrency ×1
date ×1
excel ×1
iis ×1
iterator ×1
numpy ×1
ode ×1
openpyxl ×1
pandas ×1
pydrive ×1
python-2.7 ×1
time ×1
unit-testing ×1