我可以/应该从Argparse进入我的功能和课程多远?
我在Python上写了很多CLI工具。我的大多数工具都有这样的东西来获得参数:
import argparse
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('integers', metavar='N', type=int, nargs='+',
help='an integer for the accumulator')
parser.add_argument('--sum', dest='accumulate', action='store_const',
const=sum, default=max,
help='sum the integers (default: find the max)')
args = parser.parse_args()
我编写的代码是一半的数据科学(我们是生物信息学家)。这是通常生活在“ main..py”中,然后传递到某种“运行实验”功能/方法,该功能通常会使用多处理池将任务分开(通过传递到其他功能/类)。因此,来自命令行的大多数参数都需要解析为运行函数,然后再将新过程解析为运行函数。由于各种原因,这种架构并不是没有辩论。
我认识到 https:///python-docs.readthedocs。 io/en/最新/写作/style.html
好好
def make_complex(*args):
x, y = args
return dict(**locals())
好,
def make_complex(x, y):
return {'x': x, 'y': y}
我的工具通常从argparse中存储了10-20个参数。因此,问题是 - 我应该解析它们以Args的形式包装还是打开它们并单独通过它们?如果我明确地将它们分析到每个函数/类中,它们旨在将其最终放入其中意味着我最终会得到很多冗余代码(至少一个函数将具有与ARGS相同的大量参数列表)。相反,如果我只是通过它来绕着它与禅...
I write a lot of cli tools in python. Most of my tools have something like this to get arguments:
import argparse
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('integers', metavar='N', type=int, nargs='+',
help='an integer for the accumulator')
parser.add_argument('--sum', dest='accumulate', action='store_const',
const=sum, default=max,
help='sum the integers (default: find the max)')
args = parser.parse_args()
I write code that is half data science (we're bioinformaticians). This args bit normally lives in "main.py" then gets passed to some sort of "run experiment" function/method which will often use multiprocessing Pool to break the task apart (by passing off to other functions/classes). So most of the arguments from the command line need to be parsed to the run function then the new process. This architecture is not not up for debate for various reasons.
I'm cognizant of this https://python-docs.readthedocs.io/en/latest/writing/style.html
BAD
def make_complex(*args):
x, y = args
return dict(**locals())
GOOD
def make_complex(x, y):
return {'x': x, 'y': y}
My tools often have 10-20 parameters stored in args, from argparse. So the question is - should I parse them packaged up as args or unpack them and pass them individually? If I explicitly parse each of them to each function/class that they are intended to end up in it means I end up with a lot of redundant code (at least one function will have a massive parameter list that is identical to args). Conversely, if I just pass args around it goes against the zen...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是我的看法。
args
是namespace
对象,一个简单的类,将值保存为属性。它可以轻松地将其转换为dict
带有vars
。内部
argparse
代码使用大量foo(*args,** kwargs)
,签名,尤其是对于add_argument
方法。这给出了很多参数所接受的灵活性,但也为错误打开了。在“子命令”部分中,
argparse
docs具有用args.func(args)
调用功能的示例。这允许不同的func
使用不同的参数。通常,
args
包含控制参数,debugging
,记录
等。它们不是主要参数,而是辅助参数。可以或可能不使用。通过args
(或dict
)将这些层通过几层可以很方便。另一方面,如果可以从其他功能和其他用户界面调用函数,则必须创建一个
namepace
像对象这样的namepace
仅通过参数传递可能会很痛苦。简而言之,如果该功能(或类)主要是为了由CLI使用,则通过整个
args
可以方便且合理。但是,如果该函数可以与不同的CLI或其他接口(例如在导入的模块中)一起使用,则更明确的位置和关键字参数更好。如果您的代码是围绕类组织的,则接受
args
命名空间,然后将所需属性分配给实例变量是合理的。该实例不使用args
,除了在启动过程中。Here's my opinion.
args
is aNamespace
object, a simple class that holds the values as attributes. It's easily converted to adict
withvars
.Internally
argparse
code uses a lot offoo(*args, **kwargs)
, signature, especially for theadd_argument
method. This gives a lot of flexibility in what parameters it accepts, but also leaves things open for errors.In the subcommands section, the
argparse
docs has an example of calling functions withargs.func(args)
. This allows the differentfunc
to use different parameters.Often the
args
contains control parameters, things likedebugging
,logging
, etc. They aren't the primary parameters, but auxiliary ones that the function may, or may not, use. Passing those through several layers withargs
(or adict
) can be convenient.On the other hand if a function might be called from other functions and other user interfaces, it may be a pain to have to create a
Namespace
like object just to pass in parameters.In short, if the function (or class) is written primarily for use by the CLI, passing the whole
args
can be convenient and reasonable. But if the function might be used with different CLI, or other interfaces (such as in an imported module), more explicit positional and keyword parameters are better.If your code is organized around classes, it may be reasonable to accept the
args
namespace, and then assign the desired attributes to instance variables. The instance doesn't useargs
except during initiation.