def test_parser_expression_paren_term(): toks = gen_tokens_for_lines(lines=["(1);"]) parsed = Parser(toks).parse_expression() assert isinstance(parsed.term, ParenTerm)
from parser import Parser import numpy as np import pandas as pd from glob import glob def update_latencies(cache, to_add): for end in parser.endpoints: if cache in end.latencies.keys(): for vid in to_add: end.video_lat[vid] = end.latencies[cache] for filename in glob('data/*.in'): parser = Parser(filename) parser.parse() print('Wroking with file {}'.format(filename)) for end in parser.endpoints: end.video_lat = [end.dc_latency] * parser.n_videos scores = [] mega_string = str(parser.n_caches) + "\n" for cache in range(parser.n_caches): scores.append([]) for video in parser.videos: score = 0 for req in video.requests: dc_lat = parser.endpoints[req.source].video_lat[video.index] lat = parser.endpoints[req.source].latencies.get(cache, 0)
from parser import Parser url = 'https://www.bbcgoodfood.com/recipes/meatball-black-bean-chilli' # url = 'https://www.bbcgoodfood.com/recipes/chipotle-sweet-potato-black-bean-stew-cheddar-dumplings' Parser().get_json(url) print(Parser().parse_html(url))
__author__ = 'Nestor Bermudez' __email__ = '[email protected], [email protected]' from classifier import NaiveBayesClassifier from singleBlockFeatureExtractor import SingleBlockFeatureExtractor from blockGroupFeatureExtractor import BlockGroupFeatureExtractor from unsegmentedDataParser import UnsegmentedDataParser from parser import Parser from util import Util if __name__ == '__main__': import pdb import time start = time.clock() parser = UnsegmentedDataParser('extradata') extractor = BlockGroupFeatureExtractor() classifier = NaiveBayesClassifier(smoothing=1.5) classifier.train(extractor.items(parser.items())) print('Training time: ' + str((time.clock() - start) * 1000) + 'ms') evaluationData = Parser('part1data/yes_test.txt', 'part1data/no_test.txt') confusion_matrix, acc = classifier.evaluate( extractor.items(evaluationData.items())) Util.print_confusion_matrix(confusion_matrix, 2, 2) print('Overall accuracy: ', round(acc * 100, 2))
def _setup_program(self, source: str) -> ast.Program: lexer = Lexer(source) parser = Parser(lexer) program = parser.parse_program() self._check_parser_errors(parser) return program
def get_parsed(self, filename): if filename: return Parser(filename)
test_seed = sys.argv[2] root_path = "/home/edgar/ns-3-dev-git/outputs/{0}".format(test_name) root_name = "fat-tree-{0}_{1}_{2}.fct" plt.figure(1) color = ["r--", "g--", "b--", "y--", "k--"] i = 1 for test in tests: ax = plt.subplot(310 + i) ax.set_title(test) color_i = 0 for error in errors: fct_reader = Parser(root_path + root_name.format(test, error, test_seed)) fct = fct_reader.get_attribute("fct") fct_sorted = sorted(fct) #log scale #y axis step y = 1. * np.arange(len(fct)) y = [x / (len(fct) - 1) for x in y] ##y = comulative_prob[:] plt.plot(fct_sorted, y, color[color_i]) plt.xscale('log') color_i += 1
return seq def post_append_arg_int(env, loc, seq, token): seq.append(int(token.value)) return seq def post_nothing(env, loc): return None def post_pass(env, loc, arg): return arg class Env(object): def __init__(self, symboltab, functions): self.symboltab = symboltab self.functions = functions parse = Parser(symboltab, grammar, 'file') def load(functions, path): env = Env({}, functions) grammar = parse.from_file(globals(), env, path) assert len(grammar) > 0, "empty grammar" return Parser(env.symboltab, grammar, grammar[0].lhs) def load_from_string(functions, string): env = Env({}, functions) grammar = parse(globals(), env, string) assert len(grammar) > 0, "empty grammar" return Parser(env.symboltab, grammar, grammar[0].lhs)
def load(functions, path): env = Env({}, functions) grammar = parse.from_file(globals(), env, path) assert len(grammar) > 0, "empty grammar" return Parser(env.symboltab, grammar, grammar[0].lhs)
def run_parser(code: str) -> AST: tokenizer = Tokenizer(code) parser = Parser(tokenizer) ast = parser.parse() return ast
def update(self, t, column, row): self.simulation_time = self.simulation_time + t self.column = column self.row = row if __name__ == "__main__": print(" ") print("----------------------------") print("Hashcode 2018 TEAM BING WHOO") print("----------------------------") print(" ") script, filename = argv p = Parser(filename) R, C, F, N, B, T = p.parse_simulator() rides = p.parse_rides() # create a simulation object. simulation = Simulation(R, C, F, N, B, T) i = 0 # rides is a list of lists with information from input file # it is NOT a list of ride objects for ride in rides: # create Ride objects and add to simulation.unassigned_rides a, b, x, y, s, f = [x for x in ride] simulation.unassigned_rides.append(Ride(a, b, x, y, s, f, i)) i += 1 # Assign rides to each car.
def test_parser_let(lines): toks = gen_tokens_for_lines(lines=lines) assert isinstance(Parser(toks).parse_let_statement(), LetStatement)
def test_parser_expression_with_tail(): toks = gen_tokens_for_lines(lines=["1 + 1 + 1;"]) parsed = Parser(toks).parse_expression() assert isinstance(parsed.term, ConstantTerm) assert all(isinstance(t, ConstantTerm) for _, t in parsed.tail) assert all(o.value == "+" for o, _ in parsed.tail)
def test_parser_expression_unary_term(): toks = gen_tokens_for_lines(lines=["-1;"]) parsed = Parser(toks).parse_expression() assert isinstance(parsed.term, UnaryOpTerm) assert parsed.term.op.value == "-"
from optparse import OptionParser from parser import Parser from draw import Drawer if __name__ == '__main__': opt_parser = OptionParser() opt_parser.add_option("-o", "--output", dest="output", help="where to save the image") (options, args) = opt_parser.parse_args() try: parser = Parser(args[0]) drawer = Drawer(parser.screen, parser.figures, parser.palette) drawer.draw() if vars(options)['output']: drawer.save(vars(options)['output']) except (ValueError, FileNotFoundError) as error: print(error)
def load_from_string(functions, string): env = Env({}, functions) grammar = parse(globals(), env, string) assert len(grammar) > 0, "empty grammar" return Parser(env.symboltab, grammar, grammar[0].lhs)
import socket, sys from numpy import * import scipy from pyeeg import bin_power from parser import Parser p = Parser() # Mindset raw_eeg = True spectra = [] iteration = 0 record_baseline = False # TCP Server try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) except socket.error, msg: sys.stderr.write("[ERROR] %s\n" % msg[1]) sys.exit(1) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) #reuse tcp sock.bind(('', 54321)) sock.listen(5) sock.settimeout(10) attention_prev = 0 meditation_prev = 0
import lexer from parser import Parser from interpreter import Interpreter from context import Context while True: text = input("Mscript :>>> ") tokens, error = lexer.run('<stdin>',text) if error : print(error.as_string()) continue print(tokens) parse = Parser(tokens) ast = parse.parse() interpreter = Interpreter() root_ctx = Context('<main>') result = interpreter.visit(ast.node,root_ctx) print(result.value, result.error)
class TransactionManager: """Transaction Manager class.""" parser = Parser() transaction_table = {} # {transaction_id: Transaction} ts = 0 # timestamp operation_queue = [] # list of Operations def __init__(self): """ Initialize all data managers. """ self.data_manager_list = [] for site_id in range(1, 11): self.data_manager_list.append(DataManager(site_id)) def process_line(self, line): """Core simulation process. Parse input, resolve deadlock, process instructions and operations. :param line: one line of instruction :return: True if success, False if instruction is invalid """ li = self.parser.parse_line(line) if li: command = li.pop(0) try: print("----- Timestamp: " + str(self.ts) + " -----") if self.resolve_deadlock(): self.execute_operation_queue() self.process_instruction(command, li) self.execute_operation_queue() self.ts += 1 except InvalidInstructionError as e: print("[INVALID_INSTRUCTION] " + e.message + ": " + line.strip()) return False # finally: # print() return True def process_instruction(self, command, args): """ Process an instruction. If the instruction is Read or Write, add it to the operation queue. Otherwise, execute the instruction directly. :param command: "begin", "beginRO", "R", "W", "dump", "end", "fail", or "recover" :param args: list of arguments for a command """ if command == "begin": self.begin(args[0]) elif command == "beginRO": self.beginro(args[0]) elif command == "R": self.add_read_operation(args[0], args[1]) elif command == "W": self.add_write_operation(args[0], args[1], args[2]) elif command == "dump": self.dump() elif command == "end": self.end(args[0]) elif command == "fail": self.fail(int(args[0])) elif command == "recover": self.recover(int(args[0])) else: raise InvalidInstructionError("Unknown instruction") def add_read_operation(self, transaction_id, variable_id): """ Insert a Read Operation to the operation queue :param transaction_id: the id of the transaction performing this op :param variable_id: the id of the variable """ if not self.transaction_table.get(transaction_id): raise InvalidInstructionError( "Transaction {} does not exist".format(transaction_id)) self.operation_queue.append( Operation("R", transaction_id, variable_id)) def add_write_operation(self, transaction_id, variable_id, value): """ Insert a Write Operation to the operation queue :param transaction_id: the id of the transaction performing this op :param variable_id: the id of the variable :param value: write value """ if not self.transaction_table.get(transaction_id): raise InvalidInstructionError( "Transaction {} does not exist".format(transaction_id)) self.operation_queue.append( Operation("W", transaction_id, variable_id, value)) def execute_operation_queue(self): """Go through operation queue and execute any executable operations.""" for op in list(self.operation_queue): if not self.transaction_table.get(op.transaction_id): self.operation_queue.remove(op) else: success = False if op.command == "R": if self.transaction_table[op.transaction_id].is_ro: success = self.read_snapshot(op.transaction_id, op.variable_id) else: success = self.read(op.transaction_id, op.variable_id) elif op.command == "W": success = self.write(op.transaction_id, op.variable_id, op.value) else: print("Invalid operation!") if success: # print("Executed op: {}".format(op)) self.operation_queue.remove(op) # print("Remaining ops: {}".format(self.operation_queue)) # ----------------------------------------------------- # -------------- Instruction Executions --------------- # ----------------------------------------------------- def begin(self, transaction_id): if self.transaction_table.get(transaction_id): raise InvalidInstructionError( "{} already exists".format(transaction_id)) self.transaction_table[transaction_id] = Transaction( self.ts, transaction_id, False) print("{} begins".format(transaction_id)) def beginro(self, transaction_id): if self.transaction_table.get(transaction_id): raise InvalidInstructionError( "{} already exists".format(transaction_id)) self.transaction_table[transaction_id] = Transaction( self.ts, transaction_id, True) print("{} begins and is read-only".format(transaction_id)) def read_snapshot(self, transaction_id, variable_id): """Perform read operation for read-only transactions.""" if not self.transaction_table.get(transaction_id): raise InvalidInstructionError( "Transaction {} does not exist".format(transaction_id)) ts = self.transaction_table[transaction_id].ts for dm in self.data_manager_list: if dm.is_up and dm.has_variable(variable_id): # pass the transaction's begin time into each data manager # when doing read-only result = dm.read_snapshot(variable_id, ts) if result.success: print("{} (RO) reads {}.{}: {}".format( transaction_id, variable_id, dm.site_id, result.value)) return True return False def read(self, transaction_id, variable_id): """Perform read operation for normal transactions.""" if not self.transaction_table.get(transaction_id): raise InvalidInstructionError( "Transaction {} does not exist".format(transaction_id)) for dm in self.data_manager_list: if dm.is_up and dm.has_variable(variable_id): result = dm.read(transaction_id, variable_id) if result.success: self.transaction_table[ transaction_id].sites_accessed.append(dm.site_id) print("{} reads {}.{}: {}".format( transaction_id, variable_id, dm.site_id, result.value)) return True return False def write(self, transaction_id, variable_id, value): if not self.transaction_table.get(transaction_id): raise InvalidInstructionError( "Transaction {} does not exist".format(transaction_id)) all_relevant_sites_down = True can_get_all_write_locks = True for dm in self.data_manager_list: if dm.is_up and dm.has_variable(variable_id): # check if all the relevant sites are down now all_relevant_sites_down = False result = dm.get_write_lock(transaction_id, variable_id) if not result: can_get_all_write_locks = False if not all_relevant_sites_down and can_get_all_write_locks: # print("{} will write {} with value {}".format( # transaction_id, variable_id, value)) sites_written = [] for dm in self.data_manager_list: if dm.is_up and dm.has_variable(variable_id): dm.write(transaction_id, variable_id, value) self.transaction_table[ transaction_id].sites_accessed.append(dm.site_id) sites_written.append(dm.site_id) print("{} writes {} with value {} to sites {}".format( transaction_id, variable_id, value, sites_written)) return True return False def dump(self): print("Dump:") for dm in self.data_manager_list: dm.dump() def end(self, transaction_id): """Commit or abort a transaction depending its status.""" if not self.transaction_table.get(transaction_id): raise InvalidInstructionError( "Transaction {} does not exist".format(transaction_id)) if self.transaction_table[transaction_id].will_abort: self.abort(transaction_id, True) else: self.commit(transaction_id, self.ts) def abort(self, transaction_id, due_to_site_fail=False): """Abort a transaction.""" for dm in self.data_manager_list: dm.abort(transaction_id) self.transaction_table.pop(transaction_id) if due_to_site_fail: print("{} aborts! (due to site failure)".format(transaction_id)) else: print("{} aborts! (due to deadlock)".format(transaction_id)) def commit(self, transaction_id, commit_ts): """Commit a transaction.""" for dm in self.data_manager_list: dm.commit(transaction_id, commit_ts) self.transaction_table.pop(transaction_id) print("{} commits!".format(transaction_id)) def fail(self, site_id): """Site fails.""" dm = self.data_manager_list[site_id - 1] if not dm.is_up: raise InvalidInstructionError( "Site {} is already down".format(site_id)) dm.fail(self.ts) print("Site {} fails".format(site_id)) for t in self.transaction_table.values(): if (not t.is_ro) and (not t.will_abort) and ( site_id in t.sites_accessed): # not applied to read-only transaction t.will_abort = True # print("{} will abort!!!".format(t.transaction_id)) def recover(self, site_id): """Site recovers.""" dm = self.data_manager_list[site_id - 1] if dm.is_up: raise InvalidInstructionError( "Site {} is already up".format(site_id)) dm.recover(self.ts) print("Site {} recovers".format(site_id)) # ----------------------------------------------------- # ---------------- Deadlock Detection ----------------- # ----------------------------------------------------- def resolve_deadlock(self): """ Detect deadlocks using cycle detection and abort the youngest transaction in the cycle. :return: True if a deadlock is resolved, False if no deadlock detected """ blocking_graph = defaultdict(set) for dm in self.data_manager_list: if dm.is_up: graph = dm.generate_blocking_graph() for node, adj_list in graph.items(): blocking_graph[node].update(adj_list) # print(dict(blocking_graph)) youngest_t_id = None youngest_ts = -1 for node in list(blocking_graph.keys()): visited = set() if has_cycle(node, node, visited, blocking_graph): if self.transaction_table[node].ts > youngest_ts: youngest_t_id = node youngest_ts = self.transaction_table[node].ts if youngest_t_id: print("Deadlock detected: aborting {}".format(youngest_t_id)) self.abort(youngest_t_id) return True return False
def __init__(self, database): self._collator = Collator(database) self._extractor = Extractor() self._parser = Parser()
## wikidata.links is a dictionary of type: entity -> set(entity). ## It represents all the entities connected to a ## given entity in the yago graph ## wikidata.labels is a dictionary of type: entity -> set(label). ## It represents all the labels an entity can have. ## wikidata.rlabels is a dictionary of type: label -> set(entity). ## It represents all the entities sharing a same label. ## wikidata.dates is a dictionnary of type: entity -> set(date). ## It represents all the dates associated to an entity. # Note that the class Page has a method Page.label(), # which retrieves the human-readable label of the title of an # ambiguous Wikipedia page. with open(outputFile, 'w', encoding="utf-8") as output: for page in Parser(wikipediaFile): # DO NOT MODIFY THE CODE ABOVE THIS POINT # or you may not be evaluated (you can add imports). # YOUR CODE GOES HERE: ################ Methode 1: F-score: 7 precision 24 ################## # print("page.label() \n", page.label()) # print("page.content \n", page.content) # rlabel = list(wikidata.rlabels[page.label()])[0] # link = wikidata.links[rlabel] # Q_link0 = list(link)[0] # labels = wikidata.labels[Q_link0] # sent = set(page.content) # word_label = list(labels)[0]
def getSimplecVector(self, myFile): parser = Parser() text = parser.parseText(myFile) vector = parser.createSimpleVector(text) return vector
import sys import urllib3 import subprocess from parser import Parser from fragQueue import FragQueue from errors import StatusError, NotManifestError from interval import RepeatedTimer from log import log http = urllib3.PoolManager(cert_reqs='CERT_NONE') urllib3.disable_warnings() outDir = './manifest' parser = Parser() fragStorageBase = 'frags' RECORDING_TIME = 6 urlInput = input('Enter Manifest Url: ') if not urlInput: urlInput = 'http://localhost:8880/remote/level.m3u8?url=https://test-streams.mux.dev/x36xhzz/url_0/193039199_mp4_h264_aac_hd_7.m3u8' referer = input('Enter Referer (optional): ') recordTime = input('Input Recording time in hours (default 6): ') if recordTime: RECORDING_TIME = float(recordTime) # if not input('Log to file? '): sys.stdout = open('./python-output.txt', 'w+') stopAfter = 60 * 60 * RECORDING_TIME POLL_INTERVAL = 4
def __init__(self): self.pr = Parser() self.log = "" self.t = Thread()
from loader import executeFromString from parser import Parser from prompt_toolkit import prompt from prompt_toolkit.history import FileHistory if __name__ == '__main__': grammarFile = 'grammars/JJ.g' parser = Parser(grammarFile) history = [] stack = [] while True: code = prompt('>>> ', history=FileHistory('history.txt')) if code == 'quit': break elif code == '_': if history: stack = history[-1] else: stack = [] print(stack) else: try: stack = executeFromString(code, parser, stack=stack) history.append(stack) except Exception as error: raise error continue
# _init(4, 1, 5) # print(look) # done # """ # text = r""" # var FINISH # # function main() # do # FINISH[0] := -1 # FINISH[1] := -1 # FINISH[2] := -1 # # done # """ # text = r""" # function main() # do # a[1][2][3] := 3 # done # """ parser = Parser(yacc_debug=True) ast = parser.parse(text, filename='<none>', debuglevel=False) ast.show(attrnames=True, showcoord=True) exec = nodevisitor.NodeVisitor(parser._err_flag, parser.tokens) exec.visit(ast, entry_point=exec.scopes['__global'])
return lines def split_lines(lines, separator=" "): splited_lines = [] for line in lines: splited_line = line.strip().split(separator) for i, element in enumerate(splited_line): try: splited_lines[i].append(element) except IndexError: splited_lines += [[element]] return splited_lines def plot_cwnd(time, cwnd, fig_name): plt.plot(time, cwnd, 'ro') # plt.axis([0, float(max(time)), 0, float(max(cwnd))]) plt.savefig(fig_name + '.png') plt.show() if __name__ == "__main__": parser = Parser(sys.argv[1], {'time': 0, 'cwnd': 1}, " ") time = parser.get_attribute('time') cwnd = parser.get_attribute('cwnd') plot_cwnd(time, cwnd, sys.argv[2])
from parser import Parser parse = Parser() class Assembler(): # Read file, # return assembly buffer. def assemble_file(self, filename): f = open(filename, "r") lines = f.readlines() assembly = parse.lines_to_assembly(lines) buf = parse.assembly_to_buffer(assembly) f.close() return buf # Assemble input file, # write assembly to output file. def assemble(self, filename_in, filename_out): assembly = self.assemble_file(filename_in) f = open(filename_out, "w") for b in assembly: f.write(b) f.close()
def parse(self, input, print_nodes=False): parser = Parser(input) if(print_nodes): parser.print_nodes() return parser.scene
node.sibling = creat_node(dt, total, num + 1) elif num == total: node.sibling = None return node def traverse_tree(node): print(node.name) if node.child is not None: traverse_tree(node.child) if node.sibling is not None: traverse_tree(node.sibling) with open("syntax-tree.json", 'r') as load_f: load_dict = json.load(load_f) root = SyntaxTree() root.name = load_dict['content'] root.name = load_dict['name'] root.line = load_dict['line'] dt = load_dict['children'] root.child = creat_node(dt, len(dt) - 1, 0) root.sibling = None traverse_tree(root) parser = Parser(root)