[FRPythoneers] Porting perl to python

Jim Vickroy Jim.Vickroy at noaa.gov
Fri Jul 11 11:45:25 MDT 2003


Hello Erick,

Here are a couple of examples:

>>>
>>> def fcn_1(): print '%s called' % fcn_1.func_name
...
>>> def fcn_2(): print '%s called' % fcn_2.func_name
...
>>> fcns = [fcn_1, fcn_2]
>>> for fcn in fcns: fcn()
...
fcn_1 called
fcn_2 called
>>> fcns = [fcn_1.func_name, fcn_2.func_name]
>>> for fcn in fcns: print fcn
...
fcn_1
fcn_2
>>> for fcn in fcns: eval('%s()'%fcn)
...
fcn_1 called
fcn_2 called
>>>



Erick Bodine wrote:

> All,
>
> I have convinced my place of employ to move from Perl to Python and I am
> currently porting the majority of the perl scripts we use to python.
> One of the scripts does a health check of the test systems.  The checks
> are inidividual functions that are run.  In the perl script the check
> functions are added to the script and a list of check functions, then
> the list of functions is then run at script runtime as anonymous
> subroutines.
>
> In the python version ideally I would import a separate module that
> would contain all of the present and any future functions and then run
> all the functions contained in that module.
>
> I have tried various manipulations using the dir() and callable()
> builtins to no avail.  I think that 'recipe' 15.3 in the Python cookbook
> might contain some clues (some manipulation around __import__) but I am
> not really familiar w/ how this works in Python.
>
> Any help would be appreciated.
>
> # Perl code
> my (@tests) = qw(domain_administrator
>                  scrubber_registry_size
>                  pr2051);
>
> foreach my $test_name ( @tests ) {
>   print "$test_name ", ("." x (26 - length($test_name))), " ";
>   my ($rc, $reason) = &$test_name;
> }
>
> # Python code
> def_list = ['test1', 'test2', 'test3]
> for f in def_list:
>         f()                     # This won't work because f is not
> callable
>
> --ERick
> _______________________________________________
> This message sent by the FRPythoneers mailing list.
> Unsubscribe: echo unsubscribe | FRPythoneers-request at lists.community.tummy.com
> URL: http://lists.community.tummy.com/mailman/listinfo/frpythoneers




More information about the FRPythoneers mailing list