-
Notifications
You must be signed in to change notification settings - Fork 0
/
solve.py
executable file
·94 lines (83 loc) · 2.96 KB
/
solve.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
'''
This script solves the exercises of days that have been completed. Jut in case
the students did not made it by their own.
'''
import sys
import urllib2
def download_and_replace(url, target_file):
'''
Downloads file through http with progress report. Version by PabloG
obtained in stack overflow
http://stackoverflow.com/questions/22676/how-do-i-download-a-file-over-http
-using-python
'''
# Try to connect to the internet
try:
u = urllib2.urlopen(url)
except Exception, err:
if getattr(err, 'code', None):
print "\nError: %s Could not get file %s\n" % (err.code, url)
else:
# A generic error is most possibly no available internet
print "\nCould not connect to the internet\n"
exit(1)
with open(target_file, 'wb') as f:
meta = u.info()
file_size = int(meta.getheaders("Content-Length")[0])
file_size_dl = 0
block_sz = 8192
while True:
buffer = u.read(block_sz)
if not buffer:
break
file_size_dl += len(buffer)
f.write(buffer)
status = r"%10d [%3.2f%%]" % (file_size_dl,
file_size_dl*100./file_size)
status = status + chr(8)*(len(status)+1)
# CONFIGURATION
master_URL = 'https://github.com/LxMLS/lxmls-toolkit/raw/master/'
labs_URL = 'https://github.com/LxMLS/lxmls-toolkit/raw/student/'
# FILES TO BE REPLACED FOR THAT DAY
code_day = {
'day1': ['lxmls/classifiers/multinomial_naive_bayes.py',
'lxmls/classifiers/perceptron.py'],
'day2': ['lxmls/sequences/hmm.py',
'lxmls/sequences/sequence_classification_decoder.py'],
'day3': ['lxmls/sequences/structured_perceptron.py'],
'day4': ['lxmls/parsing/dependency_decoder.py'],
'day5': ['lxmls/deep_learning/mlp.py'],
'day6': ['lxmls/deep_learning/rnn.py']
}
# ARGUMENT PROCESSING
if ((len(sys.argv) == 2) and
(sys.argv[1] in ['day0', 'day1', 'day2', 'day3', 'day4', 'day5', 'day6'])):
undo_flag = 0
day = sys.argv[1]
elif ((len(sys.argv) == 3) and
(sys.argv[1] == '--undo') and
(sys.argv[2] in ['day0', 'day1', 'day2', 'day3', 'day4', 'day5', 'day6'])):
undo_flag = 1
day = sys.argv[2]
else:
print ("\nUsage:\n"
"\n"
"python solve.py day<day number> # To solve exercise \n"
"\n"
"python solve.py --undo day<day number> # To undo solve\n"
"" )
exit(1)
# CHECK THERE ARE FILES TO SAVE
if day in code_day:
print "\nsolving %s" % day
else:
print "\nTheres actually no code to solve on %s!\n" % day
exit()
# OVERWRITE THE FILES TO SOLVE THEM
for pyfile in code_day[day]:
if undo_flag:
download_and_replace(labs_URL + pyfile, pyfile)
print "Unsolving: %s" % pyfile
else:
download_and_replace(master_URL + pyfile, pyfile)
print "Solving: %s" % pyfile