Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Help setting default class attributes

Reply
Thread Tools

Help setting default class attributes

 
 
rh0dium
Guest
Posts: n/a
 
      09-06-2007
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]))

 
Reply With Quote
 
 
 
 
Arnaud Delobelle
Guest
Posts: n/a
 
      09-06-2007
On Sep 6, 6:26 pm, rh0dium <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
attn.steven.kuo@gmail.com
Guest
Posts: n/a
 
      09-06-2007
On Sep 6, 10:26 am, rh0dium <(E-Mail Removed)> 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


 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      09-06-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> On Sep 6, 10:26 am, rh0dium <(E-Mail Removed)> 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 -------------

 
Reply With Quote
 
rh0dium
Guest
Posts: n/a
 
      09-06-2007
On Sep 6, 10:55 am, Arnaud Delobelle <(E-Mail Removed)> wrote:
> On Sep 6, 6:26 pm, rh0dium <(E-Mail Removed)> 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!!


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
class attributes & data attributes james_027 Python 2 06-20-2007 03:12 PM
Can you set a class instance's attributes to zero by setting the instance to zero? Gerard Flanagan Python 3 11-19-2005 06:58 PM
Setting Class Attributes the.theorist Python 6 10-26-2005 05:05 AM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
tomcat 4.x : setting mime type for a directory or setting a default mime type CJ Java 1 10-29-2004 07:51 PM



Advertisments