From f5442998edbbdcad423b570c69521a4813e74854 Mon Sep 17 00:00:00 2001 From: Arun Isaac Date: Tue, 27 Sep 2022 22:33:41 +0530 Subject: Abstract out writing documents into a xapian database. * wqflask/scripts/index.py (write_document): New function. (main): Use write_document. --- wqflask/scripts/index.py | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) (limited to 'wqflask/scripts/index.py') diff --git a/wqflask/scripts/index.py b/wqflask/scripts/index.py index fe2b819c..f59a8788 100644 --- a/wqflask/scripts/index.py +++ b/wqflask/scripts/index.py @@ -19,6 +19,15 @@ def index_text(termgenerator, text): termgenerator.increase_termpos() +# pylint: disable=invalid-name +def write_document(db, idterm, doctype, doc): + """Write document into xapian database.""" + # We use the XT prefix to indicate the type. + doc.add_boolean_term(f"XT{doctype}") + doc.add_boolean_term(idterm) + db.replace_document(idterm, doc) + + # pylint: disable=missing-function-docstring def main(): with database_connection() as conn, conn.cursor(MonadicDictCursor) as cursor: @@ -75,14 +84,8 @@ def main(): trait.pop("unigeneid").bind(indexer) trait.pop("probe_target_description").bind(indexer) - # Identify document as type "gene". We use the XT - # prefix to indicate the type. - doc.add_boolean_term("XTgene") doc.set_data(json.dumps(trait.data)) - # Write document into xapian database. - idterm = trait["name"].bind(lambda name: "Q" + name) - doc.add_boolean_term(idterm) - db.replace_document(idterm, doc) + write_document(db, trait["name"].bind(lambda name: f"Q{name}"), "gene", doc) cursor.execute(""" SELECT Species.Name AS species, @@ -133,14 +136,8 @@ def main(): trait["authors"] = trait["authors"].map( lambda s: [author.strip() for author in s.split(",")]) - # Identify document as type "phenotype". We use the XT - # prefix to indicate the type. - doc.add_boolean_term("XTphenotype") - # Write document into xapian database. doc.set_data(json.dumps(trait.data)) - idterm = f"Q{i}" - doc.add_boolean_term(idterm) - db.replace_document(idterm, doc) + write_document(db, f"Q{i}", "phenotype", doc) if __name__ == "__main__": -- cgit v1.2.3