def parse_sources(files): fns = {} for file in files: with open(file) as f: parser.parse_file(fns, f) parser.parse_bodies(fns) return fns
def main(): # check argument if len(sys.argv) < 4: print("Incorrect number of argument") print("Usage: python NER.py <train-file> <test-file> <output-file>") exit() # get the data train_data = parse_file(sys.argv[1]) test_data = parse_file(sys.argv[2]) # build the feature template and tags print("--Building feature templates--") (temp, tags) = (build_vector_template(train_data)) # train the data print("--Training the regression--") w = train(train_data, temp, tags) # test the data print("--Testing the regression") output = test(test_data, w, tags) # write output print("--Writing output--") write_output(sys.argv[3], output) print("--Finished--") return
def get_results(script, outfile): print("*******************************************************************") print("OUTPUT FOR " + script.upper() + ":") # put output in file for parsing f = open("bench.out", "w") old_stdout = sys.stdout sys.stdout = f # Check stats p = pstats.Stats(outfile) p.strip_dirs() # choose the functions to display if script == "bench_load_TOAs.py": p.print_stats("\(__init__", "toa") p.print_stats("\(apply_clock") p.print_stats("\(compute_TDBs") p.print_stats("\(compute_posvels") elif script == "bench_chisq_grid.py" or script == "bench_chisq_grid_WLSFitter.py": p.print_stats("\(get_designmatrix") p.print_stats("\(update_resid") p.print_stats("\(cho_factor") p.print_stats("\(cho_solve") p.print_stats("\(svd") p.print_stats("\(select_toa_mask") else: p.print_stats("only print total time") # for MCMC, only display total runtime f.close() # return output to terminal sys.stdout = old_stdout # parse file for desired info and format user-friendly output parse_file("bench.out") os.remove("bench.out")
def process_file(self, file_path): # Unzip the file zip_ref = zipfile.ZipFile(file_path, 'r') zip_ref.extractall(os.path.dirname(file_path)) zip_ref.close() csv_path = os.path.join(os.path.dirname(file_path), os.path.basename(file_path)) + '.dat' #Process the file parse_file(csv_path) os.remove(csv_path) return True
def main(filename, num_frames=None): [unscrambled, scrambled] = parser.parse_file(filename) frames = [] def callback(pos, world, ticks, hopper_contents, capacity): world = copy.deepcopy(world) for x in range(len(world)): for y in range(len(world[x])): world[x][y].append(None) x, y, z = pos world[x][y][z] = (0, 255, 0) frames.append((ticks, world, hopper_contents, capacity)) d = drone.Drone(scrambled, 0, 0, callback) b = brain.Brain(d, unscrambled) try: b.mainloop() except AssertionError: traceback.print_exc() r = renderer.Renderer() if num_frames is None: r.animate(frames) else: r.animate(frames[::len(frames)//int(num_frames)] + [frames[-1]])
def test(*args, **kw): global VERBOSE VERBOSE = 999 if len(args) > 0: kw['code'] = args[0] if 'filename' in kw: filename = kw['filename'] print "parsing file : %r" % filename with file(filename) as finp: print finp.read() print "----" php_code = parser.parse_file(filename) elif 'code' in kw: code = kw['code'] print "parsing php code :\n%s" % code print "----" php_code = parser.parse_php(code) print "----" print "Parsed Code:\n", php_code compiled_code = compile_php(php_code) print def print_node(node, depth=0): print " " * depth, if isinstance(node, TreeNode): print node.name for c in node.children: print_node(c, depth + 1) else: print node print "Compiled Code:\n" print compiled_code.prepr()
def __init__(self, f): self.title = '' self.prefix = '' self.description = '' self.songs = [] self.contents = [] config = parse_file(f) directory = '.' if 'title' in config: self.title = config.pop('title') else: raise ContentError("Section attribute 'title' not specified.", f.name) if 'prefix' in config: self.prefix = config.pop('prefix') if 'directory' in config: directory = config.pop('directory') if 'description' in config: self.description = config.pop('description') if 'contents' in config: files = config.pop('contents').split(sep='\n') for fn in files: fname = fn.strip() if fname == '-- break --': self.contents.append(PageBreak()) continue if fname == '': continue fl = open(directory + os.sep + fname, encoding='utf-8') s = Song(fl) fl.close() self.songs.append(s) self.contents.append(s) if len(config) > 0: print("Unused labels in "+f.name+": " + str(config.keys()), file=sys.stderr)
def test(*args, **kw): global VERBOSE VERBOSE = 999 if len(args) > 0: kw['code'] = args[0] if 'filename' in kw: filename = kw['filename'] print "parsing file : %r"%filename with file(filename) as finp: print finp.read() print "----" php_code = parser.parse_file(filename) elif 'code' in kw: code = kw['code'] print "parsing php code :\n%s"%code print "----" php_code = parser.parse_php(code) print "----" print "Parsed Code:\n", php_code compiled_code = compile_php(php_code) print def print_node(node, depth=0): print " "*depth, if isinstance(node, TreeNode): print node.name for c in node.children: print_node(c, depth+1) else: print node print "Compiled Code:\n" print compiled_code.prepr()
def main(): argparser = argparse.ArgumentParser(description="Compile file to bytecode") argparser.add_argument('filename', type=str, help="File to compile") args = argparser.parse_args() const = {} function = compile_function(const, [], parser.parse_file(args.filename)) gktable = function.nonlocals.values() gktable.extend(const.values()) constants = mod.Constants.empty() #gk = list() #gk = (runtime.Value*len(gktable))() for i, item in enumerate(gktable): if isinstance(item, Value): constants.val(item.value) # if isinstance(item.value, (unicode, str)): # s = item.value.encode('utf-8') # gk.append( proxy.box_cstring(s) ) # elif isinstance(item.value, (int, long, float)): # gk.append( proxy.box_double(item.value) ) # else: # raise Exception("um? %r" % item.value) else: constants.glob(item.name) #gk.append( proxy.get_global(item.name) ) constants.frozen = True #print gktable descriptor = compile_closure(function, gktable, constants, []) outfile = os.path.splitext(args.filename)[0] + '.o' asmod.writeout(outfile, args.filename, constants, descriptor)
def createTableWidget(self): self.tableWidget.setColumnCount(10) self.table_labels = [ "Sample", "Tissue", "Diagnosis", "Date", "File", "Source", "Material", "Operator_RNA_Isolation", "Operator_PCR", "RNA_Concentration" ] self.sql_labels = [ Sample.Source, Sample.Material, Sample.Operator_RNA_Isolation, Sample.Operator_PCR, ] self.tableWidget.setHorizontalHeaderLabels(self.table_labels) self.sql_label_values = get_sql_labels(self.sql_labels) self.patient_table = parse_file(self.fname) self.tableWidget.setRowCount(len(self.patient_table)) for i in self.patient_table.index: for j, column in zip(range(0, 5), self.patient_table.columns): self.tableWidget.setItem( i, j, QTableWidgetItem(self.patient_table.iloc[i][column])) for j, key in enumerate(self.sql_labels): a = QComboBox() for label in self.sql_label_values[key]: a.addItem(label) a.addItem("Add new...") a.activated.connect(self.addNewLabel) self.tableWidget.setCellWidget(i, j + 5, a) self.tableWidget.setCellWidget(i, 9, QLineEdit()) self.tableWidget.resizeColumnsToContents()
def main(): logs = '' args = get_args() maze, functions = parser.parse_file(args.file) args.file.close() cars = run.create_cars(maze, Car) if args.debug: out.init() try: while cars: if args.debug: out.output(maze, cars, logs, args.no_colors) maze, cars = run.move_cars(maze, cars) maze, cars, new_logs = run.car_actions(maze, cars, functions, debug=args.debug) if args.debug: if new_logs: logs = out.log_lines(logs + new_logs, args.log_length) time.sleep(1 / args.fps) else: print(new_logs, end='') finally: if args.debug: out.end()
def _index_file(self, path): text = parse_file(path) self._gen_terms(None, text) summary = text[0:997].translate(None, '\n') + '...' return summary
def corpus_file(prefix: str, doc_list: str, encoding='utf-8'): for doc_id, line in zip(itertools.count(0), parse_file(doc_list, encoding=encoding)): line = line.strip() if len(line) <= 0 or line.startswith('#'): continue idx = line.index(' ') class_label, file_name = line[:idx], line[idx+1:] yield Document(os.path.join(prefix, file_name), doc_id, class_label)
def render(*puzzle_files): for puzzle_file in puzzle_files: svg = parser.parse_file(puzzle_file).render().tostring() filename = os.path.splitext(puzzle_file)[0] + '.svg' f = open(filename, 'w') f.write(svg) f.close() print 'generated', filename
def test_parse_file(self): expected_commands = [ ('move',), ('turn', 'L'), ('move',), ('turn', 'L'), ('move',), ('turn', 'R'), ('move',), ('teleport', (5, 5)), ('move',), ('turn', 'L'), ('turn', 'L'), ('move',), ] correct_file = os.path.join('input', 'correct.txt') wrong_limits = os.path.join('input', 'wrong_limits.txt') wrong_position = os.path.join('input', 'wrong_position.txt') wrong_commands = os.path.join('input', 'wrong_commands.txt') limits, position, commands = parser.parse_file(correct_file) self.assertEqual(limits, (20, 30)) self.assertEqual(position, ((2, 2), 'W')) self.assertEqual(commands, expected_commands) with self.assertRaises(ValueError): parser.parse_file(wrong_limits) parser.parse_file(wrong_position) parser.parse_file(wrong_commands)
def test_parse_file_positive(self): parsed_file = parse_file(self.PATH_TO_DATA) # dictionary is not empty assert (len(list(parsed_file)) > 0) array_2d = parsed_file[0]['tokenized link'] assert (len(array_2d) == 2) # see data file assert (array_2d[0][0].token_id == TokenId.WORD_WITH_NUMBERS and array_2d[0][0].contents == '1st')
def load_file(self): filename = filedialog.askopenfilename(initialdir=Path.home(), title="Select file to load", filetypes=(("xml files", ".xml"), ("training files", "*.tcx"), ("all files", "*.*"))) hbs, watts, ffit, training_id = parse_file(filename) self.add_plot(hbs, watts, ffit, training_id)
def create_graph(filepaths): """ Prepare the `index.html` template. """ graphs = [] if isinstance(filepaths, str): filepaths = [filepaths] for path in filepaths: contributions = parser.parse_file(path) graph = { "data": gridify_contributions(contributions), "cell_class": _cell_class(list(contributions.values())), "longest_streak": statistics.longest_streak( [key for key, val in contributions.items() if val > 0]), "current_streak": statistics.current_streak( [key for key, val in contributions.items() if val > 0]), "sum": sum(contributions.values()), "repo_name": ntpath.basename(path) } graph["last_date"] = ( [""] + sorted([key for key, v in contributions.items() if v]))[-1] graphs.append(graph) env = Environment(loader=PackageLoader('render_html', 'templates')) env.filters['tooltip'] = tooltip_text env.filters['display_date'] = dateutils.display_date env.filters['elapsed_time'] = dateutils.elapsed_time template = env.get_template("index.html") weekdays = dateutils.weekday_initials() for idx in [0, 2, 4, 6]: weekdays[idx] = "" months = [ cell.date.strftime("%b") for cell in gridify_contributions(contributions)[0] ] months = filter_months(months) return template.render(graphs=graphs, today=dateutils.today(), start=dateutils.start(), weekdays=weekdays, months=months)
def test_parse_file2(): file_path = 'test/source/contact.rst' metadata = parse_file(file_path) expected_dict = {} expected_dict['title'] = 'Contact us!' expected_dict['layout'] = 'base.html' expected_dict['content'] = '\nWrite an email to [email protected].\n' assert metadata == expected_dict
def make_graph(args): filename = args.input vertex_dict, edges = parse_file(filename) graph = Graph() graph.add_vertices(len(vertex_dict.keys())) graph.vs["name"] = list(vertex_dict.values()) #for key in vertex_dict.keys(): # graph.vs["name"].append(vertex_dict[key]) graph.add_edges(edges) return graph
def main(): if len(sys.argv) > 1: lexemes = parser.parse_file(sys.argv[1]) words = parser.build_words(lexemes) tokens = tokenizer.tokenize(words) for token in tokens: print((token[0], token[1])) syntaxer.analyze(tokens) else: print('Error: missing parameter\n\t- please specify a file')
def test_parse_file3(): file_path = 'test/source/index.rst' metadata = parse_file(file_path) expected_dict = {} expected_dict['title'] = 'My awesome site' expected_dict['layout'] = 'home.html' expected_dict['content'] = '\nblah blah\n' assert metadata == expected_dict
def __init__(self, f): self.title = '' self.subtitle = '' self.author = '' self.style = Style() self.note = '' self.sections = [] self.contents = [] config = parse_file(f) directory = '.' if 'title' in config: self.title = config.pop('title') else: raise ContentError("Songbook attribute 'title' not specified.", f.name) if 'subtitle' in config: self.subtitle = config.pop('subtitle') if 'author' in config: self.author = config.pop('author') if 'directory' in config: directory = config.pop('directory') if 'style' in config: style_filename = config.pop('style') fl = open(style_filename, encoding='utf-8') self.style = Style(fl) fl.close() if 'note' in config: self.note = config.pop('note') if 'contents' in config: contents = config.pop('contents').split(sep='\n') for line in contents: cmd = line.strip() if cmd == '': continue R = re.fullmatch('section\s(?P<file>.+)', cmd) if R != None: filename = R.group('file').strip() fl = open(directory + os.sep + filename, encoding='utf-8') s = Section(fl) fl.close() self.sections.append(s) self.contents.append(s) continue R = re.fullmatch('table\s+of\s+contents\s+with\s+(?P<map>.+)', cmd) if R != None: s = TableOfContents(self, eval(R.group('map'))) self.contents.append(s) continue R = re.fullmatch('title index\s+with\s+(?P<map>.+)', cmd) if R != None: s = TitleIndex(self, eval(R.group('map'))) self.contents.append(s) continue # and so on... if len(config) > 0: print("Unused labels in "+f.name+": " + str(config.keys()), file=sys.stderr)
def import_file(f): print 'importing file ' + str(f) mongo = pymongo.MongoClient('mongodb://*****:*****@127.0.0.1:29019') database = mongo['redbook'] database.raw.create_index('id') year = f.split('/')[1].split('.')[0].split('_')[0] data = parser.parse_file(f, year) database.raw.insert_many(data)
def test_parser_fundef(self): input_str = "void foo(long a, int b) { }" output = FunDecl(tp_normal(AtomType.Void), "foo", [ FunArg(tp_normal(AtomType.Long), "a"), FunArg(tp_normal(AtomType.Int), "b") ], []) self.assertEqual(parse_file(input_str), [output])
def corpus_line(file_path: str, encoding='utf-8', zip_type=None): suffix = '' if not zip_type else '.' + zip_type # class label class_label_file = os.path.join(file_path, '.labels' + suffix) if os.path.isfile(class_label_file): class_label_iter = parse_file(class_label_file, encoding=encoding, zip_type=zip_type) else: class_label_iter = itertools.repeat('[none]') # document name doc_name_file = os.path.join(file_path, '.names' + suffix) if os.path.isfile(doc_name_file): doc_name_iter = parse_file(doc_name_file, encoding=encoding, zip_type=zip_type) else: doc_name_iter = itertools.repeat('[none]') # doc content content_iter = parse_file(file_path + suffix, encoding=encoding, zip_type=zip_type) for doc_id, class_label, name, content in\ zip(itertools.count(0), class_label_iter, doc_name_iter, content_iter): doc = Document(name, doc_id, class_label) doc.content = content yield doc
def test(file_name, ind): from parser import parse_file from struct_dump import _pprint assert file_name[-5:] == ".json" trace = parse_file(file_name) print(trace) graphs = [] tx = trace[ind] graph = Graph(tx.call_level_traces) graph.render("%s_%d.gv" % (file_name[:-5], ind)) graphs.append(graph) _pprint(graphs)
def test_protruck(self): old_links = parse_file('../data/old_test.csv') new_links = parse_file('../data/new_test.csv') good = 0 actual_data = load_file_as_array_of_pairs('../data/URL_protruck.csv') candidates = compare.find_candidates_for_file(new_links, old_links) for pair in candidates: orig_link = tokenizer.detokenize_line(pair[0]['tokenized link']) for line in actual_data: if orig_link == line[1]: for link in pair[1]: if link + '/' == line[0]: good += 1 print('found {0} of {1} lines'.format( good, len(actual_data))) else: print('bad') assert (good > 0)
def solve(puzzle_file): puzzle = parser.parse_file(puzzle_file) solutions = list(puzzle.solve()) count = 0 for s in solutions: count += 1 print s print print "%d solutions found" % count """
def scan_file_for_sources(file): """Scan document file for source identifiers and return list of sid strings.""" text = parse_file(file) sources = [] for source in list_sources(): smod = _load_source(source)() if 'scan_regex' not in dir(smod): continue prog = re.compile(smod.scan_regex) matches = prog.findall(text) if matches: for match in matches: sources.append('%s:%s' % (smod.source.lower(), match)) return sources
def get_data(f): conversation = parse_file(f) data = { "name": conversation.name, "message_counts": conversation.message_counts, "message_rate_day": conversation.message_rate("day"), "message_rate_hour": conversation.message_rate("hour"), } time_composition = [ conversation.time_composition(visualize=False)[x] for x in range(24) ] data["time_comp"] = time_composition return data
def parse(self, filename=None, text=None, stream=None): if filename: parsed = parse_file(filename) elif text: parsed = parse_string(text) elif stream: parsed = parse_stream(stream) else: raise BeaverException('Must specify filename, text, or stream to parse.') stmts = 0 for stmt in parsed: stmts += 1 self.execute(stmt) return stmts
def load_feature(fname): """ Load and parse a feature file. """ fname = os.path.abspath(fname) path = os.path.dirname(fname) feat = parser.parse_file(fname) if path not in definition_paths: log.debug("Looking for step defs in %s" % path) try: info = imp.find_module("steps", [path]) mod = imp.load_module("steps", *info) definition_paths.append(path) except ImportError, e: log.debug(traceback.format_exc())
def parse(self, filename=None, text=None, stream=None): if filename: parsed = parse_file(filename) elif text: parsed = parse_string(text) elif stream: parsed = parse_stream(stream) else: raise ScotchException("Must specify filename, text, or stream to parse.") stmts = 0 for stmt, start, end in parsed: stmts += 1 self.execute(stmt) return stmts
def resolve_file(filename, mode="dfs", max_depth=MAX_DEPTH): search_dic = {"bfs": "Breadth First Search", "dfs": "Depth First Search"} PAGE_BREAK_SIZE = 40 print("#" * PAGE_BREAK_SIZE) kb, g, name = parser.parse_file(filename) print(name + ":") print("Resolving using", search_dic[mode]) print("-" * PAGE_BREAK_SIZE) res = resolve(kb, g, mode, max_depth) if res: print_binding_list(res, g) else: print("Resolution Failed") print("#" * PAGE_BREAK_SIZE)
def main(): filename = sys.argv[1] fpath = os.path.join('testcase', filename) f = open(fpath, "r") board, word_list = parse_file(f) length_list = set([len(word) for word in word_list]) solver = CrosswordSolver(word_list) # get time pre_millis = int(round(time.time() * 1000)) board.set_space_list(length_list) print() solver.solve_board(board) #solve board and print (not returning anything) post_millis = int(round(time.time() * 1000)) print('\nSolved in', post_millis - pre_millis, 'ms')
def __init__(self, f): self.title = '' self.subtitle = '' self.author = '' self.url = '' self.translator = '' self.based = '' self.original = '' self.copyright = '' self.tags = [] self.note = [] self.parts = [] self.part_by_name = {} self.filename = f.name self.known_parts = set() config = parse_file(f) if 'title' in config: self.title = config.pop('title') else: raise ContentError("Song attribute 'title' not specified.", f.name) if 'subtitle' in config: self.subtitle = config.pop('subtitle') if 'author' in config: self.author = config.pop('author') if 'url' in config: self.url = config.pop('url') if 'tags' in config: self.tags = config.pop('tags').split() if 'note' in config: self.note = config.pop('note') lyrics = '' if 'lyrics' in config: lyrics = config.pop('lyrics') if len(config) > 0: print("Unused labels in "+f.name+": " + str(config.keys()), file=sys.stderr) sects = lycode.parse(lyrics) for sect in sects: if sect.head == 'def': self.parts.append(DefiningPart(sect, self)) elif sect.head == 'ref': self.parts.append(ReferencingPart(sect, self)) elif sect.head == 'instrumental': self.parts.append(InstrumentalPart(sect, self)) else: raise ContentError("Unknown section type", f.name)
def preprocess(self): sleep(0.5) self.server_task = Thread(target=self.background_server) self.server_task.daemon = True self.server_task.start() print('I have {} workers!'.format(self.num_workers)) self.v_to_m_dict, self.num_vertices = parse_file( self.input_filename, self.num_workers, self.masters_workers) print('num_vertices: ', self.num_vertices) for ix in range(self.num_workers): sock = self.send_to_worker([ Commons.request_preprocess, self.input_filename, self.v_to_m_dict, self.num_vertices ], self.masters_workers[ix + 2]) send_all_from_file(sock, self.input_filename, 0.001) while (self.num_preprocess_done < self.num_workers): sleep(1)
def yeet_file(file_string, yeet_table, yeet_generator): file_tokens = parser.parse_file(file_string) yeeted_file_string = '' for token, yeetable in file_tokens: if not yeetable: yeeted_file_string += token else: if token not in yeet_table: yeet_table[token] = yeet_generator.next() yeeted_file_string += '{} '.format(yeet_table[token]) return yeeted_file_string
def scan_file(self, file): """Scan document file for source identifiers Source 'scan_regex' attributes are used. Returns a list of SourceItem objects. """ text = parse_file(file) items = set() for source in self: try: regex = re.compile(source.scan_regex) except SourceAttributeError: # FIXME: warning? continue matches = regex.findall(text) if not matches: continue for match in matches: items.add(source[match]) return list(items)
def main(): if len(sys.argv) < 2: sourcefile = "sample" filename = "sample.png" elif len(sys.argv) < 3: sourcefile = sys.argv[1] filename = "%s.png" % sourcefile else: sourcefile = sys.argv[1] filename = sys.argv[2] graph = pgv.AGraph(directed=True, strict=False) graph.next_id = 1 graph.cache = {} graph.graph_attr['rankdir'] = 'TB' #graph.graph_attr['rankdir'] = 'LR' graph.node_attr['shape'] = 'Mrecord' for struct in parser.parse_file(sourcefile): populate_graph(graph, struct) print struct graph.draw(filename, prog="dot")
def load_cache_or_parse_logs(run_dir): """ Loads the results of a given simulation either from the cache on disk, if found, or directly parsing the simulation log. :param run_dir: Directory that contains the simulation logs and configuration. :return: Simulation results. """ # compute paths of relevant files log_file = run_dir + 'stdout.txt' cache_dir = run_dir + 'cache/' stats_file = cache_dir + 'stats.csv' freq_file = cache_dir + 'freq.csv' params_file = cache_dir + 'params.csv' # cache found, load it if utils.exits(stats_file) and utils.exits(freq_file) and utils.exits( params_file): print(' - Cache found... do NOT process the results again') stats = pandas.read_csv(stats_file) freq = pandas.read_csv(freq_file) parameters = list(pandas.read_csv(params_file)['parameters']) # cache not found else: print(' - Cache NOT found... process the results') stats, freq, parameters = parser.parse_file(log_file) utils.mkdir(cache_dir) stats.to_csv(stats_file, index=False) freq.to_csv(freq_file, index=False) pandas.DataFrame({ 'parameters': parameters }).to_csv(params_file, index=False) # return the parsed logs, either taken from the cache or computed from the simulation log return stats, freq, parameters
def __init__(self, f = None): if f != None: config = parse_file(f) filename = f.name else: config = dict() filename = '' def switch(s): if s in ['Y', 'y', 'ON', 'On', 'on', '1']: return True if s in ['N', 'n', 'OFF', 'Off', 'off', '0']: return False ContentError('Unknown option %s'%s, filename) def options(s): if s in ['none', 'None']: return [] else: return s.split() def opt(name, default, func = None): if not func: if type(default) == str: func = str elif type(default) == float: func = float elif type(default) == bool: func = switch else: func = str if name in config: return func(config.pop(name)) else: return default self.pagesize = opt('page.size', 'A4') self.titlepage = opt('title-page', True, switch) self.titlepage_margin_top = opt('title-page.margin.top', 128., float) self.titlepage_margin_bottom = opt('title-page.margin.bottom', 64., float) self.titlepage_title_font_name = opt('title-page.title.font.name', 'DejaVuSans-Bold') self.titlepage_title_font_size = opt('title-page.title.font.size', 48., float) self.titlepage_title_line_height = opt('title-page.title.line-height', 1.2*self.titlepage_title_font_size, float) self.titlepage_title_subtitle_spacing = opt('title-page.title-subtitle.spacing', 64., float) self.titlepage_subtitle_font_name = opt('title-page.subtitle.font.name', 'DejaVuSans') self.titlepage_subtitle_font_size = opt('title-page.subtitle.font.size', 32., float) self.titlepage_subtitle_line_height = opt('title-page.subtitle.line-height', 1.2*self.titlepage_subtitle_font_size, float) self.titlepage_author_font_name = opt('title-page.author.font.name', 'DejaVuSans') self.titlepage_author_font_size = opt('title-page.author.font.size', 24., float) self.titlepage_author_line_height = opt('title-page.author.line-height', 1.2*self.titlepage_author_font_size, float) self.notepage = opt('note-page', True, switch) self.notepage_margin_top = opt('note-page.margin.top', 64., float) self.notepage_margin_bottom = opt('note-page.margin.bottom', 64., float) self.notepage_margin_left = opt('note-page.margin.left', 64., float) self.notepage_margin_right = opt('note-page.margin.right', 64., float) self.notepage_font_name = opt('note-page.font.name', 'DejaVuSans') self.notepage_font_size = opt('note-page.font.size', 14., float) self.notepage_line_height = opt('note-page.line-height', 1.2*self.notepage_font_size, float) self.section_header_numbering_font_name = opt('section.header.numbering.font.name', 'DejaVuSans') self.section_header_numbering_font_size = opt('section.header.numbering.font.size', 12., float) self.section_header_numbering_line_height = opt('section.header.numbering.line-height', 1.2*self.section_header_numbering_font_size, float) self.section_margin_top = opt('section.margin.top', 64., float) self.section_margin_bottom = opt('section.margin.bottom', 64., float) self.section_margin_left = opt('section.margin.left', 64., float) self.section_margin_right = opt('section.margin.right', 64., float) self.section_title_font_name = opt('section.title.font.name', 'DejaVuSans') self.section_title_font_size = opt('section.title.font.size',24., float) self.section_title_line_height = opt('section.title.line-height', 28., float) self.section_description = opt('section.description', True, switch) self.section_title_description_spacing = opt('section.title-description.spacing', 20., float) self.section_description_font_name = opt('section.description.font.name', 'DejaVuSans') self.section_description_font_size = opt('section.description.font.size', 14., float) self.section_description_line_height = opt('section.description.line-height', 1.2*self.section_description_font_size, float) self.section_description_song_spacing = opt('section.description-song.spacing', 20., float) self.section_song_song_spacing = opt('section.song-song.spacing', 10., float) self.section_numbering_height = opt('section.numbering,height', 24., float) self.section_numbering_edge_distance = opt('section_numbering_edge_distance', 24., float) self.section_numbering_both = opt('section.numbering.both', True, switch) self.song_margin_inner = opt('song.margin.inner', 32., float) self.song_margin_outer = opt('song.margin.outer', 72., float) self.song_margin_top = opt('song.margin.top', 64., float) self.song_margin_bottom = opt('song.margin.bottom', 64., float) self.song_title_font_name = opt('song.title.font.name', 'DejaVuSans-Bold') self.song_title_font_size = opt('song.title.font.size', 14., float) self.song_title_line_height = opt('song.title.line-height', 1.2*self.song_title_font_size, float) self.song_title_margin_post = opt('song.title.margin.post', 4., float) self.song_subtitle = opt('song.subtitle', False) self.song_subtitle_font_name = opt('song.subtitle.font.name', 'DejaVuSans') self.song_subtitle_font_size = opt('song.subtitle.font.size', 12.) self.song_subtitle_line_height = opt('song.subtitle.line-height', 1.2*self.song_subtitle_font_size) self.song_subtitle_margin_post = opt('song.subtitle.margin.post', 4.) self.song_subtitle_indent = opt('song.subtitle.indent', 8.) self.song_author = opt('song.author', False) self.song_author_font_name = opt('song.author.font.name', 'DejaVuSans') self.song_author_font_size = opt('song.author.font.size', 12.) self.song_author_line_height = opt('song.author.line-height', 1.2*self.song_author_font_size) self.song_author_margin_post = opt('song.author.margin.post', 4.) self.song_author_indent = opt('song.author.indent', 8.) self.song_tags = opt('song.tags', False) self.song_tags_font_name = opt('song.tags.font.name', 'DejaVuSans') self.song_tags_font_size = opt('song.tags.font.size', 10.) self.song_tags_line_height = opt('song.tags.line-height', 1.2*self.song_tags_font_size) self.song_tags_margin_post = opt('song.tags.margin.post', 4.) self.song_tags_indent = opt('song.tags.indent', 16.) self.song_url_font_name = opt('song.url.font.name', 'DejaVuSans') self.song_url_font_size = opt('song.url.font.size', 10.) self.song_url_line_height = opt('song.url.line-height', 1.2*self.song_url_font_size) self.song_url_margin_post = opt('song.url.margin.post', 4.) self.song_url_indent = opt('song.url.indent', 8.) self.song_numbering_edge_distance = opt('song.numbering.edge-distance', 20.) self.song_numbering_font_name = opt('song.numbering.font.name', 'DejaVuSans') self.song_numbering_font_size = opt('song.numbering.font.size', 14.) self.song_numbering_line_height = opt('song.numbering.line-height', 1.2*self.song_numbering_font_size) self.song_qr_size = opt('song.url.qr.size', 40.) self.song_numbering_qr_spacing = opt('song.numbering-qr.spacing', 8.) self.song_url_qr_edge_distance = opt('song.url.qr.edge-distance', 16.) self.song_url = opt('song.url', ['qr'], options) self.song_skip_instrumentals = opt('song.instrumental.skip', False) self.song_repeat_character = opt('song.repeat.character', '✕') self.song_repeat_font_name = opt('song.repeat.font.name', 'DejaVuSans') self.song_repeat_font_size = opt('song.repeat.font.size', 10.) self.song_repeat_margin_left = opt('song.repeat.margin.left', 8.) self.song_repeat_line_text_spacing = opt('song.repeat.line-text.spacing', 4.) self.song_lyrics_repetition_spacing = opt('song.lyrics-repetition.spacing', 2.) self.song_repetition_chords_spacing = opt('song.repetition-chords.spacing', 2.) self.song_repetition_column_optimal_width = opt('song.repetition.column.optimal-width', 64.) self.song_chords_column_width = opt('song.chords.column.width', 80.) self.song_repetition_line_margin_outer = opt('song.repetition.line.margin-outer', 1.) self.song_text_line_indent = opt('song.text.line-indent', 20.) self.song_text_line_indent_first = opt('song.text.line-indent.first', 12.) self.song_text_font_name = opt('song.text.font.name', 'DejaVuSans') self.song_text_font_size = opt('song.text.font.size', 11.) self.song_text_line_height = opt('song.text.line-height', 12.) self.song_chords = opt('song.chords', True) self.song_chords_font_name = opt('song.chords.font.name', 'DejaVuSans-Bold') self.song_chords_font_size = opt('song.chords.font.size', 10.) self.song_chords_line_height = opt('song.chords.line-height', 1.2*self.song_chords_font_size) self.song_part_numbering_font_name = opt('song.part.numbering.font.name', 'DejaVuSans') self.song_part_numbering_font_size = opt('song.part.numbering.font.size', 11.) self.song_part_numbering_line_height = opt('song.part.numbering.line-height', 12.) self.song_part_numbering_width = opt('song.part.numbering.width', 8.) self.song_part_margin_top = opt('song.part.margin.top', 8.) self.song_referencing_text_font_name = opt('song.referencing.text.font.name', 'DejaVuSans-Oblique') self.song_referencing_text_font_size = opt('song.referencing.text.font.size', 11.) self.song_referencing_text_line_height = opt('song.referencing.text.line-height', 12.) self.song_referencing_stripped_characters = opt('song.referencing.stripped-characters', '.,:;') self.song_referencing_text_line_indent = opt('song.referencing.text.line-indent', 12.) self.song_instrumental_font_name = opt('song.instrumental.font.name', 'DejaVuSans-Oblique') self.song_instrumental_font_size = opt('song.instrumental.font.size', 11.) self.song_instrumental_line_height = opt('song.instrumental.line-height', 1.2*self.song_instrumental_font_size) self.song_instrumental_line_indent = opt('song.instrumental.line-indent', 20.) self.song_instrumental_line_indent_first = opt('song.instrumental.line-indent.first', 12.) self.toc_margin_top = opt('toc.margin.top', 64.) self.toc_title_font_name = opt('toc.title.font.name', 'DejaVuSans-Bold') self.toc_title_font_size = opt('toc.title.font.size', 24.) self.toc_title_line_height = opt('toc.title.line-height', 1.2*self.toc_title_font_size) self.toc_section_section_spacing = opt('toc.section-section.spacing', 8.) self.toc_section_title_font_name = opt('toc.section.title.font.name', 'DejaVuSans-Bold') self.toc_section_title_font_size = opt('toc.section.title.font.size', 14.) self.toc_section_title_line_height = opt('toc.section.title.line-height', 1.2*self.toc_section_title_font_size) self.toc_section_title_indent = opt('toc.section.title.indent', 48.) self.toc_section_prefix_indent = opt('toc.section.prefix.indent', 40.) self.toc_margin_bottom = opt('toc.margin.bottom', 64.) self.toc_margin_inner = opt('toc.margin.inner', 24.) self.toc_margin_outer = opt('toc.margin.outer', 48.) self.toc_song_number_font_name = opt('toc.song.number.font.name','DejaVuSans') self.toc_song_number_font_size = opt('toc.song.number.font.size', 11.) self.toc_song_title_font_name = opt('toc.song.title.font.name', 'DejaVuSans') self.toc_song_title_font_size = opt('toc.song.title.font.size', 11.) self.toc_song_number_indent = opt('toc.song.number.indent', 40.) self.toc_song_title_indent = opt('toc.song.title.indent', 48.) self.toc_song_song_spacing = opt('toc.song-song.spacing', 1.) self.toc_section_title_song_spacing = opt('toc.section.title-song.spacing', 4.) self.toc_song_line_height = opt('toc.song.line-height', 1.2*max(self.toc_song_number_font_size, self.toc_song_title_font_size)) self.title_index_margin_top = opt('title-index.margin.top', 64.) self.title_index_margin_bottom = opt('title-index.margin.bottom', 64.) self.title_index_title_font_name = opt('title-index.title.font.name', 'DejaVuSans-Bold') self.title_index_title_font_size = opt('title-index.title.font.size', 24.) self.title_index_song_song_spacing = opt('title-index.song-song.spacing', 1.) self.title_index_song_title_indent = opt('title-index.song.title.indent', 48.) self.title_index_title_line_height = opt('title-index.title.line-height', 1.2*self.title_index_title_font_size) self.title_index_song_number_font_name = opt('title-index.song.number.font.name', 'DejaVuSans') self.title_index_song_number_font_size = opt('title-index.song.number.font.size', 11.) self.title_index_song_title_font_size = opt('title-index.song.title.font.size', 11.) self.title_index_song_title_font_name = opt('title-index.song.title.font.name', 'DejaVuSans') self.title_index_song_number_indent = opt('title-index.song.number.indent', 40.) self.title_index_title_song_spacing = opt('title-index.title-song.spacing', 16.) self.title_index_margin_outer = opt('title-index.margin.outer', 48.) self.title_index_margin_inner = opt('title-index.margin.inner', 24.) self.title_index_song_line_height = opt('title-index.song.line-height', 1.2*max(self.title_index_song_number_font_size, self.title_index_song_title_font_size)) self.song_separator_height = opt('song.separator.height', 4.) self.fonts = opt('fonts', {'DejaVuSans':'DejaVuSans.ttf', 'DejaVuSans-Bold':'DejaVuSans-Bold.ttf', 'DejaVuSans-Oblique':'DejaVuSans-Oblique.ttf'}, eval) if len(config) > 0: print("Unused labels in "+f.name+": " + str(config.keys()), file=sys.stderr)
def compile_file(php_file): if type(php_file) is str: php_file = parser.parse_file(php_file) return compile_php(php_file)
def main(file_name): # graphics win = 0 # parse the file parsed_result = parser.parse_file(file_name) num_cells = parsed_result[0] num_connections = parsed_result[1] num_rows = parsed_result[2] num_cols = parsed_result[3] list_of_nets = parsed_result[4] # error checking assert(num_cells) assert(num_connections) assert(num_rows) assert(num_cols) assert(list_of_nets) # do all cells even fit on the grid assert(num_cells <= num_rows*num_cols) # create all the cells based on the net connections list_of_cells = utility.create_cells(num_cells, num_cols, num_rows, list_of_nets) # cross link the cells, each cell is referenced with all the cells it conects to list_of_cells = utility.block_X_block(list_of_cells, list_of_nets) # cross link the nets with stakeholder cells list_of_nets = utility.get_stakeholder_cells_for_net(list_of_nets, list_of_cells) # extract the edges and verteces from the nets and cells # this allows for ease of use with general graph algorithms graph_edges = coarsening.extract_edges(list_of_nets) graph_verteces = coarsening.extract_verteces(list_of_cells) G = graph.graph(graph_edges, graph_verteces) # coarsen the graph G = coarsening.coarsen_graph(G) # after coarsening, constrcut the vertex_X_vertex for fast lookup: G.vertex_X_vertex() # now, partition the simple graph #G = coarse_partition.partition_graph(G) # uncoarsen! print("uncoarseing now") # assign partition to each cell list_of_cells = partition.assign_initial_partition(list_of_cells) # verify the partition partition.verify_partition_count(list_of_cells) # compute the initial_cost for reference initital_cost = partition.compute_total_cost(list_of_nets, list_of_cells) # apply the kernigan_lin algorithm: [final_cost, final_list] = partition.kernigan_lin(list_of_cells, list_of_nets) # final_cost = KM.kernigan_lin_KM(list_of_cells, list_of_nets) # draw the board: win = draw.draw_final_result(win, num_cols, num_rows, final_list, list_of_nets) # show output statistics print('terminating execution, initial cost: ', initital_cost, ' final cost: ', final_cost) # this will leave the window open until the user clicks win.getMouse() win.close() return
def test_parse_file(self): actual = parser.parse_file('example_stdout.txt') expected_parameters = ['network_size', 'seed'] stats_attributes = expected_parameters + [ x for x in parser.stats_attributes if x != 'parameters' ] expected_stats = pandas.DataFrame(columns=stats_attributes, data=[{ 'protocol': 'core', 'metric': 'transactions', 'time': 0, 'min': float('inf'), 'max': float('-inf'), 'n': 0, 'mean': float("nan"), 'variance': 0.0, 'count_min': 0, 'count_max': 0, 'network_size': 10.0, 'seed': 1.0 }, { 'protocol': 'topology', 'metric': 'messages-addr', 'time': 1080000, 'min': 3.0, 'max': 17.0, 'n': 100, 'mean': 8.49, 'variance': 11.2, 'count_min': 4, 'count_max': 3, 'network_size': 100.0, 'seed': 1.0 }]) freq_attributes = expected_parameters + [ x for x in parser.freq_attributes if x != 'parameters' ] expected_freq = pandas.DataFrame(columns=freq_attributes, data=[{ 'protocol': 'core', 'metric': 'blockchain', 'time': 0, 'value': 0, 'frequency': 10, 'network_size': 10.0, 'seed': 1.0 }, { 'protocol': 'core', 'metric': 'blockchain', 'time': 720000, 'value': 0, 'frequency': 108, 'network_size': 100.0, 'seed': 1.0 }, { 'protocol': 'core', 'metric': 'blockchain', 'time': 720000, 'value': 1, 'frequency': 100, 'network_size': 100.0, 'seed': 1.0 }]) self.assertTrue(pandas.DataFrame.equals(actual[0], expected_stats)) self.assertTrue(pandas.DataFrame.equals(actual[1], expected_freq)) self.assertEquals(actual[2], expected_parameters)
#!/usr/bin/env python """ Entry point for analyzer execution """ __author__ = "SeongJae Park" __email__ = "*****@*****.**" import sys import anal import parser if __name__ == "__main__": if len(sys.argv) < 2: print "USAGE: %s <program source code path>" % sys.argv[0] exit(1) result = anal.analyze(parser.parse_file(sys.argv[1])) print "[RESULT]" for key in sorted(result.keys()): print "%s: %s" % (key, result[key]) if result[key].type_ == anal.TOP: print "\tDangerous! This may not end in reasonable time!!"
import os, subprocess from parser import parse_file listdir = os.listdir("sample_bin") # print(dir) # print(os.listdir("sample/"+dir[0])) path = os.path.abspath("sample_bin") + "/" devnull = open("/dev/null", "w") if not os.path.exists("sample"): os.makedirs("sample") outpath = os.path.abspath("sample") + "/" # print path, outpath for i in range(len(listdir)): name = listdir[i] fullpath = path + listdir[i] + "/" samples = os.listdir(fullpath) print(name) if not os.path.exists(outpath+name): os.makedirs(outpath+name) for j in range(len(samples)): proc = subprocess.Popen( "objdump -D -M intel " + fullpath + samples[j] + " > asm.txt", shell=True, stderr=devnull) proc.wait() parse_file(samples[j],name) print(" " + fullpath + "/" + samples[j]) devnull.close()
else: if len(phpargv) == 0: phpfile = arg phpargv.append(arg) init_scope = { '$argv' : phparray.PHPArray(*enumerate(phpargv)), '$argc' : len(phpargv) } if phpfile: if action == 'run': php_executer = executer.execute_file(phpfile, init_scope) if show_globals: print "[ended]\n-- globals --" for i in executer.globals.dict: print "%-14s -> %r"%(i, executer.globals[i]) elif action == 'compile': code_tree = compiler.compile_file(phpfile) print "[ended compilation]\n-- code tree --" print code_tree.prepr() elif action == 'parse': tokens = parser.parse_file(phpfile) print "[ended parsing]\n-- tokens --" print '\n'.join([repr(x) for x in tokens]) else : print "# No file to %s!"%action print USAGE
elif type(tag) is Literal: tag_v = tag.value elif type(tag) is Additive: # FIXME: only extract most left value of the expression tag_v = self._extract_tag_value(tag.lhs) else: raise Exception("tag type error: %s" % type(tag)) return tag_v def _extract_format_str(self, elem): format_str = elem.arguments[1] if type(format_str) is Literal: return format_str.value return None def extract_arguments(self, elem): tag_v = self._extract_tag_value(elem) format_str = None arguments = [] if len(elem.arguments) == 2: format_str= self._extract_format_str(elem) if len(elem.arguments) > 2: format_str = self._extract_format_str(elem) return tag_v, format_str if __name__ == "__main__": parser = parser.Parser() tree = parser.parse_file("java_src/MainActivity.java") tree.accept(TagVisitor(True))
def execute_file(phpfile, global_dict=None, **kwargs): if type(phpfile) is str: phpfile = parser.parse_file(phpfile) if isinstance(phpfile, parser.TokenList): phpfile = compiler.compile_php(phpfile) return execute_php(phpfile, global_dict, **kwargs)
def from_file(file): return tptp.parse_file(file)
def main(): available_strategies = { "dfs": strategies.DFS, "bfs": strategies.BFS, "iterative": strategies.IterativeDeepening, "A*": greedy.AStar, "greedy": greedy.Greedy, } needs_heuristics = ["A*", "greedy"] heuristics = {"slim": greedy.slim_heuristic, "fat": greedy.fat_heuristic, "trivial": greedy.trivial_heuristic} def usage(msg=""): print(msg) print("Usage: %s board_file_path algorithm [heuristic]" % argv[0]) print("Valid algoriths: %s" % " ".join(available_strategies.keys())) print("Valid heuristics: %s" % " ".join(heuristics.keys())) exit(0) if len(argv) < 3: usage() argv.pop(0) board_file_name = argv.pop(0) strategy_name = argv.pop(0) if strategy_name not in available_strategies: usage("Pick a valid strategy name") heuristic = None if strategy_name in needs_heuristics: if not len(argv): usage(strategy_name + " needs an heuristic") heuristic_name = argv.pop(0) if heuristic_name not in heuristics: usage("Pick a valid heuristic name") heuristic = heuristics[heuristic_name] strategy = available_strategies[strategy_name](heuristic) elif strategy_name == "iterative": strategy = available_strategies[strategy_name](3) else: strategy = available_strategies[strategy_name]() board = parser.parse_file(board_file_name) logger.debug(board) start = time() solution = strategies.solve_with_strategy(board, strategy) running_time = time() - start if solution: logger.info("\nFound solution at depth " + str(board.tiles_left() / 2)) logger.info("\nStatistics:") logger.info("\tRunning time " + str(running_time)) logger.info("\tGenerated states " + str(model.number_of_states)) logger.info("\tExpanded nodes " + str(model.number_of_explosions)) logger.info("\tFrontier nodes " + str(model.number_of_states - model.number_of_explosions))
def main(): args = sys.argv assert len(args) == 2 parse_file(args[1])