def print_percentages(): attributes = ["original_name", "original_citation", "page_described", "authority", "year"] parent_of_taxon = {} def _find_parent(taxon): if taxon.is_page_root: return taxon.id elif taxon.id in parent_of_taxon: return parent_of_taxon[taxon.id] else: return _find_parent(taxon.parent) for taxon in Taxon.select(): parent_of_taxon[taxon.id] = _find_parent(taxon) counts_of_parent = collections.defaultdict(lambda: collections.defaultdict(int)) for name in Name.select(): parent_id = parent_of_taxon[name.taxon.id] counts_of_parent[parent_id]["total"] += 1 for attribute in attributes: if getattr(name, attribute) is not None: counts_of_parent[parent_id][attribute] += 1 for parent_id, data in counts_of_parent.items(): parent = Taxon.filter(Taxon.id == parent_id)[0] print("FILE", parent) total = data["total"] del data["total"] print("Total", total) for attribute in attributes: percentage = data[attribute] * 100.0 / total print("%s: %s (%.2f%%)" % (attribute, data[attribute], percentage))
def add_original_names(): for name in Name.select(): if name.original_citation and not name.original_name: message = u"Name {} is missing an original name, but has original citation {{{}}}:{}".format( name.description(), name.original_citation, name.page_described ) name.original_name = getinput.get_line(message, handlers={"o": lambda _: name.open_description()}) if not name.page_described: name.page_described = getinput.get_line( "Enter page described", handlers={"o": lambda _: name.open_description()}, should_stop=lambda line: line == "s", ) name.save()
def check_refs(): for name in Name.select(): # if there is an original_citation, check whether it is valid if name.original_citation: if not cite_exists(name.original_citation): print("Name:", name.description()) print("Warning: invalid original citation:", name.original_citation) elif name.verbatim_citation and may_be_citation(name.verbatim_citation): if cite_exists(name.verbatim_citation): name.original_citation = name.verbatim_citation name.verbatim_citation = None name.save() elif must_be_citation(name.verbatim_citation): print("Name:", name.description()) print("Warning: invalid citation:", name.verbatim_citation)
def dup_names(): original_year = collections.defaultdict(list) for name in Name.select(): if name.original_name is not None and name.year is not None: original_year[(name.original_name, name.year)].append(name) return [original_year]