From 9f8bc8e66e4ebdda2b324352501ff5d53e6b412a Mon Sep 17 00:00:00 2001 From: Arthur O'Dwyer Date: Tue, 18 Apr 2017 20:55:56 -0700 Subject: [PATCH] Remove all `print` statements from the Copycat library. Convert the most important one to logging; kill the rest. --- copycat/codeletMethods.py | 5 +---- copycat/copycat.py | 33 ++++++++++++++++++++++++--------- copycat/workspace.py | 9 ++------- copycat/workspaceFormulas.py | 5 +++-- 4 files changed, 30 insertions(+), 22 deletions(-) diff --git a/copycat/codeletMethods.py b/copycat/codeletMethods.py index 4a08ced..a1d6254 100644 --- a/copycat/codeletMethods.py +++ b/copycat/codeletMethods.py @@ -35,7 +35,7 @@ def __showWhichStringObjectIsFrom(structure): whence = 'target' if structure.string == workspace.initial: whence = 'initial' - #print 'object chosen = %s from %s string' % (structure, whence) + logging.info('object chosen = %s from %s string' % (structure, whence)) def __getScoutSource(ctx, slipnode, relevanceMethod, typeName): @@ -740,7 +740,6 @@ def group_builder(ctx, codelet): workspace = ctx.workspace # update strength value of the group group = codelet.arguments[0] - #print '%s' % group __showWhichStringObjectIsFrom(group) equivalent = group.string.equivalentGroup(group) if equivalent: @@ -756,7 +755,6 @@ def group_builder(ctx, codelet): if len(group.objectList) > 1: previous = group.objectList[0] for objekt in group.objectList[1:]: - #print 770 leftBond = objekt.leftBond if leftBond: if leftBond.leftObject == previous: @@ -787,7 +785,6 @@ def group_builder(ctx, codelet): # create new bonds group.bondList = [] for i in xrange(1, len(group.objectList)): - #print 803 object1 = group.objectList[i - 1] object2 = group.objectList[i] if not object1.rightBond: diff --git a/copycat/copycat.py b/copycat/copycat.py index 0bc1c62..a344c75 100644 --- a/copycat/copycat.py +++ b/copycat/copycat.py @@ -1,3 +1,5 @@ +import logging + from coderack import Coderack from randomness import Randomness from slipnet import Slipnet @@ -5,7 +7,11 @@ from temperature import Temperature from workspace import Workspace -class CopycatReporter(object): +class Reporter(object): + """Do-nothing base class for defining new reporter types""" + def report_answer(self, answer): + pass + def report_coderack(self, coderack): pass @@ -23,7 +29,7 @@ class Copycat(object): self.slipnet = Slipnet() self.temperature = Temperature() self.workspace = Workspace(self) - self.reporter = reporter or CopycatReporter() + self.reporter = reporter or Reporter() def mainLoop(self, lastUpdate): currentTime = self.coderack.codeletsRun @@ -56,11 +62,20 @@ class Copycat(object): answer = None finalTemperature = self.temperature.last_unclamped_value finalTime = self.coderack.codeletsRun - print 'Answered %s (time %d, final temperature %.1f)' % (answer, finalTime, finalTemperature) - answers[answer] = answers.get(answer, {'count': 0, 'tempsum': 0, 'timesum': 0}) - answers[answer]['count'] += 1 - answers[answer]['tempsum'] += finalTemperature - answers[answer]['timesum'] += finalTime + logging.info('Answered %s (time %d, final temperature %.1f)' % (answer, finalTime, finalTemperature)) + self.reporter.report_answer({ + 'answer': answer, + 'temp': finalTemperature, + 'time': finalTime, + }) + d = answers.setdefault(answer, { + 'count': 0, + 'sumtemp': 0, + 'sumtime': 0 + }) + d['count'] += 1 + d['sumtemp'] += finalTemperature + d['sumtime'] += finalTime def run(self, initial, modified, target, iterations): self.workspace.resetWithStrings(initial, modified, target) @@ -68,6 +83,6 @@ class Copycat(object): for i in xrange(iterations): self.runTrial(answers) for answer, d in answers.iteritems(): - d['avgtemp'] = d.pop('tempsum') / d['count'] - d['avgtime'] = d.pop('timesum') / d['count'] + d['avgtemp'] = d.pop('sumtemp') / d['count'] + d['avgtime'] = d.pop('sumtime') / d['count'] return answers diff --git a/copycat/workspace.py b/copycat/workspace.py index 30bf181..b7cb098 100644 --- a/copycat/workspace.py +++ b/copycat/workspace.py @@ -98,15 +98,10 @@ class Workspace(object): """A list of all objects in the workspace with >= 1 open bond slots""" objects = [o for o in self.objects if o.string == self.initial or o.string == self.target] - #print 'A: %d' % len(objects) objects = [o for o in objects if not o.spansString()] - #print 'B: %d' % len(objects) objects = [o for o in objects if (not o.leftBond and not o.leftmost) or (not o.rightBond and not o.rightmost)] - #print 'C: %d' % len(objects) - #objects = [ o for o in objects if ] - #print 'D: %d' % len(objects) return len(objects) def numberOfUngroupedObjects(self): @@ -118,14 +113,14 @@ class Workspace(object): return len(objects) def numberOfUnreplacedObjects(self): - """A list of all unreplaced objects in the inital string""" + """A list of all unreplaced objects in the initial string""" objects = [o for o in self.objects if o.string == self.initial and isinstance(o, Letter)] objects = [o for o in objects if not o.replacement] return len(objects) def numberOfUncorrespondingObjects(self): - """A list of all uncorresponded objects in the inital string""" + """A list of all uncorresponded objects in the initial string""" objects = [o for o in self.objects if o.string == self.initial or o.string == self.target] objects = [o for o in objects if not o.correspondence] diff --git a/copycat/workspaceFormulas.py b/copycat/workspaceFormulas.py index 3a21ed3..6a7ea8e 100644 --- a/copycat/workspaceFormulas.py +++ b/copycat/workspaceFormulas.py @@ -1,3 +1,4 @@ +import logging def __chooseObjectFromList(ctx, objects, attribute): random = ctx.random @@ -14,8 +15,8 @@ def __chooseObjectFromList(ctx, objects, attribute): def chooseUnmodifiedObject(ctx, attribute, inObjects): workspace = ctx.workspace objects = [o for o in inObjects if o.string != workspace.modified] - if not len(objects): - print 'no objects available in initial or target strings' + if not objects: + logging.warning('no objects available in initial or target strings') return __chooseObjectFromList(ctx, objects, attribute)