Article View: pl.comp.lang.python
Article #28334Re: Prośba o wytknięcie błędów
From: hubert depesz lubaczewski
Date: Tue, 15 Nov 2011 14:12
Date: Tue, 15 Nov 2011 14:12
73 lines
3151 bytes
3151 bytes
On 2011-11-15, Artur M. Piwko <milusi.pysiaczek@buziaczek.pl> wrote:
> 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 = .....
a to powyżej nie zamknie uchwytu po wyjściu z funkcji?
> f = open(filename, 'r') to to samo co f = open(filename)
pewnie tak, ale akurat wolę explicite podawać tryb otwarcia pliku, takie
zboczenie.
>> 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').
czemu lepiej?
>> 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 = {}
na co uważać?
> 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.
czy one są równoważne? w szczególności - czy ich wydajność będzie taka
sama jak d ma np. 10M elementów?
>> 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
dzięki za wszystkie uwagi.
depesz
--
The best thing about modern society is how easy it is to avoid contact with it.
http://depesz.com/
Technical Details
Message-ID:
<1j8ap8-uiv.ln1@depesz.com>Path:
mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!agh.edu.pl!news.agh.edu.pl!news.onet.pl!.POSTED!depesz.com!not-for-mailReferences:
<2h09p8-kgk.ln1@depesz.com> <slrnjc4841.8or.milusi.pysiaczek@buziaczek.pl>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!agh.edu.pl!news.agh.edu.pl!news.onet.pl!.POSTED!depesz.com!not-for-mail From: hubert depesz lubaczewski <depesz@depesz.com> Newsgroups: pl.comp.lang.python Subject: Re: Prośba o wytknięcie błędów Date: Tue, 15 Nov 2011 15:12:17 +0100 Organization: depesz Software Lines: 73 Message-ID: <1j8ap8-uiv.ln1@depesz.com> References: <2h09p8-kgk.ln1@depesz.com> <slrnjc4841.8or.milusi.pysiaczek@buziaczek.pl> Reply-To: depesz@depesz.com NNTP-Posting-Host: andy.depesz.com Mime-Version: 1.0 X-Trace: news.onet.pl 1321366803 27348 88.198.47.100 (15 Nov 2011 14:20:03 GMT) X-Complaints-To: niusy@onet.pl NNTP-Posting-Date: Tue, 15 Nov 2011 14:20:03 +0000 (UTC) User-Agent: slrn/pre1.0.0-18 (Linux) Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit