Account Options

  1. Sign in
Google Groups Home
« Groups Home
Message from discussion converting a string to a function parameter
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Paul McGuire  
View profile  
 More options Mar 13 2009, 4:21 pm
Newsgroups: comp.lang.python
From: Paul McGuire <pt...@austin.rr.com>
Date: Fri, 13 Mar 2009 13:21:45 -0700 (PDT)
Local: Fri, Mar 13 2009 4:21 pm
Subject: Re: converting a string to a function parameter
On Mar 13, 11:46 am, Aaron Brady <castiro...@gmail.com> wrote:

> On Mar 13, 2:52 am, koranthala <koranth...@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):

from pyparsing import *

LPAR,RPAR,LBRACK,RBRACK,EQ,COMMA = map(Suppress,"()[]=,")

noneLiteral = Literal("None")
boolLiteral = oneOf("True False")
integer = Combine(Optional(oneOf("+ -")) + Word(nums)).setName
("integer")
real = Combine(Optional(oneOf("+ -")) + Word(nums) + "." +
               Optional(Word(nums))).setName("real")

ident = Word(alphas+"_",alphanums+"_")

listStr = Forward().setName("list")
tupleStr = Forward().setName("tuple")
listItem = real | integer | noneLiteral | boolLiteral | \
    quotedString.setParseAction(removeQuotes) | Group(listStr) |
tupleStr | ident
listStr << ( LBRACK + Optional(delimitedList(listItem)) + Optional
(COMMA) + RBRACK )
tupleStr << (LPAR + Optional(delimitedList(listItem)) + Optional
(COMMA) + RPAR)

# parse actions perform parse-time conversions
noneLiteral.setParseAction(lambda: None)
boolLiteral.setParseAction(lambda toks: toks[0]=="True")
integer    .setParseAction(lambda toks: int(toks[0]))
real       .setParseAction(lambda toks: float(toks[0]))
listStr    .setParseAction(lambda toks: toks.asList())
tupleStr   .setParseAction(lambda toks: tuple(toks.asList()))

arg = Group(ident("varname") + EQ + listItem("varvalue")) | listItem

argstring = 'True, type=rect, sizes=[3, 4,], coords = ([1,2],[3,4])'

parsedArgs = delimitedList(arg).parseString(argstring)
args = []
kwargs = {}
for a in parsedArgs:
    if isinstance(a,ParseResults):
        if isinstance(a.varvalue,ParseResults):
            val = a.varvalue.asList()
        else:
            val = a.varvalue
        kwargs[a.varname] = val
    else:
        args.append(a)

print "Args:", args
print "Kwargs:", kwargs

Prints:

Args: [True]
Kwargs: {'coords': ([1, 2], [3, 4]), 'type': 'rect', 'sizes': [3, 4]}


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.