Interpolation between multiple modules of an application. 
Author Message
 Interpolation between multiple modules of an application.

Hello,

[see below for the raw, unexplained question]

I'm writing an application which consists of multiple modules. For example,
a common.py module, which defines the function:

# common.py

def nomain():
    print "this file isn't meant to be executed,"
    print "please run the file 'discoverb'"

# end

and then, every module uses this function. I end up with having the
following in *every* module:

import common

# ...

if __name__ == '__main__':
    common.nomain()

# cut here

I also use the common module for other things.

This isn't really bad, but consider the language.py module; it defines
a Class like this:

# language.py (untested code)

import userdict

class Language(userdict.userdict):
    def __init__(self, lang):
        self.data = {...} # define self.data

Every module needs to print messages, so *every* module imports language and
*every* module created a class so *every* time a file is parsed! That's wrong!

The only, UGLY solution I found is pickling it. UGLY!

So my question is:

How do I share session-depentent objects between modules, without them all
doing the same over and over again?

regards,
Gerrit.

--
"The IETF motto is 'rough consensus and running code'"

  -- Scott Bradner (Open Sources, 1999 O'Reilly and Associates)
  2:38pm  up 34 min, 10 users,  load average: 0.00, 0.00, 0.06



Sun, 02 Jun 2002 03:00:00 GMT  
 Interpolation between multiple modules of an application.

Quote:

> This isn't really bad, but consider the language.py module; it defines
> a Class like this:

> # language.py (untested code)

> import userdict

> class Language(userdict.userdict):
>     def __init__(self, lang):
>         self.data = {...} # define self.data

> Every module needs to print messages, so *every* module imports language and
> *every* module created a class so *every* time a file is parsed! That's wrong!

yes, that's wrong.  the "import" statement only reads
the module and creates the class once -- the first time
you import the module.

see "What Does python Do to Import a Module?" on
http://www.pythonware.com/people/fredrik/fyi/fyi06.htm
for more info (and the one exception to the above rule).

Quote:
> How do I share session-depentent objects between modules, without them all
> doing the same over and over again?

create them once, on the module level.  that's all
you need to do.

</F>



Sun, 02 Jun 2002 03:00:00 GMT  
 Interpolation between multiple modules of an application.

Quote:

> yes, that's wrong.  the "import" statement only reads
> the module and creates the class once -- the first time
> you import the module.

> see "What Does Python Do to Import a Module?" on
> http://www.pythonware.com/people/fredrik/fyi/fyi06.htm
> for more info (and the one exception to the above rule).

Thanks!

Quote:
> > How do I share session-depentent objects between modules, without them all
> > doing the same over and over again?

> create them once, on the module level.  that's all
> you need to do.

Thanks! I didn't know this construction works:

# a.py
a='this'
b='that'

# main.py
import a
a.a = 'not this, but another thing'
import b

# b.py
import a
print a.a # not what I exspected!

Thanks... didn't I search well or isn't this explained in the docs
as clear as Fredrik explains it in is FYI 6?

regards,
Gerrit.

--
"The IETF motto is 'rough consensus and running code'"

  -- Scott Bradner (Open Sources, 1999 O'Reilly and Associates)
  5:19pm  up  3:15, 10 users,  load average: 0.60, 0.25, 0.66



Sun, 02 Jun 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. string interpolation with both local and module variables?

2. Module using multiple EditFields

3. Having multiple languages for same application

4. Multiple applications and one dictionary

5. One Application - Multiple Dictionaries

6. Multiple application from the same .app

7. Multiple Application Instances

8. Preventing multiple use of application

9. Preventing Multiple VO Application Instances.

10. Multiple Modules & Windows Me

11. Sharing VISA Resources With Multiple Application Instances

12. multiple module instances

 

 
Powered by phpBB® Forum Software