From 331b1ad3ebca6d11383898c7e900ff90989ebaf3 Mon Sep 17 00:00:00 2001 From: J Alan Brogan Date: Fri, 26 Oct 2012 18:20:15 +0100 Subject: [PATCH] Separate out the main method --- copycat/copycat.py | 28 +++++++++------------------- copycat/main.py | 21 +++++++++++++++++++++ 2 files changed, 30 insertions(+), 19 deletions(-) create mode 100644 copycat/main.py diff --git a/copycat/copycat.py b/copycat/copycat.py index ba538e9..d58f33d 100644 --- a/copycat/copycat.py +++ b/copycat/copycat.py @@ -1,4 +1,3 @@ -import sys import logging logging.basicConfig( level=logging.INFO, @@ -8,6 +7,7 @@ logging.basicConfig( filemode='w' ) + from workspace import workspace from workspaceFormulas import workspaceFormulas from slipnet import slipnet @@ -22,6 +22,7 @@ def updateEverything(): workspaceFormulas.updateTemperature() coderackPressures.calculatePressures() + def mainLoop(lastUpdate): temperature.tryUnclamp() result = lastUpdate @@ -32,6 +33,7 @@ def mainLoop(lastUpdate): coderack.chooseAndRunCodelet() return result + def runTrial(): """Run a trial of the copycat algorithm""" answers = {} @@ -45,24 +47,12 @@ def runTrial(): answer = workspace.rule.finalAnswer else: answer = None - print '%d: %s' % (coderack.codeletsRun,answer) - answers[answer] = answers.get(answer,0) + 1 + print '%d: %s' % (coderack.codeletsRun, answer) + answers[answer] = answers.get(answer, 0) + 1 logging.debug('codelets used:') - for answer,count in answers.iteritems(): - print '%s:%d' % (answer,count) + for answer, count in answers.iteritems(): + print '%s:%d' % (answer, count) -def main(): - #slipnet.setConceptualDepths(50.0) - """Run the program""" - argc = len(sys.argv) - if argc == 4: - workspace.setStrings(initial=sys.argv[1].lower(),modified=sys.argv[2].lower(),target=sys.argv[3].lower()) - elif argc == 1: - workspace.setStrings(initial='abc',modified='abd',target='ijk') - else: - print >> sys.stderr, 'Usage: %s [initial modified target]' % sys.argv[0] - return +def run(initial, modified, target): + workspace.setStrings(initial, modified, target) runTrial() - -if __name__ == '__main__': - main() diff --git a/copycat/main.py b/copycat/main.py new file mode 100644 index 0000000..fe38ebe --- /dev/null +++ b/copycat/main.py @@ -0,0 +1,21 @@ +"""Run the copycat program""" + + +import sys + +import copycat + + +def main(program, args): + """Run the program""" + try: + initial, modified, target = args + copycat.run(initial, modified, target) + return 0 + except ValueError: + print >> sys.stderr, 'Usage: %s initial modified target' % program + return 1 + + +if __name__ == '__main__': + sys.exit(main(sys.argv[0], sys.argv[1:]))