Help setting default class attributes
Arnaud Delobelle
arnodel at googlemail.com
Thu Sep 6 13:55:06 EDT 2007
On Sep 6, 6:26 pm, rh0dium <steven.kl... at gmail.com> wrote:
> Hi all,
>
> I have the following piece of code and I wanted to set the default
> attributes based on a dictionary. What I am looking for is a way to
> take PIPODEFAULTS and assign each one as an attribute for the class
> pipo. Can someone show me how to do this by iterating over the
> PIPODEFAULTS and assign them. What I would expect to be able to do is
> call the class and modify them.
Use the setattr(...) function.
> example:
> a = pipo()
> print a.caseSensitivity
> "preserve"
>
> a.caseSensitivity = "lower"
> print a.caseSensitivity
> "lower"
>
> Lastly - here is my code:
>
> class pipo:
>
> PIPODEFAULTS={ "caseSensitivity" : "preserve","cellMapTable" :
> "","checkPolygon" : "nil","compression" : "none",
> "convertDot" : "ignore","convertPathToPoly" :
> "nil","convertToGeo" : "nil","dumpPcellInfo" : "nil",
> "snapToGrid" : "nil","techFileChoice" :
> "nil","units": "micron","useParentXYforText" : "nil","viewName" :
> "layout",
> }
>
> def __init__(self, *args, **kwargs):
> """This simply will run a PIPO stream out
> """
> # Setup Logging
> self.pipoargs=self.setdefaults()
> def setdefaults(self):
> for x in self.PIPODEFAULTS:
> self.log.debug("Setting %s to %s" % (x,
> self.PIPODEFAULTS[x]))
def setdefaults(self):
for key, val in self.PIPODEFAULTS.iteritems():
setattr(self, key, val)
OR (but I prefer the one above)
def setdefaults(self):
self.__dict__.update(self.PIPODEFAULTS)
--
Arnaud
More information about the Python-list
mailing list