Cheap mapping of string to small fixed-length string
Question: Just for _debugging_ purposes I would like to map a big string (a session_id,
which is difficult to visualize) to a, let's say, 6 character "hash". This
hash does not need to be secure in any way, just cheap to compute, and of
fixed and reduced length (md5 is too long). The input string can have any
length.

How would you implement this "cheap_hash" in python so that it is not
expensive to compute? It should generate something like this:

    
    
    def compute_cheap_hash(txt, length=6):
        # do some computation
        return cheap_hash
    
    print compute_cheap_hash("SDFSGSADSADFSasdfgsadfSDASAFSAGAsaDSFSA2345435adfdasgsaed")
    aBxr5u
    


Answer: 
    def cheaphash(string,length=6):
        if length<len(hashlib.sha256(string).hexdigest()):
            return hashlib.sha256(string).hexdigest()[:length]
        else:
            raise Exception("Length too long. Length of {y} when hash length is {x}.".format(x=str(len(hashlib.sha256(string).hexdigest())),y=length))
    

This should do what you need it to do, it simply uses the `hashlib` module, so
make sure to import it before using this function.

