Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can easily build a wrapper object around the hash object which can transparently persist the data.</p> <p>The obvious drawback is that it needs to retain the hashed data in full in order to restore the state - so depending on the data size you are dealing with, this may not suit your needs. But it should work fine up to some tens of MB.</p> <p>Unfortunattely the hashlib does not expose the hash algorithms as proper classes, it rathers gives factory functions that construct the hash objects - so we can't properly subclass those without loading reserved symbols - a situation I'd rather avoid. That only means you have to built your wrapper class from the start, which is not such that an overhead from Python anyway.</p> <p>here is a sample code that might even fill your needs:</p> <pre><code>import hashlib from cStringIO import StringIO class PersistentSha1(object): def __init__(self, salt=""): self.__setstate__(salt) def update(self, data): self.__data.write(data) self.hash.update(data) def __getattr__(self, attr): return getattr(self.hash, attr) def __setstate__(self, salt=""): self.__data = StringIO() self.__data.write(salt) self.hash = hashlib.sha1(salt) def __getstate__(self): return self.data def _get_data(self): self.__data.seek(0) return self.__data.read() data = property(_get_data, __setstate__) </code></pre> <p>You can access the "data" member itself to get and set the state straight, or you can use python pickling functions:</p> <pre><code>&gt;&gt;&gt; a = PersistentSha1() &gt;&gt;&gt; a &lt;__main__.PersistentSha1 object at 0xb7d10f0c&gt; &gt;&gt;&gt; a.update("lixo") &gt;&gt;&gt; a.data 'lixo' &gt;&gt;&gt; a.hexdigest() '6d6332a54574aeb35dcde5cf6a8774f938a65bec' &gt;&gt;&gt; import pickle &gt;&gt;&gt; b = pickle.dumps(a) &gt;&gt;&gt; &gt;&gt;&gt; c = pickle.loads(b) &gt;&gt;&gt; c.hexdigest() '6d6332a54574aeb35dcde5cf6a8774f938a65bec' &gt;&gt;&gt; c.data 'lixo' </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload