473,385 Members | 1,375 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Help setting default class attributes

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

Sep 6 '07 #1
3 1277
On Sep 6, 6:26 pm, rh0dium <steven.kl...@gmail.comwrote:
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

Sep 6 '07 #2
On Sep 6, 10:26 am, rh0dium <steven.kl...@gmail.comwrote:
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 '07 #3
On Sep 6, 10:55 am, Arnaud Delobelle <arno...@googlemail.comwrote:
On Sep 6, 6:26 pm, rh0dium <steven.kl...@gmail.comwrote:
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!!
Sep 6 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
6
by: the.theorist | last post by:
I have a small, simple class which contains a dictionary (and some other stuff, not shown). I then have a container class (Big) that holds some instances of the simple class. When I try to edit the...
5
by: Neal | last post by:
Hi I need to change my mousepointer when the mouse moves over certain populated controls on my aspx page. I've tried the following but without success (after visiting a site recommended from...
3
by: Marty McFly | last post by:
Hello, I have a control class that inherits from System.Web.UI.WebControls.Button. When I drag this control from the "My User Controls" tab in the toolbox onto the form, I want it to reflect the...
11
by: pamelafluente | last post by:
I am doing my own PrintDialog, and have placed there a combo with the printer names, as in the PrintDialog provided by VB.NET. Here is the question: how do I open the native windows printer...
0
by: jts2077 | last post by:
I am trying to create a large nested XML object using E4X methods. The problem is the, the XML I am trying to create can only have xmlns set at the top 2 element levels. Such as: <store ...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
5
by: giddy | last post by:
Hi , I'm probably just doing something a little silly. I get a System.Configuration.ConfigurationErrorsException! This is my config file: <configSections> <section name="qConfig"...
7
by: George Sakkis | last post by:
A situation that often comes up is having to initialize several instance attributes that accept a default value. For a single class, passing the default values in __init__ is fine: class...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.