Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #35352 > unrolled thread
| Started by | prilisauer@googlemail.com |
|---|---|
| First post | 2012-12-22 02:59 -0800 |
| Last post | 2012-12-23 01:36 -0800 |
| Articles | 20 on this page of 60 — 16 participants |
Back to article view | Back to comp.lang.python
[Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) prilisauer@googlemail.com - 2012-12-22 02:59 -0800
Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) Peter Otten <__peter__@web.de> - 2012-12-22 12:43 +0100
Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) prilisauer@googlemail.com - 2012-12-22 04:38 -0800
Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) Peter Otten <__peter__@web.de> - 2012-12-22 14:54 +0100
Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) prilisauer@googlemail.com - 2012-12-22 08:36 -0800
Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) prilisauer@googlemail.com - 2012-12-22 08:36 -0800
Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) prilisauer@googlemail.com - 2012-12-22 04:38 -0800
Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) prilisauer@googlemail.com - 2012-12-22 04:45 -0800
Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) Terry Reedy <tjreedy@udel.edu> - 2012-12-22 16:30 -0500
Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) prilisauer@googlemail.com - 2012-12-22 04:45 -0800
Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) Alexander Blinne <news@blinne.net> - 2012-12-22 18:26 +0100
Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) prilisauer@googlemail.com - 2012-12-22 10:10 -0800
Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) Alexander Blinne <news@blinne.net> - 2012-12-22 20:29 +0100
Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) prilisauer@googlemail.com - 2012-12-22 12:43 -0800
Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) Alexander Blinne <news@blinne.net> - 2012-12-22 22:59 +0100
Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) prilisauer@googlemail.com - 2012-12-23 01:32 -0800
Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) Terry Reedy <tjreedy@udel.edu> - 2012-12-23 20:53 -0500
Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) Alexander Blinne <news@blinne.net> - 2012-12-24 14:10 +0100
Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) Dave Angel <d@davea.name> - 2012-12-22 17:03 -0500
Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) Cameron Simpson <cs@zip.com.au> - 2012-12-23 21:55 +1100
Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) prilisauer@googlemail.com - 2012-12-23 12:59 -0800
Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) Peter Otten <__peter__@web.de> - 2012-12-23 22:14 +0100
Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) prilisauer@googlemail.com - 2012-12-23 12:59 -0800
Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) Roy Smith <roy@panix.com> - 2012-12-23 16:15 -0500
Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) prilisauer@googlemail.com - 2012-12-23 13:42 -0800
Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) "Rhodri James" <rhodri@wildebst.demon.co.uk> - 2012-12-23 23:38 +0000
Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-12-24 00:01 -0500
Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) Thomas Bach <thbach@students.uni-mainz.de> - 2012-12-24 06:18 +0100
Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) Dave Angel <d@davea.name> - 2012-12-24 00:19 -0500
Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) prilisauer@googlemail.com - 2012-12-24 00:23 -0800
Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) Cameron Simpson <cs@zip.com.au> - 2012-12-24 21:32 +1100
Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) Dave Angel <d@davea.name> - 2012-12-24 10:48 -0500
Re: [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) Ranting Rick <rantingrickjohnson@gmail.com> - 2012-12-24 21:10 -0800
Re: [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) prilisauer@googlemail.com - 2012-12-25 04:55 -0800
Re: [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-12-25 16:04 -0500
Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) Dave Angel <d@davea.name> - 2012-12-24 11:47 -0500
Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) Dave Angel <d@davea.name> - 2012-12-24 18:09 -0500
Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) prilisauer@googlemail.com - 2012-12-24 00:23 -0800
Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-25 13:44 +0000
Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) prilisauer@googlemail.com - 2012-12-25 06:41 -0800
Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) Dave Angel <d@davea.name> - 2012-12-25 12:10 -0500
Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) prilisauer@googlemail.com - 2012-12-25 09:31 -0800
Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) prilisauer@googlemail.com - 2012-12-25 09:31 -0800
Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) Rick Johnson <rantingrickjohnson@gmail.com> - 2012-12-25 12:16 -0800
Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-12-25 16:08 -0500
Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) Rick Johnson <rantingrickjohnson@gmail.com> - 2012-12-25 15:42 -0800
Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) Michael Torrie <torriem@gmail.com> - 2012-12-26 12:20 -0700
Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) Michael Torrie <torriem@gmail.com> - 2012-12-26 11:58 -0700
Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) Rick Johnson <rantingrickjohnson@gmail.com> - 2012-12-25 15:42 -0800
Re: [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) alex23 <wuwei23@gmail.com> - 2012-12-25 19:18 -0800
Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) Rick Johnson <rantingrickjohnson@gmail.com> - 2012-12-25 12:16 -0800
Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-25 22:56 +0000
Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) Rick Johnson <rantingrickjohnson@gmail.com> - 2012-12-25 16:19 -0800
Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-26 08:29 +0000
Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) Rick Johnson <rantingrickjohnson@gmail.com> - 2012-12-26 20:07 -0800
Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-27 05:02 +0000
Re: [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) Ranting Rick <rantingrickjohnson@gmail.com> - 2012-12-26 22:50 -0800
Re: [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) Chris Angelico <rosuav@gmail.com> - 2012-12-27 23:58 +1100
Re: Require help migrating from Perl to Python 2.7 (namespaces) alex23 <wuwei23@gmail.com> - 2012-12-27 05:25 -0800
Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) prilisauer@googlemail.com - 2012-12-23 01:36 -0800
Page 1 of 3 [1] 2 3 Next page →
| From | prilisauer@googlemail.com |
|---|---|
| Date | 2012-12-22 02:59 -0800 |
| Subject | [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) |
| Message-ID | <ab077ea4-2cfd-4769-afa7-b0cd0045e2d6@googlegroups.com> |
Hello, to all,
I hope I can describe me problem correctly.
I have written a Project split up to one Main.py and different modules which are loaded using import and here is also my problem:
1. Main.py executes:
2. Import modules
3. One of the Modules is a SqliteDB datastore.
4. A second module creates an IPC socket.
5. Here is now my problem :
The IPC Socket should run a sub which is stored ad SqliteDB and returns all Rows.
Is there a elegant way to solve it? except a Queue. Is it possible to import modules multiple?! I'm unsure because the open DB file at another module.
How is this solved in bigger projects?
Thanks a lot for all answers.
BR
[toc] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2012-12-22 12:43 +0100 |
| Message-ID | <mailman.1194.1356176640.29569.python-list@python.org> |
| In reply to | #35352 |
prilisauer@googlemail.com wrote:
> Hello, to all,
>
> I hope I can describe me problem correctly.
>
> I have written a Project split up to one Main.py and different modules
> which are loaded using import and here is also my problem:
>
> 1. Main.py executes:
> 2. Import modules
> 3. One of the Modules is a SqliteDB datastore.
> 4. A second module creates an IPC socket.
> 5. Here is now my problem :
> The IPC Socket should run a sub which is stored ad SqliteDB and
> returns all Rows.
>
> Is there a elegant way to solve it? except a Queue. Is it possible to
> import modules multiple?!
If you import a module more than once code on the module level will be
executed the first time only. Subsequent imports will find the ready-to-use
module object in a cache (sys.modules).
> I'm unsure because the open DB file at another
> module.
>
> How is this solved in bigger projects?
If I'm understanding you correctly you have code on the module level that
creates a socket or opens a database. Don't do that!
Put the code into functions instead. That will give the flexibility you need
for all sizes of projects. For instance
socket_stuff.py
def send_to_socket(rows):
socket = ... # open socket
for row in rows:
# do whatever it takes to serialize the row
socket.close()
database_stuff.py
def read_table(dbname, tablename):
if table not in allowed_table_names:
raise ValueError
db = sqlite3.connect(dbname)
cursor = db.cursor()
for row in cursor.execute("select * from %s" % tablename):
yield row
db.close()
main.py
import socket_stuff
import database_stuff
if __name__ == "__main__":
socket_stuff.send_to_socket(
database_stuff.read_table("some_db", "some_table"))
[toc] | [prev] | [next] | [standalone]
| From | prilisauer@googlemail.com |
|---|---|
| Date | 2012-12-22 04:38 -0800 |
| Message-ID | <8d4a5e03-0d2f-4157-bdfb-3a10a06ffba9@googlegroups.com> |
| In reply to | #35355 |
Am Samstag, 22. Dezember 2012 12:43:54 UTC+1 schrieb Peter Otten:
> wrote:
>
>
>
> > Hello, to all,
>
> >
>
> > I hope I can describe me problem correctly.
>
> >
>
> > I have written a Project split up to one Main.py and different modules
>
> > which are loaded using import and here is also my problem:
>
> >
>
> > 1. Main.py executes:
>
> > 2. Import modules
>
> > 3. One of the Modules is a SqliteDB datastore.
>
> > 4. A second module creates an IPC socket.
>
> > 5. Here is now my problem :
>
> > The IPC Socket should run a sub which is stored ad SqliteDB and
>
> > returns all Rows.
>
> >
>
> > Is there a elegant way to solve it? except a Queue. Is it possible to
>
> > import modules multiple?!
>
>
>
> If you import a module more than once code on the module level will be
>
> executed the first time only. Subsequent imports will find the ready-to-use
>
> module object in a cache (sys.modules).
>
>
>
> > I'm unsure because the open DB file at another
>
> > module.
>
> >
>
> > How is this solved in bigger projects?
>
>
>
> If I'm understanding you correctly you have code on the module level that
>
> creates a socket or opens a database. Don't do that!
>
> Put the code into functions instead. That will give the flexibility you need
>
> for all sizes of projects. For instance
>
>
>
> socket_stuff.py
>
>
>
> def send_to_socket(rows):
>
> socket = ... # open socket
>
> for row in rows:
>
> # do whatever it takes to serialize the row
>
> socket.close()
>
>
>
> database_stuff.py
>
>
>
> def read_table(dbname, tablename):
>
> if table not in allowed_table_names:
>
> raise ValueError
>
> db = sqlite3.connect(dbname)
>
> cursor = db.cursor()
>
> for row in cursor.execute("select * from %s" % tablename):
>
> yield row
>
> db.close()
>
>
>
>
>
> main.py
>
>
>
> import socket_stuff
>
> import database_stuff
>
>
>
> if __name__ == "__main__":
>
> socket_stuff.send_to_socket(
>
> database_stuff.read_table("some_db", "some_table"))
Hello Thanks for that answer:
I think I have to write it a bit larger,
This isn't a real code, but it for better overview:
main.py
/ / \ \
-------------------------------------------------------------------
Datastore | ModbusClient | DaliBusClient | Advanced Scheduler
--------------------------------------------------------------------
Main.py:
import Datastore
Datastore.startup()
....
(Everything should run in threads)
Datastore.py
# Opens a SQLite DB
# Contains Queries to serve data for other mods
# Central point for updating / Quering etc.
ModbusClient.py
# Opens TCP connection to Modbus Device
!!! Here it's interesting !!!
This module contains predefined Queries and functions
IF I START A FUNCTION a SQL should be executed in Datastore.py
DaliBusClient.py
#Lamps, etc. Functions.
# Stored Informations should be kept in Datastore
# Advanced scheduler
I don't know, Python allways looks for me like a one script "File". But there are big projects. like the the "Model of an SQL Server", using coordinators no problems running threads and exchange Data through a Backbone. I have searched a lot, but I havent find anything to write the "core" splited up into modules and geting over the scopes etc.
DO I have anything forgotten?! everything unclear?? :-P
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2012-12-22 14:54 +0100 |
| Message-ID | <mailman.1198.1356184470.29569.python-list@python.org> |
| In reply to | #35357 |
prilisauer@googlemail.com wrote: > I don't know, Python allways looks for me like a one script "File". But > there are big projects. like the the "Model of an SQL Server", using > coordinators no problems running threads and exchange Data through a > Backbone. I have searched a lot, but I havent find anything to write the > "core" splited up into modules and geting over the scopes etc. > > DO I have anything forgotten?! everything unclear?? :-P I think you have to step back and acquire some experience with a smaller, less complex project... investigating libraries on which you can base your efforts may also be a good idea. If the Perl program is well-structured there is no reason not to copy its architecture -- but I suspect you wouldn't want to migrate then.
[toc] | [prev] | [next] | [standalone]
| From | prilisauer@googlemail.com |
|---|---|
| Date | 2012-12-22 08:36 -0800 |
| Message-ID | <028d2f79-7550-46f5-8dd1-d0c327c86581@googlegroups.com> |
| In reply to | #35362 |
Am Samstag, 22. Dezember 2012 14:54:27 UTC+1 schrieb Peter Otten: > wrote: > > > > > I don't know, Python allways looks for me like a one script "File". But > > > there are big projects. like the the "Model of an SQL Server", using > > > coordinators no problems running threads and exchange Data through a > > > Backbone. I have searched a lot, but I havent find anything to write the > > > "core" splited up into modules and geting over the scopes etc. > > > > > > DO I have anything forgotten?! everything unclear?? :-P > > > > I think you have to step back and acquire some experience with a smaller, > > less complex project... investigating libraries on which you can base your > > efforts may also be a good idea. > > > > If the Perl program is well-structured there is no reason not to copy its > > architecture -- but I suspect you wouldn't want to migrate then. Yes, but my project couldn't be smaller, my core problem is how to exchange data between modules?!...
[toc] | [prev] | [next] | [standalone]
| From | prilisauer@googlemail.com |
|---|---|
| Date | 2012-12-22 08:36 -0800 |
| Message-ID | <mailman.1206.1356194180.29569.python-list@python.org> |
| In reply to | #35362 |
Am Samstag, 22. Dezember 2012 14:54:27 UTC+1 schrieb Peter Otten: > wrote: > > > > > I don't know, Python allways looks for me like a one script "File". But > > > there are big projects. like the the "Model of an SQL Server", using > > > coordinators no problems running threads and exchange Data through a > > > Backbone. I have searched a lot, but I havent find anything to write the > > > "core" splited up into modules and geting over the scopes etc. > > > > > > DO I have anything forgotten?! everything unclear?? :-P > > > > I think you have to step back and acquire some experience with a smaller, > > less complex project... investigating libraries on which you can base your > > efforts may also be a good idea. > > > > If the Perl program is well-structured there is no reason not to copy its > > architecture -- but I suspect you wouldn't want to migrate then. Yes, but my project couldn't be smaller, my core problem is how to exchange data between modules?!...
[toc] | [prev] | [next] | [standalone]
| From | prilisauer@googlemail.com |
|---|---|
| Date | 2012-12-22 04:38 -0800 |
| Message-ID | <mailman.1196.1356179894.29569.python-list@python.org> |
| In reply to | #35355 |
Am Samstag, 22. Dezember 2012 12:43:54 UTC+1 schrieb Peter Otten:
> wrote:
>
>
>
> > Hello, to all,
>
> >
>
> > I hope I can describe me problem correctly.
>
> >
>
> > I have written a Project split up to one Main.py and different modules
>
> > which are loaded using import and here is also my problem:
>
> >
>
> > 1. Main.py executes:
>
> > 2. Import modules
>
> > 3. One of the Modules is a SqliteDB datastore.
>
> > 4. A second module creates an IPC socket.
>
> > 5. Here is now my problem :
>
> > The IPC Socket should run a sub which is stored ad SqliteDB and
>
> > returns all Rows.
>
> >
>
> > Is there a elegant way to solve it? except a Queue. Is it possible to
>
> > import modules multiple?!
>
>
>
> If you import a module more than once code on the module level will be
>
> executed the first time only. Subsequent imports will find the ready-to-use
>
> module object in a cache (sys.modules).
>
>
>
> > I'm unsure because the open DB file at another
>
> > module.
>
> >
>
> > How is this solved in bigger projects?
>
>
>
> If I'm understanding you correctly you have code on the module level that
>
> creates a socket or opens a database. Don't do that!
>
> Put the code into functions instead. That will give the flexibility you need
>
> for all sizes of projects. For instance
>
>
>
> socket_stuff.py
>
>
>
> def send_to_socket(rows):
>
> socket = ... # open socket
>
> for row in rows:
>
> # do whatever it takes to serialize the row
>
> socket.close()
>
>
>
> database_stuff.py
>
>
>
> def read_table(dbname, tablename):
>
> if table not in allowed_table_names:
>
> raise ValueError
>
> db = sqlite3.connect(dbname)
>
> cursor = db.cursor()
>
> for row in cursor.execute("select * from %s" % tablename):
>
> yield row
>
> db.close()
>
>
>
>
>
> main.py
>
>
>
> import socket_stuff
>
> import database_stuff
>
>
>
> if __name__ == "__main__":
>
> socket_stuff.send_to_socket(
>
> database_stuff.read_table("some_db", "some_table"))
Hello Thanks for that answer:
I think I have to write it a bit larger,
This isn't a real code, but it for better overview:
main.py
/ / \ \
-------------------------------------------------------------------
Datastore | ModbusClient | DaliBusClient | Advanced Scheduler
--------------------------------------------------------------------
Main.py:
import Datastore
Datastore.startup()
....
(Everything should run in threads)
Datastore.py
# Opens a SQLite DB
# Contains Queries to serve data for other mods
# Central point for updating / Quering etc.
ModbusClient.py
# Opens TCP connection to Modbus Device
!!! Here it's interesting !!!
This module contains predefined Queries and functions
IF I START A FUNCTION a SQL should be executed in Datastore.py
DaliBusClient.py
#Lamps, etc. Functions.
# Stored Informations should be kept in Datastore
# Advanced scheduler
I don't know, Python allways looks for me like a one script "File". But there are big projects. like the the "Model of an SQL Server", using coordinators no problems running threads and exchange Data through a Backbone. I have searched a lot, but I havent find anything to write the "core" splited up into modules and geting over the scopes etc.
DO I have anything forgotten?! everything unclear?? :-P
[toc] | [prev] | [next] | [standalone]
| From | prilisauer@googlemail.com |
|---|---|
| Date | 2012-12-22 04:45 -0800 |
| Message-ID | <3e36c510-f277-4b51-9be6-79ad342fd346@googlegroups.com> |
| In reply to | #35358 |
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,
>
> >
>
> > >
>
> >
>
> > > I hope I can describe me problem correctly.
>
> >
>
> > >
>
> >
>
> > > I have written a Project split up to one Main.py and different modules
>
> >
>
> > > which are loaded using import and here is also my problem:
>
> >
>
> > >
>
> >
>
> > > 1. Main.py executes:
>
> >
>
> > > 2. Import modules
>
> >
>
> > > 3. One of the Modules is a SqliteDB datastore.
>
> >
>
> > > 4. A second module creates an IPC socket.
>
> >
>
> > > 5. Here is now my problem :
>
> >
>
> > > The IPC Socket should run a sub which is stored ad SqliteDB and
>
> >
>
> > > returns all Rows.
>
> >
>
> > >
>
> >
>
> > > Is there a elegant way to solve it? except a Queue. Is it possible to
>
> >
>
> > > import modules multiple?!
>
> >
>
> >
>
> >
>
> > If you import a module more than once code on the module level will be
>
> >
>
> > executed the first time only. Subsequent imports will find the ready-to-use
>
> >
>
> > module object in a cache (sys.modules).
>
> >
>
> >
>
> >
>
> > > I'm unsure because the open DB file at another
>
> >
>
> > > module.
>
> >
>
> > >
>
> >
>
> > > How is this solved in bigger projects?
>
> >
>
> >
>
> >
>
> > If I'm understanding you correctly you have code on the module level that
>
> >
>
> > creates a socket or opens a database. Don't do that!
>
> >
>
> > Put the code into functions instead. That will give the flexibility you need
>
> >
>
> > for all sizes of projects. For instance
>
> >
>
> >
>
> >
>
> > socket_stuff.py
>
> >
>
> >
>
> >
>
> > def send_to_socket(rows):
>
> >
>
> > socket = ... # open socket
>
> >
>
> > for row in rows:
>
> >
>
> > # do whatever it takes to serialize the row
>
> >
>
> > socket.close()
>
> >
>
> >
>
> >
>
> > database_stuff.py
>
> >
>
> >
>
> >
>
> > def read_table(dbname, tablename):
>
> >
>
> > if table not in allowed_table_names:
>
> >
>
> > raise ValueError
>
> >
>
> > db = sqlite3.connect(dbname)
>
> >
>
> > cursor = db.cursor()
>
> >
>
> > for row in cursor.execute("select * from %s" % tablename):
>
> >
>
> > yield row
>
> >
>
> > db.close()
>
> >
>
> >
>
> >
>
> >
>
> >
>
> > main.py
>
> >
>
> >
>
> >
>
> > import socket_stuff
>
> >
>
> > import database_stuff
>
> >
>
> >
>
> >
>
> > if __name__ == "__main__":
>
> >
>
> > socket_stuff.send_to_socket(
>
> >
>
> > database_stuff.read_table("some_db", "some_table"))
>
>
>
> Hello Thanks for that answer:
>
> I think I have to write it a bit larger,
>
>
>
> This isn't a real code, but it for better overview:
>
> main.py
>
> / / \ \
>
> -------------------------------------------------------------------
>
> Datastore | ModbusClient | DaliBusClient | Advanced Scheduler
>
> --------------------------------------------------------------------
>
>
>
>
>
> Main.py:
>
> import Datastore
>
> Datastore.startup()
>
> ....
>
> (Everything should run in threads)
>
>
>
> Datastore.py
>
> # Opens a SQLite DB
>
> # Contains Queries to serve data for other mods
>
> # Central point for updating / Quering etc.
>
>
>
> ModbusClient.py
>
> # Opens TCP connection to Modbus Device
>
> !!! Here it's interesting !!!
>
> This module contains predefined Queries and functions
>
> IF I START A FUNCTION a SQL should be executed in Datastore.py
>
>
>
>
>
> DaliBusClient.py
>
> #Lamps, etc. Functions.
>
> # Stored Informations should be kept in Datastore
>
> # Advanced scheduler
>
>
>
>
>
> I don't know, Python allways looks for me like a one script "File". But there are big projects. like the the "Model of an SQL Server", using coordinators no problems running threads and exchange Data through a Backbone. I have searched a lot, but I havent find anything to write the "core" splited up into modules and geting over the scopes etc.
>
>
>
> DO I have anything forgotten?! everything unclear?? :-P
Ps.: The Socket, the DB has to be kept allways open, because of it's Server functionality, A lot of Sensors, Timers, User interaction, must recived , Calculated, etc so a reaction must be send in about 16~100 ms, different modules opens and closes Sockets or files, could result in a dead situation.
Or do i didn't see any Tree's in the Wood?
Br
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2012-12-22 16:30 -0500 |
| Message-ID | <mailman.1215.1356211870.29569.python-list@python.org> |
| In reply to | #35359 |
On 12/22/2012 7:45 AM, prilisauer@googlemail.com 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
[toc] | [prev] | [next] | [standalone]
| From | prilisauer@googlemail.com |
|---|---|
| Date | 2012-12-22 04:45 -0800 |
| Message-ID | <mailman.1197.1356180330.29569.python-list@python.org> |
| In reply to | #35358 |
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,
>
> >
>
> > >
>
> >
>
> > > I hope I can describe me problem correctly.
>
> >
>
> > >
>
> >
>
> > > I have written a Project split up to one Main.py and different modules
>
> >
>
> > > which are loaded using import and here is also my problem:
>
> >
>
> > >
>
> >
>
> > > 1. Main.py executes:
>
> >
>
> > > 2. Import modules
>
> >
>
> > > 3. One of the Modules is a SqliteDB datastore.
>
> >
>
> > > 4. A second module creates an IPC socket.
>
> >
>
> > > 5. Here is now my problem :
>
> >
>
> > > The IPC Socket should run a sub which is stored ad SqliteDB and
>
> >
>
> > > returns all Rows.
>
> >
>
> > >
>
> >
>
> > > Is there a elegant way to solve it? except a Queue. Is it possible to
>
> >
>
> > > import modules multiple?!
>
> >
>
> >
>
> >
>
> > If you import a module more than once code on the module level will be
>
> >
>
> > executed the first time only. Subsequent imports will find the ready-to-use
>
> >
>
> > module object in a cache (sys.modules).
>
> >
>
> >
>
> >
>
> > > I'm unsure because the open DB file at another
>
> >
>
> > > module.
>
> >
>
> > >
>
> >
>
> > > How is this solved in bigger projects?
>
> >
>
> >
>
> >
>
> > If I'm understanding you correctly you have code on the module level that
>
> >
>
> > creates a socket or opens a database. Don't do that!
>
> >
>
> > Put the code into functions instead. That will give the flexibility you need
>
> >
>
> > for all sizes of projects. For instance
>
> >
>
> >
>
> >
>
> > socket_stuff.py
>
> >
>
> >
>
> >
>
> > def send_to_socket(rows):
>
> >
>
> > socket = ... # open socket
>
> >
>
> > for row in rows:
>
> >
>
> > # do whatever it takes to serialize the row
>
> >
>
> > socket.close()
>
> >
>
> >
>
> >
>
> > database_stuff.py
>
> >
>
> >
>
> >
>
> > def read_table(dbname, tablename):
>
> >
>
> > if table not in allowed_table_names:
>
> >
>
> > raise ValueError
>
> >
>
> > db = sqlite3.connect(dbname)
>
> >
>
> > cursor = db.cursor()
>
> >
>
> > for row in cursor.execute("select * from %s" % tablename):
>
> >
>
> > yield row
>
> >
>
> > db.close()
>
> >
>
> >
>
> >
>
> >
>
> >
>
> > main.py
>
> >
>
> >
>
> >
>
> > import socket_stuff
>
> >
>
> > import database_stuff
>
> >
>
> >
>
> >
>
> > if __name__ == "__main__":
>
> >
>
> > socket_stuff.send_to_socket(
>
> >
>
> > database_stuff.read_table("some_db", "some_table"))
>
>
>
> Hello Thanks for that answer:
>
> I think I have to write it a bit larger,
>
>
>
> This isn't a real code, but it for better overview:
>
> main.py
>
> / / \ \
>
> -------------------------------------------------------------------
>
> Datastore | ModbusClient | DaliBusClient | Advanced Scheduler
>
> --------------------------------------------------------------------
>
>
>
>
>
> Main.py:
>
> import Datastore
>
> Datastore.startup()
>
> ....
>
> (Everything should run in threads)
>
>
>
> Datastore.py
>
> # Opens a SQLite DB
>
> # Contains Queries to serve data for other mods
>
> # Central point for updating / Quering etc.
>
>
>
> ModbusClient.py
>
> # Opens TCP connection to Modbus Device
>
> !!! Here it's interesting !!!
>
> This module contains predefined Queries and functions
>
> IF I START A FUNCTION a SQL should be executed in Datastore.py
>
>
>
>
>
> DaliBusClient.py
>
> #Lamps, etc. Functions.
>
> # Stored Informations should be kept in Datastore
>
> # Advanced scheduler
>
>
>
>
>
> I don't know, Python allways looks for me like a one script "File". But there are big projects. like the the "Model of an SQL Server", using coordinators no problems running threads and exchange Data through a Backbone. I have searched a lot, but I havent find anything to write the "core" splited up into modules and geting over the scopes etc.
>
>
>
> DO I have anything forgotten?! everything unclear?? :-P
Ps.: The Socket, the DB has to be kept allways open, because of it's Server functionality, A lot of Sensors, Timers, User interaction, must recived , Calculated, etc so a reaction must be send in about 16~100 ms, different modules opens and closes Sockets or files, could result in a dead situation.
Or do i didn't see any Tree's in the Wood?
Br
[toc] | [prev] | [next] | [standalone]
| From | Alexander Blinne <news@blinne.net> |
|---|---|
| Date | 2012-12-22 18:26 +0100 |
| Message-ID | <50d5ed53$0$6630$9b4e6d93@newsspool2.arcor-online.net> |
| In reply to | #35360 |
Am 22.12.2012 13:45, schrieb prilisauer@googlemail.com:
> Ps.: The Socket, the DB has to be kept allways open, because of it's Server functionality, A lot of Sensors, Timers, User interaction, must recived , Calculated, etc so a reaction must be send in about 16~100 ms, different modules opens and closes Sockets or files, could result in a dead situation.
>
>
> Or do i didn't see any Tree's in the Wood?
I would strongly recommend an object oriented view:
Suppose Datastore.py contains a Class Datastore. You can instantiate
that class once in the beginning of your main file and the resulting
object has methods to store and retrieve data in/from the store.
ModbusClient.py contains a Class Modbus. This also can be instantiated
just once which opens a TCP connection to be used many times and you can
hand over a reference to the Instance of Datastore you created earlier,
so it can speak with the Datastore. The object has methods to do the
things you want it to do.
The Same for DaliBusClient.
Now your main.py could look something linke
from Datastore import Datastore
from ModbusClient import Modbus
from DaliBusClient import DaliBus
def main():
datastore = Datastore(...)
modbus = Modbus(..., datastore)
dalibus = DaliBus(..., datastore)
modbus.read_data_and_save_to_store()
dalibus.read_data_and_save_to_store()
if __name__=="__main__":
main()
[toc] | [prev] | [next] | [standalone]
| From | prilisauer@googlemail.com |
|---|---|
| Date | 2012-12-22 10:10 -0800 |
| Message-ID | <52f8ac29-a203-4465-98a8-43957f0defcd@googlegroups.com> |
| In reply to | #35377 |
Am Samstag, 22. Dezember 2012 18:26:43 UTC+1 schrieb Alexander Blinne: > Am 22.12.2012 13:45, schrieb: > > > Ps.: The Socket, the DB has to be kept allways open, because of it's Server functionality, A lot of Sensors, Timers, User interaction, must recived , Calculated, etc so a reaction must be send in about 16~100 ms, different modules opens and closes Sockets or files, could result in a dead situation. > > > > > > > > > Or do i didn't see any Tree's in the Wood? > > > > I would strongly recommend an object oriented view: > > > > Suppose Datastore.py contains a Class Datastore. You can instantiate > > that class once in the beginning of your main file and the resulting > > object has methods to store and retrieve data in/from the store. > > > > ModbusClient.py contains a Class Modbus. This also can be instantiated > > just once which opens a TCP connection to be used many times and you can > > hand over a reference to the Instance of Datastore you created earlier, > > so it can speak with the Datastore. The object has methods to do the > > things you want it to do. > > > > The Same for DaliBusClient. > > > > Now your main.py could look something linke > > > > from Datastore import Datastore > > from ModbusClient import Modbus > > from DaliBusClient import DaliBus > > > > def main(): > > datastore = Datastore(...) > > modbus = Modbus(..., datastore) > > dalibus = DaliBus(..., datastore) > > > > modbus.read_data_and_save_to_store() > > dalibus.read_data_and_save_to_store() > > > > if __name__=="__main__": > > main() Yes, My Project is allready > 1000 lines and even more,.. I have started writing it, each module after another ... I've got allready a lot of experience in other programming languages ( also I have worked as a programmer) But the question is, could communicate a DaliModul, Modbus,.. Etc with the backend db. It's for me a view of top side down, but how could the midlevel comunicate to each oter... "not hirachical"
[toc] | [prev] | [next] | [standalone]
| From | Alexander Blinne <news@blinne.net> |
|---|---|
| Date | 2012-12-22 20:29 +0100 |
| Message-ID | <50d60a2d$0$6630$9b4e6d93@newsspool2.arcor-online.net> |
| In reply to | #35378 |
Am 22.12.2012 19:10, schrieb prilisauer@googlemail.com:
> 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
[toc] | [prev] | [next] | [standalone]
| From | prilisauer@googlemail.com |
|---|---|
| Date | 2012-12-22 12:43 -0800 |
| Message-ID | <37264cd5-8acd-416b-a808-50216f9fbfa0@googlegroups.com> |
| In reply to | #35379 |
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...
[toc] | [prev] | [next] | [standalone]
| From | Alexander Blinne <news@blinne.net> |
|---|---|
| Date | 2012-12-22 22:59 +0100 |
| Message-ID | <50d62d39$0$6581$9b4e6d93@newsspool3.arcor-online.net> |
| In reply to | #35383 |
Am 22.12.2012 21:43, schrieb prilisauer@googlemail.com: > 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.
[toc] | [prev] | [next] | [standalone]
| From | prilisauer@googlemail.com |
|---|---|
| Date | 2012-12-23 01:32 -0800 |
| Message-ID | <56900a78-fdc6-40b7-b3ac-47ec3e66d23a@googlegroups.com> |
| In reply to | #35395 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2012-12-23 20:53 -0500 |
| Message-ID | <mailman.1233.1356314056.29569.python-list@python.org> |
| In reply to | #35402 |
On 12/23/2012 4:32 AM, prilisauer@googlemail.com wrote: > 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 have not followed this thread, and do not know the context of your statement, or the code that did not work, but if you hand a Python object to an imported module, and something within the module can access the object, then all of its attributes are also accessible, the same as from the original module. -- Terry Jan Reedy
[toc] | [prev] | [next] | [standalone]
| From | Alexander Blinne <news@blinne.net> |
|---|---|
| Date | 2012-12-24 14:10 +0100 |
| Message-ID | <50d85440$0$6576$9b4e6d93@newsspool3.arcor-online.net> |
| In reply to | #35395 |
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
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <d@davea.name> |
|---|---|
| Date | 2012-12-22 17:03 -0500 |
| Message-ID | <mailman.1217.1356213828.29569.python-list@python.org> |
| In reply to | #35383 |
On 12/22/2012 03:43 PM, prilisauer@googlemail.com 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
[toc] | [prev] | [next] | [standalone]
| From | Cameron Simpson <cs@zip.com.au> |
|---|---|
| Date | 2012-12-23 21:55 +1100 |
| Message-ID | <mailman.1220.1356260119.29569.python-list@python.org> |
| In reply to | #35383 |
On 22Dec2012 12:43, prilisauer@googlemail.com <prilisauer@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@zip.com.au>
Whatever is not nailed down is mine. What I can pry loose is not nailed
down. - Collis P. Huntingdon
[toc] | [prev] | [next] | [standalone]
Page 1 of 3 [1] 2 3 Next page →
Back to top | Article view | comp.lang.python
csiph-web