def message_receiver(message): chat_id = message.chat.id try: url, time_from, time_to = Parser(message.text).all() try: stream_url = youtube_lookup(url) except Exception: raise exceptions.InputValidationException( "There was something wrong with the link you sent.") redis_queue.enqueue(video.download, chat_id, stream_url, time_from, time_to) _len = len(redis_queue.jobs) status_text = \ f"Your video is currently in position #{_len} in queue." \ if _len else "Your video will start processing immediately." bot.send_message(chat_id, f"Thank you for your request! {status_text}") except ( exceptions.InputValidationException, exceptions.ForbiddenUserOperation, ) as e: bot.send_message(chat_id, e) except Exception as e: bot.send_message(chat_id, 'Oops! Something went wrong.') raise e
def post_advanced(): first = None last = None id = None email = None if 'first' in request.form: first = request.form['first'] if 'last' in request.form: last = request.form['last'] if 'id' in request.form: id = request.form['id'] if 'email' in request.form: email = request.form['email'] if not any([first, last, id, email]): return "Error: No search query provided. Please provide one." p = Parser(dir_search.advanced(first, last, id, email)) p.parse() return jsonify(p.results)
def __init__(self): self._world = World() directions = resources.load_directions() resources.load_rooms(self._world, directions) self._player = Player(self._world.starting_room, directions) commands = resources.load_commands(self) self._user_interface = UserInterface(commands) aliases = resources.load_aliases() errors = resources.load_errors() parser = Parser(directions, commands, aliases, errors) while True: self._user_interface.describe_room(self._player.room) (cmd, *args) = parser.get_input() result = cmd(*args) if result: result.show()
def post_basic(): if request.method == 'GET': if 'query' in request.args: search = request.args['query'] p = Parser(dir_search.basic(search)) p.parse() return jsonify(p.results) return "Error: No search query provided. Please provide one." elif request.method == 'POST': search = request.form['search'] p = Parser(dir_search.basic(search)) p.parse() print(p.results) return jsonify(p.results)
def tokenize_test(self): # TODO: use test sentences and ensure that the right token list comes out TEST_SENTENCES = [ "go Bedroom", "go", 'Go', 'GO', "go to Bedroom" ] TEST_RESULTS = [ [('verb','go'),('noun','bedroom')], [('verb','go')], [('verb','go')], [('verb','go')], [('verb','go'),('preposition','to'),('noun','bedroom')] ] parser = Parser() for i in range(len(TEST_SENTENCES)): res = parser.tokenise(TEST_SENTENCES[i]) print res, '=?=', TEST_RESULTS[i] self.assertTrue(res == TEST_RESULTS[i])
orderManager = OrderManager() order_idx = 4 + nb_warehouse*2 nb_orders = int(parser.get_line(order_idx)[0]) for i in range(nb_orders): coords = parser.get_line(order_idx + 1 + i*3) x = coords[1] y = coords[0] products_needed = parser.get_line(order_idx + 3 + i*3) #print("x {0} / y {1} / pro_len {2}".format(x, y, len(products_needed))) orderManager.add_order(i, x, y, products_needed) return warehouseManager, orderManager, droneManager if __name__ == "__main__": file = sys.argv[1] if len(sys.argv) > 1 else 'busy_day' if not file.endswith('.in'): file_name = file + '.in' else: file_name = file input_fn = 'input2016-{0}'.format(file_name) input = os.path.join(INPUT_FILE_DIR, input_fn) p = Parser(input) p.parse() warehouseManager, orderManager, droneManager = create_model(p) simu(droneManager, orderManager, warehouseManager) output_file = 'output2016-{0}.out'.format(file) nb_lines = droneManager.write_output(output_file) print("{0} lines has been printed to {1}".format(nb_lines, output_file))
def get_basic(search): p = Parser(dir_search.basic(search)) p.parse() return jsonify(p.results)
class HashCodeParser: def __init__(self, file_name): self.parser = Parser(file_name) self.nb_lines = 0 self.nb_columns = 0 self.matrix = [] def parse(self): self.parser.parse() self.nb_lines = self.parser.size - 1 self.nb_columns = self.parser.get(0, 1) self.generate_matrix() def generate_matrix(self): self.matrix = [] for i in range(1, len(self.parser.lines)): self.matrix.append(self.parser.lines[i]) ''' line_idx and column_idx are 0-based. First value at 0 If value at given indices doesn't exist : returns None ''' def get(self, line_idx, column_idx): return self.parser.get(line_idx + 1, column_idx) ''' 0-based index ''' def get_column(self, column_idx): col = [] if column_idx >= self.nb_columns: return col for line in self.parser.lines[1:]: if len(line) > column_idx: col.append(line[column_idx]) return col ''' 0-based index ''' def get_line(self, line_idx): return self.parser.lines[line_idx + 1] if line_idx < self.nb_lines else [] ''' 0-based index end parameter is excluded Ex : [1, 4] : returns columns 1, 2 and 3 ''' def get_columns(self, start=0, end=None): if end is None: end = self.nb_columns while start < end: yield(self.get_column(start)) start += 1 ''' 0-based index end parameter is excluded Ex : [1, 4] : returns lines 1, 2 and 3 ''' def get_lines(self, start=0, end=None): if end is None: end = self.nb_lines while start < end: yield(self.get_line(start)) start += 1 def coherence_test(self): if self.nb_lines == 0: raise RuntimeError('You must parse before calling the coherence test') theoretical_line_nb = self.parser.get(0, 0) if theoretical_line_nb != self.nb_lines and theoretical_line_nb == len(self.parser.lines) - 1: raise SyntaxError('File should have {0} lines, but got {1}'.format(theoretical_line_nb, self.nb_lines)) first_line_len = len(self.parser.lines[0]) theoretical_first_line_nb = 2 if first_line_len != theoretical_first_line_nb: raise SyntaxError('First line not the right size ({0} instead of {1})'.format(first_line_len, theoretical_first_line_nb)) for row in self.parser.lines[1:]: if len(row) != self.nb_columns: raise SyntaxError('Row {0} is not long enough ({1} instead of {2} columns)'.format(row, len(row), self.nb_columns))
def __init__(self, file_name): self.parser = Parser(file_name) self.nb_lines = 0 self.nb_columns = 0 self.matrix = []
class HashCodeParser: def __init__(self, file_name): self.parser = Parser(file_name) self.nb_lines = 0 self.nb_columns = 0 self.matrix = [] def parse(self): self.parser.parse() self.nb_lines = self.parser.size - 1 self.nb_columns = self.parser.get(0, 1) self.generate_matrix() def generate_matrix(self): self.matrix = [] for i in range(1, len(self.parser.lines)): self.matrix.append(self.parser.lines[i]) ''' line_idx and column_idx are 0-based. First value at 0 If value at given indices doesn't exist : returns None ''' def get(self, line_idx, column_idx): return self.parser.get(line_idx + 1, column_idx) ''' 0-based index ''' def get_column(self, column_idx): col = [] if column_idx >= self.nb_columns: return col for line in self.parser.lines[1:]: if len(line) > column_idx: col.append(line[column_idx]) return col ''' 0-based index ''' def get_line(self, line_idx): return self.parser.lines[line_idx + 1] if line_idx < self.nb_lines else [] ''' 0-based index end parameter is excluded Ex : [1, 4] : returns columns 1, 2 and 3 ''' def get_columns(self, start=0, end=None): if end is None: end = self.nb_columns while start < end: yield (self.get_column(start)) start += 1 ''' 0-based index end parameter is excluded Ex : [1, 4] : returns lines 1, 2 and 3 ''' def get_lines(self, start=0, end=None): if end is None: end = self.nb_lines while start < end: yield (self.get_line(start)) start += 1 def coherence_test(self): if self.nb_lines == 0: raise RuntimeError( 'You must parse before calling the coherence test') theoretical_line_nb = self.parser.get(0, 0) if theoretical_line_nb != self.nb_lines and theoretical_line_nb == len( self.parser.lines) - 1: raise SyntaxError('File should have {0} lines, but got {1}'.format( theoretical_line_nb, self.nb_lines)) first_line_len = len(self.parser.lines[0]) theoretical_first_line_nb = 2 if first_line_len != theoretical_first_line_nb: raise SyntaxError( 'First line not the right size ({0} instead of {1})'.format( first_line_len, theoretical_first_line_nb)) for row in self.parser.lines[1:]: if len(row) != self.nb_columns: raise SyntaxError( 'Row {0} is not long enough ({1} instead of {2} columns)'. format(row, len(row), self.nb_columns))
from base import AbstractExpression, Globals, Var from input_parser import Parser print("Available functions: ", end='') print(*Globals.unary_operators, sep=', ', end='\n\n') print("Use x, y, or z as variables.", end='\n\n') input_string = input("Differentiate: ") var = Var(input("with respect to: ").strip()) expression: AbstractExpression = Parser(input_string).parse() print("Input parsed as: ", expression) print("Result: ", expression.dx(var).simplify())
def test_output_values(self): expected_results = [ ('video_url', ( 'video_url', '00:00:00', '00:01:00', )), ('video_url&t=50', ( 'video_url&t=50', '00:00:50', '00:01:50', )), ('video_url&t=60', ( 'video_url&t=60', '00:01:00', '00:02:00', )), ('video_url&t=66', ( 'video_url&t=66', '00:01:06', '00:02:06', )), ('video_url&t=66 2:00', ( 'video_url&t=66', '00:01:06', '00:02:00', )), ('video_url&t=50&someotherjunk', ( 'video_url&t=50&someotherjunk', '00:00:50', '00:01:50', )), ('video_url 20s', ( 'video_url', '00:00:00', '00:00:20', )), ('video_url 15 25', ( 'video_url', '00:00:15', '00:00:25', )), ('video_url 00:15 25s', ( 'video_url', '00:00:15', '00:00:40', )), ('video_url 25s', ('video_url', '00:00:00', '00:00:25')), ('video_url 0:15', ( 'video_url', '00:00:15', '00:01:15', )), ('video_url 00:0:15', ( 'video_url', '00:00:15', '00:01:15', )), ('video_url 01:00:15 59s', ( 'video_url', '01:00:15', '01:01:14', )), ] for parser_input, expected_outputs in expected_results: results = Parser(parser_input).all() for expected, actual in zip(expected_outputs, results): self.assertEqual(expected, actual)
from event_finder import EventFinder from graph_generator import GraphGenerator from graph_traverser import GraphTraverser from input_parser import Parser from possibilities import Possibilities from state_node import StateNode parser = Parser() startNodeSet = parser.parseStartNodes() adjList = parser.parseReachability() vulnDict, portDict = parser.parseVulnerabilities() eventMapping = parser.parseEventMapping() eventSet = EventFinder() # Generate attack graph graphGenerator = GraphGenerator(startNodeSet, adjList, vulnDict, portDict) DG = graphGenerator.generate_graph() timestamp, src, dst, port, description, accessLevel = parser.parseNotableEvent( ) graphTraverser = GraphTraverser(DG, eventSet, eventMapping, portDict.keys()) eventSequence = graphTraverser.start_traversal(timestamp, src, dst, port, description, accessLevel) # Print possibilities crownJewelSet = parser.parseCrownJewels() possibilitiesGenerator = Possibilities() notableEventStateNode = StateNode(dst, accessLevel) possibilitiesGenerator.printPossiblePaths(DG, notableEventStateNode, crownJewelSet)