473,396 Members | 1,917 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,396 software developers and data experts.

user modules

Hi,

I'm writing a python program to analyse and export volumetric data. To
make development and extension easier, and to make it more useful to the
public when it is released (LGPL), I would like to enable users to place
their own python files in a "user_extensions" directory. These files
would implement a common interface in order for the main program to be
able to read them and execute the necessary code.

My question is what is the best way of implementing this?

I have investigated importing them as modules, but unless the user
modifies the main program I cannot see how the main program can learn of
the existence of specific modules.

For example:

from user_modules import *
# directory 'user_modules' contains __init__.py allowing this
# From here I would need a list of the imported modules, in order to
# execute a common command on each of them, such as

for module in imported_modules:
module.initialise()
module.get_tab_window()
How do I get from the first bit to the second bit, or is there a better
way of obtaining the functionality I need?
--Cameron.
Oct 5 '06 #1
6 1102
Cameron Walsh wrote:
Hi,

I'm writing a python program to analyse and export volumetric data. To
make development and extension easier, and to make it more useful to the
public when it is released (LGPL), I would like to enable users to place
their own python files in a "user_extensions" directory. These files
would implement a common interface in order for the main program to be
able to read them and execute the necessary code.

My question is what is the best way of implementing this?

I have investigated importing them as modules, but unless the user
modifies the main program I cannot see how the main program can learn of
the existence of specific modules.

For example:

from user_modules import *
# directory 'user_modules' contains __init__.py allowing this
# From here I would need a list of the imported modules, in order to
# execute a common command on each of them, such as

for module in imported_modules:
module.initialise()
module.get_tab_window()
How do I get from the first bit to the second bit, or is there a better
way of obtaining the functionality I need?
--Cameron.
import os

files=os.listdir('user_modules')
tabs=[]
for fle in files:
if fle.endswith('.py'):
module=__import__(fle[0:-3], 'user_modules', None,
['initialise', 'get_tab_window'])
module.initialise()
tabs.append(module.get_tab_window())

*not tested*

print __import__.__doc__
__import__(name, globals, locals, fromlist) -module

Import a module. The globals are only used to determine the context;
they are not modified. The locals are currently unused. The fromlist
should be a list of names to emulate ``from name import ...'', or an
empty list to emulate ``import name''.
When importing a module from a package, note that __import__('A.B', ...)
returns package A when fromlist is empty, but its submodule B when
fromlist is not empty.

Tuomas

Oct 5 '06 #2
Cameron Walsh wrote:
Hi,

I'm writing a python program to analyse and export volumetric data. To
make development and extension easier, and to make it more useful to the
public when it is released (LGPL), I would like to enable users to place
their own python files in a "user_extensions" directory. These files
would implement a common interface in order for the main program to be
able to read them and execute the necessary code.

My question is what is the best way of implementing this?

I have investigated importing them as modules, but unless the user
modifies the main program I cannot see how the main program can learn of
the existence of specific modules.
One simple solution would be a shell script that adds user_extensions
(or whatever) to $PYTHONPATH and then starts your main program.

Oct 5 '06 #3
Cameron Walsh kirjoitti:
Hi,

I'm writing a python program to analyse and export volumetric data. To
make development and extension easier, and to make it more useful to the
public when it is released (LGPL), I would like to enable users to place
their own python files in a "user_extensions" directory. These files
would implement a common interface in order for the main program to be
able to read them and execute the necessary code.

My question is what is the best way of implementing this?

I have investigated importing them as modules, but unless the user
modifies the main program I cannot see how the main program can learn of
the existence of specific modules.

For example:

from user_modules import *
# directory 'user_modules' contains __init__.py allowing this
# From here I would need a list of the imported modules, in order to
# execute a common command on each of them, such as

for module in imported_modules:
module.initialise()
module.get_tab_window()
How do I get from the first bit to the second bit, or is there a better
way of obtaining the functionality I need?
--Cameron.
Sorry... I was typing faster than reading or thinking.

You could have a __init__.py file within user_extensions with
__all__ = ["package1", "package2"]

If you want every python file within some directory in here, you can
auto-generate the __init__.py file in user_extension before importing.
(Or you could have some sort of tester for new .py files detected and
only after you are sure it works, add it.)

from user_extensions import *

would import everything mentioned in __all__. You also have access to
their names through
user_extensions.__all__

The last step would be to put the modules into a list. After the
import,

user_ext_list = [eval(elem) for elem in user_extensions.__all__ ]
for ext in user_ext_list:
ext.initialize()
ext.get_tab_window()

Oct 5 '06 #4
Juho Schultz wrote:
Cameron Walsh wrote:
Hi,

I'm writing a python program to analyse and export volumetric data. To
make development and extension easier, and to make it more useful to the
public when it is released (LGPL), I would like to enable users to place
their own python files in a "user_extensions" directory. These files
would implement a common interface in order for the main program to be
able to read them and execute the necessary code.

My question is what is the best way of implementing this?

I have investigated importing them as modules, but unless the user
modifies the main program I cannot see how the main program can learn of
the existence of specific modules.

One simple solution would be a shell script that adds user_extensions
(or whatever) to $PYTHONPATH and then starts your main program.
Sorry... I was typing faster than reading or thinking.

You could have a __init__.py file within user_extensions with
__all__ = ["package1", "package2"]

If you want every python file within some directory in here, you can
auto-generate the __init__.py file in user_extension before importing.
(Or you could have some sort of tester for new .py files detected and
only after you are sure it works, add it.)

from user_extensions import *

would import everything mentioned in __all__. You also have access to
their names through
user_extensions.__all__

