[Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces)

Cameron Simpson cs at zip.com.au
Sun Dec 23 05:55:07 EST 2012


On 22Dec2012 12:43, prilisauer at googlemail.com <prilisauer at googlemail.com> wrote:
| I Think I describe my Situation wrong, the written Project is a
| Server, that should store sensor data, perfoms makros on lamps according
| a sequence stored in the DB and Rule systems schould regulate home devices and plan scheduler jobs so on.
| 
| The System Runs in a threated environment. It looks for me, like the
| limits are at the end of file. my core problem also the only one I have
| is: I don't know how to get over that limits and enable dataexchange
| like a backbone...

Maybe you should post some of the Perl code, in small pieces. Then we
can suggest ways those poarticular things might be done in Python.

Python threads really easily (with some limitations, but for many purposes
those limitations are irrelevant).

When I have a situation like yours seems to be, I tend to write a few
different items, connected together with a main program. Write modules
that define a class for the things you need to talk to: the database,
the sensors, etc. From the main program, create an instance of the
relevant classes, then dispatch threads doing what needs to be done.

The main program might be shaped like this:

  import db_module      # use a better name
                        # defines a class called "DB" to talk to a
                        # database
  import sensors_module # use a better name
                        # defines a class called "Sensors" to report
                        # sensor values

  def thread_function(db, sensors):
    ... do something that should happen in a thread ...

  # get some objects to connect to db and sensors
  db = db_module.DB(connection-info-here...)
  sensors = sensors_module.Sensors(sensor-connection-info-here...)

  # set up a Thread and start it
  T = Thread(target=thread_function, args=(db, sensors))
  T.start()
  ... create other threads as needed ...

You see here that:
  - the modules do not know _specifics_ about the db or sensors;
    they are told connection info
  - instantiating a class instance:
      db = db_module.DB(connection-info-here...)
    passes the specifics
  - you get back a class instance
  - you pass those instances (db, sensors) to the thread_function;
    it uses them to access database and sensors

So you see that the modules do not directly share information with each
other. The main program gets objects from each module and hands them to
whoever needs to work with them.

Does this clarify your namespace issues?

Cheers,
-- 
Cameron Simpson <cs at zip.com.au>

Whatever is not nailed down is mine.  What I can pry loose is not nailed
down. - Collis P. Huntingdon



More information about the Python-list mailing list