From 4580f6792063f0e1883f1f7065c81a896439eb25 Mon Sep 17 00:00:00 2001 From: Dominik Roth Date: Tue, 17 Jan 2023 23:18:26 +0100 Subject: [PATCH] cli autocomplete and defered imports --- caliGraph.py | 46 ++++++++++++++++++++++++++++++++++------------ requirements.txt | 2 ++ 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/caliGraph.py b/caliGraph.py index e35c0a5..b65730e 100755 --- a/caliGraph.py +++ b/caliGraph.py @@ -1,22 +1,34 @@ #!./.venv/bin/python3.10 import os -import re import json import math import copy import random -import requests -import numpy as np -import pandas as pd -from scipy.stats import norm +import argcomplete, pyzshcomplete +# Enable autocomplete in global completion mode for bash & zsh: +# PYTHON_ARGCOMPLETE_OK +# PYZSHCOMPLETE_OK -import matplotlib.pyplot as plt -import networkx as nx -from pyvis.network import Network -import plotly.graph_objects as go +# performance hack; only load these after validating cmd args +def defered_imports(): + global re, requests + import re + import requests -import wikipedia + global np, pd, norm + import numpy as np + import pandas as pd + from scipy.stats import norm + + global plt, nx, Network, go + import matplotlib.pyplot as plt + import networkx as nx + from pyvis.network import Network + import plotly.graph_objects as go + + global wikipedia + import wikipedia class Error(Exception): @@ -771,7 +783,6 @@ def graphAddTopLists(G, books, darkMode=False): book, top), color=readColor(book)) return G - def graphAddSeries(G, books, darkMode=False): for series in getAllSeries(books): G.add_node('s/'+series, color='red', t='series', @@ -1536,6 +1547,7 @@ def loadWeights(): def cliInterface(imgDef=False): import argparse + import argcomplete parser = argparse.ArgumentParser(description='TODO: Write Description.') parser.add_argument('--keep-priv', action="store_true") @@ -1615,6 +1627,8 @@ def cliInterface(imgDef=False): p_full = cmds.add_parser('full', description="TODO", aliases=[]) + pyzshcomplete.autocomplete(parser) + argcomplete.autocomplete(parser) args = parser.parse_args() if imgDef: @@ -1642,6 +1656,10 @@ def perfTestCLI(args): def mainCLI(args): + global weights + defered_imports() + weights = loadWeights() + if args.cmd == "train": train(args.g, args.full) exit() @@ -1738,9 +1756,13 @@ def mainCLI(args): genAndShowHTML(G, darkMode=args.dark) -weights = loadWeights() +#weights = loadWeights() +weights = None if __name__ == "__main__": try: cliInterface(imgDef=True) except Error as e: print("[!] {0}".format(e)) +else: + weights = loadWeights() + defered_imports() diff --git a/requirements.txt b/requirements.txt index 32e04a6..eb56b9a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,3 +10,5 @@ requests pandas plotly wikipedia +argcomplete +pyzshcomplete