The last step would be to put the modules into a list. After the
import,

user_ext_list = [eval(elem) for elem in user_extensions.__all__ ]
for ext in user_ext_list:
ext.initialize()
ext.get_tab_window()

Oct 5 '06 #5
Tuomas wrote:
Cameron Walsh wrote:
>Hi,

I'm writing a python program to analyse and export volumetric data.
To make development and extension easier, and to make it more useful
to the public when it is released (LGPL), I would like to enable users
to place their own python files in a "user_extensions" directory.
These files would implement a common interface in order for the main
program to be able to read them and execute the necessary code.

My question is what is the best way of implementing this?

I have investigated importing them as modules, but unless the user
modifies the main program I cannot see how the main program can learn
of the existence of specific modules.

For example:

from user_modules import *
# directory 'user_modules' contains __init__.py allowing this
# From here I would need a list of the imported modules, in order to
# execute a common command on each of them, such as

for module in imported_modules:
module.initialise()
module.get_tab_window()
How do I get from the first bit to the second bit, or is there a
better way of obtaining the functionality I need?
--Cameron.

import os

files=os.listdir('user_modules')
tabs=[]
for fle in files:
if fle.endswith('.py'):
module=__import__(fle[0:-3], 'user_modules', None,
['initialise', 'get_tab_window'])
module.initialise()
tabs.append(module.get_tab_window())

*not tested*

print __import__.__doc__
__import__(name, globals, locals, fromlist) -module

Import a module. The globals are only used to determine the context;
they are not modified. The locals are currently unused. The fromlist
should be a list of names to emulate ``from name import ...'', or an
empty list to emulate ``import name''.
When importing a module from a package, note that __import__('A.B', ...)
returns package A when fromlist is empty, but its submodule B when
fromlist is not empty.

Tuomas
Thanks Tuomas, your solution worked a charm, with two changes:
1.) Check I'm not trying to import __init__.py
2.) module=__import__(fle[0:-3], 'user_modules', None,
['initialise', 'get_tab_window'])
had to be changed to:
module=__import__("user_modules.%s" %fle[0:-3], None, None,
['initialise', 'get_tab_window'])

For some reason it wasn't setting the context properly, I'll have to
learn more about the function.

Thanks for teaching me how to read the documentation properly, about how
private keywords like "import" are defined, and for providing a solution
to a problem I'm likely to face many more times.

Best regards,

Cameron.
Oct 6 '06 #6
Juho Schultz wrote:
Juho Schultz wrote:
>Cameron Walsh wrote:
>>Hi,

I'm writing a python program to analyse and export volumetric data. To
make development and extension easier, and to make it more useful to the
public when it is released (LGPL), I would like to enable users to place
their own python files in a "user_extensions" directory. These files
would implement a common interface in order for the main program to be
able to read them and execute the necessary code.

My question is what is the best way of implementing this?

I have investigated importing them as modules, but unless the user
modifies the main program I cannot see how the main program can learn of
the existence of specific modules.
One simple solution would be a shell script that adds user_extensions
(or whatever) to $PYTHONPATH and then starts your main program.

Sorry... I was typing faster than reading or thinking.

You could have a __init__.py file within user_extensions with
__all__ = ["package1", "package2"]

If you want every python file within some directory in here, you can
auto-generate the __init__.py file in user_extension before importing.
(Or you could have some sort of tester for new .py files detected and
only after you are sure it works, add it.)

from user_extensions import *

would import everything mentioned in __all__. You also have access to
their names through
user_extensions.__all__

The last step would be to put the modules into a list. After the
import,

user_ext_list = [eval(elem) for elem in user_extensions.__all__ ]
for ext in user_ext_list:
ext.initialize()
ext.get_tab_window()
Thanks Juho,

Your solution also worked a charm. I like the suggestion of a tester
before adding a module to the __all__ list.

Thanks for teaching me about the eval() function, I've been wondering
how to turn a string into a python command.
Oct 6 '06 #7

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

Similar topics

4
by: Tim Daneliuk | last post by:
OK, I've Googled for this and cannot seem to quite find what I need. So, I turn to the Gentle Geniuses here for help. Here is what I need to do from within a script: Given a username and a...
3
by: Walter Brunswick | last post by:
I need to import modules with user-defined file extensions that differ from '.py', and also (if possible) redirect the bytecode output of the file to a file of a user-defined extension. I've...
3
by: Martin Lacoste | last post by:
Wondering if people have suggestions as to good reference sources for Access 2000 for an (almost..) intermediate user? It appears as though books along the lines of 'Access for Dummies' are way...
5
by: jqpdev | last post by:
Hello all... I'm coming from a Borland Delphi background. Delphi has a specific component called a Data Module. In the designer the Data Module behaves like a windows form. A developer can...
2
by: Boban Dragojlovic | last post by:
I'm building a complex web-based reservations system. Gathering the user's data requires between 8 and 15 pages (depending on which options they are interested in). I use the "Session" object to...
0
by: John Bailey | last post by:
I have a web site that is designed similarly to DotNetNuke in that the site has basic functionality, and there are modules (ascx controls) that add additional functionality. I am trying to upgrade...
1
by: Paul | last post by:
I have a class library that has a form and MDI class. My applications use this. Each application has a MDI form inherited from the MDI class. Each form that is displayed from within the MDI form...
7
by: hlubenow | last post by:
Hi, recently there was a thread about hiding the python-script from the user. The OP could use http://freshmeat.net/projects/pyobfuscate/ H.
2
by: jmike | last post by:
I'm using some legacy code that has a user-defined exception in it. The top level program includes this line from TestRunError import * It also imports several other modules. These other...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.