Article View: pl.comp.lang.python
Article #28332Re: Prośba o wytknięcie błędów
From: Artur M. Piwko
Date: Tue, 15 Nov 2011 08:19
Date: Tue, 15 Nov 2011 08:19
61 lines
2725 bytes
2725 bytes
In the darkest hour on Tue, 15 Nov 2011 03:48:34 +0100,
hubert depesz lubaczewski <depesz@depesz.com> screamed:
> 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()
>
W obecnych Pythonach możesz stosować konstrukcję (automagicznie zamknie uchwyt):
def calculate_hash(filename):
with open(filename) as f:
mail = .....
f = open(filename, 'r') to to samo co f = open(filename)
> def visit_fn( hash_dict, path, files ):
> for i in [ 'new', 'tmp' ]:
> if files.count ( i ) > 0:
> files.remove( i )
>
Tu lepiej użyć tupli ('new', 'tmp').
> 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()
>
Alternatywnie (z tym, że trzeba uważać):
hash_dict[checksum] = []
hash_dict = {}
Oprócz konstrukcji: if not d.has_key(k):
można też używać: if k not in d:
Dla mnie ta druga jest czytelniejsza.
> 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 )
Alternatywnie, po prostu: print '#', sha
--
[ Artur M. Piwko : Pipen : AMP29-RIPE : RLU:100918 : From == Trap! : SIG:239B ]
[ 09:10:35 user up 12987 days, 21:05, 1 user, load average: 0.09, 0.04, 0.35 ]
Peace: In international affairs, a period of cheating between two countries.
Technical Details
Message-ID:
<slrnjc4841.8or.milusi.pysiaczek@buziaczek.pl>Path:
mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.gazeta.pl!not-for-mailReferences:
<2h09p8-kgk.ln1@depesz.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.gazeta.pl!not-for-mail From: "Artur M. Piwko" <milusi.pysiaczek@buziaczek.pl> Newsgroups: pl.comp.lang.python Subject: Re: Prośba o wytknięcie błędów Date: Tue, 15 Nov 2011 09:19:13 +0100 Organization: Petroleum Drinkers (; Lines: 61 Message-ID: <slrnjc4841.8or.milusi.pysiaczek@buziaczek.pl> References: <2h09p8-kgk.ln1@depesz.com> Reply-To: milusi.pysiaczek@buziaczek.pl NNTP-Posting-Host: 81.26.9.42 Mime-Version: 1.0 X-Trace: inews.gazeta.pl 1321348086 22040 81.26.9.42 (15 Nov 2011 09:08:06 GMT) X-Complaints-To: usenet@agora.pl NNTP-Posting-Date: Tue, 15 Nov 2011 09:08:06 +0000 (UTC) X-User: pipene User-Agent: slrn/pre1.0.0-18 (Linux) Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit