472,954 Members | 1,644 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Big problem fetching members from dynamically loaded module

Hi,

I'm getting pretty desperate here:

The code below crashes on the last line (but works from a shell).

The class 'BC' exists and the loop on self.__BC_EXEC_LIST passes fine.

It's got to be something really stupid but I've been looking at it too long
I guess.

Any clue would be quite welcome.

Regards,

Philippe


__EXEC_LIST = [ \
'import os', \
'import sys', \
'from SC.pilot.SC_Script_Processor import *', \
'from SC.utils.SC_Convert import *', \
'import string ', \
'from SC.iso.SC_Dec_ISO import *', \
'from SC.iso.SC_Enc_ISO import *', \
'from SC.iso.SC_Analysis import *', \
'sc_sp = SC_Script_Processor ()', \
'sc_enc = SC_Enc_ISO ()', \
'sc_dec = SC_Dec_ISO ()', \
'sc_a = SC_Analysis ()', \
'sc_conv = SC_Convert()' ]

__BC_EXEC_LIST = ['from SC.CARDS.BC import *','from
SC.Solutions.SCF.BCInterface import *','sc_bc = BC_Interface()']
for l_s in self.__BC_EXEC_LIST: #this loop works fine
try:
exec (l_s, self.m_process_global,self.m_process_local)

except:
traceback.print_exc()

for l_s in self.__EXEC_LIST: #this loop works fine
try:
exec (l_s, self.m_process_global,self.m_process_local)

except:
print >> sys.stderr, 'FATAL INIT ERROR - PLEASE CHECK YOUR
CONFIGURATION'

l = inspect.getmembers(eval('SC_Dec_ISO')) #THIS WORKS - the class
exists
l = inspect.getmembers(eval('BC')) #THIS CRASHES - the class exists
Jul 19 '05 #1
8 1499
Hi,

Hopefully to make things clearer: this works from a shell:

In [23]:from SC.CARDS.BC import *

In [24]:l = inspect.getmembers(eval('BC'))

#l will get all members from class 'BC' whereas the code referenced below
gets an exception saying 'BC' is not defined.

Thanks,

Philippe


Philippe C. Martin wrote:
Hi,

I'm getting pretty desperate here:

The code below crashes on the last line (but works from a shell).

The class 'BC' exists and the loop on self.__BC_EXEC_LIST passes fine.

It's got to be something really stupid but I've been looking at it too
long I guess.

Any clue would be quite welcome.

Regards,

Philippe


__EXEC_LIST = [ \
'import os', \
'import sys', \
'from SC.pilot.SC_Script_Processor import *', \
'from SC.utils.SC_Convert import *', \
'import string ', \
'from SC.iso.SC_Dec_ISO import *', \
'from SC.iso.SC_Enc_ISO import *', \
'from SC.iso.SC_Analysis import *', \
'sc_sp = SC_Script_Processor ()', \
'sc_enc = SC_Enc_ISO ()', \
'sc_dec = SC_Dec_ISO ()', \
'sc_a = SC_Analysis ()', \
'sc_conv = SC_Convert()' ]

__BC_EXEC_LIST = ['from SC.CARDS.BC import *','from
SC.Solutions.SCF.BCInterface import *','sc_bc = BC_Interface()']
for l_s in self.__BC_EXEC_LIST: #this loop works fine
try:
exec (l_s, self.m_process_global,self.m_process_local)

except:
traceback.print_exc()

for l_s in self.__EXEC_LIST: #this loop works fine
try:
exec (l_s, self.m_process_global,self.m_process_local)

except:
print >> sys.stderr, 'FATAL INIT ERROR - PLEASE CHECK YOUR
CONFIGURATION'

l = inspect.getmembers(eval('SC_Dec_ISO')) #THIS WORKS - the class
exists
l = inspect.getmembers(eval('BC')) #THIS CRASHES - the class
exists


Jul 19 '05 #2
Hi,

I found the solution to my problem:

Besides the fact I still have no clue as to how the Python name spacing
works, the code I though did stuff (the __EXEC_LIST loop) did not do as I
expected as I had already imported all of the modules in an explicit
manner.

So now I can rephrase my problem:

1) I have a program (XYZ) which displays class members in menus
2) At the time I write my program, I do not know what class members should
be in thos menus
3) So I have the class names in external files so that I can send new
modules to the 'customer' without having to release a new version of 'XYZ'

