python – 子类化datetime:在算术运算之后是否有更好的方法来维
发布时间:2020-12-20 13:25:32 所属栏目:Python 来源:网络整理
导读:我最近遇到了一个情境,我需要将datetime.datetime和datetime.timedelta子类化,以便添加一些方法.但是,我立刻发现,当我希望它返回mydatetime.mydatetime实例时,任何算术运算都会返回datetime.datetime对象.以下是同事帮助我解决这个问题的解决方案.有没有人有
我最近遇到了一个情境,我需要将datetime.datetime和datetime.timedelta子类化,以便添加一些方法.但是,我立刻发现,当我希望它返回mydatetime.mydatetime实例时,任何算术运算都会返回datetime.datetime对象.以下是同事帮助我解决这个问题的解决方案.有没有人有更简洁或方便的建议?我在这里做了什么有危险吗?我错过了什么重要的事吗?
from datetime import datetime,timedelta def _to_mydatetime(native): '''Instantiates object of appropriate class based on class of the input object.''' if hasattr(native,'timetuple'): return mydatetime(*native.timetuple()[:6]) else: return mytimedelta(native.days,native.seconds) class mydatetime(datetime): '''Subclass of datetime''' def __add__(self,other): result = super(mydatetime,self).__add__(other) return _to_mydatetime(result) def __sub__(self,self).__sub__(other) return _to_mydatetime(result) class mytimedelta(timedelta): def __add__(self,other): result = super(mytimedelta,self).__sub__(other) return _to_mydatetime(result) def __div__(self,self).__div__(other) return _to_mydatetime(result) def __rmul__(self,self).__rmul__(other) return _to_mydatetime(result) def __mul__(self,self).__mul__(other) return _to_mydatetime(result) 解决方法
嗯,这是正确的方法(我只是将转换器方法分成两部分). Python允许您减少代码重复:
from datetime import * def convproxy(methods,converter): def f(cls): def _proxyfactory(method): def _convproxy(self,*args,**kwargs): return converter(getattr(super(cls,self),method)(*args,**kwargs)) return _convproxy for m in methods: setattr(cls,m,_proxyfactory(m)) return cls return f @convproxy(('__add__','__sub__'),lambda d:mydatetime(d.timetuple()[:6])) class mydatetime(datetime): pass @convproxy(('__add__','__sub__','__div__','__rmul__','__mul__'), lambda t:mytimetuple(t.days,t.seconds)) class mytimedelta(timedelta): pass convproxy下的神秘代码只是在创建类时生成指定方法的智能方法,每个类都调用超类方法并使用指定的转换器函数从结果创建子类. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |