Help setting default class attributes

R

rh0dium

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]))
 
A

Arnaud Delobelle

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)
 
A

attn.steven.kuo

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
 
S

Steve Holden

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
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:
.... caseSensitivity = 'preserve'
.... cellMapTable = 'checkPolygon'
.... # ...
....
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 -------------
 
R

rh0dium

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!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top