wrapping existing instance in new interface

Discussion in 'Python' started by insyte@gmail.com, Nov 15, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. At Wednesday 15/11/2006 19:30, 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
    Gabriel Genellina, Nov 15, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    299
    aa7im
    Sep 10, 2005
  2. Marco
    Replies:
    3
    Views:
    330
    Marco
    Oct 12, 2003
  3. Sandra-24
    Replies:
    18
    Views:
    1,837
    Steve Holden
    Apr 29, 2006
  4. Neville Dempsey
    Replies:
    1
    Views:
    253
    Bruno Desthuilliers
    Sep 1, 2008
  5. Chicken McNuggets

    Wrapping existing UNIX commands in C

    Chicken McNuggets, Jul 29, 2012, in forum: C Programming
    Replies:
    87
    Views:
    1,718
    Kaz Kylheku
    Sep 10, 2012
Loading...

Share This Page