-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.py
97 lines (79 loc) · 2.74 KB
/
search.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
from imports.linearsearch import LinearSearch
from imports.masterlist import MasterList
from imports.benchmark import Benchmark
from imports.insearch import InSearch
from imports.analyzer import Analyzer
from imports.bsearch import BSearch
from imports.logger import Logger
from pathlib import Path
import multiprocessing
import tabulate
import datetime
import argparse
###############################################################################
# Configuration
###############################################################################
# Arguments
argparser = argparse.ArgumentParser()
argparser.add_argument("-w", "--word",
help = "The word you want to find. Ignored in benchmark mode.")
argparser.add_argument("-m", "--mode",
help = "Set the search mode: linear or tree",
type = str.lower,
default = "linear")
argparser.add_argument("-n", "--num",
help = "How many test to run for each search mode. Default = 500",
type = int,
default = 500)
argparser.add_argument("-s", "--short",
help = "Use (and, if needed, create) a short version of the master file.",
action = "store_true",
default = False)
args = argparser.parse_args()
log = Logger()
# Modes
modes = {
"in": [InSearch],
"tree": [BSearch],
"linear": [LinearSearch],
"benchmark": [InSearch, BSearch, LinearSearch]
}
###############################################################################
# Program
###############################################################################
def main():
results = {}
searchTerms = []
master = MasterList(usingShort = args.short)
analyzer = Analyzer()
if args.mode in modes:
if args.mode == "benchmark":
searchTerms = master.benchmarkList(args.num)
else:
searchTerms.append(args.word)
# Loop through the modes and get a new instance of each as they come in.
for Mode in modes[args.mode]:
# Add instance of the MasterList to the current Mode
mode = Mode(master)
log.write(f"Searching in {mode.name} mode")
# Setup results for the current mode.
results[mode.name] = { "tests": [] }
# Loop through all of our search terms...
for term in searchTerms:
#... add the test to our tests list...
results[mode.name]["tests"].append({
"term": term,
"found": False,
"start": datetime.datetime.now(),
"end": None
})
#... search for the term in the current mode...
results[mode.name]["tests"][-1]["found"] = mode.find(term)
if mode.find(term):
#... if we find it, add the end time to the results.
results[mode.name]["tests"][-1]["end"] = datetime.datetime.now()
# end for
analyzer.analyze(results)
else:
log.write(f"'{mode} is not a valid search mode.")
main()