What puzzles me furher is that the 'exec' of commands such as 'sc_bc = BC()'
do work as I use them further in 'XYZ' whereas the exec of 'from xxxxxxx
import *' does not _seem_ to do anything.

Grrr! I feel more stupid every day !

Any insight ?

Thanks,

Philippe




Philippe C. Martin wrote:
Hi,

I'm getting pretty desperate here:

The code below crashes on the last line (but works from a shell).

The class 'BC' exists and the loop on self.__BC_EXEC_LIST passes fine.

It's got to be something really stupid but I've been looking at it too
long I guess.

Any clue would be quite welcome.

Regards,

Philippe


__EXEC_LIST = [ \
'import os', \
'import sys', \
'from SC.pilot.SC_Script_Processor import *', \
'from SC.utils.SC_Convert import *', \
'import string ', \
'from SC.iso.SC_Dec_ISO import *', \
'from SC.iso.SC_Enc_ISO import *', \
'from SC.iso.SC_Analysis import *', \
'sc_sp = SC_Script_Processor ()', \
'sc_enc = SC_Enc_ISO ()', \
'sc_dec = SC_Dec_ISO ()', \
'sc_a = SC_Analysis ()', \
'sc_conv = SC_Convert()' ]

__BC_EXEC_LIST = ['from SC.CARDS.BC import *','from
SC.Solutions.SCF.BCInterface import *','sc_bc = BC_Interface()']
for l_s in self.__BC_EXEC_LIST: #this loop works fine
try:
exec (l_s, self.m_process_global,self.m_process_local)

except:
traceback.print_exc()

for l_s in self.__EXEC_LIST: #this loop works fine
try:
exec (l_s, self.m_process_global,self.m_process_local)

except:
print >> sys.stderr, 'FATAL INIT ERROR - PLEASE CHECK YOUR
CONFIGURATION'

l = inspect.getmembers(eval('SC_Dec_ISO')) #THIS WORKS - the class
exists
l = inspect.getmembers(eval('BC')) #THIS CRASHES - the class
exists


Jul 19 '05 #3
Philippe C. Martin wrote:
l*=*inspect.getmembers(eval('BC'))*#THIS*CRASHES*-*the*class*exists
Care to provide the traceback?
In [23]:from SC.CARDS.BC import *

In [24]:l = inspect.getmembers(eval('BC'))


What happened in lines 1 through 22? My guess would be

In [13]:from SC.CARDS import *

Be that as is may, eval("BC") can be simplified to BC,

from module import *

is the last roadhouse en route to chaos and an unqualified

try ... except

shows you are willing to drive with defunct brakes. By introducing exec and
eval() you are throwing the steering wheel out of the window.

Seriously, try to make do with __import__() and getattr() to clean up your
code a bit.

Driving-analogies-well-beyond-the-abyss-ly yours
Peter
Jul 19 '05 #4
OK Peter, first of all thanks.

You seem to be German and although I leave in the states, I'm French and
your english is clearly far more advanced than mine: I have yet to
understand a few of your comments ;-)
Care to provide the traceback?
Traceback (most recent call last):
File "SC_Shell.py", line 1095, in ?
l_d = SC_Shell()
File "SC_Shell.py", line 326, in __init__
self.__Make_Menu_Area()
File "SC_Shell.py", line 828, in __Make_Menu_Area
l = inspect.getmembers(eval(c))
File "<string>", line 0, in ?
NameError: name 'BC' is not defined
What happened in lines 1 through 22? My guess would be just import inspect
and after that .... QUID ?

Thanks and regards,

Philippe

Peter Otten wrote:
Philippe C. Martin wrote:
lÂ*=Â*inspect.getmembers(eval('BC'))Â*#THISÂ*CRASH ESÂ*-Â*theÂ*classÂ*exists


