Generating an unique seed for a given filename

When creating large simulations, you may sometimes create unique identifiers for each of it. This is useful to cache intermediate results for instance. This is the main function of hashes. We will here create a simple one-liner function to generate one.

In [1]:
filename = ''
print ('filename=', filename)
In [2]:
import os
import hashlib
hash_str = hashlib.sha224(filename.encode('utf-8')).hexdigest()
print('hash_str=', hash_str)
hash_str= d9dee4e30946f8306a87b83b3a5723e9b9546e114e9f477025090ba2
In [3]:
filename = ''
print ('filename=', filename)
print('hash_str=', hashlib.sha224(filename.encode('utf-8')).hexdigest())
hash_str= fd94b7e0e5e950996d548c0777ccf7f800205065d9c28c1f255a3e82

Done! This gives a simple one-liner to associate a filename to a (most probably) hash.


In [4]:
%load_ext watermark

CPython 3.6.5
IPython 6.4.0

compiler   : GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)
system     : Darwin
release    : 17.5.0
machine    : x86_64
processor  : i386
CPU cores  : 4
interpreter: 64bit
In [5]:
%load_ext version_information
%version_information numpy, hashlib
Software Version
Python 3.6.5 64bit [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)]
IPython 6.4.0
OS Darwin 17.5.0 x86_64 i386 64bit
numpy 1.14.4
hashlib The 'hashlib' distribution was not found and is required by the application
Wed Jun 13 13:09:46 2018 CEST