# HG changeset patch # User Brian Neal # Date 1375321032 18000 # Node ID 98eb01502cf5b12bec808ec97e1a0d04611d0fd8 # Parent 2b0e229e163cc17e52ac3dd5a209fdc9353ffc59 Follow up to last commit. Re-orient the r-pentomino. Added progress display. diff -r 2b0e229e163c -r 98eb01502cf5 Life.py --- a/Life.py Thu Jul 25 21:53:44 2013 -0500 +++ b/Life.py Wed Jul 31 20:37:12 2013 -0500 @@ -25,7 +25,8 @@ n: the number of rows and columns """ - def __init__(self, n, mode='wrap', cval=0.0, random=False): + def __init__(self, n, mode='wrap', cval=0.0, random=False, + show_progress=None): """Attributes: n: number of rows and columns mode: how border conditions are handled @@ -35,6 +36,9 @@ self.n = n self.mode = mode self.cval = cval + self.show_progress = show_progress + self.steps = 0 + if random: self.array = numpy.random.random_integers(0, 1, (n, n)) else: @@ -51,7 +55,8 @@ def loop(self, steps=1): """Executes the given number of time steps.""" - [self.step() for i in xrange(steps)] + for i in xrange(steps): + self.step() def step(self): """Executes one time step.""" @@ -63,6 +68,10 @@ boolean = (con==3) | (con==12) | (con==13) self.array = numpy.int8(boolean) + self.steps += 1 + if self.show_progress and self.steps % self.show_progress == 0: + print self.steps + class LifeViewer(object): """Generates an animated view of the grid.""" diff -r 2b0e229e163c -r 98eb01502cf5 ch7ex2.py --- a/ch7ex2.py Thu Jul 25 21:53:44 2013 -0500 +++ b/ch7ex2.py Wed Jul 31 20:37:12 2013 -0500 @@ -11,14 +11,16 @@ class RPentomino(Life.Life): def __init__(self, n): - super(RPentomino, self).__init__(n, mode='constant', cval=0, random=False) + super(RPentomino, self).__init__(n, mode='constant', cval=0, + random=False, show_progress=10) i, j = n / 2, n / 2 self.array[i, j] = 1 self.array[i, j - 1] = 1 - self.array[i, j + 1] = 1 + self.array[i + 1, j] = 1 + self.array[i + 1, j + 1] = 1 self.array[i - 1, j] = 1 - self.array[i + 1, j - 1] = 1 + if __name__ == '__main__': life = RPentomino(150)