def testReports(self): "look at election outputs" b = '''3 2 4 1 2 0 2 3 0 0 "Castor" "Pollux" "Helen" "Pollux and Helen should tie"''' profile = ElectionProfile(data=b) rulename = droop.electionRuleNames()[0] # pick the first rule arbitrarily E = Election(profile, dict(rule=rulename)) E.count() self.assertEqual(E.report().find('interrupted'), -1) self.assertTrue(E.report(intr=True).find('interrupted') > 0) E = Election(profile, dict(rule=rulename)) E.count() self.assertEqual(E.dump().find('interrupted'), -1) self.assertTrue(E.dump(intr=True).find('interrupted') > 0) E = Election(profile, dict(rule=rulename)) E.count() self.assertEqual(E.json().find('interrupted'), -1) self.assertTrue(E.json(intr=True).find('interrupted') > 0) r = E.record() self.assertTrue(r, dict) self.assertEqual(r['actions'][-1]['tag'], 'log')
def testReports(self): "look at election outputs" b = '''3 2 4 1 2 0 2 3 0 0 "Castor" "Pollux" "Helen" "Pollux and Helen should tie"''' profile = ElectionProfile(data=b) rulename = droop.electionRuleNames()[ 0] # pick the first rule arbitrarily E = Election(profile, dict(rule=rulename)) E.count() self.assertEqual(E.report().find('interrupted'), -1) self.assertTrue(E.report(intr=True).find('interrupted') > 0) E = Election(profile, dict(rule=rulename)) E.count() self.assertEqual(E.dump().find('interrupted'), -1) self.assertTrue(E.dump(intr=True).find('interrupted') > 0) E = Election(profile, dict(rule=rulename)) E.count() self.assertEqual(E.json().find('interrupted'), -1) self.assertTrue(E.json(intr=True).find('interrupted') > 0) r = E.record() self.assertTrue(r, dict) self.assertEqual(r['actions'][-1]['tag'], 'log')
def main(options=None): "run an election" if not options: raise droop.common.UsageError("no ballot file specified") # process options # # we know about (path, profile) # all the others are passed to the various consumers # path = None # ballot path must be specified doProfile = False # performance profiling reps = 1 # repetitions (for profiling) for opt, arg in options.items(): if opt == 'path': # path=<path to ballot file> path = arg elif opt == 'profile': # profile=<number of repetitions> import cProfile import pstats reps = int(arg) doProfile = True profilefile = "profile.out" if not path: raise droop.common.UsageError("no ballot file specfied") # run the election # # fetch the election profile # create the Election object # count # report # def countElection(E, repeat=1): "encapsulate for optional profiling" for i in xrange(repeat): # pylint: disable=W0612 E.count() electionProfile = ElectionProfile( path=path) # don't repeat the profile loading E = Election(electionProfile, options) try: intr = False if doProfile: cProfile.runctx('countElection(E, reps)', globals(), locals(), profilefile) else: countElection(E, reps) except KeyboardInterrupt: intr = True E.options.setopt('dump', default=False) E.options.setopt('json', default=False) ereport = '' if E.options.setopt('report', default=True): ereport += E.report(intr) if E.options.getopt('dump'): ereport += E.dump(intr) if E.options.getopt('json'): ereport += E.json(intr) if doProfile: p = pstats.Stats(profilefile) p.strip_dirs().sort_stats('time').print_stats(50) return ereport
def doDumpCompare(options, filename, subdir=''): ''' helper: do a count and compare dump/report to reference ''' if not filename.endswith('.blt'): filename += '.blt' base, ext = os.path.splitext(filename) # pylint: disable=W0612 blt = os.path.join(testdir, 'blt', subdir, filename) E = Election(ElectionProfile(blt), options) E.count() tag = '%s-%s-%s' % (base, E.rule.tag(), E.V.tag()) def readFile(path): "read a json/dump/report file" f = open(path, 'r') data = f.read() f.close() return data def writeFile(path, data): "write a json/dump/report file" if not os.path.isdir(os.path.dirname(path)): os.makedirs(os.path.dirname(path)) f = open(path, 'w') f.write(data) f.close() # first do report # rref = os.path.join(testdir, 'ref', 'report', subdir, '%s.txt' % tag) rout = os.path.join(testdir, 'out', 'report', subdir, '%s.txt' % tag) report = E.report() if not os.path.isfile(rref): writeFile(rref, report) reportref = readFile(rref) if os.path.isfile(rout): os.unlink(rout) # don't include version number in comparison report0 = re.sub(r'droop v\d+\.\d+', 'droop v0.0', report) reportref = re.sub(r'droop v\d+\.\d+', 'droop v0.0', reportref) if report0 != reportref: writeFile(rout, report) if compare_report: return False # same logic with json # sref = os.path.join(testdir, 'ref', 'json', subdir, '%s.txt' % tag) sout = os.path.join(testdir, 'out', 'json', subdir, '%s.txt' % tag) json = E.json() if not os.path.isfile(sref): writeFile(sref, json) jsonref = readFile(sref) if os.path.isfile(sout): os.unlink(sout) # don't include version number in comparison json0 = re.sub(r'"droop_version": "\d+\.\d+"', '"droop_version": "0.0"', json) jsonref = re.sub(r'"droop_version": "\d+\.\d+"', '"droop_version": "0.0"', jsonref) if json0 != jsonref: writeFile(sout, json) if compare_json: return False # same logic with dump # dref = os.path.join(testdir, 'ref', 'dump', subdir, '%s.txt' % tag) dout = os.path.join(testdir, 'out', 'dump', subdir, '%s.txt' % tag) dump = E.dump() if not os.path.isfile(dref): writeFile(dref, dump) dumpref = readFile(dref) if os.path.isfile(dout): os.unlink(dout) if dump != dumpref: writeFile(dout, dump) if compare_dump: return False return True
def main(options=None): "run an election" if not options: raise droop.common.UsageError("no ballot file specified") # process options # # we know about (path, profile) # all the others are passed to the various consumers # path = None # ballot path must be specified doProfile = False # performance profiling reps = 1 # repetitions (for profiling) for opt, arg in options.items(): if opt == 'path': # path=<path to ballot file> path = arg elif opt == 'profile': # profile=<number of repetitions> import cProfile import pstats reps = int(arg) doProfile = True profilefile = "profile.out" if not path: raise droop.common.UsageError("no ballot file specfied") # run the election # # fetch the election profile # create the Election object # count # report # def countElection(E, repeat=1): "encapsulate for optional profiling" for i in xrange(repeat): # pylint: disable=W0612 E.count() electionProfile = ElectionProfile(path=path) # don't repeat the profile loading E = Election(electionProfile, options) try: intr = False if doProfile: cProfile.runctx('countElection(E, reps)', globals(), locals(), profilefile) else: countElection(E, reps) except KeyboardInterrupt: intr = True E.options.setopt('dump', default=False) E.options.setopt('json', default=False) ereport = '' if E.options.setopt('report', default=True): ereport += E.report(intr) if E.options.getopt('dump'): ereport += E.dump(intr) if E.options.getopt('json'): ereport += E.json(intr) if doProfile: p = pstats.Stats(profilefile) p.strip_dirs().sort_stats('time').print_stats(50) return ereport