How to pretty-print cyclic dictionaries?

Dietrich Bollmann diresu at web.de
Thu Apr 29 23:24:58 EDT 2010


Hi Chris and Garrick :)

On Thu, 2010-04-29 at 18:09 +0000, Garrick P wrote:
> Chris Rebert <clp2 <at> rebertia.com> writes:
> 
> ...
> 
> > If you want a prettier print, you could try serializing it to YAML and
> > printing the result out; YAML has syntax for "tags".
> > 
> > Cheers,
> > Chris
> > --
> > http://blog.rebertia.com
> 
> Works fairly well.
> 
> $ python
> Python 2.6.4 (r264:75706, Mar  1 2010, 14:28:00)
> [GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import yaml
> >>> a = {}
> >>> a['a'] = a
> >>> a
> {'a': {...}}
> >>> print yaml.dump(a)
> &id001
> a: *id001
> >>> b = {}
> >>> b['b'] = b
> >>> b['a'] = a
> >>> a['b'] = b
> >>> print yaml.dump(a)
> &id001
> a: *id001
> b: &id002
>   a: *id001
>   b: *id002

Great!

This is exactly what I was looking for (...even if I still prefer
parentheses to indentation - yaml and python with (the option to use)
parentheses would be my dream :).

Thank you very much!


> 
> 
> 
> 





More information about the Python-list mailing list