Code: Rolling a Container Into a String

Pierre-Frédéric Caillaud peufeu at free.fr
Fri Jun 25 02:51:36 EDT 2004


	Use YAML

import yaml

then from your code:

yaml.dump( whatever ) =>

then yaml.loadstring(str)...

It handles objects.
{'name': 'the name', 'tuple': ('tuple1', 'tuple2'), 'int': 1, 'float':  
1.5, 'list': ['ol1', 'ol2'], 'long': 12345678901234567890,
'dict': {'od1': None, 'od2': 2}, 'bool': True, 'password': 'the password'}

--- !!__main__.simple	# instanciates class for you
bool: True
dict:
     od1: ~
     od2: 2
float: 1.5
int: 1
list:
     - ol1
     - ol2
long: 12345678901234567890
name: the name
password: the password
tuple:
     - tuple1
     - tuple2

It won't roll attributes whose name starts with '_'
{'name': 'the name', 'tuple': ('tuple1', 'tuple2'), 'int': 1, 'float':  
1.5, 'list': ['ol1', 'ol2'], 'long': 12345678901234567890,
'dict': {'od1': None, 'od2': 2}, 'bool': True, 'password': 'the password'}

--- !!__main__.simple
_recursion:		# yaml does not handle recursion (I have an old version)
     - tuple1
     - tuple2
bool: True
dict:
     od1: ~
     od2: 2
float: 1.5
int: 1
list:
     - ol1
     - ol2
long: 12345678901234567890
name: the name
password: the password
tuple:
     - tuple1
     - tuple2

It will raise an exception if it detects recursion.
This next one will cause recursion.
--- !!__main__.simple
_recursion:
     - tuple1
     - tuple2
bool: True
dict:
     od1: ~
     od2: 2
float: 1.5
int: 1
list:
     - ol1
     - ol2
long: 12345678901234567890
name: the name
password: the password
recursion:
     - tuple1
     - tuple2
tuple:
     - tuple1
     - tuple2



More information about the Python-list mailing list