User talk:Darkfrost

From Discworld MUD Wiki
Revision as of 19:42, 15 June 2017 by Darkfrost (Talk | contribs) (sorting script for Dead letter office jobs: indentation)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Tools

sorting script for Dead letter office jobs

Usage: Mark range in editor, pipe through, check.
Keep number of regexes low.
Changes: added aka
#!/usr/bin/env python3

import sys
import re

if len(sys.argv) > 1:
    iname = sys.argv[1]
else:
    iname = "/dev/stdin"

npc = None
cities = dict()

class NPC():
    def __init__(self, name=None, city=None, place=None, aka=None):
        self.name = name
        self.plain_name = self.name.strip("'")
        self.city = city
        self.place = place
        self.aka = []

for line in open(iname).readlines():
    line = line.lstrip(" ").rstrip("\n ")
    if line.startswith("|-"):
        npc = None
        continue
    elif line.startswith("|}"):
        npc = None
        continue
    elif line.startswith("|"):
        line = line.lstrip("| ")
        if npc is None:
            npc = NPC(name=line)
            continue
        else:
            _ = re.split('(, | \()', line)
            npc.city = _[0]
            if len(_) > 1:
                npc.place = "".join(_[1:])
            else:
                npc.place = None
            if npc.city not in cities.keys():
                cities.update({npc.city: [npc]})
            else:
                cities[npc.city] = cities[npc.city] + [npc]
            npc = None
    elif line.startswith("*"):
        npc.aka.append(line)

for city in sorted(cities, key=lambda x: x.lower()):
    for npc in sorted(cities[city], key=lambda x: x.plain_name.lower()):
        record = " |-\n |{}\n".format(npc.name)
        for aka in npc.aka:
            record += "{}\n".format(aka)
        record += " |{}".format(npc.city)
        if npc.place is not None:
            record += npc.place
        print(record)