Python 2.3b1: RuntimeError using rexec

Lulu of the Lotus-Eaters mertz at gnosis.cx
Tue Apr 29 16:28:00 EDT 2003


|"Russell E. Owen" <no at spam.invalid> writes:
|> I stumbled across this when testing some existing code. This code uses
|> rexec to convert a string representation of a dictionary to a dictionary
|> as part of reading in font preferences.
|> Any suggestion on how to do that without rexec would also be
|> appreciated. (I was quite disappointed that "dict" didn't do it!)

I'm not sure exactly what string representation of a dictionary you use.
But it's not too hard to use dict().  E.g.:

  >>> d = '''
  ... a:b
  ... c:d
  ... e:f
  ... '''
  >>> dict([x.split(':') for x in d.split()])
  {'a': 'b', 'c': 'd', 'e': 'f'}

Or:

  >>> dct = "{'a': 'b', 'c': 'd', 'e': 'f'}"
  >>> dict([map(str.strip, x.split(':'))
  ...       for x in dct[1:-1].replace("'",'').split(',')])
  {'a': 'b', 'c': 'd', 'e': 'f'}


--
 mertz@  _/_/_/_/ THIS MESSAGE WAS BROUGHT TO YOU BY: \_\_\_\_    n o
gnosis  _/_/             Postmodern Enterprises            \_\_
.cx    _/_/                                                 \_\_  d o
      _/_/_/ IN A WORLD W/O WALLS, THERE WOULD BE NO GATES \_\_\_ z e






More information about the Python-list mailing list