-
Notifications
You must be signed in to change notification settings - Fork 0
/
makeloss.py
executable file
·66 lines (49 loc) · 1.27 KB
/
makeloss.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
#!/usr/bin/python3
import sys
import os
import lossy_data
import logger
import getopt
import random
def usage():
print("""\
Usage: makeloss.py [-r lossrate] <path> <result path>
""")
class MakeLoss:
def run(self, path, path_res, lossrate):
data = lossy_data.LossyData()
if not data.load(path):
return 1
data.go_lossy(lossrate)
data.save(path_res)
return 0
lossrate = 0.01
def parseArgs():
global lossrate, args
try:
opts, args = getopt.getopt(sys.argv[1:], "r:")
except getopt.GetoptError:
return False
for o, a in opts:
if o == '-r':
lossrate = float(a)
if lossrate < 0 or lossrate > 1:
logger.error("invalid loss rate: {}".format(lossrate))
return False
if len(args) != 2:
return False
return True
if __name__ == "__main__":
import makeloss
logger.init("makeloss")
if not parseArgs():
usage()
exit(1)
if not os.path.isfile(args[0]):
logger.error("file not found: {}".format(args[0]))
exit(1)
if os.path.exists(args[1]):
logger.error("result path already exist: {}".format(args[1]))
exit(1)
ml = makeloss.MakeLoss()
exit(ml.run(args[0], args[1], lossrate))