parsing args

2 views
Skip to first unread message

prologic

unread,
Jul 18, 2010, 12:13:12 AM7/18/10
to creoleparser
Hi all,

No doubt some of you have seen my post issues on the
creoleparser project. This is because I'm using it in a new
project and am actually quite happy with the design and
extensibility of the parser so far...

What I want to do now which I can't figure out how to do
is the following (let me demonstrate):

>>> parse_args('foo=True')
('foo', 'True')
([], {'foo': True})
>>> parse_args('foo="True"')
('foo', '"True"')
([], {'foo': True})
>>>

What current happens is this:


>>> parse_args('foo=True')
('foo', 'True')
([], {'foo': True})
>>> parse_args('foo="True"')
('foo', 'True')
([], {'foo': True})
>>>

Basically I want any quotes strings found
to be left quoted. Make sense ?

Thanks heaps!

cheers
James

PS:
If the author of creoleparse is reading this...
I am currently maintaining a custom version
of creoleparser in my project with modifications
as I go... I will at some point try to share this.

Stephen Day

unread,
Jul 18, 2010, 1:11:00 AM7/18/10
to creoleparser
Hi James,

For the parse_args function, quoting is optional, except when values
include spaces or special characters like quotes. Therefore, the
output of the first two examples below are equivalent. The third
example shows how quotes can be included in values:

>>> from creoleparser import parse_args
>>> parse_args(""" foo=True """)
([], {'foo': 'True'})
>>> parse_args(""" foo='True' """)
([], {'foo': 'True'})
>>> parse_args(""" foo='"True"' """)
([], {'foo': '"True"'})

To get the exact behavior you are looking for, you could try removing
quoted_arg from dialects.creepy10_base (by over-riding the
top_elements method ).

Stephen

James Mills

unread,
Jul 18, 2010, 1:19:34 AM7/18/10
to creole...@googlegroups.com
On Sun, Jul 18, 2010 at 3:11 PM, Stephen Day <stephe...@gmail.com> wrote:
> To get the exact behavior you are looking for, you could try removing
> quoted_arg from dialects.creepy10_base  (by over-riding the
> top_elements method ).

This is precisely what I want. Thanks!

I'm not really interested (in my case) in creepy10 to remove
quotes from the input string. I'd rather they stay there so my
unrepr(...) function can work correctly.

In my wiki/cms/blog (1) engine macros (will now) will accept real
python values not just purely strings.

--james

1. http://sahriswiki.org/


--
-- James Mills
--
-- "Problems are solved by method"

James Mills

unread,
Jul 18, 2010, 1:59:44 AM7/18/10
to creole...@googlegroups.com
On Sun, Jul 18, 2010 at 3:19 PM, James Mills
<prol...@shortcircuit.net.au> wrote:
> This is precisely what I want. Thanks!

Turns out this isn't what I want, nor does it do what i want :/

Here's the thing, say you have a macro:

<<foo "foobar" flag=True>>

flag comes back as a kwarg {"flag": 'True'}

What I need is some kind of rudamentary support for given
arguments and keywords arguments to macros real python ones.

Any ideas ?

My idea of simply running unrepr(...) a function which I've borrowed
from Cherrypy's project which they borrowed from some public domain
module lets you basically safely unrepr any given string. For example:

~/sahriswiki-0.8/sahriswiki
$ python
Python 2.6.5 (r265:79063, Jun 13 2010, 14:03:16)
[GCC 4.4.4 (CRUX)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from unrepr import unrepr
>>> unrepr("'foo'") # a string
'foo'
>>> unrepr("True") # a bool
True
>>> unrepr("1234") # an int
1234
>>> unrepr("[1, 2, 3, 4]") # a list
[1, 2, 3, 4]
>>>

cheers
James

Stephen Day

unread,
Jul 18, 2010, 8:31:17 AM7/18/10
to creoleparser
Instead of running unrepr() on the returned dictionary, why not
unrepr() the individual dictionary values instead?

new_dict = {}
for k,v in original_dict.items():
new_dict[k] = unrepr(v)

Same idea for the list of positional arguments.



On Jul 18, 1:59 am, James Mills <prolo...@shortcircuit.net.au> wrote:
> On Sun, Jul 18, 2010 at 3:19 PM, James Mills
>

James Mills

unread,
Jul 18, 2010, 4:45:14 PM7/18/10
to creole...@googlegroups.com
On Sun, Jul 18, 2010 at 10:31 PM, Stephen Day <stephe...@gmail.com> wrote:
> Instead of running unrepr() on the returned dictionary, why not
> unrepr() the individual dictionary values instead?
>
> new_dict = {}
> for k,v in original_dict.items():
>    new_dict[k] = unrepr(v)
>
> Same idea for the list of positional arguments.

The reason this doesn't work as I want is because of
how the input string is parsed.

<<foo "bar">>

is translated to args = ['bar']

Where I really need args ['\'bar\'']

And <<foo bar="foobar">>

is translated to kwargs {'bar': 'foobar'}

where I really need {'bar': '\'foobar\''}

I guess I want to do value conversion myselfl.

Stephen Day

unread,
Jul 18, 2010, 5:08:28 PM7/18/10
to creoleparser
>
> I guess I want to do value conversion myselfl.
>
Probably true...

You may want to try this parser out (along with the per value unrepr
thing). Though I'm really not sure what other issues you may run into:

import creoleparser
Base = creoleparser.dialects.creepy10_base()
class MyDialect(Base):
@property
def top_elements(self):
return [self.kw_arg, self.spaces]

no_quote_arg_parser = creoleparser.core.ArgParser(MyDialect)

James Mills

unread,
Jul 18, 2010, 5:15:03 PM7/18/10
to creole...@googlegroups.com
On Mon, Jul 19, 2010 at 7:08 AM, Stephen Day <stephe...@gmail.com> wrote:
> You may want to try this parser out (along with the per value unrepr
> thing). Though I'm really not sure what other issues you may run into:
>
> import creoleparser
> Base = creoleparser.dialects.creepy10_base()
> class MyDialect(Base):
>    @property
>    def top_elements(self):
>        return [self.kw_arg, self.spaces]
>
> no_quote_arg_parser = creoleparser.core.ArgParser(MyDialect)

I did try this when you suggested it the first time.

It didn't quite work as well as I wanted behavior-wise :/

*sigh* :)

--James

Reply all
Reply to author
Forward
0 new messages