Re: Does hashlib support a file mode?

2011-07-08 Thread Steven D'Aprano
Phlip wrote: I worded that poorly. None is (AFAIK) the only instance of NoneType, but I should've clarified the difference. The is operator does not compare types, it compares instances for identity. None is typesafe, because it's strongly typed. Everything in Python is strongly typed. Why

Re: Does hashlib support a file mode?

2011-07-08 Thread Phlip
On Jul 8, 12:42 am, Steven D'Aprano steve +comp.lang.pyt...@pearwood.info wrote: Phlip wrote: I worded that poorly. None is (AFAIK) the only instance of NoneType, but I should've clarified the difference. The is operator does not compare types, it compares instances for identity. None is

Re: Does hashlib support a file mode?

2011-07-08 Thread Steven D'Aprano
Phlip wrote: On Jul 8, 12:42 am, Steven D'Aprano steve +comp.lang.pyt...@pearwood.info wrote: Phlip wrote: I worded that poorly. None is (AFAIK) the only instance of NoneType, but I should've clarified the difference. The is operator does not compare types, it compares instances for

Re: Does hashlib support a file mode?

2011-07-07 Thread Anssi Saari
Mel mwil...@the-wire.com writes: def file_to_hash(path, m = hashlib.md5()): hashlib.md5 *is* called once; that is when the def statement is executed. Very interesting, I certainly wasn't clear on this. So after that def, the created hashlib object is in the module's scope and can be accessed

Re: Does hashlib support a file mode?

2011-07-07 Thread Paul Rudin
Anssi Saari a...@sci.fi writes: Mel mwil...@the-wire.com writes: def file_to_hash(path, m = hashlib.md5()): hashlib.md5 *is* called once; that is when the def statement is executed. Very interesting, I certainly wasn't clear on this. So after that def, the created hashlib object is in the

Re: Does hashlib support a file mode?

2011-07-07 Thread Andrew Berg
On 2011.07.06 06:16 PM, Steven D'Aprano wrote: Phlip wrote: Note the fix also avoids comparing to None, which, as usual, is also icky and less typesafe! Typesafe? Are you trying to make a joke? Maybe he has a duck phobia. Maybe he denies the existence of ducks. Maybe he doesn't like the

Re: Does hashlib support a file mode?

2011-07-07 Thread Phlip
On 2011.07.06 06:16 PM, Steven D'Aprano wrote: Phlip wrote: Note the fix also avoids comparing to None, which, as usual, is also icky and less typesafe! Typesafe? Are you trying to make a joke? No, I was pointing out that passing a type is more ... typesafe. --

Re: Does hashlib support a file mode?

2011-07-07 Thread Andrew Berg
On 2011.07.07 08:11 AM, Phlip wrote: No, I was pointing out that passing a type is more ... typesafe. None is a type. None.__class__ class 'NoneType' -- http://mail.python.org/mailman/listinfo/python-list

Re: Does hashlib support a file mode?

2011-07-07 Thread Phlip
On Jul 7, 6:24 am, Andrew Berg bahamutzero8...@gmail.com wrote: On 2011.07.07 08:11 AM, Phlip wrote: No, I was pointing out that passing a type is more ... typesafe. None is a type. I never said it wasn't. -- http://mail.python.org/mailman/listinfo/python-list

Re: Does hashlib support a file mode?

2011-07-07 Thread Andrew Berg
On 2011.07.07 08:39 AM, Phlip wrote: On Jul 7, 6:24 am, Andrew Berg bahamutzero8...@gmail.com wrote: On 2011.07.07 08:11 AM, Phlip wrote: No, I was pointing out that passing a type is more ... typesafe. None is a type. I never said it wasn't. You are talking about this code, right?

Re: Does hashlib support a file mode?

2011-07-07 Thread Steven D'Aprano
Andrew Berg wrote: On 2011.07.07 08:39 AM, Phlip wrote: On Jul 7, 6:24 am, Andrew Berg bahamutzero8...@gmail.com wrote: On 2011.07.07 08:11 AM, Phlip wrote: No, I was pointing out that passing a type is more ... typesafe. None is a type. I never said it wasn't. Unfortunately, it

Re: Does hashlib support a file mode?

2011-07-07 Thread Andrew Berg
On 2011.07.07 08:46 PM, Steven D'Aprano wrote: None is not a type, it is an instance. isinstance(None, type) # is None a type? False isinstance(None, type(None)) # is None an instance of None's type? True So None is not itself a type, although it *has* a type: type(None) type

Re: Does hashlib support a file mode?

2011-07-07 Thread Phlip
I worded that poorly. None is (AFAIK) the only instance of NoneType, but I should've clarified the difference. The is operator does not compare types, it compares instances for identity. None is typesafe, because it's strongly typed. However, what's even MORE X-safe (for various values of X)

Re: Does hashlib support a file mode?

2011-07-06 Thread Chris Rebert
On Tue, Jul 5, 2011 at 10:54 PM, Phlip phlip2...@gmail.com 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

Re: Does hashlib support a file mode?

2011-07-06 Thread Thomas Rachel
Am 06.07.2011 07:54 schrieb Phlip: 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.

Re: Does hashlib support a file mode?

2011-07-06 Thread Anssi Saari
Phlip phlip2...@gmail.com writes: 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.) I did a crc32 calculator like that and actually ran into some kind of string length

Re: Does hashlib support a file mode?

2011-07-06 Thread Adam Tauno Williams
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

Re: Does hashlib support a file mode?

