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
--
http://mail.python.org/mailman/listinfo/python-list