Care to provide the traceback?
In [23]:from SC.CARDS.BC import *

In [24]:l = inspect.getmembers(eval('BC'))


What happened in lines 1 through 22? My guess would be

In [13]:from SC.CARDS import *

Be that as is may, eval("BC") can be simplified to BC,

from module import *

is the last roadhouse en route to chaos and an unqualified

try ... except

shows you are willing to drive with defunct brakes. By introducing exec
and eval() you are throwing the steering wheel out of the window.

Seriously, try to make do with __import__() and getattr() to clean up your
code a bit.

Driving-analogies-well-beyond-the-abyss-ly yours
Peter


Jul 19 '05 #5
I meant live, not leave ! (this is getting pretty bad)
Philippe C. Martin wrote:
OK Peter, first of all thanks.

You seem to be German and although I leave in the states, I'm French and
your english is clearly far more advanced than mine: I have yet to
understand a few of your comments ;-)
Care to provide the traceback?


Traceback (most recent call last):
File "SC_Shell.py", line 1095, in ?
l_d = SC_Shell()
File "SC_Shell.py", line 326, in __init__
self.__Make_Menu_Area()
File "SC_Shell.py", line 828, in __Make_Menu_Area
l = inspect.getmembers(eval(c))
File "<string>", line 0, in ?
NameError: name 'BC' is not defined
What happened in lines 1 through 22? My guess would be

just import inspect
and after that .... QUID ?

Thanks and regards,

Philippe

Peter Otten wrote:
Philippe C. Martin wrote:
lÂ*=Â*inspect.getmembers(eval('BC'))Â*#THISÂ*CRASH ESÂ*-Â*theÂ*classÂ*exists


Care to provide the traceback?
In [23]:from SC.CARDS.BC import *

In [24]:l = inspect.getmembers(eval('BC'))


What happened in lines 1 through 22? My guess would be

In [13]:from SC.CARDS import *

Be that as is may, eval("BC") can be simplified to BC,

from module import *

is the last roadhouse en route to chaos and an unqualified

try ... except

shows you are willing to drive with defunct brakes. By introducing exec
and eval() you are throwing the steering wheel out of the window.

Seriously, try to make do with __import__() and getattr() to clean up
your code a bit.

Driving-analogies-well-beyond-the-abyss-ly yours
Peter


Jul 19 '05 #6
Philippe C. Martin wrote:
OK Peter, first of all thanks.

You seem to be German and although I leave in the states, I'm French and
your english is clearly far more advanced than mine: I have yet to
understand a few of your comments ;-)


My French is mostly read-only, so let me rephrase:

from module import *, try ... except, eval(), exec all work together to make
your program harder to understand and more likely to fail in obscure ways.
Care to provide the traceback?


Traceback (most recent call last):
File "SC_Shell.py", line 1095, in ?
l_d = SC_Shell()
File "SC_Shell.py", line 326, in __init__
self.__Make_Menu_Area()
File "SC_Shell.py", line 828, in __Make_Menu_Area
l = inspect.getmembers(eval(c))
File "<string>", line 0, in ?
NameError: name 'BC' is not defined


That traceback and the code you posted do not fit together. So back to
square one.

Staring at the code you posted, I think one thing you could try would be to
modify the eval() to

eval("BC",*self.m_process_global, self.m_process_local)

to ensure that eval() and exec share the same namespace.
Generally speaking, I would prefer (I hope I got that right)

module = __import__("package.module", globals(), locals(), ["module"])
members = inspect.getmembers(module)

Members of the module would then be accessed via getattr():

member = getattr(module, member_name)

Peter

Jul 19 '05 #7
Philippe C. Martin wrote:
Any clue would be quite welcome.


I didn't recognize the pattern in the code you posted, but sometimes the
order of imports matters:

