Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:: [': 0.03; 'subsequent': 0.04; 'subject:Python': 0.05; 'cache': 0.05; 'socket': 0.05; '"__main__":': 0.07; '%s"': 0.07; '__name__': 0.07; 'elegant': 0.07; 'executed': 0.07; 'problem:': 0.07; 'subject:help': 0.07; 'cursor': 0.09; 'imports': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'sub': 0.09; 'subject:2.7': 0.09; 'valueerror': 0.09; 'stored': 0.10; 'subject:Help': 0.10; 'def': 0.10; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'row': 0.16; 'socket.': 0.16; 'subject:] [': 0.16; 'wrote:': 0.17; 'instance': 0.17; 'yield': 0.17; 'creates': 0.18; 'subject:] ': 0.19; 'module': 0.19; 'written': 0.20; 'all,': 0.21; 'import': 0.21; 'flexibility': 0.23; 'split': 0.23; 'raise': 0.24; 'second': 0.24; 'header:User- Agent:1': 0.26; 'instead.': 0.27; 'module.': 0.27; 'header:X -Complaints-To:1': 0.28; 'run': 0.28; "i'm": 0.29; 'projects.': 0.30; 'code': 0.31; 'file': 0.32; 'correctly.': 0.33; 'that!': 0.33; 'problem': 0.33; 'to:addr:python-list': 0.33; 'another': 0.33; 'project': 0.34; 'whatever': 0.35; 'bigger': 0.35; 'open': 0.35; 'table': 0.35; 'there': 0.35; 'received:org': 0.36; 'except': 0.36; 'loaded': 0.36; 'modules': 0.36; 'should': 0.36; 'subject: (': 0.36; 'possible': 0.37; 'correctly': 0.37; 'level': 0.37; 'object': 0.38; 'to:addr:python.org': 0.39; 'takes': 0.39; 'hello,': 0.39; 'skip:" 10': 0.40; 'header:Received:5': 0.40; 'skip:u 10': 0.60; 'first': 0.61; 'solve': 0.62; 'different': 0.63; 'more': 0.63; 'here': 0.65 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces) Date: Sat, 22 Dec 2012 12:43:54 +0100 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p50849b8d.dip.t-dialin.net User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 64 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1356176640 news.xs4all.nl 6958 [2001:888:2000:d::a6]:39961 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:35355 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"))