By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,676 Members | 2,262 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,676 IT Pros & Developers. It's quick & easy.

wrapping existing instance in new interface

P: n/a
I am writing a class that subclasses datetime.datetime in order to add
a few specialized methods. So far the __init__ looks like this:

class myDateTime(datetime.datetime):
def __init__(self, time, *args, **kwargs):
if isinstance(time, str):
timeTuple, tzOffset = self.magicMethod(timeStr)
datetime.__init__(self, tzinfo=GenericTZ(tzoffset),
**timeTuple)

I would also like to pass in instances of datetime.datetime and have my
class wrap it in the new interface. Something like this:

mdt = myDateTime(datetime.datetime.now())

I suppose I could do something like this:

elif isinstance(time, datetime.datetime):
timetuple = time.timetuple()
tzoffset = time.utcoffset()
datetime.__init__(self, tzinfo=GenericTZ(tzoffset),
**timetuple)

However, that feels rather... awkward. Is there a better/cleaner way?
Perhaps a way to directly wrap my new interface around the passed-in
datetime.datetime instance?

Thanks...

-Ben

Nov 15 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
At Wednesday 15/11/2006 19:30, in****@gmail.com wrote:
>I am writing a class that subclasses datetime.datetime in order to add
a few specialized methods. So far the __init__ looks like this:

class myDateTime(datetime.datetime):
def __init__(self, time, *args, **kwargs):
if isinstance(time, str):
timeTuple, tzOffset = self.magicMethod(timeStr)
datetime.__init__(self, tzinfo=GenericTZ(tzoffset),
**timeTuple)
(I assume you mean *timeTuple)
>I would also like to pass in instances of datetime.datetime and have my
class wrap it in the new interface. Something like this:

mdt = myDateTime(datetime.datetime.now())

I suppose I could do something like this:

elif isinstance(time, datetime.datetime):
timetuple = time.timetuple()
tzoffset = time.utcoffset()
datetime.__init__(self, tzinfo=GenericTZ(tzoffset),
**timetuple)

However, that feels rather... awkward. Is there a better/cleaner way?
Perhaps a way to directly wrap my new interface around the passed-in
datetime.datetime instance?
I don't see any other suitable constructor for datetime; perhaps one
taking a datetime instance would be useful here.
Each individual component (year, month, etc.) is stored by itself, so
timetuple() isn't complex; apart from the overhead of constructing a
new object, you get an unneeded extra check of validity.

Another way would be monkey-patching the datetime class adding your
own methods. This way the new methods would be available on any
datetime instance - you don't need myDateTime class.
You can move "magicmethod" into a factory function used to construct
datetime objects.
--
Gabriel Genellina
Softlab SRL

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ˇgratis!
ˇAbrí tu cuenta ya! - http://correo.yahoo.com.ar
Nov 15 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.