From: OdarR on
On 8 fév, 11:57, Klaus Neuner <klausneune...(a)googlemail.com> wrote:
> Hello,
>
> I am writing a program that analyzes files of different formats. I
> would like to use a function for each format. Obviously, functions can
> be mapped to file formats. E.g. like this:
>
> if file.endswith('xyz'):
>     xyz(file)
> elif file.endswith('abc'):
>     abc(file)
>
> ...
>
> Yet, I would prefer to do something of the following kind:
>
> func = file[-3:]
> apply_func(func, file)
>
> Can something of this kind be done in Python?


and with eval(), did you try ?

import sys

def functext():
print "texte"

def funcdoc():
print "doc"

def funcabc():
print "abc"


if __name__ == "__main__":
#replace filename with suitable value
filename = sys.argv[1].split('.')[1]
try:
eval('func' + filename + '()')
except:
print 'error'

From: Gary Herron on
OdarR wrote:
> On 8 fév, 11:57, Klaus Neuner <klausneune...(a)googlemail.com> wrote:
>
>> Hello,
>>
>> I am writing a program that analyzes files of different formats. I
>> would like to use a function for each format. Obviously, functions can
>> be mapped to file formats. E.g. like this:
>>
>> if file.endswith('xyz'):
>> xyz(file)
>> elif file.endswith('abc'):
>> abc(file)
>>
>> ...
>>
>> Yet, I would prefer to do something of the following kind:
>>
>> func = file[-3:]
>> apply_func(func, file)
>>
>> Can something of this kind be done in Python?
>>

I may have missed a bit of this thread -- so I have to ask: Has anyone
mentioned using getattr yet? It's a way of looking up *any* attribute
using a string to specify the name. Like this for your particular example:

class Functions: # This could be a module instead of a class
def xyz(...):
...
def abc(...):
...
... and so on ...


ext = os.path.splitext(file) # Parses out the extension
fn = getattr(Functions, ext) # Lookup the correct function
fn(...) # and call it

Gary Herron


From: Aahz on
In article <0efe23a6-b16d-4f92-8bc0-12d056bf599d(a)z26g2000yqm.googlegroups.com>,
OdarR <olivier.darge(a)gmail.com> wrote:
>
>and with eval(), did you try ?

WARNING: eval() is almost always the wrong answer to any question
--
Aahz (aahz(a)pythoncraft.com) <*> http://www.pythoncraft.com/

import antigravity
From: OdarR on
On 8 fév, 22:28, a...(a)pythoncraft.com (Aahz) wrote:
> In article <0efe23a6-b16d-4f92-8bc0-12d056bf5...(a)z26g2000yqm.googlegroups..com>,
>
> OdarR  <olivier.da...(a)gmail.com> wrote:
>
> >and with eval(), did you try ?
>
> WARNING: eval() is almost always the wrong answer to any question

warning : it works !
another question ?


> --
> Aahz (a...(a)pythoncraft.com)           <*>        http://www.pythoncraft.com/
>
> import antigravity

From: Aahz on
In article <5790c33c-13d0-4596-91b0-b3c9aeebf637(a)f8g2000yqn.googlegroups.com>,
OdarR <olivier.darge(a)gmail.com> wrote:
>On 8 f=E9v, 22:28, a...(a)pythoncraft.com (Aahz) wrote:
>> In article <0efe23a6-b16d-4f92-8bc0-12d056bf5...(a)z26g2000yqm.googlegroups=
>.com>,
>> OdarR =A0<olivier.da...(a)gmail.com> wrote:
>>>
>>>and with eval(), did you try ?
>>
>> WARNING: eval() is almost always the wrong answer to any question
>
>warning : it works !

Works for what?
--
Aahz (aahz(a)pythoncraft.com) <*> http://www.pythoncraft.com/

import antigravity