wrapping existing instance in new interface

I

insyte

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
 
G

Gabriel Genellina

At said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top