简体   繁体   English

将参数传递给内部函数

[英]Passing Arguments to Inner Function

I wrote a little helper function to check how many movies I've added to my Flask-SQLAlchemy table in the last x hours/minutes/days: 我编写了一个辅助函数,以检查在过去的x小时/分钟/天,我已经向Flask-SQLAlchemy表添加了多少部电影:

#### Check how many movies were added in the last X (hours, minutes) Y (int)
def movies_added_last(x,y):
    current_time = dateconvert.arrow.utcnow().naive
    tf_hours_ago = current_time - timedelta(x = y)
    movies_within_the_last_tf = models.Movie.query.filter(models.Movie.timeadded > tf_hours_ago).count()
    print movies_within_the_last_tf

The goal is to be able to call it like so: (hours,24) or (minutes,10). 目标是能够这样称呼它:(hours,24)或(minutes,10)。

First of all, x is being recognized by timedelta() as an invalid attribute, as it only accepts arguments like hours , minutes , and weeks . 首先, x正在被识别timedelta()是无效的属性,因为它仅接受像参数hoursminutes ,和weeks timedelta() isn't treating x as a variable. timedelta()不会将x视为变量。

How can I make this function work? 如何使此功能起作用?

EDIT 编辑

I should explain myself a bit better: 我应该给自己更好的解释:

x is supposed to be the format argument that is passed to timedelta, and y is supposed to be the integer argument, so when I call movies_added_last(seconds, 60) I want movies added in the last 60 seconds, another way to say it is that I want the inner timedelta to evaluate as timedelta(seconds = 60) . x应该是传递给timedelta的格式参数,而y应该是整数参数,所以当我调用movies_added_last(seconds, 60)我想在过去60秒内添加电影,另一种说法是我希望内部timedelta评估为timedelta(seconds = 60)

It looks to me like you want to use: timedelta(**{x:y}) rather than timedelta(x=y) . 在我看来,您想要使用: timedelta(**{x:y})而不是timedelta(x=y)

With the latter, you're passing a default argument named x (with value y ) to timedelta . 对于后者,您要将一个名为x (值为y )的默认参数传递给timedelta With the former, you're passing a default argument whose name is the value of x (still with a value of y ) to timedelta . 对于前者,您要将默认参数传递给timedelta ,该默认参数的名称为x (仍为y的值)。 eg 例如

>>> from datetime import timedelta
>>> x = 'hours'
>>> timedelta(x=3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'x' is an invalid keyword argument for this function
>>> timedelta(**{x:3})
datetime.timedelta(0, 10800)

If you do this, then you'll call your function like so: 如果这样做,那么您将像这样调用函数:

 movies_added_last('seconds', 60)
 movies_added_last('minutes', 20)
 # etc.

Other alternatives (which might be cleaner) are to unpack keyword arguments to your function: 其他替代方法(可能更简洁)是将关键字参数解压缩到函数中:

def movies_added_last(**kwargs):
    current_time = dateconvert.arrow.utcnow().naive
    tf_hours_ago = current_time - timedelta(**kwargs)
    movies_within_the_last_tf = models.Movie.query.filter(models.Movie.timeadded > tf_hours_ago).count()
    print movies_within_the_last_tf

Now you can call this with any keyword arguments that you can use to call timedelta : 现在,您可以使用可用于调用timedelta任何关键字参数来调用timedelta

movies_added_last(seconds=60)
movies_added_last(minutes=20)
# etc.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM