Reputation: 22282
I'm sorry about I don't know how to explain this...for example, here is my code:
def get_hash(choice):
if choice == 1:
sha1 = hashlib.sha1()
sha1.update('a test for sha1'.encode('utf-8'))
print(sha1.hexdigest())
elif choice == 2:
sha256 = hashlib.sha256()
sha256.update('a test for sha256'.encode('utf-8'))
print(sha256.hexdigest())
get_hash(int(input('Enter your choice between one and two: '))
Look, choice 1 and choice 2 only 'sha1' and 'sha256' are difference.
So I want to know, how to decrease the code size? Can I do some thing like 'sha{0} = hashlib.sha{0}'.format('256')
?
Upvotes: 0
Views: 182
Reputation: 18727
In python, everything is an object!
hashlib_mapping = {
1: "sha1",
2: "sha256",
... }
def hash(choice):
hasher = getattr(hashlib, hashlib_mapping[choice])()
^1^ ^2^ ^3^
That means, you can map values with method names. Then use getattr
(1) to get related method (2) of th hashlib class and call it(3). You can even use sha method numbers as below
# Pass sha method number to define the relatd sha method.
hasher = getattr(hashlib, "sha{num}".format(num=choice))()
hash(1) # will use sha1
hash(224) # will use sha224
hash(384) # will use sha384
Upvotes: 2
Reputation: 2807
You can do this and keeping one argument in your function:
def hash(choice):
map_dico = { 1: [ 'sha1', hashlib.sha1()] ,
2: [ 'sha256', hashlib.sha256() ]}
map_dico[choice][1].update('a test for {}'.format(map_dico[choice][0]).encode('utf-8'))
print(map_dico[choice][1].hexdigest())
hash(int(input('Enter your choice between one and two: ')) )
Upvotes: 0
Reputation: 9986
You could rewrite your hash function to take the hasher as an argument.
def hash_test(hasher):
hasher.update('a hash test'.encode('utf-8'))
print(hasher.hexdigest())
hashes = {'sha256': hashlib.sha256(), 'sha1': hashlib.sha1()}
hash_type = input('Do you want to try sha1, or sha256?')
hasher = hashes[hash_type]
hash_test(hasher)
There's no (sane) way to create/use dynamic variables names, especially if you're trying to create objects from a library.
Upvotes: 2