From dd1fe6569e3448495588ccfdbcdd0e784a984e4f Mon Sep 17 00:00:00 2001 From: Johann Dreo Date: Tue, 25 Mar 2014 20:58:52 +0100 Subject: [PATCH] Move tour, LOG and LOGN in utils, add vertices_from_set --- ants.py | 23 ++--------------------- uberplot.py | 4 ++-- utils.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 23 deletions(-) diff --git a/ants.py b/ants.py index a0f2aa4..0af848b 100644 --- a/ants.py +++ b/ants.py @@ -5,27 +5,8 @@ import math import random from collections import Counter import shortpath - - -def LOG( *args ): - """Print something on stderr and flush""" - for msg in args: - sys.stderr.write( str(msg) ) - sys.stderr.write(" ") - sys.stderr.flush() - - -def LOGN( *args ): - """Print something on stdeer, with a trailing new line, and flush""" - LOG( *args ) - LOG("\n") - - -def tour(lst): - # consecutive pairs in lst + last-to-first element - for a,b in zip(lst, lst[1:] + [lst[0]]): - yield (a,b) - +from utils import tour +from utils import LOG,LOGN def euclidian_distance( ci, cj, graph = None): return math.sqrt( float(ci[0] - cj[0])**2 + float(ci[1] - cj[1])**2 ) diff --git a/uberplot.py b/uberplot.py index 95d82e0..46d073c 100644 --- a/uberplot.py +++ b/uberplot.py @@ -28,11 +28,11 @@ def plot_segments( ax, segments, **kwargs ): ax.add_patch(patch) -def scatter_segments( ax, segments, color="black", alpha=1.0, linewidth=1.0 ): +def scatter_segments( ax, segments, **kwargs ): xy = [ ((i[0],j[0]),(i[1],j[1])) for (i,j) in segments] x = [i[0] for i in xy] y = [i[1] for i in xy] - ax.scatter( x,y, s=20, marker='o', color=color, alpha=alpha, linewidth=linewidth) + ax.scatter( x,y, s=20, marker='o', **kwargs) if __name__=="__main__": diff --git a/utils.py b/utils.py index c7e9eec..f9cd4e6 100644 --- a/utils.py +++ b/utils.py @@ -1,4 +1,20 @@ +import sys + +def LOG( *args ): + """Print something on stderr and flush""" + for msg in args: + sys.stderr.write( str(msg) ) + sys.stderr.write(" ") + sys.stderr.flush() + + +def LOGN( *args ): + """Print something on stdeer, with a trailing new line, and flush""" + LOG( *args ) + LOG("\n") + + def adjacency_from_set( segments ): graph = {} for start,end in segments: @@ -8,3 +24,17 @@ def adjacency_from_set( segments ): graph[end].append( start ) return graph + +def vertices_from_set( segments ): + vertices = set() + for start,end in segments: + vertices.add(start) + vertices.add(end) + return vertices + + +def tour(lst): + # consecutive pairs in lst + last-to-first element + for a,b in zip(lst, lst[1:] + [lst[0]]): + yield (a,b) +