如何使用 PyYAML 创建当前本地日期和时间

发布于 2024-10-03 01:17:11 字数 826 浏览 6 评论 0原文

我希望能够使用 datetime.datetime.now() PyYAML 创建一个日期时间对象。调用一些函数很容易:

>>> y = """#YAML
... description: Something
... ts: !!python/object/apply:time.time []"""
>>> yaml.load(y)
{'description': 'Something', 'ts': 1289955567.940973}
>>> 

但是,我似乎不知道如何获取datetime.now()。我已经尝试了使用各种 python yaml 标签 进行调用的尽可能多的排列。

这些都失败了:

tests = [ 
        'dt: !!python/object:datetime.datetime.now []',
        'dt: !!python/object/new:datetime.datetime.now []',
        'dt: !!python/object/apply:datetime.datetime.now []',
]

for y in tests:
    try:
        print yaml.load(y)
    except Exception, err:
        print '==>', err

I'd like to be able to create a datetime object with datetime.datetime.now() PyYAML. It's easy to call some functions:

>>> y = """#YAML
... description: Something
... ts: !!python/object/apply:time.time []"""
>>> yaml.load(y)
{'description': 'Something', 'ts': 1289955567.940973}
>>> 

However, I can't seem to figure out how to get a datetime.now(). I've tried as many permutations with calls to that using the various python yaml tags.

These all fail:

tests = [ 
        'dt: !!python/object:datetime.datetime.now []',
        'dt: !!python/object/new:datetime.datetime.now []',
        'dt: !!python/object/apply:datetime.datetime.now []',
]

for y in tests:
    try:
        print yaml.load(y)
    except Exception, err:
        print '==>', err

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

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

发布评论

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

评论(1

夜吻♂芭芘 2024-10-10 01:17:11

我认为这个例子实现了你正在寻找的东西:

dt = yaml.load("""dt: !!python/object/apply:apply
    - !!python/object/apply:getattr
        - !!python/name:datetime.datetime
        - now
    - []
""")

但是,我认为它太牵强了,因为 PyYAML 支持的 !!python/object 语法不应该调用类方法(datetime.datetime.now 实际上就像日期时间对象的“静态”工厂方法)。正如您所说,这更简单(尽管不是您正在寻找的):

dt = yaml.load("dt: !!python/object/apply:time.gmtime []")
dt = yaml.load("dt: !!python/object/apply:time.time []")

另一种可能的解决方法是创建一个自定义辅助函数,该函数将调用包装到 datetime.datetime.now 以便它很容易用 !!python/object/apply 序列化。缺点是此序列化无法移植到未找到此自定义函数的环境中。

无论如何,在我看来,序列化一个始终返回当前日期时间(实际上是解析 YAML 的时间)的值并没有多大意义。 PyYAML 提供了用于序列化某个时间戳的快捷方式:

dt = yaml.load("""dt: !!timestamp '2010-11-17 13:12:11'""")

I think this example achieves what you're looking for:

dt = yaml.load("""dt: !!python/object/apply:apply
    - !!python/object/apply:getattr
        - !!python/name:datetime.datetime
        - now
    - []
""")

However, I think it is too far-fetched because the !!python/object syntax supported by PyYAML is not supposed to call class methods (datetime.datetime.now is actually like a "static" factory method for datetime objects). As you said, this is simpler (though not what you're looking for):

dt = yaml.load("dt: !!python/object/apply:time.gmtime []")
dt = yaml.load("dt: !!python/object/apply:time.time []")

Another possible work-around would be to create a custom helper function that wraps the call to datetime.datetime.now so that it is easily serialized with !!python/object/apply. The cons is that this serialization would not be portable to an environment where this custom function is not found.

Anyway, in my opinion it does not make too much sense to serialize a value that always returns the current datetime (which would actually be the time when the YAML was parsed). PyYAML provides this shortcut for serializing a certain timestamp:

dt = yaml.load("""dt: !!timestamp '2010-11-17 13:12:11'""")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文