fortune/tools/do_uniq.py

slight imporvements to do_uniq.py:
	- use with open()
	- don't depend on sys.argv directly
	- fix style
This commit is contained in:
Eitan Adler 2015-04-02 22:42:23 +00:00
parent 16a38a930d
commit 62e8d5e1b0
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=281007

View File

@ -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])