Can this be done simpler ?

Stef Mientki stef.mientki at gmail.com
Mon Apr 19 17:52:30 CEST 2010


hello,

I want to use Python to give users the possibility to analyze data and
create their custom reports.
So I want a very simple language definition for the user, like :
- the script must be case-insensitive
- "user-words" are automatically translated into function names
- users strings, should be entered without the quotes, so these strings will
be defined as names

Now the code below seems to fulfill these wishes (in real life, the number
of dummy procedures is about 40),
but I always wonder if there's an easier way to achieve the same effect.

thanks,
Stef Mientki

def _Meting ( Nr, Test, Subschaal ) :
    """
    here the real calculation will be done
    """
    global Result
    Result += str ( Nr ) + ': ' + Test + '/' + Subschaal + '\n'

# Dummy procedures to add the extra argument "Nr"
def _Meting_1 ( *args, **kwargs ) :
    _Meting ( 1, *args, **kwargs )

def _Meting_2 ( *args, **kwargs ) :
    _Meting ( 2, *args, **kwargs )
# end of dummy procedures

# These are names definied by the user, retrieved from a database
Meting_Namen = {}
Meting_Namen [1] = 'Voormeting'
Meting_Namen [2] = 'Nameting'

# Make the user definied names available in the current namespace
for Meting in Meting_Namen :
    Name = Meting_Namen [ Meting ].lower ()   # 'voormeting'
    exec ( Name + '= _Meting_' + str ( Meting ) )

# Another set of strings should be available as names (also retrieved from a
database)
test1 = 'Test1'
fat = 'Fat'

# Storage for the results
Result = ''

# Script entered by the user
Code = "Voormeting ( Test1, fat )"

# execute the user script in the current namespace  (make code
case-insensitive)
exec ( Code.lower () )

# for test print the result
print Result
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100419/b9d7cf04/attachment.html>


More information about the Python-list mailing list