473,403 Members | 2,270 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

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

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?

Jul 18 '05 #1
2 3068

"Tian" <wa*********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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



Jul 18 '05 #2
Tian wrote:
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
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Vio | last post by:
Hi, I've been trying to embed (statically) wxPy alongside an embedded py interpreter on a linux/gtk box. At one point, for some reason misc.o linking reported "multiple definitions of...
2
by: Torsten Mohr | last post by:
Hi, i tried to find the file and line in the C sources of python where the command "import" is implemented. Can anybody give me some hint on this? Thanks, Torsten.
3
by: gerald.maher | last post by:
Hi, I am trying to excuate the follwong code: I get the following error: Here is my Lib path:
0
by: Bill Davy | last post by:
I am working with MSVC6 on Windows XP. I have created an MSVC project called SHIP I have a file SHIP.i with "%module SHIP" as the first line (file is below). I run SHIP.i through SWIG 1.3.24...
3
by: Mudcat | last post by:
I have a directory structure that contains different modules that run depending on what the user selects. They are identical in name and structure, but what varies is the content of the functions....
3
by: Chris | last post by:
Hi, 1) In file test.aspx, i put: <%@ Page Language="VB" AutoEventWireup="false" CodeFile="test.aspx.vb" Inherits="test" %> <%@ import namespace="System.Data"%> <%@ import...
1
by: lemke_juergen | last post by:
Hi everyone, I define some vars and functions in a "support" module which gets called from my main app module. Using Python 2.5. I import all symbols in the support module at the top of the...
14
by: Paulo da Silva | last post by:
Hi! If I have two files .py such as m.py from c import * ... x=c() ... os.any_method ...
1
by: Eric Hanchrow | last post by:
(This is with Python 2.5.2, on Ubuntu Hardy, if it matters.) This seems so basic that I'm surprised that I didn't find anything about it in the FAQ. (Yes, I am fairly new to Python.) Here are...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.