finding the right data structure...?

D

David Hoare

Hi all...
I'm a python newbie, migrating from a fair bit of VB experience.

I have an application in VB that I was using as a sample to 're-create'
it using python. It involved storing information about sound and light
cues (this was for a lighting / sound theater controller).

The data was stored like this:
- a custom 'type' called lightCue which looked like this:
Type lightCue
Name As String 'optional short name
Description As String 'optional longer description
CueLine As String 'optional line in script where cue occurs
upTime As Single 'time for lights fading UP
downTime As Single 'time for lights fading down
chanLevel(0 To maxChan) As Single 'stored intensity level (0 to 100) for each channel on the lighting console
End Type

I then created an array...
dim lq(1 to 200) as lightCue
....allowing me to iterate through the list of cues for any information I
needed

I am struggling to come up with a similar (in function) way to do this
in Python. Any ideas? I think I understand about classes, but then I
need to 'name' each instance of that class when they are created, don't I?

Thanks for your guidance!
David Hoare
(e-mail address removed)
 
L

Larry Bates

There's more than one way, but here is one:

Create a class that is the equivalent of lightCue type:

class lightCue:
def __init__(self, Name, Description, CueLine,
upTime, downTime, chanLevel):

self.Name=Name
self.Description=Description
self.CueLine=CueLine
self.upTime=upTime
self.downTime=downTime
self.chanLevel=chanLevel
return

lq=[] # Create an empty list
lq.append(lightCue('Name1','Description2',
'Cueline1', 10, 10, 1))
lq.append(lightCue('Name2','Description2',
'Cueline2', 11, 11, 2))
....

for entry in lq:
print "--------------------------------------------"
print "Name..........%s" % entry.Name
print "Description...%s" % entry.Description
print "CueLine.......%s" % entry.CueLine
print "upTime........%i" % entry.upTime
print "downTime......%i" % entry.downTime
print "chanLevel.....%i" % entry.chanLevel

You don't have to "name" the instances of the class,
you just need to 'put' it somewhere that you can
reference it later. The easiest thing is to append
the instance on to a list (but there might be cases
when you want to insert them into the values of a
dictionary). One of the things that is 'different'
about Python is that everything (values, functions,
classes, etc) are all just an objects.

a=1

means a is a pointer to an object that contains an
integer 1

a=Class()

means a is a pointer to an object that contains a
class instance.

You can have classes inside of classes inside of
classes... you get the picture. They are all just
pointers to the object.

lq[0] is a pointer to the first lightCue class you
appended to list lq.

lq[0].Name is a pointer to the Name attribute of
that class.

lq[0].Name[0] is a pointer to the first character
of that Name attribute in the first lightCue instance
in list lq.

Everything is a pointer and you can do whatever you
want with assignment (even clobber Python's built
in functions). Common newbie mistake:

str="abc"

Now the str() function is gone and replaced by a
string that contains "abc".

Hope information helps,
Larry Bates
Syscon, Inc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,654
Latest member
LannySinge

Latest Threads

Top