2011-07-06 Thread Phlip
wow, tx y'all! I forgot to mention that hashlib itself is not required; I could also use Brand X. But y'all agree that blocking up the file in python adds no overhead to hashing each block in C, so hashlib in a loop it is! -- http://mail.python.org/mailman/listinfo/python-list

Re: Does hashlib support a file mode?

2011-07-06 Thread Phlip
Tx, all!. But... 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:                

Re: Does hashlib support a file mode?

2011-07-06 Thread Peter Otten
Phlip wrote: Tx, all!. But... 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)

Re: Does hashlib support a file mode?

2011-07-06 Thread Phlip
- Open the file in binary mode. I had tried open(path, 'rb') and it didn't change the wrong number. And I added --binary to my evil md5sum version, and it didn't change the right number! Gods bless those legacy hacks that will never die, huh? But I'm using Ubuntu (inside VMWare, on Win7, on a

Re: Does hashlib support a file mode?

2011-07-06 Thread Peter Otten
Phlip wrote: - Open the file in binary mode. I had tried open(path, 'rb') and it didn't change the wrong number. And I added --binary to my evil md5sum version, and it didn't change the right number! Gods bless those legacy hacks that will never die, huh? But I'm using Ubuntu (inside

Re: Does hashlib support a file mode?

2011-07-06 Thread Phlip
def file_to_hash(path, m=None): if m is None: m = hashlib.md5() The first call will give you the correct checksum, the second: not. As the default md5 instance remembers the state from the previous function call you'll get the checksum of both files combined. Ouch. That was it. Python

Re: Does hashlib support a file mode?

2011-07-06 Thread Chris Torek
- Do the usual dance for default arguments: def file_to_hash(path, m=None): if m is None: m = hashlib.md5() [instead of def file_to_hash(path, m = hashlib.md5()): ] In article b317226a-8008-4177-aaa6-3fdc30125...@e20g2000prf.googlegroups.com Phlip

Re: Does hashlib support a file mode?

2011-07-06 Thread Andrew Berg
On 2011.07.06 12:38 PM, Phlip wrote: Python sucks. m = md5() looks like an initial assignment, not a special magic storage mode. Principle of least surprise fail, and principle of most helpful default behavior fail. func() = whatever the function returns func = the function object itself (in

Re: Does hashlib support a file mode?

2011-07-06 Thread Phlip
On Jul 6, 11:42 am, Andrew Berg bahamutzero8...@gmail.com wrote: On 2011.07.06 12:38 PM, Phlip wrote: Python sucks. m = md5() looks like an initial assignment, not a special magic storage mode. Principle of least surprise fail, and principle of most helpful default behavior fail. func() =

Re: Does hashlib support a file mode?

2011-07-06 Thread geremy condra
On Wed, Jul 6, 2011 at 3:07 PM, Phlip phlip2...@gmail.com wrote: On Jul 6, 11:42 am, Andrew Berg bahamutzero8...@gmail.com wrote: On 2011.07.06 12:38 PM, Phlip wrote: Python sucks. m = md5() looks like an initial assignment, not a special magic storage mode. Principle of least surprise fail,

Re: Does hashlib support a file mode?

2011-07-06 Thread Andrew Berg
On 2011.07.06 02:07 PM, Phlip wrote: If I call m = md5() twice, I expect two objects. You get two objects because you make the function run again. Of course, the first one is garbage collected if it doesn't have another reference. m1 = hashlib.md5() m2 = hashlib.md5() m1 is m2 False Are you

Re: Does hashlib support a file mode?

2011-07-06 Thread Mel
Phlip wrote: If I call m = md5() twice, I expect two objects. I am now aware that Python bends the definition of call based on where the line occurs. Principle of least surprise. Actually, in def file_to_hash(path, m = hashlib.md5()): hashlib.md5 *is* called once; that is when the def

Re: Does hashlib support a file mode?

2011-07-06 Thread Ian Kelly
On Wed, Jul 6, 2011 at 1:07 PM, Phlip phlip2...@gmail.com wrote: If I call m = md5() twice, I expect two objects. I am now aware that Python bends the definition of call based on where the line occurs. Principle of least surprise. There is no definition-bending. The code: def

Re: Does hashlib support a file mode?

2011-07-06 Thread Ethan Furman
Phlip wrote: On 2011.07.06 12:38 PM, Phlip wrote: Python sucks. m = md5() looks like an initial assignment, not a special magic storage mode. Principle of least surprise fail, and principle of most helpful default behavior fail. If I call m = md5() twice, I expect two objects. You didn't

Re: Does hashlib support a file mode?

2011-07-06 Thread Carl Banks
On Wednesday, July 6, 2011 12:07:56 PM UTC-7, Phlip wrote: If I call m = md5() twice, I expect two objects. I am now aware that Python bends the definition of call based on where the line occurs. Principle of least surprise. Phlip: We already know about this violation of the least surprise

Re: Does hashlib support a file mode?

2011-07-06 Thread Phlip
On Jul 6, 1:25 pm, Carl Banks pavlovevide...@gmail.com wrote: We already know about this violation of the least surprise principle; most of us acknowledge it as small blip in an otherwise straightforward and clean language. Here's the production code we're going with - thanks again all:

Re: Does hashlib support a file mode?

2011-07-06 Thread Steven D'Aprano
Phlip wrote: Note the fix also avoids comparing to None, which, as usual, is also icky and less typesafe! Typesafe? Are you trying to make a joke? -- Steven -- http://mail.python.org/mailman/listinfo/python-list