1
2
3
4 """The main file parse the command line and run the program in the desired way.
5
6 There's three possible runing options:
7 - -c or --cmdl: Run the program using the command line interface (not
8 really suitable)
9 - -g or --gui: Run the program using the graphic user interface.
10 - -p or --profile: Simulate the predictions computation for a sample
11 string and print the profiling statistics for the session. This is
12 useful to see what part of the program took the largest part of the
13 program's execution time.
14
15 Examples of profiling use::
16 C{python3 -m cProfile -s time main.py -p '' -l 10}
17 -> Simulate the input and predction of ten consecutive inputs of the
18 'hello world' string (default string).
19
20 C{python3 -m cProfile -s time main.py -p 'the seasons wither'}
21 -> Simulate the input and predction of the 'the seasons wither'
22 string.
23
24 @note: You must specify your own test string after -p or --profile, if
25 none are specified a default one will be used.
26 @note: The option -l or -loop indicate how many times the profiling
27 operation have to be carried out.
28
29 @todo 0.1.0:
30 Add memoize classes and decorators to improve performance.
31 """
32
33 from PyQt5 import QtCore, QtGui, QtWidgets
34 import clbk
35 import cntxt
36 import drvr
37 import gui
38 import sys
39 import getopt
40
41
42
43
44 CONFIG_FILE = 'profile.ini'
45
46
48 """Initialize the Driver instance and start a cmdl REP loop."""
49 callback = clbk.Callback()
50 driver = drvr.Driver(callback, CONFIG_FILE)
51 for c in string:
52 callback.update(c)
53 suggestions = driver.predict()
54 for p in suggestions:
55 print(p)
56 print('-- Context: %s<|>' % (driver.callback.left))
57
58
60 """Initialize the Driver instance and start a cmdl REP loop."""
61 callback = clbk.Callback()
62 driver = drvr.Driver(callback, CONFIG_FILE)
63 while True:
64 buffer = input('> ')
65 callback.update(buffer)
66 suggestions = driver.predict()
67 for p in suggestions:
68 print(p)
69 print('-- Context: %s<|>' % (driver.callback.left))
70
71
73 """Initialize the Driver instance and run the gui."""
74 callback = clbk.Callback()
75 driver = drvr.Driver(callback, CONFIG_FILE)
76 app = QtWidgets.QApplication(sys.argv)
77 mySW = gui.MainWindow(driver)
78 mySW.show()
79 sys.exit(app.exec_())
80
81
83 """Print the usage message."""
84 print('USAGE: python3 main.py [-h -c -g -p <string> -l <loops>]\n')
85 print('OPTIONS:')
86 print('\t-h or --help: print the help')
87 print('\t-c or --cmdl: use the command line version')
88 print('\t-g or --gui: use the graphic user interface version')
89 print('\t-p or --profile: use the profile module to evaluate program '
90 'performance.')
91 print('\t\t<string> is the string to use for prediction computation.')
92 print('\t-l or --loops: run the profiling operation multiple times.')
93 print('\t\t<loops> is the number of loops.')
94
95
97 """The main function of the program.
98
99 Take care of the command line options and run the program (gui or command
100 line) or print the help.
101 """
102 try:
103 opts, args = getopt.getopt(
104 sys.argv[1:], "hcgp:l:",
105 ['help', 'cmdl', 'gui', 'profile=', 'loop='])
106 except getopt.GetoptError as err:
107 print(str(err))
108 usage()
109 sys.exit(2)
110 mode = 'gui'
111 profileString = 'hello world'
112 profileLoops = 1
113 for o, a in opts:
114 if o in ("-h", "--help"):
115 usage()
116 sys.exit()
117 elif o in ("-c", "--cmdl"):
118 mode = 'cmdl'
119 elif o in ("-g", "--gui"):
120 mode = 'gui'
121 elif o in ("-p", "--profile"):
122 mode = 'profile'
123 if a:
124 profileString = a
125 elif o in ("-l", "--loop"):
126 if a:
127 profileLoops = int(a)
128 else:
129 assert False, "ERROR: Unrecognized option"
130 if mode == 'cmdl':
131 run_cmdl()
132 elif mode == 'profile':
133 for i in range(profileLoops):
134 run_profile(profileString)
135 else:
136 run_gui()
137
138
139 if __name__ == "__main__":
140 main()
141