From: Dietrich Bollmann on 29 Apr 2010 12:23 Hi, I would like to represent graphs as cyclic dictionaries in Python. The python code for the graphs is generated by some other program (written in lisp) and I wonder what would be the best syntax for writing the cycles in Python? The following works: >>> a = {} >>> a['a'] = a As can be seen here: >>> a {'a': {...}} >>> a['a'] {'a': {...}} >>> a['a']['a'] {'a': {...}} >>> a['a']['a']['a'] {'a': {...}} >>> but I wonder if there is some easier syntax to represent the cycles? "Tags" like the following would be ideal: [0] {'a': [0]} but is there something like this in Python? Thanks, Dietrich PS: If there is such a representation, how could I make Python print it out? The normal printing is not very informative: >>> a {'a': {...}} And Pythons pretty-print functionality is not better either: >>> pprint.pprint(a) {'a': <Recursion on dict with id=3076782660>} Any idea? Thanks again :)
From: Robert Kern on 29 Apr 2010 12:56 On 4/29/10 11:23 AM, Dietrich Bollmann wrote: > Hi, > > I would like to represent graphs as cyclic dictionaries in Python. > > The python code for the graphs is generated by some other program > (written in lisp) and I wonder what would be the best syntax for writing > the cycles in Python? You can implement your ideas using Armin Ronacher's pretty.py: http://pypi.python.org/pypi/pretty The default pretty printer for dicts looks like this: def dict_pprinter(obj, p, cycle): if cycle: return p.text('{...}') p.begin_group(1, '{') keys = obj.keys() try: keys.sort() except Exception, e: # Sometimes the keys don't sort. pass for idx, key in enumerate(keys): if idx: p.text(',') p.breakable() p.pretty(key) p.text(': ') p.pretty(obj[key]) p.end_group(1, '}') You could conceivably subclass RepresentationPrinter (the variable p above is an instance of this) that will assign increasing ID numbers to repeated objects so you can tag them. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
From: Chris Rebert on 29 Apr 2010 12:56 On Thu, Apr 29, 2010 at 9:23 AM, Dietrich Bollmann <diresu(a)web.de> wrote: > Hi, > > I would like to represent graphs as cyclic dictionaries in Python. > > The python code for the graphs is generated by some other program > (written in lisp) and I wonder what would be the best syntax for writing > the cycles in Python? > > The following works: > >>>> a = {} >>>> a['a'] = a > > As can be seen here: > >>>> a > {'a': {...}} >>>> a['a'] > {'a': {...}} >>>> a['a']['a'] > {'a': {...}} >>>> a['a']['a']['a'] > {'a': {...}} >>>> > > but I wonder if there is some easier syntax to represent the cycles? > > "Tags" like the following would be ideal: > > [0] {'a': [0]} > > but is there something like this in Python? Pretty sure that's a No. > PS: Â If there is such a representation, how could I make Python print it > out? > > The normal printing is not very informative: > >>>> a > {'a': {...}} > > And Pythons pretty-print functionality is not better either: > >>>> pprint.pprint(a) > {'a': <Recursion on dict with id=3076782660>} > > Any idea? 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
From: Garrick P on 29 Apr 2010 14:09 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
From: Dietrich Bollmann on 29 Apr 2010 23:24
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! > > > > |