Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces)

Reply
Thread Tools

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

 
 
Alexander Blinne
Guest
Posts: n/a
 
      12-22-2012
Am 22.12.2012 19:10, schrieb :
> It's for me a view of top side down, but how could the midlevel comunicate to each oter... "not hirachical"


You could use something like the singleton pattern in order to get a
reference to the same datastore-object every time Datastore.Datastore()
is called. But you still need to close the connection properly at some
point, propably using a classmethod Datastore.close().

e.g.:

main.py:

from Datastore import Datastore
from ModbusClient import Modbus
from DaliBusClient import DaliBus

def main():
modbus = Modbus(...)
dalibus = DaliBus(...)

modbus.read_data_and_save_to_store()
dalibus.read_data_and_save_to_store()
Datastore.close()

if __name__=="__main__":
main()


ModbusClient.py:

import Datastore

class Modbus(object):
def read_data_and_save_to_store(self):
datastore = Datastore.Datastore()
#do something with datastore
 
Reply With Quote
 
 
 
 
prilisauer@googlemail.com
Guest
Posts: n/a
 
      12-22-2012
Am Samstag, 22. Dezember 2012 20:29:49 UTC+1 schrieb Alexander Blinne:
> Am 22.12.2012 19:10, schrieb:
>
> > It's for me a view of top side down, but how could the midlevel comunicate to each oter... "not hirachical"

>
>
>
> You could use something like the singleton pattern in order to get a
>
> reference to the same datastore-object every time Datastore.Datastore()
>
> is called. But you still need to close the connection properly at some
>
> point, propably using a classmethod Datastore.close().
>
>
>
> e.g.:
>
>
>
> main.py:
>
>
>
> from Datastore import Datastore
>
> from ModbusClient import Modbus
>
> from DaliBusClient import DaliBus
>
>
>
> def main():
>
> modbus = Modbus(...)
>
> dalibus = DaliBus(...)
>
>
>
> modbus.read_data_and_save_to_store()
>
> dalibus.read_data_and_save_to_store()
>
> Datastore.close()
>
>
>
> if __name__=="__main__":
>
> main()
>
>
>
>
>
> ModbusClient.py:
>
>
>
> import Datastore
>
>
>
> class Modbus(object):
>
> def read_data_and_save_to_store(self):
>
> datastore = Datastore.Datastore()
>
> #do something with datastore



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...

 
Reply With Quote
 
 
 
 
Terry Reedy
Guest
Posts: n/a
 
      12-22-2012
On 12/22/2012 7:45 AM, wrote:
> Am Samstag, 22. Dezember 2012 13:38:11 UTC+1 schrieb prili...@googlemail.com:
>> Am Samstag, 22. Dezember 2012 12:43:54 UTC+1 schrieb Peter Otten:
>>
>>> wrote:

>>
>>>

>>
>>>

>>
>>>

>>
>>>> Hello, to all,

>>
>>>

>>
>>>>

>>
>>>

>>


And my mail reader text window is filled up without any content. If you
are going to post from google groups, learn how to do so without
doubling the spaces each time. Also learn to snip what is not needed for
your reply.

--
Terry Jan Reedy

 
Reply With Quote
 
Alexander Blinne
Guest
Posts: n/a
 
      12-22-2012
Am 22.12.2012 21:43, schrieb :
> 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.


I really don't understand your problem and I don't think anyone else
does. A python programm written like I have explained could do all those
things with no problem whatsoever, if only the classes contain all the
relevant methods.

> 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...


There is no limit at the end of a file. A module is only a namespace and
you can access the names of another module simply by importing it first.
You also can freely pass around objects as parameters in function/method
calls or even just store them to module-level global names. Threads
don't change anything about that.

Greetings.



 
Reply With Quote
 
Dave Angel
Guest
Posts: n/a
 
      12-22-2012
On 12/22/2012 03:43 PM, wrote:
> <snip>
> 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...


Please post in English.



--

DaveA

 
Reply With Quote
 
prilisauer@googlemail.com
Guest
Posts: n/a
 
      12-23-2012
By the way, I think I have found the correct "wording".
for my understood, the "handover" of objects to imported modules doesn't work because, e.g. trying to hand-over an SQLite connection into a imported module, can't work because the "attributes" are not transfered.

I'm sorry for my bad english, it's fascinating. 2 years ago I've written very large english technical documents for my company. As you can see, the last two years I've forgotten a lot and it tooks me some time to get back into.
 
Reply With Quote
 
prilisauer@googlemail.com
Guest
Posts: n/a
 
      12-23-2012
secondly, it is absolutely not bad meaned, but, why does people post, their personal meaning, but nothing about the "Posters" Problem?

Everybody is free to read or not, but correcting the WWW could became a very very big task, (maybe it's easier to climb the 7 summits)

Best Regards.
 
Reply With Quote
 
Cameron Simpson
Guest
Posts: n/a
 
      12-23-2012
On 22Dec2012 12:43, <> 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 <>

Whatever is not nailed down is mine. What I can pry loose is not nailed
down. - Collis P. Huntingdon
 
Reply With Quote
 
prilisauer@googlemail.com
Guest
Posts: n/a
 
      12-23-2012
Thanks to all your answers, I have read a lot about namespaces, but still there's something I do not understood. I have tried your example but as I expected:

line 13, in HandoverSQLCursor
curs.execute("SELECT * FROM lager")
AttributeError: 'builtin_function_or_method' object has no attribute 'execute'

I will try my best to write tomorrow a sample as detailed as possible.

Good evening
 
Reply With Quote
 
prilisauer@googlemail.com
Guest
Posts: n/a
 
      12-23-2012
Thanks to all your answers, I have read a lot about namespaces, but still there's something I do not understood. I have tried your example but as I expected:

line 13, in HandoverSQLCursor
curs.execute("SELECT * FROM lager")
AttributeError: 'builtin_function_or_method' object has no attribute 'execute'

I will try my best to write tomorrow a sample as detailed as possible.

Good evening
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: migrating from python 2.4 to python 2.6 Mark Lawrence Python 0 07-09-2012 11:05 AM
Tabnanny errors when Migrating Python 2.4 code to 2.5 kyosohma@gmail.com Python 3 01-04-2008 10:28 PM
advice required re migrating php app to python and most likely zope Ken Guest Python 2 01-06-2006 10:59 AM
migrating c code to perl chetan Perl Misc 3 01-13-2004 06:35 AM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM



Advertisments