Help setting default class attributes

rh0dium steven.klass at gmail.com
Thu Sep 6 15:31:34 EDT 2007


On Sep 6, 10:55 am, Arnaud Delobelle <arno... at googlemail.com> wrote:
> 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)
>

Hey this is great!!!  Simple and clear.   I appreciate it!!





More information about the Python-list mailing list