Skip to content

Commit

Permalink
extract a filenames processing func
Browse files Browse the repository at this point in the history
I wish to implement a "--fortune-mod-dwim" flag
  • Loading branch information
shlomif committed Nov 15, 2024
1 parent 84ecf64 commit 04ac560
Showing 1 changed file with 47 additions and 38 deletions.
85 changes: 47 additions & 38 deletions fortune-mod/util/find_duplicate_fortunes.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,45 +39,54 @@
'''

locations_by_text = {}

for filename in sys.argv:
with open(filename) as fh:
text = ""
startlineno = 1

for lineno, line in enumerate(fh, 1):
if line == "%\n":
def files_processing_transaction(filenames_list):
"""docstring for files_processing_transaction"""

locations_by_text = {}

for filename in filenames_list:
with open(filename) as fh:
text = ""
startlineno = 1

for lineno, line in enumerate(fh, 1):
if line == "%\n":
if text not in locations_by_text:
locations_by_text[text] = []
locations_by_text[text].append(
(filename, startlineno, lineno)
)
text = ""
startlineno = lineno + 1
else:
text += line

if text:
if text not in locations_by_text:
locations_by_text[text] = []
locations_by_text[text].append((filename, startlineno, lineno))
text = ""
startlineno = lineno + 1
else:
text += line

if text:
if text not in locations_by_text:
locations_by_text[text] = []
locations_by_text[text].append((filename, startlineno, lineno))

byfn = {}
for text, locations in locations_by_text.items():
if len(locations) > 1:
print(f"Multiple occurrences of '{text.__repr__()[:60]}':")
for filename, startlineno, lineno in locations[1:]:
if filename not in byfn:
byfn[filename] = []
byfn[filename].append((startlineno, lineno))
# print(f"{filename}:{startlineno}:{lineno}")

for filename, matches in byfn.items():
m = list(reversed(sorted(matches)))
print(filename, m)
with open(filename) as fh:
lines = fh.readlines()
for start, end in m:
lines = lines[:(start - 1)] + lines[(end+0):]
with open(filename, "wt") as fh:
for li in lines:
fh.write(li)

byfn = {}
for text, locations in locations_by_text.items():
if len(locations) > 1:
print(f"Multiple occurrences of '{text.__repr__()[:60]}':")
for filename, startlineno, lineno in locations[1:]:
if filename not in byfn:
byfn[filename] = []
byfn[filename].append((startlineno, lineno))
# print(f"{filename}:{startlineno}:{lineno}")

for filename, matches in byfn.items():
m = list(reversed(sorted(matches)))
print(filename, m)
with open(filename) as fh:
lines = fh.readlines()
for start, end in m:
lines = lines[:(start - 1)] + lines[(end+0):]
with open(filename, "wt") as fh:
for li in lines:
fh.write(li)


files_processing_transaction(filenames_list=sys.argv)

0 comments on commit 04ac560

Please sign in to comment.