Skip to content

Commit 2903ce8

Browse files
authored
Make code short with dicts 1 (#1)
* Create branch for make-code-short-with-dicts-1 * Update webArchiver.py
2 parents 1a1e02e + 45ed4d2 commit 2903ce8

File tree

1 file changed

+17
-34
lines changed

1 file changed

+17
-34
lines changed

webArchiver.py

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
hashblocksize=int(piargs.hashblocksize)
6868
if piargs.hashtype is not None:
6969
hashertype=piargs.hashtype
70+
7071
#define functions
7172
def file_len(fname):
7273
if False == os.path.isfile(fname):
@@ -81,37 +82,22 @@ def forcedir(file_path):
8182
if not os.path.exists(directory):
8283
os.makedirs(directory)
8384
def hasher(filein, hname, blocksize):
84-
if hname=="blake2s":
85-
htype=hashlib.blake2s()
86-
elif hname=="sha384":
87-
htype=hashlib.sha384()
88-
elif hname=="sha512":
89-
htype=hashlib.sha512()
90-
elif hname=="sha3_256":
91-
htype=hashlib.sha3_256()
92-
elif hname=="sha256":
93-
htype=hashlib.sha256()
94-
elif hname=="md5":
95-
htype=hashlib.md5()
96-
elif hname=="sha3_512":
97-
htype=hashlib.sha3_512()
98-
elif hname=="sha3_224":
99-
htype=hashlib.sha3_224()
100-
elif hname=="shake_128":
101-
htype=hashlib.shake_128()
102-
elif hname=="shake_256":
103-
htype=hashlib.shake_256()
104-
elif hname=="sha1":
105-
htype=hashlib.sha1()
106-
elif hname=="blake2b":
107-
htype=hashlib.blake2b()
108-
elif hname=="sha224":
109-
htype=hashlib.sha224()
110-
elif hname=="sha3_384":
111-
htype=hashlib.sha3_384()
112-
else:
113-
print("wrong hash type")
114-
exit(1)
85+
htype={
86+
"blake2s": lambda: hashlib.blake2s(),
87+
"sha384": lambda: hashlib.sha384(),
88+
"sha512": lambda: hashlib.sha512(),
89+
"sha3_256": lambda: hashlib.sha3_256(),
90+
"sha256": lambda: hashlib.sha256(),
91+
"md5": lambda: hashlib.md5(),
92+
"sha3_512": lambda: hashlib.sha3_512(),
93+
"sha3_224": lambda: hashlib.sha3_224(),
94+
"shake_128": lambda: hashlib.shake_128(),
95+
"shake_256": lambda: hashlib.shake_256(),
96+
"sha1": lambda: hashlib.sha1(),
97+
"blake2b": lambda: hashlib.blake2b(),
98+
"sha224": lambda: hashlib.sha224(),
99+
"sha3_384": lambda: hashlib.sha3_384(),
100+
}.get(hname, lambda: (print("wrong hash type"), exit(1)))()
115101
with open(filein, 'rb') as fi:
116102
while True:
117103
data = fi.read(blocksize)
@@ -163,6 +149,3 @@ def wfolder(directory):
163149
wfolder(infolder)
164150
shutil.move(infolder, archivedir2 + "/")
165151
exit(0)
166-
167-
168-

0 commit comments

Comments
 (0)