$ find .
..
../package
../package/beta.py
../package/alpha.py
../package/__init__.py
$ python
Python 2.3.3 (#1, Feb 5 2005, 16:22:10)
[GCC 3.3.3 (SuSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
from package.beta import *
from package import *
from package.alpha import *
alpha Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'alpha' is not defined beta

<module 'package.beta' from 'package/beta.py'>

The 'right' way to do the imports if you want both 'alpha' and 'beta' is of
course

from package import alpha
from package import beta

Peter

Jul 19 '05 #8
> My French is mostly read-only, so let me rephrase:
Thank you :-)
from module import *, try ... except, eval(), exec all work together to
make your program harder to understand and more likely to fail in obscure
ways. What would you suggest then, I just want my code to
1) be sturdy
2) some of the modules might not be there depending on the configuration,
and I want their members in the menus if they are installed.

If there is a cleaner way, I'm ready to try it.

eval("BC", self.m_process_global, self.m_process_local)

to ensure that eval() and exec share the same namespace.
Generally speaking, I would prefer (I hope I got that right)
That was it, thank you!

module = __import__("package.module", globals(), locals(), ["module"])
members = inspect.getmembers(module)

Members of the module would then be accessed via getattr():

member = getattr(module, member_name)
I will study that.
Many thanks

Philippe

Peter Otten wrote:
Philippe C. Martin wrote:
OK Peter, first of all thanks.

You seem to be German and although I leave in the states, I'm French and
your english is clearly far more advanced than mine: I have yet to
understand a few of your comments ;-)


My French is mostly read-only, so let me rephrase:

from module import *, try ... except, eval(), exec all work together to
make your program harder to understand and more likely to fail in obscure
ways.
Care to provide the traceback?


Traceback (most recent call last):
File "SC_Shell.py", line 1095, in ?
l_d = SC_Shell()
File "SC_Shell.py", line 326, in __init__
self.__Make_Menu_Area()
File "SC_Shell.py", line 828, in __Make_Menu_Area
l = inspect.getmembers(eval(c))
File "<string>", line 0, in ?
NameError: name 'BC' is not defined


That traceback and the code you posted do not fit together. So back to
square one.

Staring at the code you posted, I think one thing you could try would be
to modify the eval() to

eval("BC",Â*self.m_process_global, self.m_process_local)

to ensure that eval() and exec share the same namespace.
Generally speaking, I would prefer (I hope I got that right)

module = __import__("package.module", globals(), locals(), ["module"])
members = inspect.getmembers(module)

Members of the module would then be accessed via getattr():

member = getattr(module, member_name)

Peter


Jul 19 '05 #9

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

Similar topics

5
by: Christoph Haas | last post by:
Dear coders... I'm working on an application that is supposed to support "plugins". The idea is to use the plugins as packages like this: Plugins/ __init__.py Plugin1.py Plugin2.py...
4
by: DotNetJunkies User | last post by:
Hi, Does anyone know how/if you can instantiate a C# reference type object dynamically? More specifically, my project has a number of classes that I've created and in some cases it would be very...
6
by: mflanagan | last post by:
I have unmanaged C++ program that will load a managed C++ dll and then call a function in that dll. The managed C++ routine will call some C# routines. The unmanaged C++ main program will make...
7
by: Tim T | last post by:
Hi, I have the need to use dynamically loaded user controls in a webform page. I have the controls loading dynamically, and that part works fine. this is the code used in a webform to dynamically...
8
by: Donald Xie | last post by:
Hi, I noticed an interesting effect when working with controls that are dynamically loaded. For instance, on a web form with a PlaceHolder control named ImageHolder, I dynamically add an image...
1
by: mhearne808 | last post by:
I have a question about how dynamically loaded C++ modules work, which I will phrase as a hypothetical scenario involving the Numeric module. Please understand that I don't really care about...
22
by: Sandman | last post by:
So, I have this content management system I've developed myself. The system has a solid community part where members can register and then participate in forums, write weblogs and a ton of other...
4
by: rkmr.em | last post by:
Hi I have a function data, that I need to import from a file data, in the directory data If I do this from python interactive shell (linux fedora core 8) from dir /home/mark it works fine: ...
1
by: Jordan S. | last post by:
I plan to load an assembly during application startup, and load that assembly via reflection (i.e., it isn't referenced in the application's assembly manifest). The assembly will be loaded into the...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
1
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.