[Python-checkins] r46703 - in sandbox/trunk/pybch: Tests/__init__.py pybch.py

sean.reifschneider python-checkins at python.org
Wed Jun 7 07:06:45 CEST 2006


Author: sean.reifschneider
Date: Wed Jun 7 07:06:44 2006
New Revision: 46703
Modified:
 sandbox/trunk/pybch/Tests/__init__.py
 sandbox/trunk/pybch/pybch.py
Log:
Adding real option parsing code, mostly to make it quick to run test of
comparison code.
Modified: sandbox/trunk/pybch/Tests/__init__.py
==============================================================================
--- sandbox/trunk/pybch/Tests/__init__.py	(original)
+++ sandbox/trunk/pybch/Tests/__init__.py	Wed Jun 7 07:06:44 2006
@@ -14,5 +14,4 @@
 		'TestHelpers',
 		'Tuples',
 		'Unicode',
-		'stringbench',
 		]
Modified: sandbox/trunk/pybch/pybch.py
==============================================================================
--- sandbox/trunk/pybch/pybch.py	(original)
+++ sandbox/trunk/pybch/pybch.py	Wed Jun 7 07:06:44 2006
@@ -3,10 +3,23 @@
 # Copyright (c) 2006, Sean Reifschneider, tummy.com, ltd.
 # All Rights Reserved.
 
-import time, sys, pickle, os
+import time, sys, pickle, os, optparse, re
 
 verbose = 4
-verbose = 0
+
+#################################
+def skipTest(nameList, testName):
+ 'Return 1 if test should be skipped based on nameList'
+ returnValue = False
+ for rx in nameList:
+ if rx[0] != '!':
+ returnValue = True
+ if re.match(rx, testName): return(False)
+ else:
+ returnValue = False
+ if re.match(rx[1:], testName): return(True)
+ return(returnValue)
+
 
 ######################
 def rusageTimer(self):
@@ -92,19 +105,38 @@
 
 
 ##################################
-if sys.argv[1] == '-r':
+parser = optparse.OptionParser()
+parser.add_option('-s', dest = 'compareSrcFileLoad',
+ type = 'str', help = 'Compare with results stored in this file.',
+ metavar = 'FILE_NAME')
+parser.add_option('-d', dest = 'compareDestFileLoad',
+ type = 'str',
+ help = 'Instead of running tests, load data from this file',
+ metavar = 'FILE_NAME')
+parser.add_option('-w', dest = 'compareDestFileSave',
+ type = 'str', help = 'Write test results out to this file',
+ metavar = 'FILE_NAME')
+def testNameCallback(option, opt, value, parser):
+ getattr(parser.values, 'testNames').append(value)
+parser.add_option('-t', dest = 'testNames',
+ action = 'callback', callback = testNameCallback, nargs = 1,
+ type = 'string', default = [],
+ help = 'May be specified once or more to select tests to run. '
+ 'Prefix regex with ! to specify tests not to run.',
+ metavar = 'TEST_REGEX')
+parser.add_option('-v', '--verbose', dest = 'verbose', action = 'count',
+ default = 0, help = 'Increase verbosity level.')
+options, args = parser.parse_args()
+
+if options.compareDestFileLoad:
 # load results from a file
- testResults = pickle.load(open(sys.argv[2], 'r'))
- sys.argv = sys.argv[:1] + sys.argv[3:]
+ testResults = pickle.load(open(options.compareDestFileLoad, 'r'))
 else:
- if len(sys.argv) != 3 or sys.argv[1] != '-f':
- raise ValueError, 'You must specify "-f <filename>"'
-
 # run tests locally
 import Tests
 testResults = {}
 for moduleName in Tests.testModules:
- if verbose >= 3: print moduleName
+ if options.verbose >= 3: print moduleName
 exec('from Tests import %s' % moduleName)
 module = eval(moduleName)
 for testClass in module.__dict__.values():
@@ -112,38 +144,47 @@
 or 'TestHelpers' in str(testClass)):
 continue
 
+ # skip based on -t option
+ if skipTest(options.testNames, str(testClass)[6:]): continue
+
 # set up test
- if verbose >= 1: print 'Test:', moduleName, testClass
+ if options.verbose >= 1: print 'Test:', moduleName, testClass
 test = testClass()
 test.timer = time.time
 test.runtimeTarget = 0.5
 test.runtimeAccuracyTarget = 0.025
 test.requiredStablePasses = 10
 test.passSleepTime = 0.1
- test.verbose = verbose
+ test.verbose = options.verbose
 
 # run test
- if verbose >= 2: print 'Calibrating...'
+ if options.verbose >= 2: print 'Calibrating...'
 test.cowlibrate()
- if verbose >= 3: print 'Calibrated to %d rounds' % test.rounds
+ if options.verbose >= 3:
+ print 'Calibrated to %d rounds' % test.rounds
 print '%s.rounds = %d' % ( testClass, test.rounds )
 continue
 
- if verbose >= 2: print 'Running tests...'
+ if options.verbose >= 2: print 'Running tests...'
 first = None
 passResults = []
 while len(passResults) < 1:
 latest = test.run()
 passResults.append(latest)
 if first == None: first = latest
- if verbose >= 3: print ' %3.2f' % ((first / latest) * 100)
+ if options.verbose >= 3:
+ print ' %3.2f' % ((first / latest) * 100)
 testResults[( moduleName, str(testClass) )] = passResults
 
+ # save results to a file
+ if options.compareDestFileSave:
+ environment = getEnvironmentInfo()
+ pickle.dump({ 'environment' : environment, 'results' : testResults },
+ open(options.compareDestFileSave, 'w'))
+
 # deal with results
-if sys.argv[1] == '-f':
- environment = getEnvironmentInfo()
- pickle.dump({ 'environment' : environment, 'results' : testResults },
- open(sys.argv[2], 'w'))
-if sys.argv[1] == '-c':
- compareResults(testResults, verbose, pickle.load(open(sys.argv[2], 'r')))
+if options.compareSrcFileLoad:
+ compareResults(testResults, options.verbose,
+ pickle.load(open(options.compareSrcFileLoad, 'r')))
+
 sys.exit(0)


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /