Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #8927

Re: Does hashlib support a file mode?

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!selfless.tophat.at!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <awilliam@whitemice.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'ok.': 0.04; 'subject:Does': 0.04; 'memory.': 0.05; 'chunk': 0.07; 'mode,': 0.07; 'data:': 0.09; 'underlying': 0.09; 'output': 0.11; 'subject:file': 0.13; 'wrote:': 0.15; 'allocate': 0.16; 'dma': 0.16; 'file.close()': 0.16; 'file.read()': 0.16; 'from:addr:awilliam': 0.16; 'from:addr:whitemice.org': 0.16; 'from:name:adam tauno williams': 0.16; 'hashlib': 0.16; 'message- id:@linux-yu4c.site': 0.16; 'operations?': 0.16; 'received:72.14.190': 0.16; 'received:72.14.190.87': 0.16; 'received:mail.wmmi.net': 0.16; 'received:wmmi.net': 0.16; 'reply- to:addr:awilliam': 0.16; 'reply-to:addr:whitemice.org': 0.16; 'def': 0.16; 'digest': 0.19; 'header:In-Reply-To:1': 0.22; 'tue,': 0.23; 'string': 0.26; 'function': 0.26; 'received:72.14': 0.26; 'import': 0.29; 'example': 0.30; 'i/o': 0.30; 'subject:support': 0.30; 'subject:?': 0.31; 'does': 0.32; 'break': 0.33; 'to:addr :python-list': 0.34; 'rule': 0.34; 'skip:o 20': 0.36; 'file': 0.36; 'streaming': 0.37; 'some': 0.37; 'could': 0.37; 'subject:: ': 0.38; 'something': 0.38; 'header:Mime-Version:1': 0.39; 'data': 0.39; 'to:addr:python.org': 0.39; 'your': 0.60; 'matter': 0.61; 'header:Reply-To:1': 0.71; 'reply-to:no real name:2**0': 0.72; 'stream': 0.77; 'blocks.': 0.84; 'filesystem.': 0.84; 'streams': 0.84; 'subject:mode': 0.84; 'movies': 0.93
Subject Re: Does hashlib support a file mode?
From Adam Tauno Williams <awilliam@whitemice.org>
To python-list@python.org
Date Wed, 06 Jul 2011 06:55:31 -0400
In-Reply-To <952b0e40-3308-4dbc-b107-8fbe96014199@e17g2000prj.googlegroups.com>
References <952b0e40-3308-4dbc-b107-8fbe96014199@e17g2000prj.googlegroups.com>
Content-Type text/plain; charset="UTF-8"
X-Mailer Evolution 3.0.2
Content-Transfer-Encoding 7bit
Mime-Version 1.0
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
Reply-To awilliam@whitemice.org
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.697.1309951505.1164.python-list@python.org> (permalink)
Lines 38
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1309951505 news.xs4all.nl 21790 [2001:888:2000:d::a6]:34050
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:8927

Show key headers only | View raw


On Tue, 2011-07-05 at 22:54 -0700, Phlip wrote:
> Pythonistas
> Consider this hashing code:
>   import hashlib
>   file = open(path)
>   m = hashlib.md5()
>   m.update(file.read())
>   digest = m.hexdigest()
>   file.close()
> If the file were huge, the file.read() would allocate a big string and
> thrash memory. (Yes, in 2011 that's still a problem, because these
> files could be movies and whatnot.)

Yes, the simple rule is do not *ever* file.read().  No matter what the
year this will never be OK.  Always chunk reading a file into reasonable
I/O blocks.

For example I use this function to copy a stream and return a SHA512 and
the output streams size:

    def write(self, in_handle, out_handle):
        m = hashlib.sha512()
        data = in_handle.read(4096)
        while True:
            if not data:
                break
            m.update(data)
            out_handle.write(data)
            data = in_handle.read(4096)
        out_handle.flush()
        return (m.hexdigest(), in_handle.tell())

> Does hashlib have a file-ready mode, to hide the streaming inside some
> clever DMA operations?

Chunk it to something close to the block size of your underlying
filesystem.

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Does hashlib support a file mode? Phlip <phlip2005@gmail.com> - 2011-07-05 22:54 -0700
  Re: Does hashlib support a file mode? Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2011-07-06 08:37 +0200
  Re: Does hashlib support a file mode? Chris Rebert <clp2@rebertia.com> - 2011-07-05 23:44 -0700
  Re: Does hashlib support a file mode? Anssi Saari <as@sci.fi> - 2011-07-06 12:47 +0300
    Re: Does hashlib support a file mode? Phlip <phlip2005@gmail.com> - 2011-07-06 06:47 -0700
  Re: Does hashlib support a file mode? Adam Tauno Williams <awilliam@whitemice.org> - 2011-07-06 06:55 -0400

csiph-web