Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!feeder1.cambriumusenet.nl!feed.tweaknews.nl!194.109.133.84.MISMATCH!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'subject:Does': 0.04; 'memory.': 0.05; 'bits': 0.07; 'mode,': 0.07; '(rather': 0.09; 'attribute': 0.09; 'awake': 0.09; 'byte,': 0.09; 'referenced': 0.09; 'essentially': 0.11; 'subject:file': 0.13; 'wrote:': 0.15; 'allocate': 0.16; 'chunks': 0.16; 'dma': 0.16; 'file.read()': 0.16; 'hashlib': 0.16; 'operations?': 0.16; 'received:209.85.213.174': 0.16; 'received:mail- yx0-f174.google.com': 0.16; 'thru': 0.16; 'cc:addr:python-list': 0.16; 'pm,': 0.16; 'this:': 0.16; 'written': 0.17; 'cheers,': 0.19; 'cc:2**0': 0.21; 'cc:no real name:2**0': 0.22; "doesn't": 0.22; 'header:In-Reply-To:1': 0.22; 'loop,': 0.23; 'tue,': 0.23; 'appear': 0.23; 'there.': 0.25; 'string': 0.26; "i'm": 0.27; 'bit': 0.28; 'message-id:@mail.gmail.com': 0.28; 'cc:addr:python.org': 0.30; 'hash': 0.30; 'objects.': 0.30; 'skip:\xc2 20': 0.30; 'subject:support': 0.30; 'undocumented': 0.30; 'url:library': 0.31; 'subject:?': 0.31; 'chris': 0.32; 'does': 0.32; 'instead': 0.34; 'lie': 0.35; 'file': 0.36; 'url:python': 0.37; 'streaming': 0.37; 'some': 0.37; 'could': 0.37; 'received:google.com': 0.38; 'received:209.85': 0.38; 'url:org': 0.38; 'subject:: ': 0.38; 'url:docs': 0.39; 'ways': 0.39; 'received:209': 0.40; 'as:': 0.71; 'movie': 0.71; 'skip:\xc2 10': 0.74; 'night': 0.77; 'stream': 0.77; 'cpu.': 0.84; 'flame': 0.84; 'sender:addr:chris': 0.84; 'slow.': 0.84; 'subject:mode': 0.84; 'url:hash': 0.84; 'url:html#io': 0.84; 'url:rebertia': 0.84; 'sum': 0.89; 'movies': 0.93; 'url:io': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=aG1hNeMTl6rxFnB97ZQXVKlahpvSsvM8wMdHyuzH9Os=; b=JS1PNcuDl3e0dGmV9RyCAOvJdH56lHzIavlTyYmS8L5q/jHsc6kUW5uMvX6Gl7zfna hyO7hvBB/DOsBXTkaY4awJxPk3Y5toHAbIPbR/ticPd4pf/g+70hWXErlWIPXBepuOyT qlYOLDE0mYhA2c6JlURqCpPwe+pM4kG1qW6pQ= MIME-Version: 1.0 Sender: chris@rebertia.com In-Reply-To: <952b0e40-3308-4dbc-b107-8fbe96014199@e17g2000prj.googlegroups.com> References: <952b0e40-3308-4dbc-b107-8fbe96014199@e17g2000prj.googlegroups.com> Date: Tue, 5 Jul 2011 23:44:28 -0700 X-Google-Sender-Auth: nY89Un4oIiCFstStevh3kdNVfWM Subject: Re: Does hashlib support a file mode? From: Chris Rebert To: Phlip Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 45 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1309934672 news.xs4all.nl 21873 [2001:888:2000:d::a6]:42057 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:8907 On Tue, Jul 5, 2011 at 10:54 PM, Phlip wrote: > Pythonistas: > > Consider this hashing code: > > =C2=A0import hashlib > =C2=A0file =3D open(path) > =C2=A0m =3D hashlib.md5() > =C2=A0m.update(file.read()) > =C2=A0digest =3D m.hexdigest() > =C2=A0file.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.) > > So if I do the stream trick - read one byte, update one byte, in a > loop, then I'm essentially dragging that movie thru 8 bits of a 64 bit > CPU. So that's the same problem; it would still be slow. > > So now I try this: > > =C2=A0sum =3D os.popen('sha256sum %r' % path).read() > > Those of you who like to lie awake at night thinking of new ways to > flame abusers of 'eval()' may have a good vent, there. Indeed (*eyelid twitch*). That one-liner is arguably better written as: sum =3D subprocess.check_output(['sha256sum', path]) > Does hashlib have a file-ready mode, to hide the streaming inside some > clever DMA operations? Barring undocumented voodoo, no, it doesn't appear to. You could always read from the file in suitably large chunks instead (rather than byte-by-byte, which is indeed ridiculous); see io.DEFAULT_BUFFER_SIZE and/or the os.stat() trick referenced therein and/or the block_size attribute of hash objects. http://docs.python.org/library/io.html#io.DEFAULT_BUFFER_SIZE http://docs.python.org/library/hashlib.html#hashlib.hash.block_size Cheers, Chris -- http://rebertia.com