生成动态时间增量:python

发布于 2024-08-09 22:22:03 字数 718 浏览 8 评论 0原文

这是我的情况:

import foo, bar, etc

frequency = ["hours","days","weeks"]

class geoProcessClass():

    def __init__(self,geoTaskHandler,startDate,frequency,frequencyMultiple=1,*args):
        self.interval = self.__determineTimeDelta(frequency,frequencyMultiple)

    def __determineTimeDelta(self,frequency,frequencyMultiple):
        if frequency in frequency:
            interval = datetime.timedelta(print eval(frequency + "=" + str(frequencyMultiple)))
            return interval
        else:
            interval = datetime.timedelta("days=1")
            return interval

我想用 timedelta 动态定义时间间隔,但这似乎不起作用。

有什么具体方法可以使这项工作有效吗?我在这里得到无效语法。

还有更好的方法吗?

Here's my situation:

import foo, bar, etc

frequency = ["hours","days","weeks"]

class geoProcessClass():

    def __init__(self,geoTaskHandler,startDate,frequency,frequencyMultiple=1,*args):
        self.interval = self.__determineTimeDelta(frequency,frequencyMultiple)

    def __determineTimeDelta(self,frequency,frequencyMultiple):
        if frequency in frequency:
            interval = datetime.timedelta(print eval(frequency + "=" + str(frequencyMultiple)))
            return interval
        else:
            interval = datetime.timedelta("days=1")
            return interval

I want to dynamically define a time interval with timedelta, but this does not seem to work.

Is there any specific way to make this work? I'm getting invalid syntax here.

Are there any better ways to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

毁我热情 2024-08-16 22:22:03

您可以使用 func(**kwargs) 之类的语法调用带有动态参数的函数,其中 kwargs 是命名参数的名称/值映射的字典。

我还将全局频率列表重命名为频率,因为ifFrequencyinFrequency这一行没有多大意义。

class geoProcessClass():
    def __init__(self, geoTaskHandler, startDate, frequency, frequencyMultiple=1, *args):
        self.interval = self.determineTimeDelta(frequency, frequencyMultiple)

    def determineTimeDelta(self, frequency, frequencyMultiple):
        frequencies = ["hours", "days", "weeks"]

        if frequency in frequencies:
            kwargs = {frequency: frequencyMultiple}
        else:
            kwargs = {"days": 1}

        return datetime.timedelta(**kwargs)

不管它的价值如何,从风格上来说,默默地纠正调用者所犯的错误通常是不受欢迎的。如果呼叫者用无效的参数给你打电话,你可能应该立即大声失败,而不是试图继续喋喋不休。我建议不要使用 if 语句。

有关可变长度和关键字参数列表的更多信息,请参阅:

You can call a function with dynamic arguments using syntax like func(**kwargs) where kwargs is dictionary of name/value mappings for the named arguments.

I also renamed the global frequency list to frequencies since the line if frequency in frequency didn't make a whole lot of sense.

class geoProcessClass():
    def __init__(self, geoTaskHandler, startDate, frequency, frequencyMultiple=1, *args):
        self.interval = self.determineTimeDelta(frequency, frequencyMultiple)

    def determineTimeDelta(self, frequency, frequencyMultiple):
        frequencies = ["hours", "days", "weeks"]

        if frequency in frequencies:
            kwargs = {frequency: frequencyMultiple}
        else:
            kwargs = {"days": 1}

        return datetime.timedelta(**kwargs)

For what it's worth, stylistically it's usually frowned upon to silently correct errors a caller makes. If the caller calls you with invalid arguments you should probably fail immediately and loudly rather than try to keep chugging. I'd recommend against that if statement.

For more information on variable-length and keyword argument lists, see:

挽梦忆笙歌 2024-08-16 22:22:03

您对 print eval(...) 的使用看起来有点过于复杂(正如您提到的,这是错误的)。

如果您想将关键字参数传递给函数,只需这样做:

interval = datetime.timedelta(frequency = str(frequencyMultiple)

不过,我没有看到名为 Frequency 的关键字参数,因此这可能是一个单独的问题。

Your use of print eval(...) looks a bit over-complicated (and wrong, as you mention).

If you want to pass a keyword argument to a function, just do it:

interval = datetime.timedelta(frequency = str(frequencyMultiple)

I don't see a keyword argument called frequency though, so that might be a separate problem.

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