From a3b977846e1fda74a4877d74e65424f6e0c787a7 Mon Sep 17 00:00:00 2001 From: Arthur O'Dwyer Date: Tue, 18 Apr 2017 01:52:50 -0700 Subject: [PATCH] git mv context.py -> copycat.py; and start work on a "reporter" API. The goal here is to use `curses` to display the coderack, slipnet, and temperature in real time. A further goal would be a reporter that sent the data over websockets to a browser, at which point I could throw this thing up on Heroku and let people mess with it. (Not that that would be very entertaining, yet.) --- copycat/{context.py => copycat.py} | 19 +++++++++++++++++-- copycat/main.py | 27 +++++++++++---------------- copycat/tests.py | 4 ++-- 3 files changed, 30 insertions(+), 20 deletions(-) rename copycat/{context.py => copycat.py} (81%) diff --git a/copycat/context.py b/copycat/copycat.py similarity index 81% rename from copycat/context.py rename to copycat/copycat.py index d8eab77..0bc1c62 100644 --- a/copycat/context.py +++ b/copycat/copycat.py @@ -5,13 +5,25 @@ from temperature import Temperature from workspace import Workspace -class Context(object): - def __init__(self, rng_seed=None): +class CopycatReporter(object): + def report_coderack(self, coderack): + pass + + def report_slipnet(self, slipnet): + pass + + def report_temperature(self, temperature): + pass + + +class Copycat(object): + def __init__(self, rng_seed=None, reporter=None): self.coderack = Coderack(self) self.random = Randomness(rng_seed) self.slipnet = Slipnet() self.temperature = Temperature() self.workspace = Workspace(self) + self.reporter = reporter or CopycatReporter() def mainLoop(self, lastUpdate): currentTime = self.coderack.codeletsRun @@ -23,7 +35,10 @@ class Context(object): self.slipnet.update(self.random) self.temperature.update(self.workspace.getUpdatedTemperature()) lastUpdate = currentTime + self.reporter.report_slipnet(self.slipnet) self.coderack.chooseAndRunCodelet() + self.reporter.report_coderack(self.coderack) + self.reporter.report_temperature(self.temperature) return lastUpdate def runTrial(self, answers): diff --git a/copycat/main.py b/copycat/main.py index d6d670e..d9e8962 100644 --- a/copycat/main.py +++ b/copycat/main.py @@ -1,31 +1,26 @@ -"""Run the copycat program""" - import logging import sys -from context import Context +from copycat import Copycat -def main(program, args): - """Run the program""" - logging.basicConfig(level=logging.WARN, format='%(message)s', - filename='./copycat.log', filemode='w') + +if __name__ == '__main__': + logging.basicConfig(level=logging.WARN, format='%(message)s', filename='./copycat.log', filemode='w') try: + args = sys.argv[1:] if len(args) == 4: initial, modified, target = args[:-1] iterations = int(args[-1]) 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']) - return 0 except ValueError: - print >> sys.stderr, 'Usage: %s initial modified target [iterations]' % program - return 1 + print >>sys.stderr, 'Usage: %s initial modified target [iterations]' % sys.argv[0] + sys.exit(1) + copycat = Copycat() + answers = copycat.run(initial, modified, target, iterations) -if __name__ == '__main__': - sys.exit(main(sys.argv[0], sys.argv[1:])) + 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 e55e197..731c2b6 100644 --- a/copycat/tests.py +++ b/copycat/tests.py @@ -1,6 +1,6 @@ import unittest -from context import Context +from copycat import Copycat def pnormaldist(p): @@ -67,7 +67,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 = Context().run(initial, modified, target, iterations) + actual = Copycat().run(initial, modified, target, iterations) self.assertEqual(sum(a['count'] for a in actual.values()), iterations) self.assertProbabilitiesLookRoughlyLike(actual, expected)