Python:pytz返回不可用的

Python:pytz返回不可用的,python,instance,representation,pytz,Python,Instance,Representation,Pytz,我知道repr()的目的是返回一个字符串,该字符串可以作为python命令进行计算,并返回相同的对象。不幸的是,pytz似乎对该函数不太友好,尽管它应该很容易,因为pytz实例是通过单个调用创建的: import datetime, pytz now = datetime.datetime.now(pytz.timezone('Europe/Berlin')) repr(now) 返回: datetime.datetime(2010, 10, 1, 13, 2, 17, 659333, tzi

我知道
repr()
的目的是返回一个字符串,该字符串可以作为python命令进行计算,并返回相同的对象。不幸的是,
pytz
似乎对该函数不太友好,尽管它应该很容易,因为
pytz
实例是通过单个调用创建的:

import datetime, pytz
now = datetime.datetime.now(pytz.timezone('Europe/Berlin'))
repr(now)
返回:

datetime.datetime(2010, 10, 1, 13, 2, 17, 659333, tzinfo=<DstTzInfo 'Europe/Berlin' CEST+2:00:00 DST>)
'Europe/Berlin'
字符串在
repr()的原始输出中已经清晰可见时

请注意,由于夏令时,夏季的
pytz.timezone(“欧洲/柏林”)
可能意味着与冬季的
pytz.timezone(“欧洲/柏林”)
有所不同。因此,monkeypatched
\uuu repr\uuu
并不是始终正确地表示
自我。但在复制和粘贴到IPython的过程中,它应该可以正常工作(极端情况除外)


另一种方法是子类化
datetime.tzinfo

class MyTimezone(datetime.tzinfo):
    def __init__(self,zone):
        self.timezone=pytz.timezone(zone)
    def __repr__(self):
        return 'MyTimezone("{z}")'.format(z=self.timezone.zone)
    def utcoffset(self, dt):
        return self.timezone._utcoffset
    def tzname(self, dt):
        return self.timezone._tzname
    def dst(self, dt):
        return self.timezone._dst

berlin=MyTimezone('Europe/Berlin')
now = datetime.datetime.now(berlin)
print(repr(now))
# datetime.datetime(2010, 10, 1, 19, 2, 58, 702758, tzinfo=MyTimezone("Europe/Berlin"))

它将永远是欧洲/柏林还是任意时区?它可以是任何时区。我想要一个datetime对象的pythonic表示,我可以在ipython shell中复制、粘贴和执行该对象,并获取原始时间戳及其时区。因此,请不要全局转换为UTC。仅供参考:建议使用
repr
来复制对象,但这不是必需的。@Daenyth:如果建议这样做,那么如果这些公共库的开发人员能够使用它就好了。看起来像我想要的,尽管看起来不是很优雅。子类化不是真正的可移植性(在每封电子邮件中快速向某人发送datetime(…)构造函数调用行),但这可能是ipython中本地使用的最简单的解决方案。谢谢!这看起来很糟糕,但您是否考虑过发送pickle对象,例如,
import cPickle;cPickle.load(“cdatetime\ndatetime\np1\n(S'\\x07\\xda\\n\\x01\\x151+\\x0c\\x13\\xd0'\ncpytz\n\u p\np2\n(S'Europe/Berlin'\np3\nI7200\nI3600\nS'CEST'\np4\ntRp5\ntRp6\n.)
不,因为它看起来很糟糕,我也想能够读这行。我正在考虑联系pytz开发人员,询问他们是否可以更改他们的repr…请注意,
\uuuuuuurepr\uuuuu
应该提供足够的信息来重新生成对象。设置
\uuuurepr\uuuuu
以返回
“pytz.timezone(…)“
失败,因为它忽略了日光节约时间的问题。
import datetime
import pytz
import pytz.tzinfo

def tzinfo_repr(self):
    return 'pytz.timezone({z})'.format(z=self.zone)
pytz.tzinfo.DstTzInfo.__repr__=tzinfo_repr

berlin=pytz.timezone('Europe/Berlin')
now = datetime.datetime.now(berlin)
print(repr(now))
# datetime.datetime(2010, 10, 1, 14, 39, 4, 456039, tzinfo=pytz.timezone("Europe/Berlin"))
class MyTimezone(datetime.tzinfo):
    def __init__(self,zone):
        self.timezone=pytz.timezone(zone)
    def __repr__(self):
        return 'MyTimezone("{z}")'.format(z=self.timezone.zone)
    def utcoffset(self, dt):
        return self.timezone._utcoffset
    def tzname(self, dt):
        return self.timezone._tzname
    def dst(self, dt):
        return self.timezone._dst

berlin=MyTimezone('Europe/Berlin')
now = datetime.datetime.now(berlin)
print(repr(now))
# datetime.datetime(2010, 10, 1, 19, 2, 58, 702758, tzinfo=MyTimezone("Europe/Berlin"))