From a7ca40bfe1c3ca86e239574a01d958e4eba751ee Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Tue, 7 Apr 2015 16:53:43 +0200 Subject: Add a couple of utility functions to Node --- monkey/graph.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'monkey') diff --git a/monkey/graph.py b/monkey/graph.py index 3e1d3a2..cbe44f7 100644 --- a/monkey/graph.py +++ b/monkey/graph.py @@ -20,6 +20,19 @@ class Node(object): target.ein.append(self) return target + # Return a list of nodes in [self]. + def preorder(self): + nodes = [self] + for n in self.eout: + nodes += n.preorder() + return nodes + + # Return a one-line string representation of [self]. + def __str__(self): + return '(' + str(self.data) + ' ' + \ + ' '.join([str(c) if c.eout else '"'+str(c.data)+'"' for c in self.eout]) + \ + ')' + def __repr__(self): return str(self.data) -- cgit v1.2.1