[wingide-users] global imports

Mclam, GeorgeX georgex.mclam at intel.com
Wed Jun 8 10:33:38 MDT 2011


Paul,

> I don't see "import" in the init routine, so I don't understand what
> "imported into the init routine" means.  Perhaps you mean "passed into
> the init routine", but the items passed into the init routine are
> explicitly made global to the module.

Module names are passed to subroutines.init and they are then imported......

The object is to make other modules' parameters available to a module I call subroutines.
Presently I have to have a separate copy of subroutines for each project simply because
of the need of subroutines to import the projects' modules.  Subroutines contains many
functions called by the main module of the project.  When the import statements are at
the top of the subroutines module, everything imported is global and available to all
functions in the module.  When import is in a function, everything imported is not
global to the module.

My first thought was to create a new function in subroutines, let's call it init(), 
that can be called from the main project module to set things up.  init() would be 
passed the names of all modules it needs to import.  The PROBLEM is that whatever is 
imported in init() is only available in THAT function and not to other functions in 
the subroutines module.


> Or perhaps you mean that you
> don't care for the extra level of indirection in the subroutines module;
> you want to reference them without prepending "ga."?

That is definitely NOT a solution.  It would require editing a lot of existing 
working code.


> I don't understand what a "parameter of the code" refers to.  Does it
> mean contents of the parameter modules?  

Any and all functions, labels, etc used made available by the project modules 
being imported by subroutines.  Having to identify each and every name being 
imported would be impractical and problematic.


> I think you should place less emphasis on using "import"; it's a red
> herring.  Once your caller's compiled and loaded the .py file you have
> no need for import or __import__ or any of that functionality; trying to
> use import otherwise is making you chase down over-complicated
> solutions.  For example:
>
> def init(a,b,c):
>   global ga, gb, gc
>   ga,gb,gc = a,b,c
> def do_something():
>   # from (some variable module name) import x,y,z       # NO
>   x,y,z = ga.x, ga.y, ga.z
>   # alternatively, just reference them as ga.x, ga.y, ga.z

The reason this is not a good solution is because I must name each and everything I am
importing.  Subroutines has no clue of what it importing.... and that will take some
time to explain but is not necessary to understand the issue.  The issue is how to 
"import" into a module so that all names being imported are global to the module w/o
having to state each name.


>> If you really truly care about not having that extra level of
>> indirection, try the moral equivalent of this (and careful that you
>> don't copy too much -- you might want to skip everything with a "__"
>> prefix)
>> 
>> def init(a, b, c):
>>  for module in a,b,c:
>>    globals().update(module.__dict__)

Hmmm.... so a, b & c are the names of modules that I want to "import", correct?
If so, this just might work.  I'll have to check the docs on this one.


Thanks,
George




-----Original Message-----
From: wingide-users-bounces at wingware.com [mailto:wingide-users-bounces at wingware.com] On Behalf Of Paul Du Bois
Sent: Tuesday, June 07, 2011 11:25 AM
To: wingide-users at wingware.com
Subject: RE: [wingide-users] global imports

> > # project_main.py
> > import parameter1, parameter2, parameter3
> > import subroutines
> > subroutines.init(parameter1, parameter2, parameter3)

> > # subroutines.py
> > def init(a,b,c):
> >    global ga, gb, gc
> >    ga,gb,gc = a,b,c

> Yup, did that.  No problem in that aspect.  The problem is that items
imported into the
> init routine are local to that routine.  I need them to be global to
the entire
> subroutines module.

I don't see "import" in the init routine, so I don't understand what
"imported into the init routine" means.  Perhaps you mean "passed into
the init routine", but the items passed into the init routine are
explicitly made global to the module.  Or perhaps you mean that you
don't care for the extra level of indirection in the subroutines module;
you want to reference them without prepending "ga."?

> One of us is not understanding the other.
> Virtually EVERY PARAMETER of the code is needed by the subroutines
module.

I don't understand what a "parameter of the code" refers to.  Does it
mean contents of the parameter modules?  They are all accessible through
that extra level of indirection (see last example below).

> # subroutines.py
> def init(module_name)
>     global *
>     import module_name

If you really truly care about not having that extra level of
indirection, try the moral equivalent of this (and careful that you
don't copy too much -- you might want to skip everything with a "__"
prefix)

def init(a, b, c):
  for module in a,b,c:
    globals().update(module.__dict__)

> In the first case I was considering having to put import statements in
each function of
> subroutines.py so that they could gain access to the parameters in
lieu of doing it once
> at the top of the module.

I think you should place less emphasis on using "import"; it's a red
herring.  Once your caller's compiled and loaded the .py file you have
no need for import or __import__ or any of that functionality; trying to
use import otherwise is making you chase down over-complicated
solutions.  For example:

def init(a,b,c):
   global ga, gb, gc
   ga,gb,gc = a,b,c
def do_something():
   # from (some variable module name) import x,y,z       # NO
   x,y,z = ga.x, ga.y, ga.z
   # alternatively, just reference them as ga.x, ga.y, ga.z

_________________________________________________
Wing IDE users list
http://wingware.com/lists/wingide


More information about the wingide-users mailing list