Create list/dict from string

2010-01-19 Thread SoxFan44
I was wondering if there was a way to create a list (which in this
case would contain several dicts) based on a string passed in by the
user.  Security is not an issue.  Basically I want to be able to have
the user pass in using optparse:
--actions=[{action_name: action_1, val: asdf, val2:
asdf},
   {action_name: action_2, val: asdf, val2:
asdf},
   {action_name: action_1, val: asdf, val2:
asdf}]

And have this create a list/dict.  I'm aware of pickle, but it won't
work as far as I can tell.

Thanks.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Create list/dict from string

2010-01-19 Thread Peter Otten
SoxFan44 wrote:

 I was wondering if there was a way to create a list (which in this
 case would contain several dicts) based on a string passed in by the
 user.  Security is not an issue.  Basically I want to be able to have
 the user pass in using optparse:
 --actions=[{action_name: action_1, val: asdf, val2:
 asdf},
{action_name: action_2, val: asdf, val2:
 asdf},
{action_name: action_1, val: asdf, val2:
 asdf}]
 
 And have this create a list/dict.  I'm aware of pickle, but it won't
 work as far as I can tell.

Both eval() and json.loads() will do. eval() is dangerous as it allows the 
user to run arbitrary python code.

Peter
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Create list/dict from string

2010-01-19 Thread Gerald Britton
Can't you just use the dict() built-in function like this:

dict({action_name: action_1, val: asdf})

Of course if the list is not properly formed, this will fail.  But I
guess you have thought of that already.

On Tue, Jan 19, 2010 at 11:33 AM, SoxFan44 gregchag...@gmail.com wrote:
 I was wondering if there was a way to create a list (which in this
 case would contain several dicts) based on a string passed in by the
 user.  Security is not an issue.  Basically I want to be able to have
 the user pass in using optparse:
 --actions=[{action_name: action_1, val: asdf, val2:
 asdf},
               {action_name: action_2, val: asdf, val2:
 asdf},
               {action_name: action_1, val: asdf, val2:
 asdf}]

 And have this create a list/dict.  I'm aware of pickle, but it won't
 work as far as I can tell.

 Thanks.
 --
 http://mail.python.org/mailman/listinfo/python-list




-- 
Gerald Britton
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Create list/dict from string

2010-01-19 Thread Simon Brunning
2010/1/19 Peter Otten __pete...@web.de:
 Both eval() and json.loads() will do. eval() is dangerous as it allows the
 user to run arbitrary python code.

Something like http://code.activestate.com/recipes/364469/ might be
worth a look too.

-- 
Cheers,
Simon B.
-- 
http://mail.python.org/mailman/listinfo/python-list