[Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces)
Alexander Blinne
news at blinne.net
Mon Dec 24 08:10:23 EST 2012
At this point I think i could just refer to my other 2 postings and urge
you to read them again. They offer the idea of encapsulating the
function QuerySqlite into a method of an object that can be passed over
to some object (possibly throu the __init__-method) and store it in an
attribute of that other object. Those other objects can then simply call
the method belonging to the object.
If you really don't understand what I mean by this maybe you should
learn a bit about the basics of object-oriented programming.
Some pseudo-code illustrating this idea (which differs a bit from the
first singleton-like suggestion):
datastore.py:
class Datastore(object):
def __init__(self, some_args):
#do all things needed to open datastore and store everything to
#self.something and self.someotherthing
def query(self, query, *values):
#execute query with values inserted
#using self.something and self.someotherting
#return result
modbus.py:
class Modbus(self):
def __init__(self, datastore):
#store the argument datastore to an attribute of the newly
#created object
self.datastore = datastore
def read_bus(self, sensor):
#read from bus the value of sensor and return value
def read_temp_and_store(self, sensor):
#read and store
value = self.read_bus(sensor)
self.datastore.query("some query string", value)
scheduler.py:
class Scheduler(object):
def __init__(self, datastore, modbus):
#store the arguments datastore and modbus to attributes
#of the newly created object
self.datastore = datastore
self.modbus = modbus
#maybe read some config data from datastore
self.config = self.datastore.query("some initialising query if
necessary")
def do_things(self):
#do things you wanna do, perhaps in some loop or in a thread or
#something, does not really matter.
#Threading may require locking of some kind, but this also is
#not really related to your problem as I understand ist.
self.modbus.read_temp_and_store("sensor1")
main.py:
from scheduler import Scheduler
from datastore import Datastore
from modbus import Modbus
def main():
datastore = Datastore(some_args)
modbus = Modbus(datastore)
scheduler = Scheduler(datastore, modbus)
scheduler.do_things()
if __name__=="__main__":
main()
Please feel free to ask specific questions about this approach.
merry christmas everyone
Alexander Blinne
More information about the Python-list
mailing list