def do_plug_list_all(session: engine.Session) -> str: """Lists all the plugins that can be connected to all of the Sockets""" strings = [] for i in session.get_socket_names(): plugins = "; ".join(session.plug_list(i)) strings.append(f"Plugins available locally for {i}:\n{plugins}") return "\n\n".join(strings)
def do_setup_delete(session: engine.Session, name: str) -> str: """Deletes a setup Arguments: - Setup name [str] """ try: session.setup_delete(name) except EngineError as e: return str(e) else: return "The setup was deleted"
def add_imdb_info(): films: typing.Iterable[models.Film] = models.Film.objects.all() session = Session() for film in films: tconst = film.t_const # add info from title_basic title_basic: imdb.TitleBasics = session.query(imdb.TitleBasics).filter( imdb.TitleBasics.tconst == tconst).first() # add genres for genre in title_basic.genres.split(','): g = models.Genre.upsert(label=genre) film.hasGenre.add(g) film.isAdult = bool(title_basic.isAdult) if title_basic.isAdult == 1: film.hasAudience = models.Audience.upsert(label='Adults') else: film.hasAudience = models.Audience.upsert(label='Children') film.hasFeatureLengthInMinutes = title_basic.runtimeMinutes # add info from title_akas title_akas_lst: typing.Iterable[imdb.TitleAkas] = session.query(imdb.TitleAkas, imdb.ProductionCompanies, imdb.Certificates)\ .filter(imdb.TitleAkas.titleId == tconst)\ .filter(imdb.TitleAkas.isOriginalTitle == 1) \ .join(imdb.TitleBasics, imdb.TitleBasics.tconst == imdb.TitleAkas.titleId)\ .join(imdb.Certificates, imdb.Certificates.title == imdb.TitleAkas.title and imdb.Certificates.year == imdb.TitleBasics.startYear)\ .join(imdb.ProductionCompanies, imdb.ProductionCompanies.title == imdb.TitleAkas.title and imdb.ProductionCompanies.year == imdb.TitleBasics.startYear ).all() try: film.sync_from_wikidata() except KeyError: pass for three in title_akas_lst: title_akas = three[0] prod = three[1] if title_akas.isOriginalTitle == 1: try: if prod.country_code: country = models.Country.objects.get( alpha_2__iexact=prod.country_code.upper()) film.hasCountryOfOrigin = country print(f'found: {title_akas}') except ObjectDoesNotExist: pass print(film)
def do_load(session: engine.Session, filename: str): """Loads a saved proof Arguments: - filename [str] """ try: ret = session.load_proof(filename) for i in session.proof.nodes.getbranchnames(): session.deal_closure(i) return ret except EngineError as e: return e
def do_access_colors(session: engine.Session, val: int) -> str: """Changes the color palette of branches to a given set Based on https://sashamaps.net/docs/resources/20-colors/ Possible values: - 0 - No colors, instead only animal names will be used for the branches - 1 - Yellow, Blue, Black, White, Grey - 2 - Everything in 1 + Orange, Lavender, Maroon, Navy - 3 - Everything in 2 + Red, Green, Cyan, Magenta, Pink, Teal, Brown, Beige, Mint - 4 - Everything in 3 + Purple, Lime, Olive, Apricot """ session.change_accessibility(val) return "Color accessibility changed"
def do_plug_gen(session: engine.Session, socket: str, name: str) -> str: """Generates an empty plugin Arguments: - Socket name [str] - Plugin name [str] """ try: session.plug_gen(socket, name) except engine.EngineError as e: logger.error(e) return e else: return f"Generated plugin {name} from template"
def do_jump(session: engine.Session, where: str) -> str: """Changes the branch Arguments: - Branch name [str/">"/"right"/"left"/"<"] """ try: session.jump({'<': 'left', '>': 'right'}.get(where, where)) name = { '<': 'the left neighbour', '>': 'the right neighbour' }.get(where, where) return f"Branch changed to {name}" except engine.EngineError as e: return str(e)
def do_plug_switch(session: engine.Session, socket_or_name: str, new: str) -> str: """Allows to plug in a plugin to a socket Arguments: - Socket/Old plugin name [str] - New plugin's name [str] """ try: session.plug_switch(socket_or_name, new) except BaseException as e: # Not sure if this should be rewritten logger.error(f"Exception caught: {e}") return str(e) else: return f"Plugin succesfully installed: {new}"
def get_rprompt(session: engine.Session): """ Generuje podgląd gałęzi po prawej """ DEF_PROMPT = "Miejsce na twój dowód".split() THRESHOLD = 128 # Proof retrieval if session.proof: prompt, closed = session.getbranch_strings() color = COLORS.get(session.proof.branch, DEFAULT_COLOR) else: prompt = DEF_PROMPT closed = None color = DEFAULT_COLOR # Formatting to_show = [] max_len = max((len(i) for i in prompt)) + 1 for i in range(len(prompt)): spaces = max_len - len(prompt[i]) - int(log10(i + 1)) to_show.append("".join((str(i + 1), ". ", prompt[i], " " * spaces))) # Adding branch closing symbol if closed: s = str(closed) spaces = max_len - len(s) + int(log10(i + 1)) + 3 to_show.append(s + spaces * " ") # Foreground color calculating foreground = "#FFFFFF" if color.text_bright else "#000000" new = " \n ".join(to_show) return ptk.HTML( f'\n<style fg="{foreground}" bg="{color.rgb}"> {escape(new)} </style>')
def do_contra(session: engine.Session, branch: str) -> str: """Detects contradictions and handles them by closing their branches""" cont = session.deal_closure(branch) if cont: return cont else: return f"No contradictions found on branch {branch}."
def do_undo(session: engine.Session, amount: int) -> str: """Undos last [arg] actions""" try: rules = session.undo(amount) return "\n".join(f'Undid rule: {i.rule}' for i in rules) except engine.EngineError as e: return str(e)
def do_gen_form(session: engine.Session, length: int, variable_amount: int): """Generates a random formula Arguments: - Formula length [int] - Amount of variables in the formula [int] """ return " ".join(session.gen_formula(length, variable_amount).getLexems())
def do_save(session: engine.Session, filename: str): """Saves current state of proof if needed Arguments: - filename [str] """ try: return session.save_proof(filename) except engine.EngineError as e: return e
def do_hint(session: engine.Session) -> str: """Gives you a hint""" try: hints = session.hint() except engine.EngineError as e: return str(e) if hints is not None: return "\n\n".join(hints) else: return "Podpowiedzi nie ma, trzymaj się tam"
def do_check(session: engine.Session) -> str: """Checks the proof""" try: problems = session.check() except engine.EngineError as e: return str(e) if problems: return "\n".join(problems) else: return "Dowód jest poprawny"
def do_plug_list(session: engine.Session, socket: str) -> str: """Lists all the plugins that can be connected to a socket Arguments: - Socket name [str] """ try: plugins = "; ".join(session.plug_list(socket)) except engine.EngineError as e: return str(e) else: return f"Plugins available locally for {socket}:\n{plugins}"
def _handle_reader(reader: Iterator[pd.DataFrame], model_cls: Type[DeclaredModel], *, chunksize: int, null_values: List[str], ignore_cols: List[str], skip_rows_data: int, read_until: int, verbose: int): session = Session() counter = 0 for chunk in reader: chunk: pd.DataFrame if counter < skip_rows_data: counter += chunksize continue for row in chunk.iterrows(): line, series = row counter += 1 if hasattr(model_cls, 'create_instance'): instance = model_cls.create_instance(series) else: instance = create_instance(model_cls, series, null_values=null_values, ignore_cols=ignore_cols) session.add(instance) if verbose > 1: if counter % 5000 == 0: print(f"Added till row {counter}") session.commit() if verbose > 0: print(f'Committed till row {counter}') if read_until is not None and counter == read_until: break
def do_setup_update(session: engine.Session, name: str) -> tp.Iterator[str]: """Updates a given setup and initiates it Arguments: - Setup name [str] """ try: yield from session.setup_open(name, True) except EngineError as e: yield str(e) return else: yield f"You are now using {name}"
def add_award_info(): films: typing.Iterable[models.Film] = models.Film.objects.all() session = Session() # oscars = session.query(awards.Oscar).all() # baftas = session.query(awards.Bafta).all() for film in films: oscar = session.query(awards.Oscar)\ .filter(awards.Oscar.film == film.hasTitle)\ .filter(awards.Oscar.year_film == film.hasInitialReleaseYear).first() if not oscar: continue res, name = snlp.categorize(oscar.name) if res == 'ORG': agent = models.Organization.upsert(hasName=name, label=name) elif res == 'PERSON': agent = models.Person.upsert(hasName=name) else: raise ValueError award = models.Award.objects.get(hasNickname='Oscars') award_cat = models.AwardCategory.get_instance_from_kaggle_oscar_data( oscar.category) award_cem = models.AwardCeremony.upsert( hasAward=award, yearHeld=oscar.year_ceremony, yearScreened=oscar.year_film, hasEditionNumber=oscar.ceremony) nom = models.NominationSituation.upsert(forFilm=film, hasAward=award, hasAwardCategory=award_cat, hasAwardCeremony=award_cem, isGivenTo=agent, win=oscar.winner) print(f'{film} {nom}')
def do_plug_update(session: engine.Session, socket_or_name: str, new: str) -> tp.Iterator[str]: """Updates a plugin using the <code>Larch-Team/larch-plugins</code> repository Arguments: - Socket/current plugin name [str] - Plugin name [str] """ try: yield from session.plug_download(socket_or_name, new, True) except engine.EngineError as e: logger.error(e) yield str(e) else: yield f"Plugin succesfully downloaded: {new}"
def do_prove(session: engine.Session, sentence: str) -> str: """Initiates a new proof Arguments: - Sentence to prove [str] """ if session.proof: return "A proof would be deleted" try: text = session.new_proof(sentence) except engine.EngineError as e: return str(e) else: if text: return "\n".join(text) else: return "Sentence tokenized successfully \nProof initialized"
def do_write(session: engine.Session, filename: str): """ Writes a whole proof to a file with the provided name; if the file already exists program will append to it. Arguments: - filename [str] """ proof = session.gettree() if os.path.exists(filename): with open(filename, 'ab') as f: f.write('\n---\n') f.writelines([(i + '\n').encode('utf-8') for i in proof]) return f"Proof appended to {filename}" else: with open(filename, 'wb') as f: f.writelines([(i + '\n').encode('utf-8') for i in proof]) return f"Proof saved as {filename}"
def insert_data(in_directory: Union[str, Path], out_directory: Union[str, Path], *, null_values: List[str]) -> None: # Ensure directories are of type 'Path' in_directory = Path(in_directory) out_directory = Path(out_directory) module_names = [] for pyfile in out_directory.glob('*.py'): if pyfile.name in ['base.py', '__init__.py']: continue module_names.append(pyfile.stem) class_names = [snake_to_capwords(name) for name in module_names] # invalidate_caches() path_to_package = Path(out_directory).relative_to(ROOT_DIR.absolute()) package_path = str(path_to_package).replace('/', '.').replace('\\', '.') generated_package = import_module(package_path) models: Dict[str, DeclaredModel] = { class_name: generated_package.__dict__[class_name] for class_name in class_names } # session = Session() for csvfile in in_directory.glob('*.*sv'): info = _get_info_from_filename(csvfile.name) model_name = info['name'] dialect = get_dialect_from_suffix(info['format']) print(f"Opening {csvfile}") class_name = snake_to_capwords(snake_case(model_name)) if class_name not in class_names: raise AssertionError( "class name from data files doesn't match models") print(f"Writing from {csvfile} to {class_name}\n") counter = 0 for chunk in pd.read_csv(csvfile, chunksize=10_000, dialect=dialect):
def do_redo(session: engine.Session, amount: int) -> str: """Redoes last [arg] undone actions""" try: session.redo(amount) except engine.EngineError as e: return str(e)
def do_debug_get_methods(session: engine.Session) -> str: """Returns all methods of the session object""" return "\n".join(session.get_methods())
def do_get_tree(session: engine.Session) -> str: """Returns the proof in the form of a tree""" try: return "\n".join(session.gettree()) except engine.EngineError as e: return str(e)
def do_next(session: engine.Session): """Finds an open branch and jumps onto it""" try: session.next() except engine.EngineError as e: return str(e)
def do_setup_list(session: engine.Session) -> str: """Lists all local setups""" try: return session.setup_list() except EngineError as e: return str(e)
def do_use(session: engine.Session, command) -> str: """Uses a rule in the proof Arguments: - Rule name [str] - Depends on rule context """ if len(command) < 2: return "Full rule name needed" comm_split = command.split() name = " ".join(comm_split[:2]) out = [] # Context compiling context = {} c_values = comm_split[2:] context_info = session.context_info(name) if context_info is None: return "No such rule" if len(c_values) > len(context_info): out.append("Too many args, but the program will continue") for i, c in enumerate(context_info): if i == len(c_values): return "More arguments needed: {}".format(", ".join( (i.official for i in context_info[i:]))) vartype = engine.contextdef_translate(c.type_) try: new = vartype(c_values[i]) except ValueError: return f"{c.official} is of a wrong type" # Specific context handling if c.type_ == 'sentenceID': new -= 1 context[c.variable] = new # Rule usage try: info = session.use_rule(name, context) except engine.EngineError as e: return str(e) if info is None: out.append(f"Used '{name}' successfully") # Contradiction handling branches = session.proof.get_last_modified_branches() for i in branches: out.append(do_contra(session, i)) ended, closed = session.proof_finished() if closed: out.append("Proof was succesfully finished") elif ended: out.append("All branches are closed") else: out.extend(info) return "\n".join(out)
def do_solve(session: engine.Session) -> str: """Solves the proof""" try: return "\n".join(session.solve()) except engine.EngineError as e: return str(e)