Python 根据输入不同的函数

发布于 2024-10-06 20:34:50 字数 1215 浏览 6 评论 0原文

我编写了一个程序来解决我的物理章节的问题,该程序获取所有给定的数据并对其执行所有操作。我使用了一长串 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

野稚 2024-10-13 20:34:50

将函数放入列表中,然后过滤该列表,确保对于该函数中的每个变量名,这些参数都不是空的。

示例:

def func_filter(item, arguments):
    needed_args = item.func_code.co_varnames
    all(map(lambda x: getattr(arguments, x) , needed_args))

funcs = (find_velocity, find_nth_frequency_standing_wave)
funcs = filter(lambda x: func_filter(x, args), funcs)
#now execute all of the remaining funcs with the necessary arguments,
#similar to code in func filter

不要在语法上让我生气,如果我在任何地方搞砸了,请告诉我,我只在解释器上尝试了其中的一部分。

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:

def func_filter(item, arguments):
    needed_args = item.func_code.co_varnames
    all(map(lambda x: getattr(arguments, x) , needed_args))

funcs = (find_velocity, find_nth_frequency_standing_wave)
funcs = filter(lambda x: func_filter(x, args), funcs)
#now execute all of the remaining funcs with the necessary arguments,
#similar to code in func filter

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.

一抹苦笑 2024-10-13 20:34:50

根据程序的设计,您已经找到了一种实现您想要做的事情的不太糟糕的方法。但我认为你的程序设计有些可疑。

如果我理解正确,您允许用户根据自己的意愿传递尽可能多或尽可能少的参数,然后调用定义了哪些参数的所有有意义的函数。为什么不要求传递所有参数或在调用时命名其中一个函数?


如果您坚持这种设计,您可以尝试以下操作:

  • 制作函数的 dict ->所需参数:

    {find_velocity: ("t", "ld"), ...}
    
  • 循环字典并检查是否拥有每个属性:

    对于 func,funcs.items() 中的要求:
        args = [getattr(args, req) for req in reqs]
        如果全部(参数):
            函数(*参数)
    

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:

    {find_velocity: ("t", "ld"), ...}
    
  • loop over the dict and check whether you have each property:

    for func, reqs in funcs.items():
        args = [getattr(args, req) for req in reqs]
        if all(args):
            func(*args)
    
山川志 2024-10-13 20:34:50

看起来您想调用一个函数,然后将其传递给一些参数。在这种情况下,您可能根本不需要 argparse。相反,尝试将您想要的函数作为第一个命令行参数,然后将其他所有内容作为该函数的参数。

您可以使用 sys.argv[1] 访问第一个参数,并使用 sys.argv[2:] 访问所有其他参数。

然后你可以像这样调用函数:

locals()[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 with sys.argv[2:].

Then you can call the function like this:

locals()[sys.argv[1]](*sys.argv[2:])

Assuming your functions are defined in the local module.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文