hi all,
can anyone explain howto get function from module, known by string
names?
I.e. something like
def myfunc(module_string1, func_string2, *args):
eval('from ' + module_string1 + 'import ' + func_string2')
return func_string2(*args)
or, if it's impossible, suppose I have some modules
module1 module2 module3 ... etc
each module has it own funcs 'alfa', 'beta' and class module1,
module2,... with same string name as module name.
can I somehow pass module as function param and then import function
'alfa' from (for example) module8? and/or import class moduleN from
moduleN?
something like
def myfunc(moduleK, *args):
return moduleK.moduleK(*args)
or return moduleK.alfa(*args)
Thx, D 7 5065
On 15 May 2007 04:29:56 -0700, dmitrey wrote
hi all,
can anyone explain howto get function from module, known by string
names?
I.e. something like
def myfunc(module_string1, func_string2, *args):
eval('from ' + module_string1 + 'import ' + func_string2')
return func_string2(*args)
To find a module by its name in a string, use __import__. To find an object's
attribute by its name in a string, use getattr.
Together, that becomes something like this:
>>def myfunc(module_string1, func_string2, *args):
.... func = getattr(__import__(module_string1), func_string2)
.... return func(*args)
....
>>myfunc("math", "sin", 0)
0.0
>>myfunc("operator", "add", 2, 3)
5
Hope this helps,
--
Carsten Haese http://informixdb.sourceforge.net
And howto check does module 'asdf' exist (is available for import) or
no? (without try/cache of course)
Thx, D.
Carsten Haese wrote:
On 15 May 2007 04:29:56 -0700, dmitrey wrote
hi all,
can anyone explain howto get function from module, known by string
names?
I.e. something like
def myfunc(module_string1, func_string2, *args):
eval('from ' + module_string1 + 'import ' + func_string2')
return func_string2(*args)
To find a module by its name in a string, use __import__. To find an object's
attribute by its name in a string, use getattr.
Together, that becomes something like this:
>def myfunc(module_string1, func_string2, *args):
... func = getattr(__import__(module_string1), func_string2)
... return func(*args)
...
>myfunc("math", "sin", 0)
0.0
>myfunc("operator", "add", 2, 3)
5
Hope this helps,
--
Carsten Haese http://informixdb.sourceforge.net
And howto check does module 'asdf' exist (is available for import) or
no? (without try/cache of course)
Thx, D.
Carsten Haese wrote:
On 15 May 2007 04:29:56 -0700, dmitrey wrote
hi all,
can anyone explain howto get function from module, known by string
names?
I.e. something like
def myfunc(module_string1, func_string2, *args):
eval('from ' + module_string1 + 'import ' + func_string2')
return func_string2(*args)
To find a module by its name in a string, use __import__. To find an object's
attribute by its name in a string, use getattr.
Together, that becomes something like this:
>def myfunc(module_string1, func_string2, *args):
... func = getattr(__import__(module_string1), func_string2)
... return func(*args)
...
>myfunc("math", "sin", 0)
0.0
>myfunc("operator", "add", 2, 3)
5
Hope this helps,
--
Carsten Haese http://informixdb.sourceforge.net
And howto check does module 'asdf' exist (is available for import) or
no? (without try/cache of course)
Thx, D.
Carsten Haese wrote:
On 15 May 2007 04:29:56 -0700, dmitrey wrote
hi all,
can anyone explain howto get function from module, known by string
names?
I.e. something like
def myfunc(module_string1, func_string2, *args):
eval('from ' + module_string1 + 'import ' + func_string2')
return func_string2(*args)
To find a module by its name in a string, use __import__. To find an object's
attribute by its name in a string, use getattr.
Together, that becomes something like this:
>def myfunc(module_string1, func_string2, *args):
... func = getattr(__import__(module_string1), func_string2)
... return func(*args)
...
>myfunc("math", "sin", 0)
0.0
>myfunc("operator", "add", 2, 3)
5
Hope this helps,
--
Carsten Haese http://informixdb.sourceforge.net
Sorry, some problems with internet connection yield these messages
En Mon, 21 May 2007 10:13:02 -0300, dmitrey <op*****@ukr.netescribió:
And howto check does module 'asdf' exist (is available for import) or
no? (without try/cache of course)
What's wrong with this:
try: import asdf
except ImportError: asdf = None
....later...
if asdf:
asdf.zxcv(1)
...
--
Gabriel Genellina
On Mon, 2007-05-21 at 06:13 -0700, dmitrey wrote:
And howto check does module 'asdf' exist (is available for import) or
no? (without try/cache of course)
Why would you need to do that? What are you planning to do when you have
determined that the module doesn't exist? Surely you're not planning on
swallowing the error silently, because that would make your program very
hard to debug. The Pythonic response is to raise an exception, but then
you might as well just propagate the exception that __import__ already
raises:
>>def myfunc(module_string1, func_string2, *args):
.... func = getattr(__import__(module_string1), func_string2)
.... return func(*args)
....
>>myfunc("asdf", "spam")
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 2, in myfunc
ImportError: No module named asdf
Hope this helps,
--
Carsten Haese http://informixdb.sourceforge.net This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Penn Markham |
last post by:
Hello all,
I am writing a script where I need to use the system() function to call
htpasswd. I can do this just fine on the command line...works great
(see attached file, test.php). When my...
|
by: Marc Shapiro |
last post by:
I am relatively new to python (I have used it on and off for a few small
projects over the last few years) so I imagine that what I am trying to do
has already been done, but practical experience,...
|
by: Mark English |
last post by:
I'd like to write a Tkinter app which, given a class, pops up a
window(s) with fields for each "attribute" of that class. The user could
enter values for the attributes and on closing the window...
|
by: Andy |
last post by:
What do people think of this?
'prefixed string'.lchop('prefix') == 'ed string'
'string with suffix'.rchop('suffix') == 'string with '
'prefix and suffix.chop('prefix', 'suffix') == ' and '
...
|
by: Guy Robinson |
last post by:
Hello,
I have a directory of python scripts that all (should) contain a number of
attributes and methods of the same name.
I need to import each module, test for these items and unload the...
|
by: Bruce Lawrence |
last post by:
I'm running Access 97 and my modules are looping if someone puts an
invalid value in.
The setup:
3 macros - get_clock_num, verify_clocknum, append_to_history
3 functions. each in their own...
|
by: ATS |
last post by:
HOWTO Implement LoadLibrary, GetProcAdress, and FreeLibrary.
Below is code that I want to be able to use simple
LoadLibrary\GetProcAddress\FreeLibrary technqiues on. I've used the code that
was...
|
by: Ray Dukes |
last post by:
What I am looking to do is map the implementation of interface
properties and functions to an inherited method of the base class.
Please see below.
...
|
by: Gillard |
last post by:
hello,
anyone have a snippet to get the name of the season for a date????
something like
GetSeason(today)
and it return summer
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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: 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,...
| | |