Python 根据输入不同的函数
我编写了一个程序来解决我的物理章节的问题,该程序获取所有给定的数据并对其执行所有操作。我使用了一长串 if 语句来检查哪些函数可以安全调用(函数本身并不安全),但我觉得必须有更好的方法来做到这一点。
完整的代码是这里
这是罪犯的片段(argparse默认为None):
# EVALUATE:
if args.t and args.ld:
print 'Velocity:', find_velocity(args.t, args.ld)
if args.t and args.l and args.m:
print 'Velocity:', find_velocity(args.t, args.l, args.m)
if args.l:
print 'Longest possible standing wave length:', find_longest_possible_standing_wave_length(args.l)
if args.l and args.m and args.t and args.n:
print 'Frequency of the standing wave with', args.n, 'nodes:', find_nth_frequency_standing_wave(args.t, args.n, args.l, args.m)
if args.s and args.t and args.n and args.l:
print 'Frequency of', args.n, 'standing wave:', find_nth_frequency_standing_wave(args.t, args.n, args.l, velocity=args.s)
if args.ld and args.t and args.f:
print 'Angular wave number: ', find_angular_wave_number(args.ld, args.t, args.f)
if args.p:
print 'Difference in amplitude of twins:', find_amplitude_difference_of_twins(args.p)
if args.f:
print 'Angular wave frequency:', find_angular_wave_frequency(args.f)
谢谢!
I wrote a program to solve the problems for my physics chapter which takes all the given data and does everything it can with it. I used a long string of if statements to check which functions are safe to call (the functions themselves are not safe), but I feel like there must be a better way to do this.
the full code is here
Here's a snippet of the offender (argparse default is None):
# EVALUATE:
if args.t and args.ld:
print 'Velocity:', find_velocity(args.t, args.ld)
if args.t and args.l and args.m:
print 'Velocity:', find_velocity(args.t, args.l, args.m)
if args.l:
print 'Longest possible standing wave length:', find_longest_possible_standing_wave_length(args.l)
if args.l and args.m and args.t and args.n:
print 'Frequency of the standing wave with', args.n, 'nodes:', find_nth_frequency_standing_wave(args.t, args.n, args.l, args.m)
if args.s and args.t and args.n and args.l:
print 'Frequency of', args.n, 'standing wave:', find_nth_frequency_standing_wave(args.t, args.n, args.l, velocity=args.s)
if args.ld and args.t and args.f:
print 'Angular wave number: ', find_angular_wave_number(args.ld, args.t, args.f)
if args.p:
print 'Difference in amplitude of twins:', find_amplitude_difference_of_twins(args.p)
if args.f:
print 'Angular wave frequency:', find_angular_wave_frequency(args.f)
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将函数放入列表中,然后过滤该列表,确保对于该函数中的每个变量名,这些参数都不是空的。
示例:
不要在语法上让我生气,如果我在任何地方搞砸了,请告诉我,我只在解释器上尝试了其中的一部分。
Put the functions in a list, then filter that list on making sure that for each variable name in that function those arguments are not none.
Example:
Don't hold me over the coals on syntax, just let me know if I messed up anywhere, I only tried parts of it on the interpreter.
根据程序的设计,您已经找到了一种实现您想要做的事情的不太糟糕的方法。但我认为你的程序设计有些可疑。
如果我理解正确,您允许用户根据自己的意愿传递尽可能多或尽可能少的参数,然后调用定义了哪些参数的所有有意义的函数。为什么不要求传递所有参数或在调用时命名其中一个函数?
如果您坚持这种设计,您可以尝试以下操作:
制作函数的
dict
->所需参数:循环字典并检查是否拥有每个属性:
Given the design of your program, you have found a not-too-bad way of implementing what you want to do. But I think there's something fishy with the design of your program.
If I understand correctly, you permit the user to pass as many or as few arguments as s/he wishes and then call all the functions which make sense given which arguments are defined. Why not require either all the arguments to be passed or one of the functions to be named on call?
If you are stuck to this design you could try the following:
make a
dict
of function -> required arguments:loop over the dict and check whether you have each property:
看起来您想调用一个函数,然后将其传递给一些参数。在这种情况下,您可能根本不需要 argparse。相反,尝试将您想要的函数作为第一个命令行参数,然后将其他所有内容作为该函数的参数。
您可以使用
sys.argv[1]
访问第一个参数,并使用sys.argv[2:]
访问所有其他参数。然后你可以像这样调用函数:
假设你的函数是在本地模块中定义的。
It seems like you want to call a function and then pass it in some arguments. In this case, you may not need argparse at all. Instead try making the function you want to be the first command line argument, then take everything else as arguments to that function.
You can access the first argument with
sys.argv[1]
, and all other arguments withsys.argv[2:]
.Then you can call the function like this:
Assuming your functions are defined in the local module.