Help setting default class attributes

Discussion in 'Python' started by rh0dium, Sep 6, 2007.

  1. rh0dium

    rh0dium Guest

    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.

    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]))
    rh0dium, Sep 6, 2007
    #1
    1. Advertising

  2. On Sep 6, 6:26 pm, rh0dium <> 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
    Arnaud Delobelle, Sep 6, 2007
    #2
    1. Advertising

  3. rh0dium

    Guest

    On Sep 6, 10:26 am, rh0dium <> 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.
    >
    > example:
    > a = pipo()
    > print a.caseSensitivity
    > "preserve"
    >
    > a.caseSensitivity = "lower"
    > print a.caseSensitivity
    > "lower"
    >



    I infer from your example that you want
    to set default attributes for *instances of* class pipo
    (not for class pipo itself).

    Use setattr:

    class pipo(object):
    PIPODEFAULTS = {'caseSensitivity':'preserve',
    'cellMapTable':'checkPolygon', # etc
    }
    def __init__(self, *args, **kwargs):
    for attr, value in pipo.PIPODEFAULTS.iteritems():
    setattr(self, attr, value)

    a = pipo()
    b = pipo()
    print a.caseSensitivity
    a.caseSensitivity = 'lower'
    print a.caseSensitivity
    print b.caseSensitivity

    --
    Hope this helps,
    Steven
    , Sep 6, 2007
    #3
  4. rh0dium

    Steve Holden Guest

    wrote:
    > On Sep 6, 10:26 am, rh0dium <> 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.
    >>
    >> example:
    >> a = pipo()
    >> print a.caseSensitivity
    >> "preserve"
    >>
    >> a.caseSensitivity = "lower"
    >> print a.caseSensitivity
    >> "lower"
    >>

    >
    >
    > I infer from your example that you want
    > to set default attributes for *instances of* class pipo
    > (not for class pipo itself).
    >
    > Use setattr:
    >
    > class pipo(object):
    > PIPODEFAULTS = {'caseSensitivity':'preserve',
    > 'cellMapTable':'checkPolygon', # etc
    > }
    > def __init__(self, *args, **kwargs):
    > for attr, value in pipo.PIPODEFAULTS.iteritems():
    > setattr(self, attr, value)
    >
    > a = pipo()
    > b = pipo()
    > print a.caseSensitivity
    > a.caseSensitivity = 'lower'
    > print a.caseSensitivity
    > print b.caseSensitivity
    >
    > --
    > Hope this helps,
    > Steven
    >
    >

    This is actually way over-complicated. Remembering that the MRO will
    look in the class to resolve the name of an attribute it doesn't find in
    the instance you can simply use the class as a repository for the defaults:

    >>> class pipo(object):

    .... caseSensitivity = 'preserve'
    .... cellMapTable = 'checkPolygon'
    .... # ...
    ....
    >>> p1 = pipo()
    >>> p2 = pipo()
    >>> p1.caseSensitivity

    'preserve'
    >>> p2.caseSensitivity = "lower"
    >>> p1.caseSensitivity

    'preserve'
    >>> p2.caseSensitivity

    'lower'
    >>>


    You could extend the class to allow per-instance defaults to be set in
    the __init__() method too (untested):

    class pipo(object):
    caseSensitivity = "preserve"
    cellMapTable = 'checkPolygon'
    def __init__(self, ..., **kw):
    self.__dict__.update(kw)

    p1 = pipo(caseSensitivity='lower')

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://del.icio.us/steve.holden
    --------------- Asciimercial ------------------
    Get on the web: Blog, lens and tag the Internet
    Many services currently offer free registration
    ----------- Thank You for Reading -------------
    Steve Holden, Sep 6, 2007
    #4
  5. rh0dium

    rh0dium Guest

    On Sep 6, 10:55 am, Arnaud Delobelle <> wrote:
    > On Sep 6, 6:26 pm, rh0dium <> 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!!
    rh0dium, Sep 6, 2007
    #5
    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. CJ
    Replies:
    1
    Views:
    1,566
    Andrew Thompson
    Oct 29, 2004
  2. E11
    Replies:
    1
    Views:
    4,743
    Thomas Weidenfeller
    Oct 12, 2005
  3. the.theorist

    Setting Class Attributes

    the.theorist, Oct 25, 2005, in forum: Python
    Replies:
    6
    Views:
    314
    Ron Adam
    Oct 26, 2005
  4. Gerard Flanagan
    Replies:
    3
    Views:
    445
    Terry Hancock
    Nov 19, 2005
  5. james_027

    class attributes & data attributes

    james_027, Jun 20, 2007, in forum: Python
    Replies:
    2
    Views:
    370
    Bruno Desthuilliers
    Jun 20, 2007
Loading...

Share This Page