473,396 Members | 1,738 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,396 software developers and data experts.

automating assignment of class variables

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
Jul 18 '05 #1
0 1105

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

Similar topics

50
by: Charles Stapleton | last post by:
Given the folowing class class Ctest{ public: Ctest( int i, int j) :a(i) { b = j; } private: int a, b; } When creating an object of type Ctest, what advantage is there to setting
14
by: Tony Johansson | last post by:
Hello Experts! Assume I have a class called SphereClass as the base class and a class called BallClass that is derived from the SphereClass. The copy constructor initialize the left hand object...
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
7
by: xzzy | last post by:
I need to automate a report in a different database (and thank yous to Terry Kreft for pointing me in the right direction). below is the code with the one line that does not work, marked: 'Does...
37
by: Tim N. van der Leeuw | last post by:
Hi, The following might be documented somewhere, but it hit me unexpectedly and I couldn't exactly find this in the manual either. Problem is, that I cannot use augmented assignment operators...
3
by: Pulpeet | last post by:
I have a class with const member variables. I want to put objects from that class in a vector. For adding objects to the vector, the compiler (g++ 4.0.1 on mac os x 10.4.8) requires me to provide a...
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
3
by: jerry.teshirogi | last post by:
I have the following class and main: ////////////////////////////////////////////////////////// #include <iostream.h> class myVector { public: double x, y, z:
1
by: khalfan | last post by:
Assignment Objectives: The objective of this assignment is to implement java programs with major object oriented concepts and applet with event handling. Assignment Purposes This assignment aims...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.