converting a string to a function parameter

Aaron Brady castironpi at gmail.com
Fri Mar 13 22:45:07 CET 2009


On Mar 13, 3:21 pm, Paul McGuire <pt... at austin.rr.com> wrote:
> On Mar 13, 11:46 am, Aaron Brady <castiro... at gmail.com> wrote:
>
>
>
> > On Mar 13, 2:52 am, koranthala <koranth... at gmail.com> wrote:
>
> > > Hi,
> > >     Is it possible to convert a string to a function parameter?
> > > Ex:
> > > str = 'True, type=rect, sizes=[3, 4]'
> > > and I should be able to use it as:
> > > test(convert(str)) and the behaviour should be same as calling test
> > > with those values :
> > > i.e. test(True, type=rect, sizes=[3, 4])
>
> > > I tried eval, but it did not work. And any other mechanism I think
> > > turns out to be creating a full fledged python parser.
>
> > > Is there any mechanism with which we can do this straight away?
>
> > I heard 'pyparsing' was good.  ...Not that I've even been to its
> > webpage.
>
> Did someone say 'pyparsing'? :)  Here is a first cut (partially lifted
> from a previous post):
snip 40 lines
> Prints:
>
> Args: [True]
> Kwargs: {'coords': ([1, 2], [3, 4]), 'type': 'rect', 'sizes': [3, 4]}

Ha, ok, out of my league.  It's a bit heavyweight I accede.  The OP
didn't say what s/he knew about his/er data prior, what fault
tolerance s/he needed, what complexity and nesting of data in the
string, etc.

Hmmm..., just thinking.  Could the strings come from a python file:
test1= fargs(True, type=rect, sizes=[3, 4])
test2= fargs(...)
?



More information about the Python-list mailing list