[yt-svn] commit/yt: ngoldbaum: Merged in rthompson/sphgr_yt (pull request #2034)

commits-noreply at bitbucket.org commits-noreply at bitbucket.org
Wed Mar 9 09:13:14 PST 2016


1 new commit in yt:

https://bitbucket.org/yt_analysis/yt/commits/a79baee298d9/
Changeset:   a79baee298d9
Branch:      yt
User:        ngoldbaum
Date:        2016-03-09 17:13:04+00:00
Summary:     Merged in rthompson/sphgr_yt (pull request #2034)

adding get_hash() function to yt/funcs.py which returns a hash for a file.
Affected #:  1 file

diff -r fe826d7cd6ba606461cf25fc50404f1d3836dbf9 -r a79baee298d9cc1a4346de3878d0d0ec6d27442c yt/funcs.py
--- a/yt/funcs.py
+++ b/yt/funcs.py
@@ -860,3 +860,55 @@
         return 'unitary'
     else:
         return u
+
+def get_hash(infile, algorithm='md5', BLOCKSIZE=65536):
+    """Generate file hash without reading in the entire file at once.
+
+    Original code licensed under MIT.  Source:
+    http://pythoncentral.io/hashing-files-with-python/
+
+    Parameters
+    ----------
+    infile : str
+        File of interest (including the path).
+    algorithm : str (optional)
+        Hash algorithm of choice. Defaults to 'md5'.
+    BLOCKSIZE : int (optional)
+        How much data in bytes to read in at once.
+
+    Returns
+    -------
+    hash : str
+        The hash of the file.
+
+    Examples
+    --------
+    >>> import yt.funcs as funcs
+    >>> funcs.get_hash('/path/to/test.png')
+    'd38da04859093d430fa4084fd605de60'
+
+    """
+    import hashlib
+
+    try:
+        hasher = getattr(hashlib, algorithm)()
+    except:
+        raise NotImplementedError("'%s' not available!  Available algorithms: %s" %
+                                  (algorithm, hashlib.algorithms))
+
+    filesize   = os.path.getsize(infile)
+    iterations = int(float(filesize)/float(BLOCKSIZE))
+
+    pbar = get_pbar('Generating %s hash' % algorithm, iterations)
+
+    iter = 0
+    with open(infile,'rb') as f:
+        buf = f.read(BLOCKSIZE)
+        while len(buf) > 0:
+            hasher.update(buf)
+            buf = f.read(BLOCKSIZE)
+            iter += 1
+            pbar.update(iter)
+        pbar.finish()
+
+    return hasher.hexdigest()

Repository URL: https://bitbucket.org/yt_analysis/yt/

--

This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.


More information about the yt-svn mailing list