Hi, I need to build 3 classes for my work, but each have dozens or hundreds of variables, self bound. I have about 250 columns in about 10 mysql tables. I can squeeze everything into 10 classes, each representing single table. I happily utilize the argument checks when a class is instantiated, so that some variable must be non-empty. But, variables corresponding to ID's (foreign keys) are allowed to be empty, as these objects I want to be able to hold data to be saved, hence no requirements for ID's to be non-empty.
Two classes define a two partly different combinations of MySQL tables to be manipulated. The overlapping part in in superclass called "kommon".
I can save some typing using the superclass approach, but still find it boring to do:
class kommon: def __init__(self): self.root = cElementTree.Element("xml dump")
def set_submitter(self, submitter_object): # keep the reference to an external object hodling important data self.submitter = submitter_object
# xml stuff to represent that data # submitter = cElementTree.SubElement(self.root, "submitter") first_name = cElementTree.SubElement(submitter, "first name") first_name.text = self.submitter.first_name last_name = cElementTree.SubElement(submitter, "last name") last_name.text = self.submitter.last_name email = cElementTree.SubElement(submitter, "email") email.text = self.submitter.email login = cElementTree.SubElement(submitter, "login") login.text = str(self.submitter.login) user_id = cElementTree.SubElement(submitter, "user_id") user_id.text = str(self.submitter.user_id)
# more abstract and more multiplicated example def set_a(self, b_object, c_object, d_object): # a, b, c, d are objects, containing variable names corresponding to every # column in MySQL table, where table a points to row in tables b, c, d # Actually, we must know the data held in b, c, d as the data are not yet flushed into # MySQL. self.b = b self.c = c self.d = d
b_ = cElementTree.SubElement(self.root, "B") b_var1 = cElementTree.SubElement(b_, "VAR 1") b_var1.text = a.var1 b_var2 = cElementTree.SubElement(b_, "VAR 2") b_var2.text = b.var2 # ... many times the same logic
c_ = cElementTree.SubElement(a_, "C")
c_var1 = cElementTree.SubElement(c_, "VAR 1")
c_var1.text = c.var1
# ... many times the same logic
d_ = cElementTree.SubElement(a_, "D")
d_var1 = cElementTree.SubElement(d_, "VAR 1")
d_var1.text = d.var1
# ... many times the same logic
def set_x(self, c_object, d_object):
# damn, now I'm almost cut&pasting c_ = cElementTree.SubElement(self.root, "C")
c_var1 = cElementTree.SubElement(c_, "VAR 1")
c_var1.text = c.var1
# ... many times the same logic
d_ = cElementTree.SubElement(c_, "D") d_var1 = cElementTree.SubElement(d_, "VAR 1") d_var1.text = d.var1 # ... many times the same logic
The code outlined above has actually one serious error. I want to to self.a_ etc, not a_ as it gets overwritten by successive instances of the same class. But am lazy to fix it now as I believe it's anyway waste of time. ;) Thansk for help! Martin -- http://mail.python.org/mailman/listinfo/python-list