Generate variables at run time?

Mike Meyer mwm at mired.org
Thu Jan 9 11:12:18 EST 2003


"Byron Morgan" <lazypointer at yahoo.com> writes:

> I have been using REXX for years for all my scripting needs. In REXX, I can
> use any string as a variable with no special handling.

I coded Rexx for years as well. Python isn't Rexx, so you'll need to
do things a different way.

> Here is the project:
> 
> A server supplies an alphanumeric data stream over an internet socket. The
> data is output from a train control system. Trains constantly enter and
> leave the controlled territory, and each has a unique ID. I want to use a
> class named  "train", and name each new instance based on this ID. For
> example, train 123 will cause creation of  T123=train(). While the train is
> in the system, it will be frequently updated based on new data, then it will
> be destroyed when the train leaves the system.
> 
> for the curious:
> In REXX, for a similar situation, I use a stem "train", then add the train
> number, then add each attribute as events are reported.
> train. = 0
> train.123 = 1 (value represents the number of cars in train)
> train.123.stat = 1 (0 if stopped, 1 if moving)
> train.123.doors = 1 (1 if closed, 0 if open)

The closest things to stemmed variables is a dictionary. In fact,
they're almost the same thing, just with different syntax.

Assuming you have a class Train, here's how to

Create the list of trains: train = {}
Create a new train: train[123] = Train()
Set the status: train[123].stat = 1
Read the status: train[123].stat

Close the door on all stopped trains:
for t in train.values():
    if t.stat: t.door = 1

Get a list of the names of all stopped trains:
    [x for x in train.keys() if train[x].stat]
        # Note: in recent pythons, "train.keys()" can be replaced by just "train"

While I miss the easy access to applications, I generally find that
programming in python is much more pleasant than programming in Rexx.

        <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.




More information about the Python-list mailing list