dictionaries/pointers
I dont know how to do this and can't think of a simple way to. All I want is a dictionary where two keys point to the same object. (to steal the ascii art from http://starship.python.net/crew/mwh/hacks/objectthink.html) I want sometihng like this: ,--. +---+ | dict |-->|+-+| +---+ `--' || "a" |+>| 1 | |+-+| +---+ | | ^ |+-+| | || "b" |+---' |+-+| +---+ | | |+-+| +---+ || "c" |+>| 2 | |+-+| +---+ +---+ Where if I change "a" or "b" to 3 the other one will change? Is this even possible? How would I do it? -- http://mail.python.org/mailman/listinfo/python-list
inheriting from datetime
So this is simple, why can't I run the following code? I've tried many variances of this, but simply cannot inherit from datetime or datetime.datetime. I get this on line 3. TypeError: function takes at most 2 arguments (3 given) import datetime _datetime = datetime.datetime class MyDateTime(_datetime): """ Identical to builtin datetime.datetime, except it accepts invalid dates and times as input. """ _valid = True def __init__(self, year, month, day, *args, **kw): try: _datetime.__init__(self, year, month, day, *args, **kw) except _datetime.ValueError: _valid = False self.year = year self.month = month self.day= day self.args = args self.kw = kw -- http://mail.python.org/mailman/listinfo/python-list
Re: inheriting from datetime
gah, yeah that was strange. but i got it now. thanks. > side question: what is the point of accepting invalid dates? thats a long story. but it would be nice to have invalid dates at least just stored. so i want to try to put a class together that does it. -- http://mail.python.org/mailman/listinfo/python-list
Why does __init__ not get called?
I'm still working on my DateTime class from last week... Why does __init__ not get called? The docs at http://www.python.org/dev/doc/devel/ref/customization.html read "If __new__() returns an instance of cls, then the new instance's __init__() method will be invoked" and as far as I can tell cls is very much an instance of DateTime import datetime _datetime = datetime.datetime class DateTime(_datetime): """ Identical to builtin datetime.datetime, except it accepts invalid dates and times as input. """ _valid = True __dict__ = _datetime.__dict__ def __init__(self, year, month, day, *args, **kw): print "init called" _valid = False self.year = year self.month = month self.day= day self.args = args self.kw = kw def throwError(): raise ValueError, 'Invalid Date' for method in _datetime.__dict__.keys(): if method!='__doc__': setattr(self, method, throwError) def __new__(cls, year, month, day, *args, **kw): print "new called" try: return _datetime.__new__(cls, year, month, day, *args, **kw) except ValueError: return cls * -- http://mail.python.org/mailman/listinfo/python-list
Re: Why does __init__ not get called?
Ah ok, thats interesting I hadn't even tried a valid date yet. Now how do I get this thing to call __init__ when I pass in an invalid date and the ValueError is thrown and caught within __new__. dt = DateTime(2005, 02, 30) -- http://mail.python.org/mailman/listinfo/python-list
Re: Why does __init__ not get called?
Not takers? This is my attempt to get some attention by bumping my own post. -- http://mail.python.org/mailman/listinfo/python-list
Re: Why does __init__ not get called?
I'm out of my league too. I don't know enough about __new__ and __init__. I just went another route and did a wrapper for datetime, and didn't extend it. Thanks for the effort. By chance... does anyone know, if I wrote a class, and just wanted to override __new__ just for the fun of it. What would __new__ look like so that it behaves exactly the same as it does any other time. -- http://mail.python.org/mailman/listinfo/python-list
Re: providing arguments to base.__init__
seems like you are not running the correct wavePlayer. make sure you don't have 2 wavePlayer vars. -- http://mail.python.org/mailman/listinfo/python-list
Redundant code in multiple methods
No you don't need to know Zope to help me. The whole reason I'd even want to do this is because of Zope though. I made a Zope product, and now want to perfect it. some simple example code... class User: def View(self): # play with data here myHtmlDoc = "pretend this is a uper profile" return myHtmlDoc index_html = View def Edit(self): # play with data here myHtmlDoc = "editing the user" return myHtmlDoc So when visiting "website.com/User" zope will call User.index_html() or when you visit "website.com/User/View" zope will call User.View() In all of the testing/learning I've done on Zope I'm pretty sure that last item (index_html or View) must be a method, but possible it only needs to have an __doc__ attribute (otherwise Zope will Error) The problem comes when I want to have code put into every method. Perhaps a counter I want to increment on every visit to a User page. I can do this.. def View(self): incrementCounter() # play with data here myHtmlDoc = "pretend this is a uper profile" return myHtmlDoc index_html = View def Edit(self): incrementCounter() # play with data here myHtmlDoc = "editing the user" return myHtmlDoc ... but in reality in my real code that one counter increment line ends up being 20 lines long. An al lot of that "counter code" is actaully setting up variables I'd like to access within the scope of the given method. So if you follow me so far, I was wondering how I might change things to only have one place where I have to maintain the "setup my method" code, which is pretty much a lot of the same code typed over and over into all of the methods. (for a more real life example of things) - def View(self): REQUEST = self.REQUEST SESSION = REQUEST.SESSION dbConnection = self.getDBConnection() logger = self.getLogger() trackStatsHere() # set up some local variables here # change some global variables here try: myHtmlDoc = """make the htmldocument here using all of the previous variables""" # play with data here return myHtmlDoc except: raise "handle the error here" finally: dbConnection.close() index_html = View def Edit(self): REQUEST = self.REQUEST SESSION = REQUEST.SESSION dbConnection = self.getDBConnection() logger = self.getLogger() trackStatsHere() # set up some local variables here # change some global variables here try: myHtmlDoc = """make the htmldocument here using all of the previous variables""" # play with data here return myHtmlDoc except: raise "handle the error here" finally: dbConnection.close() I would ideally like to do something such as this this, or something where I don't have all of that redundant code. def __allmethods__(self, methodname): "gets called when all methods are called" REQUEST = self.REQUEST SESSION = REQUEST.SESSION dbConnection = self.getDBConnection() logger = self.getLogger() trackStatsHere() # set up some local variables here # change some global variables here try: methodname(localvariables) except: raise "handle the error here" finally: dbConnection.close() def View(self, localvariables): myHtmlDoc = """make the htmldocument here using all of the previous variables""" # play with data here return myHtmlDoc index_html = View def Edit(self): myHtmlDoc = """make the htmldocument here using all of the previous variables""" # play with data here return myHtmlDoc __getattr__ almost does the trick but not quite. So any suggestions on how to streamline my code here and make it slightly more maintainable. -- http://mail.python.org/mailman/listinfo/python-list
Re: Redundant code in multiple methods
Genius! Thanks guys that was exactly the help I was looking for. I'll be implementing this later today. I don't forsee any problems, so if I don't post anything else, thank you so much for the help. -- http://mail.python.org/mailman/listinfo/python-list