Python Scripts

Over the years I’ve written quite a few python utilities. Most of them are really short, and too specific to be useful to others. Those that aren’t are so long that they don’t feel elegant enough to share.

And then, between these two extremes, are the gems that I find myself coming back to time and again. I thought I’d share these with you. Keep in mind that all of these are covered by the Paul Spooner “IP is Wicked Nonsense” license, and you are encouraged to evaluate them on their own merits as distinct from their history. If you insist that the origins of ideas are important, then go pledge your support to my efforts. is a text manipulation script to convert letters in UTF-8 to fairly similar looking letters. It makes text that looks like this ḽӧœĸ ŀįҟԙ ҭԡ༑ṩ ѩʼnᵴťӕаԂ. You can find the source below, or through the link at the start of the description.

# Similar letter substituter
# To make text look a bit strange

from random import choice

SLD = { 'A':'ĀĂĄАѦѨӐӒӔḀẲⰡ⍲ᗗ','a':'āăąаѧѩӑӓӕḁẳ⎀',

def oddify(text):
    result = ""
    for i in text:
        try: result += choice(SLD[i])
        except: result += i
    return result

s = input("enter the text you want to oddify: ")
#s = "I don't even really know how this is going to work any more"

input("Press enter when done:") is a number base converter that we collaborated on to convert numbers to and from arbitrary radix. The neat thing about it is it works on floating point numbers as well as integers. The example (and test case) is 135.5 converted to base 12 is B3.6. Going the other way around, S4 in base 35 is 984 in decimal! You can find the source below, or through the link at the start of the description.


BASE = 12
PREC = 8

def compute_glyphs():
	GLYPHS = {}
	VALUES = {}

	for i in range(BASE):
		if i < 10: GLYPHS[i] = str(i)
		else: GLYPHS[i] = chr(i+55)

	for i in range(len(GLYPHS)):

def rebase(num):
	if len(GLYPHS) != BASE: compute_glyphs()
	if num < 0:
		sign = '-'
		num = -num
	else: sign = ''
	whole = int(num)
	frac = num - whole

	whole_parts = []
	frac_parts = []
	prec = PREC
	while prec > 0:
		prec -= 1
		frac *= BASE

		part = int(frac)
		frac -= part
		if part == 0: break


	while whole > 0:
		mod = whole % BASE
		whole = whole // BASE

	if len(whole_parts) == 0:
	if len(frac_parts) == 0:
		return sign + "".join(reversed(whole_parts))

	return sign + "{}.{}".format("".join(reversed(whole_parts)),

def debase(string):
	if len(GLYPHS) != BASE: compute_glyphs()
	if string[0] == '-':
		negative = True
		string = string[1:]
	else: negative = False
		whole_parts, frac_parts = string.split(".")
		whole_parts = string
		frac_parts = ''

	whole = 0

	for idx in range(len(whole_parts)):
		whole += VALUES[whole_parts[idx]]
		if idx < len(whole_parts) - 1:
			whole *= BASE

	frac = 0
	max_idx = len(frac_parts) - 1

	for idx in range(len(frac_parts)):
		part = frac_parts[max_idx - idx]
		frac += VALUES[part] / BASE

		if idx < max_idx:
			frac /= BASE

	result = whole + round(frac,PREC)
	if negative: return -result
	return result

TestConvert = 135.5
TestDozenal = rebase(TestConvert)
print("{} converted to base {} is".format(TestConvert,BASE), TestDozenal)
ConvertedBack = debase(TestDozenal)
print("{} in base {} is".format(TestDozenal,BASE), ConvertedBack, "in decimal")
if TestConvert == ConvertedBack: print("success!")
else: print("Something went wrong")
print("Assign BASE to set the number base,\nand PREC to set the precision.\nCall rebase() to convert,\nand debase() to convert back to decimal.") Is a short program I wrote to do simple renaming operations on files. It only operates on the files in the folder, so it’s pretty easy to target. I include it here mostly as a syntax reminder. Change “rename” to “renames” to create directories (folders), which are separated by a forward slash.

# Rename files to add "_"

from os import rename, listdir
thesefiles = listdir()
target = "Prefix"
tlen = len(target)
	for f in thesefiles:
	if f[:tlen] == target:
		n = f
		n = n[:tlen] + '_' + n[tlen:]
		rename(f, n)
		print(n) is kind of an odd one. I really like the McMaster-Carr website, and enjoy browsing their catalog for inspiration. Time was when the catalog was paper that you could turn to a random page and peruse it. However, the online catalog is so efficient at delivering what you want that this becomes difficult. I’d also like to ensure that I don’t keep seeing the same page over again, at least until I’ve gone through the whole catalog. This script does all of that, opening a random page of the catalog when you start it, and allowing a save file listing all the pages you haven’t visited yet. Could be easily modified for other things… monte-carlo webcomic binges for example.

# opens a random McMaster Carr catalog page
from random import choice
import webbrowser

# 3873 fetched on 2017-09-20
# 3939 fetched on 2018-11-29, catalog 124
# 4061 fetched on 2020-11-17, catalog 126

    f = open('mcm_page_numbers.txt','r')
    maxpgraw = f.readline()
    raw = f.readline()
    maxpg = int(maxpgraw)
    rem_pgs = eval(raw)
    if maxpg == HIGHEST_PAGE_NUMBER: pass
    elif maxpg > HIGHEST_PAGE_NUMBER:
        while len(rem_pgs)>0:
            if rem_pgs[-1] > HIGHEST_PAGE_NUMBER: rem_pgs.pop()
            else: break
    elif maxpg < HIGHEST_PAGE_NUMBER:
        for i in range(maxpg,HIGHEST_PAGE_NUMBER+1):
    else: print("Something went terribly wrong")
    rem_pgs = &#91;i for i in range(1,HIGHEST_PAGE_NUMBER+1)&#93;

inchoice = ""
print('{} pages left'.format(len(rem_pgs)))
print('Enter to bring up a McMaster page\n"s" to save, "sc" to save and close, "p" to print')
while len(rem_pgs) > 0:
    printflag = False
    if len(inchoice)!= 0:
        inchoice = inchoice.lower()
        initial = inchoice[0]
        if initial == 's':
            f = open('mcm_page_numbers.txt','w')
            if inchoice == 'sc': break
            else: print('{} pages left'.format(len(rem_pgs)))
            inchoice = input('Saved :')
        elif initial == 'p':
            inchoice = input('Those are the currently loaded indicies :')
    idx = choice(range(len(rem_pgs)))
    chosen_page = rem_pgs.pop(idx)'{}'.format(chosen_page))
    inchoice = input('Page {} queued:'.format(chosen_page))

input("There are no more pages. Press return to close.") Is a dumb little thing that converts characters into their equivalent in the NATO Phonetic Alphabet. More of a fun toy than a useful tool.

#NATO Phonetic Alphabetizer

SLD = { 'A':'Alfa',

def phoneticize(text):
    result = []
    for i in text.upper():
        try: result += [SLD[i]]
        except: result += [i]
    return " ".join(result)

    s = input("enter text: ")
    #s = "I don't even really know how this is going to work any more"


I have enjoyed playing with the Fantasy Genesis book for several years now, and at one point wrote this python script to cut out all the dice rolling.

# A Fantasy Genesis Nested Lists

# The Lists:
# Anima
Anim_1_Sea_Life = ["Mollusk",
                   "Crab, Lobster",
                   "Squid, Mudskipper",
                   ["Fish: Deep sea", "Fish: Fresh water"],
                   [["Whale", "Killer Whale", "Narwhale"], "Dolphin", "Walrus", "Otter"],
                   ["Shell: spiral", "Conch", "Shell: ribbed, clam"],
                   "Eel, Leech, Hagfish",
                   "Coral, Anemone",
                   "Shark, ray"]

Anim_2_Insect = ["Worm",
                 "Moth, Butterfly",
                 "Fly, dragonfly",
                 "Lotus, Mantis",
                 "Bee, Wasp",
                 "Caterpillar, Centipede, Milipede",
                 "Beetle, Scarab",
                 "Flea, Mite",

Anim_3_Mammal = ["Sheep, cow",
                 "Mouse, Rabbit",
                 "Pig, Boar",
                 "Deer, Pronghorn",
                 "Ram, Bull, Buck",
                 "Elephant, Giraffe",
                 "Lupine: Wild Dog",
                 "Horse, Zeebra",
                 "Feline: Wild Cat",

Anim_4_Reptile = ["Crocodile, Gila",
                  "Frog, Newt",
                  "Lizard, Snake",

Anim_5_Bird = ["Wild Fowl, Duck, Goose",
               "Farm Fowl, Rooster",
               "Seabird, Penguin, Gull",
               "City bird: Raven, Sparrow",
               "Tropical bird: Parrot, Heron",
               "Bird of Prey: Hawk, Owl",

# Veggie

Vegi_1_Plant = ["Seaweed",
                "Desert Cacti",
                "Thick Leaf Plant, Jade",
                "Flower: Domestic",
                "Grass, Dandelion",
                "Flower: Wild",
                "Carnivorous Plant"]

Vegi_2_Fruit_Vegi = ["Asparagus",
                     "Berry, Grapes",
                     "Tree fruit (apple, orange)",
                     "Pumpkin, Gourd",
                     "Broccoli, Artichoke",
                     "Grain, Wheat",

Vegi_3_Fungi = ["Moss",
                "Slime Fungi: Ooze, Jelly",

Vegi_4_Tree = ["Willow",
               "Maple, Oak",

# Elemental & Mineral

Elem_1_Fire_Electric = ["Fire, Vapor",
                        "Electric Bolt",
                        "Ember, Hot Coal",
                        "Molten Lava"]

Elem_2_Liquid = ["Icicles",
                 "Fog, Vapor",
                 "Dew Drops",
                 "Frost, Snow",
                 "Suds, Bubbles",
                 "Tar, Gum"]

Elem_3_Earth_Metal = ["Malachite",
                      "Mountain, Cliff Face",
                      "Brick, Cobblestone",
                      "Rust, Oxide",
                      "Cracked Clay",
                      "Stalactite, Stalagmite",
                      "Glass, Crystals",
                      "Powder, Sand",
                      "Slate, Shale",
                      "Cement, Sediment",
                      "Mercury, Chrome"]

Elem_4_Astral_Atmosphere = ["Moon Cycles",
                            "Crater, Asteroid",
                            "Solar Flare",
                            "Galaxy form",
                            "Planets, Saturn's Rings",
                            "Cloud, Cyclone, Turbulence"]


Tech_1_Transportation = ["Car, Truck, Bus",
                         "Rail, Train, Trolley",
                         "Cycle (motor or bi)",
                         "Sled, Ski",
                         "Boat, Ship",
                         "Tank, Caterpillar Tread"]

Tech_2_Architecture = [["Ornament, Gargoyle", "Ornament, Pillar"],
                       "Bridge, Framework",
                       "Castle, Dome",
                       "Modern Skyscraper",
                       ["Place of Worship", "Totem", "Cathedral", "Temple"],
                       ["Doorway, Archway", "Window"],
                       "Old Village, Cottage",

Tech_3_Tool = ["Drill",
               ["Cup", "Bowl", "Plate", "Silverware"],
               "Bundle, Bale",
               "Hammer, Axe",
               "Brush: Hair, Tooth",
               "Razor, Knife",
               "Spigot, Faucet",
               "Lock, Key",
               "Adhesive, Bandage",
               "Shovel, Pick",
               "Capsule, Tablet",
               "Nuts, Bolts",
               "Thread, Stitch",
               "Shears, Scissors",
               "Pen, Paintbrush",
               "Spring, Coil",
               "Tube, Plumbing",
               "Wrench, Pliers"]

Tech_4_Machine = [["Switch", "Dial, Knob", "Button", "Lever", "Foot Pedal"],
                  ["Bulb, Lamp", "Arc lamp", "Spotlight"],
                  ["Clock, Gears", "Piston, Cylinder"],
                  ["Mechanical Saw","Laser Beam"],
                  ["Reactor Core", "Engine", "Solar Panel"],
                  ["Microchip", "Circuit Board", "Network Room, Cables"],
                  "Dish Antenna",
                  ["Rocket", "Turbine", "Fan, Propeller"]]

#Character Elements

Emotions = ["Embarrassed",
            "Timid, Bashfull",
            "Giggle, Smiling",
            "Squint, Wink",
            "Stressed, Fatigued",
            "Thought, Meditation",
            "Insane, Berserker",
            "Insane, Happy",
            "Pining, Furrowed",
            "Laughing, Hysterical",
            "Attentive, Shock",
            "Stern, Grumpy",
            "Clenched Teeth",
            "Gape, Gawk",
            "Paranoid, Shifty",
            "Bliss, Joy",

Actions = ["Recoil, Akimbo",
           "Drenched, Thirst",
           "Blown by Cyclone",
           "Push, Pull",
           "Snoop, Listen",
           "Crouched for Attack",
           "Hang, Climb",
           "Recoil, Head/Torso",
           "Float, Levitate",
           "Swinging Weapon",
           "Twisting, Stretching",
           "Kicking, Punching",
           "Squeeze, Tackle",
           "Absorb, Eat",
           "Limp, Injured",
           "Curse, Swear",
           "Run, Jump",
           "Melt, Glow, Fire",
           "Stuck, Trapped",
           "Pull, Push",
           "Shoot Weapon",
           "Dying, Gaunt",
           "Fly, Swim",
           "Shed, Molting",
           "Chant, Recite",
           "Crawl, Emerge"]

# Compliations of the different Sets

Anima = [Anim_1_Sea_Life,

EleMineral = [Elem_1_Fire_Electric,

Veggie = [Vegi_1_Plant,

Techne = [Tech_1_Transportation,

Visual_Elements = [Anima, Veggie, EleMineral, Techne]

Character_Elements = [Emotions, Actions]

All_Things = [Visual_Elements, Character_Elements]

from random import choice

def RecurseChoice(Root):
    # Randomly select from nested lists until you get to
    # a string instead of a list. Return the string.
    if isinstance(Root,str):
        return Root
        Sub_Root = choice(Root)
        return RecurseChoice(Sub_Root)

#Choose one from each category

print("Anima: ", end='')
print("Veggie: ", end='')
print("EleMineral: ", end='')
print("Techne: ", end='')

print("Emotions: ", end='')
print("Actions: ", end='')

# Some test stuff

# Run it a ton of times looking for an entry
# or just checking for a crash, that too.
Search_target = "Narwhale"
Number_of_tries = 4000
for i in range(Number_of_tries):
    result = RecurseChoice(All_Things)
    if result == Search_target: print("Target found! Try #{}".format(i))


I’ve uploaded a bunch of my python files here:
They are also on GitHub:

Leave a Reply

Your email address will not be published. Required fields are marked *