title: Shortest hash in python to name cache files
body: <p>What is the shortest hash (in filename-usable form, like a hexdigest) available in python? My application wants to save <em>cache files</em> for some objects. The objects must have unique repr() so they are used to 'seed' the filename. I want to produce a possibly unique filename for each object (not that many). They should not collide, but if they do my app will simply lack cache for that object (and will have to reindex that object's data, a minor cost for the application).</p>
  
  <p>So, if there is one collision we lose one cache file, but it is the collected savings of caching all objects makes the application startup much faster, so it does not matter much.</p>
  
  <p>Right now I'm actually using abs(hash(repr(obj))); that's right, the string hash! Haven't found any collisions yet, but I would like to have a better hash function. hashlib.md5 is available in the python library, but the hexdigest is really long if put in a filename. Alternatives, with reasonable collision resistance?</p>
  
  <p>Edit:
  Use case is like this:
  The data loader gets a new instance of a data-carrying object. Unique types have unique repr. so if a cache file for <code>hash(repr(obj))</code> exists, I unpickle that cache file and replace obj with the unpickled object. If there was a collision and the cache was a false match I notice. So if we don't have cache or have a false match, I instead init obj (reloading its data).</p>
  
  <p><strong>Conclusions (?)</strong></p>
  
  <p>The <code>str</code> hash in python may be good enough, I was only worried about its collision resistance. But if I can hash <code>2**16</code> objects with it, it's going to be  more than good enough.</p>
  
  <p>I found out how to take a hex hash (from any hash source) and store it compactly with base64:</p>
  
  <pre><code># 'h' is a string of hex digits 
  bytes = "".join(chr(int(h[i:i+2], 16)) for i in xrange(0, len(h), 2))
  hashstr = base64.urlsafe_b64encode(bytes).rstrip("=")
  </code></pre>