473,405 Members | 2,421 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

wrapping existing instance in new interface

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
1 1214
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Roy Smith | last post by:
I've got a C library with about 50 calls in it that I want to wrap in Python. I know I could use some tool like SWIG, but that will give me a too-literal translation; I want to make some...
5
by: nimdez | last post by:
Hi, I am working on an existing code base in which a lot of data displayed to the user is formatted in tables. Most tables are printed row-by-row using printf() with "%s" print conversion...
2
by: Andrzej Kaczmarczyk | last post by:
Hi I am experiencing something weird. maybe you could help me. I have two ineditable classes from outsource libraries: DataColumn and GridColumn I have built a wrapper class around...
5
by: Michael Dyremo | last post by:
Hello. We have a web-application built in ASP.NET using WebForms and Remoting. When selling this application we always incorporate it into the customers existing web-site. Our latest customer...
18
by: Sean Kirkpatrick | last post by:
I have a very ugly problem and I need some sincere guidance. My legacy VB6 application depends heavily on DAO, especially as it relates to custom properties on database objects. These custom...
5
by: tarnap | last post by:
Hello, I have an instance of a form, lets say formA. This form is passed to a method that must create a new form, lets say formB of the same type. How can this be accomplished except by using...
7
by: christian2.schmidt | last post by:
Hi, I'm trying to wrap an IList-instance in a native template class with a "vector"-like interface. cliext::vector seems not suited, as it copies the list - correct me if I'm wrong. template...
0
by: Neville Dempsey | last post by:
Basically I have an existing (maybe a rather large and complicated (existing) instance) that I want to add new member to. Cheers N Hacks/attempts follow: from math import sqrt
0
by: Neville Dempsey | last post by:
What do I need to add to HTMLDecorator? A simpler example: import cgi class ClassX(object): pass # ... with own __repr__ class ClassY(object):
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.