🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: pl.comp.lang.python
Article #28341

Re: Prośba o wytknięcie błędów

#28341
From: Adam Przybyla
Date: Tue, 15 Nov 2011 09:09
76 lines
3097 bytes
hubert depesz lubaczewski <depesz@depesz.com> wrote:
> elo
> jako pierwsze coś napisałem program wyszukujący duplikaty maili.
> 
> funkcjonalność jest relatywnie mało istotna - chodzi mi o to czy
> moglisbyście mi powiedzieć co zrobiłem nie po pythonowemu, lub co można
> zrobić lepiej/bardziej pythonowo.
> 
> dzięki,
> 
> depesz
> 
> 
> ---------
> #!/usr/bin/env python
> from os.path import walk, join, getsize, isfile, basename
> import email
> import sys
> from hashlib import sha256
> from pipes import quote
> import pprint
> 
> def calculate_hash( filename ):
>    f = open( filename, 'r' )
>    mail = email.message_from_file( f )
>    keep_headers =  ( 'subject', 'from', 'to', 'cc', 'in-reply-to', 'references', 'date', 'message-id', 'content-type', 'mime-version' )
>    for h in mail.keys():
>        if h.lower() not in keep_headers:
>            del mail[ h ]
>    return sha256( mail.as_string() ).hexdigest()
	... mozna to napisac krocej:
def calculate_hash( filename ):
	mail=email.message_from_file(open(filename))
	[mail.__delitem__(h) for h in mail.keys() if h.lower not in keep_headers]
	return sha256( mail.as_string() ).hexdigest()

> 
> def visit_fn( hash_dict, path, files ):
>    for i in [ 'new', 'tmp' ]:
>        if files.count ( i ) > 0:
>            files.remove( i )
> 
>    if basename( path ) != 'cur':
>        return
> 
>    for file in files:
>        full_path=join( path, file )
> 
>        if not isfile( full_path ):
>            continue
> 
>        if getsize( full_path ) > 10 * 1024 * 1024:
>            continue
> 
>        checksum = calculate_hash( full_path )
>        if not hash_dict.has_key( checksum ):
>            hash_dict[ checksum ] = list()
>        hash_dict[ checksum ].append( full_path )
> 
> 
> hash_dict = dict()
> 
> walk( sys.argv[1], visit_fn, hash_dict )
> 
> for i in hash_dict.keys():
>    if len( hash_dict[i] ) == 1:
>        del hash_dict[i]
> 
> for sha in hash_dict.keys():
>    print "# %s" % ( sha )
>    print "# keep: %s" % ( hash_dict[ sha ][0] )
>    print "rm", " ".join( [ quote(i) for i in hash_dict[ sha ][1:] ] )
>    print
> ---------
> Z powazaniem
								Adam Przybyla
Technical Details
Message-ID: <j9ta7o$7ae$2@polsl.pl>
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.icm.edu.pl!polsl.pl!not-for-mail
References: <2h09p8-kgk.ln1@depesz.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.icm.edu.pl!polsl.pl!not-for-mail
From: Adam Przybyla <adam@rybnik.pl>
Newsgroups: pl.comp.lang.python
Subject: Re: Prośba o
 wytknięcie błędów
Date: Tue, 15 Nov 2011 09:09:12 +0000 (UTC)
Organization: Politechnika Slaska, Gliwice
Lines: 76
Sender: Adam Przybyla <adam@smtp.ertel.com.pl>
Message-ID: <j9ta7o$7ae$2@polsl.pl>
References: <2h09p8-kgk.ln1@depesz.com>
NNTP-Posting-Host: smtp.ertel.com.pl
Mime-Version: 1.0
X-Trace: polsl.pl 1321348152 7502 62.182.231.178 (15 Nov 2011 09:09:12 GMT)
X-Complaints-To: news@polsl.pl
NNTP-Posting-Date: Tue, 15 Nov 2011 09:09:12 +0000 (UTC)
User-Agent: tin/1.8.3-20070201 ("Scotasay") (UNIX) (Linux/2.6.18-194.8.1.el5xen (i686))
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit