Hi to everyone who has repsonded. I'll try to clarify my problem in more detail. I believe I have the answers how to assign to self. in superclasses. In case you would know of yet another way, let me know. ;)
Steve Holden wrote:
Martin MOKREJŠ wrote:
Diez B. Roggisch wrote:
See my post on Mar 2 about "automating assignment of class variables". I got no answers, maybe I wasn't clear enough ... :(
class Foo: def __init__(self, a, b, .....): self.a = a self.b = b ....
If that is what you want, then this might help you: Put all the values in a
The data passed to an instance come from sql tables. I have the idea every table will be represented by an object. At the most upper level of abstraction, I work with, partly overlapping set of tables.
imagine two objects, X and Y. X refers to table T1, T2, T3 while Y refers to T1, T3, T4, T5.
let's say: T1 has columns T1C1, T1C2, T1C3 T2 has columns T2C1, T2C2, T2C3, ... and so on
object t1 is something like:
class t1: def __init__(self, T1C1, T1C2, T1C3=None): self.T1C1 = T1C1 self.T1C2 = T1C2 self.T1C3 = T1C3
similarly in case of t2 or any other table. The column names/types are different, also requirements for non-NULL values differ (T1C3 above is allowed to be empty). T1C2 is ENUM type, for example which must be equal ether to "a" or "b" or "d" ... etc.
object X is something like:
class x: def __init__(self, t1object, t2object, t3object): self.t1object = t1object self.t2object = t2object self.t3object = t3object self.xml_root = cElementTree.Element("xml root")
# and now the boring stuff comes self.xml_t1 = cElementTree.SubElement(self.xml_root, "table 1") self.xml_t1_T1C1 = cElementTree.SubElement(self.xml_T1C1, "name of T1C1 comlumn") self.xml_t1_T1C1.text = self.t1object.T1C1 self.xml_t1_T1C2 = cElementTree.SubElement(self.xml_T1C2, "name of T1C2 comlumn") self.xml_t1_T1C2.text = self.t1object.T1C2 # ... and more or less the same for tables t2 and t3
def set_more_data_from_optional_tables(self, t6object, t7object): # I'd do (self, anyobject) on the line above, but the objects represent different # sql tables, so different variable names apply. I believe the best I could do # is to walk their __dict__ so I could assign the data under self.xml_root ... # can any of teh XML: libraries represent an object in XML? self.xml_t6_T6C1 = cElementTree.SubElement(self.xml_T6C1, "name of T6C1 comlumn") self.xml_t6_T6C1.text = self.t6object.T1C1 self.xml_t6_T6C2 = cElementTree.SubElement(self.xml_T6C2, "name of T6C2 comlumn") self.xml_t6_T6C2.text = self.t6object.T1C2 # ... and more or less the same for tables t7
class y: def __init__(self, t1object, t3object, t4object, t5object=None): self.t1object = t1object self.t3object = t3object self.t4object = t4object if t5object: self.t5object = t5object
self.xml_root = cElementTree.Element("xml root")
# now the code from x.__init__() to table t1 and t3 would be cut&pasted, # or now I can say any of the two or three approaches suggested in this # thread will be used
Well, consider that you haven't actually made any kind of a case for using variables!Good guess! ;)
dictionary - like this:
my_vals = {"a": 1, "b" : 2, ....}
There are plenty of other ways to create such a dictionary, but I won't digress on that here.
Now in your class, you pass than dict to your constructor and then simply
update the instance's __dict__ so that the keys-value-pairs in my_vals
become attributes:
class Foo:
def __init__(self, my_vals): self.__dict__.update(my_vals)
foo = Foo(my_vals)
print foo.a
-> 1
Hope this helps,
Sure. Thanks! Would you prefer exactly for this method over the method posted by Kent Johnson
few minutes ago?
Am I so deperately fighting the language? No-one here on the list needs to set hundreds
variables at once somewhere in their code? I still don't get why:
OK, imagine I want to read the data from sql into memory and check the values (there're some conditions they have to fullfill). Therefore, once I get the classes defined, I'll implement methods to check every table t1 to tx for it's contents. ^H^H^H^H^H^H^H^H^H^H^H^H Actually, the checks have to be based on X or Y instance. The check differs between X.t1 and Y.t1. :(
If you ask me why do I have such a mess, my answer is that I don't want to store same datatype into two different tables. So the location is only one, but the value has to be filled in only when X and is unused when Y. Similarly it happens when some foreign keys are/aren't defined.
If the names of these things are dynamic then why would you want to put them in an object's namespace, when you could just as easily include
self.insDict = {}
in your __init__() method and then simply update self.insDict whenever you want.
If you "set hundreds variables", and their names aren't predictable, then presumably you will have to go through similar contortions to access them.
So it is generally simpler just to use a dictionary to hold the values whose names aren't known in advance. Techniques for inserting names into namespaces are known (as you have discovered), but when a beginner wants to know about them it's usually considered a sign of "fighting the language".
I can't smash everything into dictionary, I'd have to keep about 3 or 4-levels of dictionaries withing dictionaries. Imagine, t10 refers to rows in tables t8 and t9 but also t20 refers to other rows in t8 and t9. I may not loose the information what refers to what.
The easiest is to use variables. Anyway, I need them to test their content. Actually, also to assign to them new data to write into the database. I have about 20 tables and about 250 columns in total. And I use foreign keys a lot.
So, think about this a while and then tell us exactly *why* it's so important that these values are stored in variables.
"include somefile.py" would be that non-pythonic so that it's not available, but
I have already two choices how to proceed anyway. Thanks. ;)
Now have to figure out how to assign them easily into the XML tree.
martin
You do know there are lots of Python libraries that support XML, right?
Well, see baove. ;)
M. -- http://mail.python.org/mailman/listinfo/python-list