How to organize source code and "import"s???

T

Tian

I am writing a python program which needs to support some plug-ins. I
have an XML file storing some dynamic structures. XML file records some
class names whose instance needs to be created in the run time while
parsing the XML file. I wonder what is the best solution for this
problem?

I also have some problem about the "import". How should I design my
packages?
Say, I have all code locates at c:\projects\sami, "c:\project" is in my
PYTHONPATH environment variable. Suppose my folder structure is like
this:

c:
projects\ <-----this directory is in PYTHONPATH
sami\
__init__.py
main.py
xmlparser.py
window.py
proc.py
support\
__init__.py
helper.py
plugins\
__init__.py
BaseClass.py <---no instance for this one
ExtClassA.py
ExtClassB.py
ExtClassC.py
ExtClassD.py



Each file in \projects\sami\plugins contains a class with a same name
as the file, (ExtClassA.py has class ExtClassA), the instance of these
classes need to be created at runtime while functions in xmlparser.py
is parsing an XML file. main.py is the start point of the program.

Other files in the \projects\sami\ and projects\sami\support are
supporting modules.

1. What should I write in each __init__.py ???
In "main.py", if I need functions in "proc.py", should I write
"import proc"? If I need functions in "helper.py", can i write "import
support.helper"?? what else should I do to support all these?

2. What is the best way to make instance of a class from a string type
name? One method I have successfully tried is using "from SomeWhere
import *", then get class from globals() and make instance. But How can
I import all "ExtClass?.py"? Where should I write these import codes?
 
J

John Roth

Tian said:
I am writing a python program which needs to support some plug-ins. I
have an XML file storing some dynamic structures. XML file records some
class names whose instance needs to be created in the run time while
parsing the XML file. I wonder what is the best solution for this
problem?

I also have some problem about the "import". How should I design my
packages?
Say, I have all code locates at c:\projects\sami, "c:\project" is in my
PYTHONPATH environment variable. Suppose my folder structure is like
this:

c:
projects\ <-----this directory is in PYTHONPATH
sami\
__init__.py
main.py
xmlparser.py
window.py
proc.py
support\
__init__.py
helper.py
plugins\
__init__.py
BaseClass.py <---no instance for this one
ExtClassA.py
ExtClassB.py
ExtClassC.py
ExtClassD.py



Each file in \projects\sami\plugins contains a class with a same name
as the file, (ExtClassA.py has class ExtClassA), the instance of these
classes need to be created at runtime while functions in xmlparser.py
is parsing an XML file. main.py is the start point of the program.

The solution to most dynamic import issues lies in the __import__()
function, which is the very first function documented in the built-in
functions page.

I've found that keeping it as simple as possible helps a lot. Just load
the module, get the address of the top level module from the __import()__
function, and use getattr() to trace down the chain of modules to the
class you want to access.
Other files in the \projects\sami\ and projects\sami\support are
supporting modules.

1. What should I write in each __init__.py ???f
Nothing.

In "main.py", if I need functions in "proc.py", should I write
"import proc"? If I need functions in "helper.py", can i write "import
support.helper"?? what else should I do to support all these?

If you know you're going to need something, just import it.
There's no need to get fancy unless you've got import loops.
Then you need to break them and do the other accesses dynamically
after the modules have finished loading.
2. What is the best way to make instance of a class from a string type
name? One method I have successfully tried is using "from SomeWhere
import *", then get class from globals() and make instance.

Use the getattr() function.
But How can
I import all "ExtClass?.py"? Where should I write these import codes?

If you want to import all the names in a module, look in the module's
__dict__. That's not recommended, however.

If you want to import all the modules in a directory, then you need to
read the directory (see the os module) and import them individually.

John Roth
 
S

Steven Bethard

Tian said:
I also have some problem about the "import". How should I design my
packages?
Say, I have all code locates at c:\projects\sami, "c:\project" is in my
PYTHONPATH environment variable. Suppose my folder structure is like
this:

c:
projects\ <-----this directory is in PYTHONPATH
sami\
__init__.py
main.py
xmlparser.py
window.py
proc.py
support\
__init__.py
helper.py
plugins\
__init__.py
BaseClass.py <---no instance for this one
ExtClassA.py
ExtClassB.py
ExtClassC.py
ExtClassD.py



Each file in \projects\sami\plugins contains a class with a same name
as the file, (ExtClassA.py has class ExtClassA), the instance of these
classes need to be created at runtime while functions in xmlparser.py
is parsing an XML file. main.py is the start point of the program.

Other files in the \projects\sami\ and projects\sami\support are
supporting modules.

1. What should I write in each __init__.py ???

If I understand your intent correctly here, you shouldn't need anything
in any of them.
In "main.py", if I need functions in "proc.py", should I write
"import proc"? If I need functions in "helper.py", can i write "import
support.helper"??

No, you should use absolute imports and write:
import sami.proc
import sami.support.helper
or perhaps
import sami.proc as samiproc
import sami.support.helper as samihelper
2. What is the best way to make instance of a class from a string type
name?

Why do you expect to need to do this? Usually you can just import the
class's module directly and then use getattr, e.g.:

py> modules = [__import__('sami.plugins.%s' % filename[:-3],
.... globals(), locals(), ['ExtClass'])
.... for filename in os.listdir('sami/plugins')
.... if filename.endswith('.py')
.... and filename not in ['BaseClass.py', '__init__.py']]
py> modules
[<module 'sami.plugins.ExtClassA' from
'D:\Steve\sami\plugins\ExtClassA.py'>,
<module 'sami.plugins.ExtClassB' from 'D:\Steve\sami\plugins\ExtClassB.py'>]
py> modules[0]
<module 'sami.plugins.ExtClassA' from 'D:\Steve\sami\plugins\ExtClassA.py'>
py> modules[0].ExtClass
<class 'sami.plugins.ExtClassA.ExtClass'>
py> getattr(modules[0], 'ExtClass')
<class 'sami.plugins.ExtClassA.ExtClass'>

See the documentation for __import__ for more details[1].
But How can I import all "ExtClass?.py"?

I would do this as above, using __import__.

STeVe

[1] http://docs.python.org/lib/built-in-funcs.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top