def test_parse(self): self.assertEqual(core.parse(""), []) self.assertEqual(core.parse("1+2"), [1, "+", 2]) self.assertEqual(core.parse("1^2^3"), [1, "^", 2, "^", 3]) self.assertEqual(core.parse("3^log2(8)"), [3, "^", 3]) self.assertEqual(core.parse("log(16)"), [math.log(16)]) self.assertEqual(core.parse("log(16,2)"), [4]) self.assertEqual(core.parse("abs(5)"), [5]) self.assertEqual(core.parse("log10(2*(50-1)+2)"), [2]) self.assertEqual(core.parse("1.0/2.0"), [1.0, "/", 2.0]) self.assertEqual(core.parse("10//2"), [10, "//", 2])
def repl(prompt="spill> "): "The spill read-eval-print loop" try: while True: inp = input(prompt) if inp: evaluate(parse(tokenize(inp))) except EOFError: print()
def make_message(username, text): if re.match(PATTERN, text): # reschedule list file parse # then get reschedule list match = re.search(PATTERN, text) data = core.parse() data = list(core.filter1(match.group(1), match.group(2), data)) if len(data) == 0: return "@" + username + " 直近の授業変更はありません:" + text else: tmp = core.dumps(data) if len(tmp) > 100: tmp = tmp[0:100] return "@" + username + " " + tmp else: return "@" + username + " 構文が間違っています:" + text
def main(): """Parses command line arguments and runs the LTL contract checker tool""" # initialize default command line values version = '1.0' verbose = False spec_file = 'system.spec' smv_file = 'nusmv.smv' # configure command line short-form and long-form options options, _ = getopt.getopt(sys.argv[1:], 'hvi:o:', ['verbose=', 'spec=', 'smv=']) # parse command line arguments for opt, arg in options: if opt == '-h': print 'test.py -i <specfile> -o <smvfile>' sys.exit() elif opt in ('-v', '--verbose'): verbose = True elif opt in ('-i', '--spec'): spec_file = arg elif opt in ('-o', '--smv'): smv_file = arg # print tool configurations if verbose: print 'VERSION :', version print 'VERBOSE :', verbose print 'SPEC_FILE :', spec_file print 'SMV_FILE :', smv_file # parse system specification file contracts, checks = parse(spec_file) # compile NuSMV file generate(contracts, checks, smv_file) print checks # run NuSMV file run(smv_file, checks)
def search_weather(self): city = self.CityEdit.text() if not city: self.statusBar().setStyleSheet("color: red;") self.statusBar().showMessage("请输入想要查询的城市名称!") return False city_code = get_city_code(city) if not city_code: self.statusBar().setStyleSheet("color: red;") self.statusBar().showMessage("没有找到您想要查询的城市名称,请检查名称是否错误!") return False try: response = search(city_code) data = parse(response) self.WeatherResult.setText("\n".join(data)) except Exception as error: self.statusBar().setStyleSheet("color: red;") self.statusBar().showMessage(str(error))
def test_compiler(): # test 1: compile simple commands lines = [['FWD']] print 'TESTING COMPILE FORWARD' commands = core.simulate(core.parse(lines)) print commands print # test 2: compile multiple simple commands lines = [['FWD', 'FWD', 'TRN']] print 'TESTING COMPILE FORWARD FORWARD TURN' commands = core.simulate(core.parse(lines)) print commands print # test 3: compile multiple lines lines = [['FWD', 'FWD'], ['TRN']] print 'TESTING COMPILE FORWARD FORWARD NEWLINE TURN' commands = core.simulate(core.parse(lines)) print commands print # test 4: compile for loop lines = [['FOR', 'CHC', 'FWD', 'TRN', 'EAT', 'CHC']] print 'TESTING COMPILE FOR LOOP' commands = core.simulate(core.parse(lines)) print commands print # test 5: compile for loop and more lines = [['FOR', 'CHC', 'FWD', 'TRN', 'EAT', 'CHC'], ['TRN']] print 'TESTING COMPILE FOR LOOP AND MORE' commands = core.simulate(core.parse(lines)) print commands print # test 6: compile a lot of commands lines = [['FOR', 'CHC', 'FWD', 'FWD', 'FWD', 'FWD', 'FWD', 'EAT', 'CHC']] print 'TESTING COMPILE FOR LOOP AND MORE' commands = core.simulate(core.parse(lines)) print commands print
def interpret(filename): with open(filename, "rt") as file: inp = file.read() if inp: evaluate(parse(tokenize(inp)))
t = threading.Timer(3,reactor.stop) t.start() elif _args and _args[0] == 'set': if len(_args)==3 and _args[1] == 'message_on_highlight': if _args[2] == 'on': _registered['message_on_highlight'] = True self.msg(_registered['name'],'I will message you directly for certain commands.', to=name) elif _args[2] == 'off': _registered['message_on_highlight'] = False self.msg(_registered['name'],'I will notify you for certain commands.', to=name) else: core.parse(_args,self,_registered['alert_channel'],_registered) if channel==self.nickname or _highlighted: _parse = self.parse(msg) else: _parse = None if _parse: _parse['keywords'][0] = _parse['command'] _parse['module'].parse(_parse['keywords'],self,_registered['alert_channel'],_registered) else: for module in self.modules: _bypass = False try: if module['module'].__parse_always__:
def run(program, prim=repr): return core.parse(program).eval(global_env).show(prim)
def load(filename): return core.parse(open(filename).read()).eval(global_env)
import os import sys from core import parse, generate, run from operations import * sys.path.append(os.path.join(os.getcwd(), os.path.pardir)) if __name__ == "__main__": smv_file = 'nusmvfile.smv' contracts, checks = parse('spec/test_2.txt') for contract in contracts.get_contracts(): print(contract) generate(contracts, checks, smv_file) results = run(smv_file, checks) print("Check results:" + str(results))
def main(): def rprint(name, data, result): """Prints hashing results.""" if isinstance(data, bytes): data = '"' + data.decode() + '"' print("{} ({}) = {}".format(name.upper(), data, result)) guaranteed = sorted(core.algorithm.keys()) parser = argparse.ArgumentParser( usage="%(prog)s -h | %(prog)s [ALGORITHM] OPTION", description="A convenient command-line file hashing utility.", epilog=textwrap.dedent(""" When using checklists, the argument specifying the hashing algorithm is ignored. It will be determined by the checklist. A checklist is a plain text file with lines like this: CRC32 (document.txt) = ad0c2001 CRC32 (photo.png) = 1629491b It can contain different algorithms. CRC32 (document.txt) = ad0c2001 CRC32 (photo.png) = 1629491b MD5 (audio.flac) = 69afdf17b98ed394964f15ab497e12d2 SHA1 (video.mkv) = 1f09d30c707d53f3d16c530dd73d70a6ce7596a9 Comments can be on a line by itself, at the beginning, or the end. I am a string. CRC32 ("hello, world!") = 58988d13 Double quote me! %(prog)s 4.0, Ching Chow http://chingc.tumblr.com """), formatter_class=argparse.RawDescriptionHelpFormatter ) parser.add_argument("algorithm", nargs="?", default="sha1", choices=guaranteed, help="{} {}".format(", ".join(guaranteed), "(default: sha1)"), metavar="ALGORITHM" ) mutex = parser.add_mutually_exclusive_group(required=True) mutex.add_argument("-c", help="verify a checklist", metavar="CHECKLIST" ) mutex.add_argument("-d", nargs=2, help="see if one is a duplicate of the other", metavar=("FILE1", "FILE2") ) mutex.add_argument("-e", nargs="+", help="see if the filename embedded digest matches", metavar="FILE" ) mutex.add_argument("-i", nargs="+", help="get the digest of the given input files", metavar="FILE" ) mutex.add_argument("-m", nargs=2, help="see if the given digest matches", metavar=("FILE", "DIGEST") ) mutex.add_argument("-s", type=lambda s: s.encode(), help="get the digest of a string", metavar="STRING" ) args = parser.parse_args() name = args.algorithm try: if args.c: for name, data, digest in core.parse(args.c): try: rprint(name, data, core.match(name, data, digest)) except Exception as error: print(error, file=sys.stderr) elif args.d: result1 = core.calculate(name, args.d[0]) rprint(name, args.d[0], result1) result2 = core.calculate(name, args.d[1]) rprint(name, args.d[1], result2) print(result1 == result2) elif args.e: for e in args.e: try: rprint(name, e, core.match(name, e)) except Exception as error: print(error, file=sys.stderr) elif args.i: for i in args.i: try: rprint(name, i, core.calculate(name, i)) except Exception as error: print(error, file=sys.stderr) elif args.m: rprint(name, args.m[0], core.match(name, args.m[0], args.m[1])) elif args.s: rprint(name, '"' + args.s.decode() + '"', core.calculate(name, args.s)) else: print("send me your input because you shouldn't be here!", file=sys.stderr) except Exception as error: print(error, file=sys.stderr)