From 7388eaec54de0fc75c6aff371dfed1a33463c176 Mon Sep 17 00:00:00 2001 From: Arthur O'Dwyer Date: Mon, 17 Apr 2017 23:17:47 -0700 Subject: [PATCH] Teach `Context` to be self-sufficient. NFC. You can now create and run a Copycat instance by just saying `Context().run('abc', 'abd', 'efg', 100)`! --- copycat/context.py | 22 ++++++++++++---------- copycat/copycat.py | 18 ------------------ copycat/main.py | 4 ++-- copycat/tests.py | 4 ++-- copycat/workspace.py | 7 ++----- 5 files changed, 18 insertions(+), 37 deletions(-) delete mode 100644 copycat/copycat.py diff --git a/copycat/context.py b/copycat/context.py index 10efe2a..59cd241 100644 --- a/copycat/context.py +++ b/copycat/context.py @@ -1,13 +1,18 @@ import logging +from coderack import Coderack +from randomness import Randomness +from slipnet import Slipnet +from temperature import Temperature +from workspace import Workspace class Context(object): - def __init__(self): - self.coderack = None - self.random = None - self.slipnet = None - self.temperature = None - self.workspace = None + def __init__(self, rng_seed=None): + self.coderack = Coderack(self) + self.random = Randomness(rng_seed) + self.slipnet = Slipnet() + self.temperature = Temperature() + self.workspace = Workspace(self) def mainLoop(self, lastUpdate): currentTime = self.coderack.codeletsRun @@ -45,7 +50,7 @@ class Context(object): answers[answer]['timesum'] += finalTime def run(self, initial, modified, target, iterations): - self.workspace.setStrings(initial, modified, target) + self.workspace.resetWithStrings(initial, modified, target) answers = {} for i in xrange(iterations): self.runTrial(answers) @@ -53,6 +58,3 @@ class Context(object): d['avgtemp'] = d.pop('tempsum') / d['count'] d['avgtime'] = d.pop('timesum') / d['count'] return answers - - -context = Context() diff --git a/copycat/copycat.py b/copycat/copycat.py deleted file mode 100644 index d1adc58..0000000 --- a/copycat/copycat.py +++ /dev/null @@ -1,18 +0,0 @@ -from coderack import Coderack -from randomness import Randomness -from slipnet import Slipnet -from temperature import Temperature -from workspace import Workspace - -from context import context - - -context.coderack = Coderack(context) -context.random = Randomness(42) -context.slipnet = Slipnet() -context.temperature = Temperature() -context.workspace = Workspace(context) - - -def run(initial, modified, target, iterations): - return context.run(initial, modified, target, iterations) diff --git a/copycat/main.py b/copycat/main.py index feb1a68..d6d670e 100644 --- a/copycat/main.py +++ b/copycat/main.py @@ -3,8 +3,7 @@ import logging import sys -import copycat - +from context import Context def main(program, args): """Run the program""" @@ -18,6 +17,7 @@ def main(program, args): else: initial, modified, target = args iterations = 1 + copycat = Context(rng_seed=42) answers = copycat.run(initial, modified, target, iterations) for answer, d in sorted(answers.iteritems(), key=lambda kv: kv[1]['avgtemp']): print '%s: %d (avg time %.1f, avg temp %.1f)' % (answer, d['count'], d['avgtime'], d['avgtemp']) diff --git a/copycat/tests.py b/copycat/tests.py index ae2e343..c3c5ee0 100644 --- a/copycat/tests.py +++ b/copycat/tests.py @@ -2,7 +2,7 @@ import unittest -import copycat +from context import Context def pnormaldist(p): table = { @@ -66,7 +66,7 @@ class TestCopycat(unittest.TestCase): self.fail('No instances of expected key %s were produced! %r != %r' % (k, actual, expected)) def run_testcase(self, initial, modified, target, iterations, expected): - actual = copycat.run(initial, modified, target, iterations) + actual = Context().run(initial, modified, target, iterations) self.assertEqual(sum(a['count'] for a in actual.values()), iterations) self.assertProbabilitiesLookRoughlyLike(actual, expected) diff --git a/copycat/workspace.py b/copycat/workspace.py index d7209eb..30bf181 100644 --- a/copycat/workspace.py +++ b/copycat/workspace.py @@ -16,10 +16,7 @@ def __adjustUnhappiness(values): class Workspace(object): def __init__(self, ctx): - #logging.debug('workspace.__init__()') self.ctx = ctx - self.setStrings('', '', '') - self.reset() self.totalUnhappiness = 0.0 self.intraStringUnhappiness = 0.0 self.interStringUnhappiness = 0.0 @@ -28,13 +25,13 @@ class Workspace(object): return '' % ( self.initialString, self.modifiedString, self.targetString) - def setStrings(self, initial, modified, target): + def resetWithStrings(self, initial, modified, target): self.targetString = target self.initialString = initial self.modifiedString = modified + self.reset() def reset(self): - #logging.debug('workspace.reset()') self.foundAnswer = False self.changedObject = None self.objects = []