Skip to main content
Commonmark migration
Source Link

Anytime you have a stacked if with basically all of the same comparisions, you should consider another method.

###Use a dict:

Use a dict:

The most common way to do this sort of thing is a dict. The dict makes it easy to map a string to a method like:

sha_methods = dict(
    sha1=hashlib.sha1,
    sha224=hashlib.sha224,
    sha256=hashlib.sha256,
    sha384=hashlib.sha384,
    sha512=hashlib.sha512,
)

def hexrandom(minint, maxint, shamode="sha1"):
    if shamode not in sha_mthods:
        raise ValueError("Unknown hashlib method {}".format(shamode))
    x = str(rand.randint(int(minint), int(maxint)))
    return sha_methods[shamode](x.encode()).hexdigest()

Use a direct method lookup:

In this case, since the shamode string matches the method name hashlib, we can use getattr to directly lookup this method like:

def hexrandom(minint, maxint, shamode="sha1"):
    method = getattr(hashlib, shamode, None)
    if method is None:
        raise ValueError("Unknown hashlib method {}".format(shamode))
    x = str(rand.randint(int(minint), int(maxint)))
    return method(x.encode()).hexdigest() 

Anytime you have a stacked if with basically all of the same comparisions, you should consider another method.

###Use a dict:

The most common way to do this sort of thing is a dict. The dict makes it easy to map a string to a method like:

sha_methods = dict(
    sha1=hashlib.sha1,
    sha224=hashlib.sha224,
    sha256=hashlib.sha256,
    sha384=hashlib.sha384,
    sha512=hashlib.sha512,
)

def hexrandom(minint, maxint, shamode="sha1"):
    if shamode not in sha_mthods:
        raise ValueError("Unknown hashlib method {}".format(shamode))
    x = str(rand.randint(int(minint), int(maxint)))
    return sha_methods[shamode](x.encode()).hexdigest()

Use a direct method lookup:

In this case, since the shamode string matches the method name hashlib, we can use getattr to directly lookup this method like:

def hexrandom(minint, maxint, shamode="sha1"):
    method = getattr(hashlib, shamode, None)
    if method is None:
        raise ValueError("Unknown hashlib method {}".format(shamode))
    x = str(rand.randint(int(minint), int(maxint)))
    return method(x.encode()).hexdigest() 

Anytime you have a stacked if with basically all of the same comparisions, you should consider another method.

Use a dict:

The most common way to do this sort of thing is a dict. The dict makes it easy to map a string to a method like:

sha_methods = dict(
    sha1=hashlib.sha1,
    sha224=hashlib.sha224,
    sha256=hashlib.sha256,
    sha384=hashlib.sha384,
    sha512=hashlib.sha512,
)

def hexrandom(minint, maxint, shamode="sha1"):
    if shamode not in sha_mthods:
        raise ValueError("Unknown hashlib method {}".format(shamode))
    x = str(rand.randint(int(minint), int(maxint)))
    return sha_methods[shamode](x.encode()).hexdigest()

Use a direct method lookup:

In this case, since the shamode string matches the method name hashlib, we can use getattr to directly lookup this method like:

def hexrandom(minint, maxint, shamode="sha1"):
    method = getattr(hashlib, shamode, None)
    if method is None:
        raise ValueError("Unknown hashlib method {}".format(shamode))
    x = str(rand.randint(int(minint), int(maxint)))
    return method(x.encode()).hexdigest() 
Source Link
Stephen Rauch
  • 4.2k
  • 12
  • 23
  • 36

Anytime you have a stacked if with basically all of the same comparisions, you should consider another method.

###Use a dict:

The most common way to do this sort of thing is a dict. The dict makes it easy to map a string to a method like:

sha_methods = dict(
    sha1=hashlib.sha1,
    sha224=hashlib.sha224,
    sha256=hashlib.sha256,
    sha384=hashlib.sha384,
    sha512=hashlib.sha512,
)

def hexrandom(minint, maxint, shamode="sha1"):
    if shamode not in sha_mthods:
        raise ValueError("Unknown hashlib method {}".format(shamode))
    x = str(rand.randint(int(minint), int(maxint)))
    return sha_methods[shamode](x.encode()).hexdigest()

Use a direct method lookup:

In this case, since the shamode string matches the method name hashlib, we can use getattr to directly lookup this method like:

def hexrandom(minint, maxint, shamode="sha1"):
    method = getattr(hashlib, shamode, None)
    if method is None:
        raise ValueError("Unknown hashlib method {}".format(shamode))
    x = str(rand.randint(int(minint), int(maxint)))
    return method(x.encode()).hexdigest()