cove-scripts/count_parentheses.py
Joscha 0f35fa3701 Print copy-paste-ready parentheses for balancing
Though capped at 80 parentheses per type, if you don't supply --all.
2025-10-24 02:01:40 +02:00

52 lines
1.4 KiB
Python

import json
from argparse import ArgumentParser
from pathlib import Path
from typing import Counter
PARENS = list("()[]{}<>")
SMILEYS = [":D", "D:"]
def print_a_bunch(what: str, how_much: int, all: bool) -> None:
while how_much > 0:
print(what * min(80, how_much))
if not all:
break
how_much -= 80
def balance(count: Counter[str], a: str, b: str, all: bool) -> None:
if count[a] > count[b]:
print_a_bunch(b, count[a] - count[b], all)
if count[b] > count[a]:
print_a_bunch(a, count[b] - count[a], all)
def main() -> None:
parser = ArgumentParser()
parser.add_argument("path", type=Path)
parser.add_argument("--all", "-a", action="store_true")
args = parser.parse_args()
count = Counter[str]()
with open(args.path) as f:
print("Reading lines")
for line in f:
content: str = json.loads(line)["content"]
count += Counter(content)
for smiley in SMILEYS:
count[smiley] += content.count(smiley)
for paren in PARENS:
print(f" {paren} - {count[paren]:6}")
for smiley in SMILEYS:
print(f"{smiley} - {count[smiley]:6}")
balance(count, "(", ")", args.all)
balance(count, "[", "]", args.all)
balance(count, "{", "}", args.all)
balance(count, "<", ">", args.all)
if __name__ == "__main__":
main()