Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Does hashlib support a file mode?

Reply
Thread Tools

Re: Does hashlib support a file mode?

 
 
Carl Banks
Guest
Posts: n/a
 
      07-06-2011
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 principle; most of us acknowledge it as small blip in an otherwise straightforward and clean language. (Incidentally, fixing it would create different surprises, butprobably much less common ones.)

We've helped you with your problem, but you risk alienating those who helped you when you badmouth the whole language on account of this one thing, and you might not get such prompt help next time. So try to be nice.

You are wrong about Python bending the definition of "call", though. Surprising though it be, the Python language is very explicit that the default arguments are executed only once, when creating the function, *not* when calling it.


Carl Banks

 
Reply With Quote
 
 
 
 
Phlip
Guest
Posts: n/a
 
      07-06-2011
On Jul 6, 1:25*pm, Carl Banks <(E-Mail Removed)> 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:


def file_to_hash(path, hash_type=hashlib.md5):
"""
Per: http://groups.google.com/group/comp....1c46f77ac1738c
"""

hash = hash_type()

with open(path, 'rb') as f:

while True:
s = f.read(8192) # CONSIDER: io.DEFAULT_BUFFER_SIZE
if not s: break
hash.update(s)

return hash.hexdigest()

Note the fix also avoids comparing to None, which, as usual, is also
icky and less typesafe!

(And don't get me started about the extra lines needed to avoid THIS
atrocity!

while s = f.read(8192):
hash.update(s)


 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      07-06-2011
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

 
Reply With Quote
 
Andrew Berg
Guest
Posts: n/a
 
      07-07-2011
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 sound of ducks. Maybe he just weighs the same
as a duck. In any case, duck tolerance is necessary to use Python
effectively.


On a side note, it turns out there's no word for the fear of ducks. The
closest phobia is anatidaephobia, which is the fear of being /watched/
by a duck.
 
Reply With Quote
 
Phlip
Guest
Posts: n/a
 
      07-07-2011
> 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.

 
Reply With Quote
 
Andrew Berg
Guest
Posts: n/a
 
      07-07-2011
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'>
 
Reply With Quote
 
Phlip
Guest
Posts: n/a
 
      07-07-2011
On Jul 7, 6:24*am, Andrew Berg <(E-Mail Removed)> wrote:
> On 2011.07.07 08:11 AM, Phlip wrote:> No, I was pointing out that passinga type is more ... typesafe.
>
> None is a type.


I never said it wasn't.
 
Reply With Quote
 
Andrew Berg
Guest
Posts: n/a
 
      07-07-2011
On 2011.07.07 08:39 AM, Phlip wrote:
> On Jul 7, 6:24 am, Andrew Berg <(E-Mail Removed)> 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?

def file_to_hash(path, m=None):
if m is None:
m = hashlib.md5()

What's not a type? The is operator compares types (m's value isn't the
only thing compared here; even an separate instance of the exact same
type would make it return False), and m can't be undefined.
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      07-08-2011
Andrew Berg wrote:

> On 2011.07.07 08:39 AM, Phlip wrote:
>> On Jul 7, 6:24 am, Andrew Berg <(E-Mail Removed)> 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 isn't.

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 'NoneType'>
>>> isinstance(type(None), type) # is NoneType itself a type?

True


> You are talking about this code, right?
>
> def file_to_hash(path, m=None):
> if m is None:
> m = hashlib.md5()
>
> What's not a type? The is operator compares types (m's value isn't the
> only thing compared here; even an separate instance of the exact same
> type would make it return False), and m can't be undefined.


The is operator does not compare types, it compares instances for identity.
There is no need for is to ever care about the type of the arguments --
that's just a waste of time, since a fast identity (memory location) test
is sufficient.

This is why I initially thought that Phlip was joking when he suggested
that "m is None" could be type-unsafe. It doesn't matter what type m
has, "m is <anything>" will always be perfectly safe.




--
Steven

 
Reply With Quote
 
Andrew Berg
Guest
Posts: n/a
 
      07-08-2011
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 'NoneType'>
> >>> isinstance(type(None), type) # is NoneType itself a type?

> True

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.
> There is no need for is to ever care about the type of the arguments --
> that's just a waste of time, since a fast identity (memory location) test
> is sufficient.

"Compare" was the wrong word. I figured the interpreter doesn't
explicitly compare types, but obviously identical instances are going to
be of the same type.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Does hashlib support a file mode? Phlip Python 5 07-07-2011 12:13 PM
Re: Does hashlib support a file mode? Phlip Python 6 07-06-2011 06:42 PM
Does hashlib support a file mode? Phlip Python 6 07-06-2011 01:47 PM
my small hashlib - using pythons hash-functions Mathias Panzenboeck Python 5 11-26-2006 12:18 AM
python 2.5 and hashlib rtilley Python 2 02-11-2006 08:19 PM



Advertisments