Using filepath method to identify an .html page

MRAB python at mrabarnett.plus.com
Tue Jan 22 19:49:45 CET 2013


On 2013-01-22 18:26, Ferrous Cranus wrote:
> Τη Τρίτη, 22 Ιανουαρίου 2013 6:11:20 μ.μ. UTC+2, ο χρήστης Chris Angelico έγραψε:
>> On Wed, Jan 23, 2013 at 2:59 AM, Ferrous Cranus <nikos.gr33k at gmail.com> wrote:
>>
>> > I just need a way to CONVERT a string(absolute path) to a 4-digit unique number with INT!!! That's all i want!! But i cannot make it work :(
>>
>> Either you are deliberately trolling, or you have a major
>> comprehension problem. Please go back and read, carefully, all the
>> remarks you've been offered in this thread. Feel free to ask for
>> clarification of anything that doesn't make sense, but be sure to read
>> all of it. You are asking something that is fundamentally
>> impossible[1]. There simply are not enough numbers to go around.
>>
>> ChrisA
>>
>> [1] Well, impossible in decimal. If you work in base 4294967296, you
>>
>> could do what you want in four "digits".
>
> Fundamentally impossible?
>
Yes.

> Well....
>
> OK: How about this in Perl:
>
> $ cat testMD5.pl
> use strict;
>
> foreach my $url(qw@ /index.html /about/time.html @){
>          hashit($url);
> }
>
> sub hashit {
>     my $url=shift;
>     my @ltrs=split(//,$url);
>     my $hash = 0;
>
>     foreach my $ltr(@ltrs){
>          $hash = ( $hash + ord($ltr)) %10000;
>     }
>     printf "%s: %0.4d\n",$url,$hash
>
> }
>
>
> which yields:
> $ perl testMD5.pl
> /index.html: 1066
> /about/time.html: 1547
>
That shortens the int to 4 digits.

A hash isn't guaranteed to be unique. A hash is an attempt to make an
int which is highly sensitive to a change in the data so that a small
change in the data will result in a different int. If the change is big
enough it _could_ give the same int, but the hope is that it probably
won't. (Ideally, if the hash has 4 decimal digits, you'd hope that the
chance of different data giving the same hash would be about 1 in
10000.)



More information about the Python-list mailing list