Date: Thu, 2 Apr 2015 22:42:24 +0000 (UTC) From: Eitan Adler <eadler@FreeBSD.org> To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r281007 - head/games/fortune/tools Message-ID: <201504022242.t32MgOib040343@svn.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: eadler Date: Thu Apr 2 22:42:23 2015 New Revision: 281007 URL: https://svnweb.freebsd.org/changeset/base/281007 Log: fortune/tools/do_uniq.py slight imporvements to do_uniq.py: - use with open() - don't depend on sys.argv directly - fix style Modified: head/games/fortune/tools/do_uniq.py Modified: head/games/fortune/tools/do_uniq.py ============================================================================== --- head/games/fortune/tools/do_uniq.py Thu Apr 2 21:55:03 2015 (r281006) +++ head/games/fortune/tools/do_uniq.py Thu Apr 2 22:42:23 2015 (r281007) @@ -4,7 +4,8 @@ # # an aggressive little script for trimming duplicate cookies -import re, sys +import argparse +import re wordlist = [ 'hadnot', @@ -15,6 +16,7 @@ wordlist = [ 'a', 'd', 'i', 'm', 's', ] + def hash(fortune): f = fortune f = f.lower() @@ -27,37 +29,40 @@ def hash(fortune): # f = f[-30:] return f + def edit(datfile): dups = {} fortunes = [] fortune = "" - for line in file(datfile): - if line == "%\n": - key = hash(fortune) - if key not in dups: - dups[key] = [] - dups[key].append(fortune) - fortunes.append(fortune) - fortune = "" - else: - fortune += line + with open(datfile, "r") as datfiledf: + for line in datfiledf: + if line == "%\n": + key = hash(fortune) + if key not in dups: + dups[key] = [] + dups[key].append(fortune) + fortunes.append(fortune) + fortune = "" + else: + fortune += line for key in list(dups.keys()): if len(dups[key]) == 1: del dups[key] - o = file(datfile + '~', "w") - for fortune in fortunes: - key = hash(fortune) - if key in dups: - print('\n' * 50) - for f in dups[key]: - if f != fortune: - print(f, '%') - print(fortune, '%') - if input("Remove last fortune? ") == 'y': - del dups[key] - continue - o.write(fortune + "%\n") - o.close() + with open(datfile + "~", "w") as o: + for fortune in fortunes: + key = hash(fortune) + if key in dups: + print('\n' * 50) + for f in dups[key]: + if f != fortune: + print(f, '%') + print(fortune, '%') + if input("Remove last fortune? ") == 'y': + del dups[key] + continue + o.write(fortune + "%\n") -assert len(sys.argv) == 2 -edit(sys.argv[1]) +parser = argparse.ArgumentParser(description="trimming duplicate cookies") +parser.add_argument("filename", type=str, nargs=1) +args = parser.parse_args() +edit(args.filename[0])
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201504022242.t32MgOib040343>