def __init__(self, greet, load): """ Initialise the interpreter session. """ # true if a prompt is needed on next cycle self.prompt = True # input mode is AUTO (used by AUTO) state.basic_state.auto_mode = False # interpreter is executing a command state.basic_state.parse_mode = False # interpreter is waiting for INPUT or LINE INPUT state.basic_state.input_mode = False # previous interpreter mode self.last_mode = False, False # syntax error prompt and EDIT state.basic_state.edit_prompt = False # initialise the display display.init() # initialise the console console.init_mode() # set up event handlers state.basic_state.events = events.Events() # load initial program if load: # on load, accept capitalised versions and default extension with disk.open_native_or_dos_filename(load) as progfile: program.load(progfile) # set up interpreter and memory model state reset.clear() # greeting and keys if greet: console.clear() console.write_line(greeting.format(version=plat.version, free=var.fre())) console.show_keys(True)
def convert(): """ Perform file format conversion. """ import program import disk import error # set conversion output # first arg, if given, is mode; second arg, if given, is outfile mode = config.get('convert') infile = (config.get(0) or config.get('run') or config.get('load')) outfile = config.get(1) # keep uppercase first letter mode = mode[0].upper() if mode else 'A' # load & save in different format try: prog_infile = None if infile: prog_infile = open_native_or_dos_filename(infile) elif plat.has_stdin: # use StringIO buffer for seekability in_buffer = StringIO(sys.stdin.read()) prog_infile = disk.open_diskfile(in_buffer, filetype='ABP', mode='I') if prog_infile: with prog_infile: program.load(prog_infile) prog_outfile = None if outfile: # on save from command-line, use exact file name prog_outfile = disk.open_diskfile(open(outfile, 'wb'), filetype=mode, mode='O') elif plat.has_stdout: prog_outfile = disk.open_diskfile(sys.stdout, filetype=mode, mode='O') if prog_outfile: with prog_outfile: program.save(prog_outfile) except error.RunError as e: logging.error(error.get_message(e.err)) except EnvironmentError as e: logging.error(str(e))
def convert(): """ Perform file format conversion. """ import program import disk import error # set conversion output # first arg, if given, is mode; second arg, if given, is outfile mode = config.get('convert') infile = (config.get(0) or config.get('run') or config.get('load')) outfile = config.get(1) # keep uppercase first letter mode = mode[0].upper() if mode else 'A' # load & save in different format try: prog_infile = None if infile: prog_infile = open_native_or_dos_filename(infile) elif plat.has_stdin: # use StringIO buffer for seekability in_buffer = StringIO(sys.stdin.read()) prog_infile = disk.create_file_object(in_buffer, filetype='ABP', mode='I') if prog_infile: with prog_infile: program.load(prog_infile) prog_outfile = None if outfile: # on save from command-line, use exact file name prog_outfile = disk.create_file_object(open(outfile, 'wb'), filetype=mode, mode='O') elif plat.has_stdout: prog_outfile = disk.create_file_object(sys.stdout, filetype=mode, mode='O') if prog_outfile: with prog_outfile: program.save(prog_outfile) except error.RunError as e: logging.error(error.get_message(e.err)) except EnvironmentError as e: logging.error(str(e))
def action(): cmd = 'EMPTY' journal_name = 'default' existing_entries = program.load(journal_name) # [] # list() print() print('What do you want to do with your journal?') while cmd != 'x' and cmd: print('[L]ist, [A]dd, or E[x]it? ') cmd = input().lower().strip() if cmd == 'l': list_entries(existing_entries) elif cmd == 'a': add_entry(existing_entries) elif cmd != 'x' and cmd: print("Sorry, we don't understand '{}'.".format(cmd)) print('Done, goodbye.') program.save(journal_name, existing_entries)
def start_basic(): """ Load & run programs and commands and hand over to interactive mode. """ import program import run import error import state import devices import disk import cassette import reset import sound do_reset = False backend, console = None, None try: # resume from saved emulator state if requested and available resume = config.get('resume') and state.load() # choose the video and sound backends backend, console = prepare_console() # greet, load and run only if not resuming if resume: # override selected settings from command line cassette.override() disk.override() # suppress double prompt if not state.basic_state.execute_mode: state.basic_state.prompt = False run.start('', False, config.get('quit')) else: # load/run program config.options['run'] = config.get(0) or config.get('run') prog = config.get('run') or config.get('load') if prog: # on load, accept capitalised versions and default extension with open_native_or_dos_filename(prog) as progfile: program.load(progfile) reset.clear() print_greeting(console) # start the interpreter (and get out if we ran with -q) run.start(config.get('exec'), config.get('run'), config.get('quit')) except error.RunError as e: msg = error.get_message(e.err) if console and config.get('wait'): console.write_error_message(msg, None) if backend and backend.video: # close terminal to avoid garbled error message backend.video.close() backend.video = None logging.error(msg) except error.Exit: pass except error.Reset: do_reset = True except KeyboardInterrupt: if config.get('debug'): raise except Exception as e: logging.error("Unhandled exception\n%s", traceback.format_exc()) finally: try: # fix the terminal on exit (important for ANSI terminals) # and save display interface state into screen state state.console_state.screen.close() except (NameError, AttributeError) as e: logging.debug('Error on closing screen: %s', e) # delete state if resetting if do_reset: state.delete() if plat.system == 'Android': shutil.rmtree(plat.temp_dir) else: state.save() try: # close files if we opened any devices.close_files() except (NameError, AttributeError) as e: logging.debug('Error on closing files: %s', e) try: devices.close_devices() except (NameError, AttributeError) as e: logging.debug('Error on closing devices: %s', e) try: sound.audio.close() except (NameError, AttributeError) as e: logging.debug('Error on closing audio: %s', e)
def start_basic(): """ Load & run programs and commands and hand over to interactive mode. """ import program import run import error import state import devices import disk import cassette import reset import sound import audio do_reset = False backend, console = None, None exit_error = '' try: # resume from saved emulator state if requested and available resume = config.get('resume') and state.load() # choose the video and sound backends backend, console = prepare_console() # greet, load and run only if not resuming if resume: # override selected settings from command line cassette.override() disk.override() # suppress double prompt if not state.basic_state.execute_mode: state.basic_state.prompt = False run.start('', False, config.get('quit')) else: # load/run program config.options['run'] = config.get(1) or config.get('run') prog = config.get('run') or config.get('load') if prog: # on load, accept capitalised versions and default extension with open_native_or_dos_filename(prog) as progfile: program.load(progfile) reset.clear() print_greeting(console) # start the interpreter (and get out if we ran with -q) run.start(config.get('exec'), config.get('run'), config.get('quit')) except error.RunError as e: exit_error = error.get_message(e.err) except error.Exit: # pause before exit if requested if config.get('wait'): backend.video_queue.put(backend.Event(backend.VIDEO_SET_CAPTION, 'Press a key to close window')) backend.video_queue.put(backend.Event(backend.VIDEO_SHOW_CURSOR, False)) state.console_state.keyb.pause = True # this performs a blocking keystroke read if in pause state backend.check_events() except error.Reset: do_reset = True except KeyboardInterrupt: if config.get('debug'): raise except Exception as e: print traceback.print_exc() exit_error = "Unhandled exception\n%s" % traceback.format_exc() finally: try: audio.close() except (NameError, AttributeError) as e: logging.debug('Error on closing audio: %s', e) try: # fix the terminal on exit (important for ANSI terminals) # and save display interface state into screen state state.console_state.screen.close() except (NameError, AttributeError) as e: logging.debug('Error on closing screen: %s', e) # delete state if resetting if do_reset: state.delete() if plat.system == 'Android': shutil.rmtree(plat.temp_dir) else: state.save() try: # close files if we opened any devices.close_files() except (NameError, AttributeError) as e: logging.debug('Error on closing files: %s', e) try: devices.close_devices() except (NameError, AttributeError) as e: logging.debug('Error on closing devices: %s', e) if exit_error: logging.error(exit_error)
# result and expected should be 2 arrays of docIDs, result being of lenght k return len(set(result).intersection(set(expected))) / len(result) def get_rappel(result, expected): # result and expected should be 2 arrays of docIDs, result being of lenght k return len(set(result).intersection(set(expected))) / len(expected) if __name__ == "__main__": # parse documents and create corpus documents = parse_file("Data/CACM/cacm.all") corpus = Corpus(documents) # load vectors for each file in corpus, and inverted indexes corpus.vectors = load('CACM_tf_idf_vectors') corpus.term2termID = load("cacm_term2termID") corpus.termID2docIDs = load("cacm_termID2docIDs") corpus.docID2doc = load("cacm_docID2doc") # parse queries and expected queries queries = parse_queries('queries/query.text') expected_results = parse_expected_results('queries/qrels.text') n = 10 precisions, rappels = search_and_compare(corpus, queries[n], expected_results[n]) #search and compare wrong_queries = [] # for n in range(63):
def start_basic(): """ Load & run programs and commands and hand over to interactive mode. """ import program import run import error import state import devices import disk import cassette import reset import sound import audio do_reset = False backend, console = None, None exit_error = '' try: # resume from saved emulator state if requested and available resume = config.get('resume') and state.load() # choose the video and sound backends backend, console = prepare_console() # greet, load and run only if not resuming if resume: # override selected settings from command line cassette.override() disk.override() # suppress double prompt if not state.basic_state.execute_mode: state.basic_state.prompt = False run.start('', False, config.get('quit')) else: # load/run program config.options['run'] = config.get(0) or config.get('run') prog = config.get('run') or config.get('load') if prog: # on load, accept capitalised versions and default extension with open_native_or_dos_filename(prog) as progfile: program.load(progfile) reset.clear() print_greeting(console) # start the interpreter (and get out if we ran with -q) run.start(config.get('exec'), config.get('run'), config.get('quit')) except error.RunError as e: exit_error = e.message except error.Exit: # pause before exit if requested if config.get('wait'): backend.video_queue.put( backend.Event(backend.VIDEO_SET_CAPTION, 'Press a key to close window')) backend.video_queue.put( backend.Event(backend.VIDEO_SHOW_CURSOR, False)) state.console_state.keyb.pause = True # this performs a blocking keystroke read if in pause state backend.check_events() except error.Reset: do_reset = True except KeyboardInterrupt: if config.get('debug'): raise except Exception as e: exit_error = "Unhandled exception\n%s" % traceback.format_exc() finally: try: audio.close() except (NameError, AttributeError) as e: logging.debug('Error on closing audio: %s', e) try: # fix the terminal on exit (important for ANSI terminals) # and save display interface state into screen state state.console_state.screen.close() except (NameError, AttributeError) as e: logging.debug('Error on closing screen: %s', e) # delete state if resetting if do_reset: state.delete() if plat.system == 'Android': shutil.rmtree(plat.temp_dir) else: state.save() try: # close files if we opened any devices.close_files() except (NameError, AttributeError) as e: logging.debug('Error on closing files: %s', e) try: devices.close_devices() except (NameError, AttributeError) as e: logging.debug('Error on closing devices: %s', e) if exit_error: logging.error(exit_error)