def archive_links(archive_path, links, source=None, resume=None): check_dependencies() to_archive = Peekable(links_after_timestamp(links, resume)) idx, link = 0, to_archive.peek(0) try: for idx, link in enumerate(to_archive): link_dir = os.path.join(ARCHIVE_DIR, link['timestamp']) archive_link(link_dir, link) except (KeyboardInterrupt, SystemExit, Exception) as e: print( '{lightyellow}[X] [{now}] Downloading paused on link {timestamp} ({idx}/{total}){reset}' .format( **ANSI, now=datetime.now().strftime('%Y-%m-%d %H:%M:%S'), idx=idx + 1, timestamp=link['timestamp'], total=len(links), )) print(' Continue where you left off by running:') print(' {} {}'.format( pretty_path(sys.argv[0]), link['timestamp'], )) if not isinstance(e, KeyboardInterrupt): raise e raise SystemExit(1)
def postfix_sum(it): #accounts for unary negation if peek(it) is "-": n = next(it) nn = next(it) yield str(int(nn) * (-1)) #main loop while peek(it) is not None: n = next(it) if n in [")", "("]: if n == "(": adder = Peekable(postfix_sum(it)) while peek(adder) is not None: yield next(adder) else: break elif n in ["^"]: i, it = itertools.tee(it) i = Peekable(i) it = Peekable(it) nO = next_op(i) if nO is None: adder = Peekable(postfix_sum(it)) while peek(adder) is not None: yield next(adder) else: yield next(it) yield n elif n in ["*", "/", "&"]: i, it = itertools.tee(it) i = Peekable(i) it = Peekable(it) nO = next_op(i) if nO not in ["+", "-", "*", "/", "&"]: adder = Peekable(postfix_sum(it)) while peek(adder) is not None: yield next(adder) else: yield next(it) yield n elif n in ["+", "-"]: i, it = itertools.tee(it) i = Peekable(i) it = Peekable(it) nO = next_op(i) if nO not in ["+", "-"]: adder = Peekable(postfix_sum(it)) while peek(adder) is not None: yield next(adder) else: yield next(it) yield n else: yield n
def eval_postfix(iter): iter = Peekable(iter) s = LinkedList() #s = stack s.push(next(iter)) #first value will always be a number while peek(iter ) is not None: #continues until the iterator runs out of values if peek(iter) == '+': second = s.pop() s.push(s.pop() + second) next(iter) elif peek(iter) == '-': second = s.pop() s.push(s.pop() - second) next(iter) elif peek(iter) == '*': second = s.pop() s.push(s.pop() * second) next(iter) elif peek(iter) == '/': second = s.pop() s.push(s.pop() // second) next(iter) elif peek(iter) == '%': second = s.pop() s.push(s.pop() % second) next(iter) else: s.push(next(iter)) #the solution should be the only value in the stack return s.top()
def evaluate(expr): itera = Peekable(new_split_iter(expr)) if peek(itera) == "deffn": name, p, body = define_func(itera) functions.assign(name, (p, body)) else: print(expr,':',to_expr_tree(expr).evaluate(variables, functions))
def evaluate(expr): """Define a new function, or evaluate an expression The decision is based on the first token in the input line """ iterator = Peekable(new_split_iter(expr)) if peek(iterator) == "deffn": name, parms, body = define_func(iterator) functions.assign(name, (parms, body)) else: print(expr,':',tree_assign(iterator).evaluate(variables, functions))
def compile_line(program, expr): iterator = Peekable(new_split_iter(expr)) if peek(iterator) == "deffn": """ name, parms, body = define_func(iterator) local_variables = VarTree() code_line = len(program.code) program.functions.assign(name, (parms, body, code_line, local_variables)) body.comp( local_variables, program ) """ pass else: if program.first_stmt == -1: # if 'main' not yet assigned program.first_stmt = len(program.code) # start executing here to_expr_tree(expr).comp(program.variables, program) program.code.append(Print(program.code[-1].get_temp()))
def evaluate(expr): """Define a new function, or evaluate an expression If the first word in the line "deffn", it should appear as deffn <function name> ( <parameters> ) = <function body> a VarTree will associate the function name with a list of parameters (at least one, maybe more) and a tree representing the function body otherwise the input line is evaluated in an expression tree """ iterator = Peekable(new_split_iter(expr)) if peek(iterator) == "deffn": name, parms, body = define_func(iterator) functions.assign(name, (parms, body)) else: print(expr, ':', to_expr_tree(expr).evaluate(variables, functions))
def compile_line(program,expr): """Define a new function, or evaluate an expression If the first word in the line "deffn", it should appear as deffn <function name> ( <parameters> ) = <function body> otherwise the input line is evaluated in an expression tree """ iterator = Peekable(new_split_iter(expr)) if peek(iterator) == "deffn": name, parms, body = define_func(iterator) local_variables = VarTree() code_line = len(program.code) program.functions.assign(name, (parms, body, code_line, local_variables)) body.comp( local_variables, program ) else: if program.first_stmt == -1: # if 'main' not yet assigned program.first_stmt = len(program.code) # start executing here to_expr_tree(expr).comp( program.variables, program ) program.code.append( Print( program.code[-1].get_temp() ) )
def to_expr_tree(expr): return tree_assign(Peekable(new_split_iter(expr)))
def eval_infix_iter(iterator): """evaluate an expression, given an iterator to its tokens""" return eval_infix_sum(Peekable(iterator))
def update_archive(archive_path, links, source=None, resume=None, append=True): """update or create index.html+json given a path to an export file containing new links""" start_ts = datetime.now().timestamp() if resume: print('{green}[▶] [{}] Resuming archive downloading from {}...{reset}'. format( datetime.now().strftime('%Y-%m-%d %H:%M:%S'), resume, **ANSI, )) else: print( '{green}[▶] [{}] Updating content for {} pages in archive...{reset}' .format( datetime.now().strftime('%Y-%m-%d %H:%M:%S'), len(links), **ANSI, )) check_links_structure(links) # prefetch the first link off the generator so that if we pause or fail # immediately we can show that we paused on the first link and not just None to_archive = Peekable(links_after_timestamp(links, resume)) idx, link = 0, to_archive.peek(0) # loop over links and archive them try: check_dependencies() for idx, link in enumerate(to_archive): link_dir = os.path.join(ARCHIVE_DIR, link['timestamp']) archive_link(link_dir, link) except (KeyboardInterrupt, SystemExit, Exception) as e: print( '\n{lightyellow}[X] [{now}] Downloading paused on link {timestamp} ({idx}/{total}){reset}' .format( **ANSI, now=datetime.now().strftime('%Y-%m-%d %H:%M:%S'), idx=idx + 1, timestamp=link['timestamp'], total=len(links), )) print(' To view your archive, open: {}/index.html'.format( OUTPUT_DIR.replace(REPO_DIR + '/', ''))) print(' Continue where you left off by running:') print(' {} {}'.format( pretty_path(sys.argv[0]), link['timestamp'], )) if not isinstance(e, KeyboardInterrupt): print() raise e raise SystemExit(1) # print timing information & summary end_ts = datetime.now().timestamp() seconds = end_ts - start_ts if seconds > 60: duration = '{0:.2f} min'.format(seconds / 60, 2) else: duration = '{0:.2f} sec'.format(seconds, 2) print('{}[√] [{}] Update of {} pages complete ({}){}'.format( ANSI['green'], datetime.now().strftime('%Y-%m-%d %H:%M:%S'), len(links), duration, ANSI['reset'], )) print(' - {} entries skipped'.format(_RESULTS_TOTALS['skipped'])) print(' - {} entries updated'.format(_RESULTS_TOTALS['succeded'])) print(' - {} errors'.format(_RESULTS_TOTALS['failed'])) print(' To view your archive, open: {}/index.html'.format( OUTPUT_DIR.replace(REPO_DIR + '/', '')))
def to_postfix (expr): return postfix_sum(Peekable(new_split_iter(expr)))
def postfix_iter(iterator): return postfix_assign(Peekable(iterator))
def eval_infix_iter(iterator): return eval_infix_sum(Peekable(iterator))