[Tutor] Use iterator to refer to an object's attribute?
Ron Britton
9bh4frs02 at sneakemail.com
Thu Apr 20 08:51:02 CEST 2006
Short version: How do I use an iterator to refer to an object's
attribute? E.g., "z" is a list of attributes of "b":
for x, y in z:
for a in b.x.y
Also, any suggestions for my overall structure below?
----------
Long version:
I need to make a program to send commands to an instrument. I've been
working on it for a while, and my employer is starting to get itchy.
Here's a simplified description:
• The instrument has three modules (motherboard, controller, and end
user).
• Each module has an arbitrary number of components (motherboard has
internal camera; controller has major and minor controllers; and end
user has x-, y-, and z-motors).
• Each component has an arbitrary number of commands.
• Each command has a fixed number of attributes.
I will ultimately create a GUI with a 4-pane browser:
• Pane 1 will display the modules. When the user clicks on a module,
its components will appear in pane 2.
• When the user clicks on a component, its commands will appear in pane
3.
• When the user clicks on a command, its parameters will appear in pane
4.
• The user will then click a Send button, which will send the command
and its parameters to the instrument.
I haven't coded the GUI yet, because I'm stuck on the underlying
structure.
Here's how I'm tackling it:
• I have a bunch of lists that have everything defined in them
(module_list; cpt_??_lists for components; and cmd_??_lists for
commands).
• I'm creating a database object, "db", which will hold everything.
• I iterate across the module list. The idea here is to create within
the database one module object for each real object in the instrument,
in this case three. I should end up with: db.mb, db.ct, and db.eu.
• There is a short name (for use by the program), a long name (which
will be displayed to the user), and the name of that module's component
list.
• I also create an empty list object, which will ultimately be
populated with the contents of that module's component list.
• Then (theoretically) for each module, I iterate across that module's
component list to create several component objects inside the module (I
will then continue this process, creating command objects within the
components.).
----------
My immediate problem is that Python doesn't let me use the iterator as
the name of an object's attribute. E.g.:
I have object "db.mb". I have iterator "shortmod" with a value of
"mb". Why can't I call "db.shortmod"?
Also, if you have any better ideas on how to put this thing together,
I'm all ears. The reason for doing it this way is that I wanted my
database to be the logical equivalent of the instrument.
Here's the error message I get:
----------
Traceback (most recent call last):
File "/Developer/Python/CZM Scripts/CmdList.py", line 111, in
-toplevel-
main()
File "/Developer/Python/CZM Scripts/CmdList.py", line 108, in main
fillout_DB(db)
File "/Developer/Python/CZM Scripts/CmdList.py", line 62, in
fillout_DB
for shortcpt, longcpt, cmdlist in filldb.shortmod.cptlist:
AttributeError: Database instance has no attribute 'shortmod'
----------
My code follows. If you jump down to the section marked
"#---Logic---", you should be able to see the immediate culprit.
I greatly appreciate any help that anybody on this list can provide!
Thanks!
Ron
************************************************************************
**
#---
Definitions-----------------------------------------------------------
# shortname, longname, component list name
#---Modules
module_list = [
['mb', 'Motherboard', 'cpt_mb_list'],
['ct', 'Controller', 'cpt_ct_list'],
['eu', 'End User', 'cpt_eu_list']]
#---Components
# shortname, longname, command list name
# Motherboard
cpt_mb_list = [
['icam', 'Internal Camera', 'cmd_icam_list']]
# Controller
cpt_ct_list = [
['maj', 'Major Controller', 'cmd_maj_list'],
['min', 'Minor Controller', 'cmd_min_list']]
# End User
cpt_eu_list = [
['xmtr', 'X Motor', 'cmd_xmtr_list'],
['ymtr', 'Y Motor', 'cmd_ymtr_list'],
['zmtr', 'Z Motor', 'cmd_zmtr_list']]
#---Commands
# Abbreviation, string, command, param1, param2
# Motherboard Commands
cmd_icam_list = [
['ss', 'Shutter Speed', 'shutSpeed', 0, 0],
['ap', 'Aperture', 'aperture', 0, 0],
['exp', 'Exposure', 'exposure', 0, 0]]
# Controller Commands
cmd_maj_list = [
['on', 'Turn it on', 'turnON', 0, 0],
['off', 'Turn it off', 'turnOFF', 0, 0]]
cmd_min_list = [
['low', 'Set Power Low', 'setLow', 0, 0],
['hi', 'Set Power High', 'setHigh', 0, 0]]
# End User Commands
cmd_xmtr_list = [
['xpos', 'Go to X-Position', 'xPosition', 0, 0]]
cmd_ymtr_list = [
['ypos', 'Go to Y-Position', 'yPosition', 0, 0]]
cmd_zmtr_list = [
['zpos', 'Go to Z-Position', 'zPosition', 0, 0]]
#---
Logic-----------------------------------------------------------------
#****************My immediate problem occurs in this
object***************
def fillout_DB(filldb):
'''Build the database of modules, components, and commands.
Feed it an empty database, and it will fill it out.'''
# Add modules
for shortmod, longmod, cptlist in module_list:
filldb.addMod([shortmod, longmod, cptlist])
print filldb.modlist
print 'Just loaded %r, %r, and %r' % (shortmod, longmod,
cptlist)
# Add components
#********************The following line
chokes********************
for shortcpt, longcpt, cmdlist in filldb.shortmod.cptlist:
# It doesn't get to next line. Suspect it won't work
either.
filldb.shortmod.addCpt([shortcpt, longcpt, cmdlist])
print 'Just loaded %r, %r, and %r' % (shortcpt, longcpt,
cmdlist)
print 'Finished buildModules()!!!!'
return filldb
class Database:
'''This is the root class. The database contains all of the module
objects. Each module contains all of its component objects, etc.'''
def __init__(self):
self.modlist = []
def addMod(self, mod):
self.modlist.append(mod)
class Module:
def __init__(self, shortname, longname, cptlistname):
self.name = shortname
self.longname = longname
self.cptlistname = cptlistname
self.cptlist = []
def addCpt(self, cpt):
self.cptlist.append(cpt)
class Component:
def __init__(self, shortname, longname, cmdlistname):
self.name = shortname
self.longname = longname
self.cmdlistname = cmdlistname
self.cmdlist = []
def addCmd(self, cmd):
self.cmdlist.append(cmd)
class Command:
def __init__(self, shortname, longname, actualcommand, param1,
param2):
self.name = shortname
self.longname = longname
self.actcmd = actualcommand
self.paramlist = [param1, param2]
def main():
print "It's working so far!"
db = Database()
fillout_DB(db)
if __name__ == "__main__":
main()
More information about the Tutor
mailing list