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.

TextOddifier.py is a text manipulation script to convert letters in UTF-8 to fairly similar looking letters. It makes text that looks like this ḽӧœĸ ŀįҟԙ ҭԡ༑ṩ ѩŉᵴťӕаԂ. 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':'āăąаѧѩӑӓӕḁẳ⎀', 'B':'ВѢҌ฿ḂḄḆᙗᙫ','b':'ЉЊБЪЬъыьѣҍḃḅḇᒀᖲ', 'C':'ĆĊČСҪℂ℃','c':'ćĉċčсҫ', 'D':'ĎĐḊḌḎḐḒ⫐⫒ᗫᗤ','d':'ďđԀԁԂԃḓḑḏḍḋᑽᖱ', 'E':'ĒĔĖĘĚŒЀЁЄЕѤӔӖԘ⋵⋶ᕮ','e':'ēĕėęěœеѐёѥӕӗԙ', 'F':'ҒӺḞ℉ᖴ','f':'ſғӻẛẜẝḟ', 'G':'ĜĞĠĢԌḠ','g':'ĝğġģḡ', 'H':'ĤĦЊНҔҢҤӇӉԊԢԨԪ','h':'ĥħЂЋђћҕңҥӈӊԡԦԧℎℏ', 'I':'ĨĪĬİĲІЇЮѤѨӀ','i':'ĩīĭįıĳіїѩ༐༑', 'J':'ĲĴЈЉӇԈԒԔᒏ','j':'ĳĵЛјӈԓԕ', 'K':'ĶЌЖКҖҚҜҞҠӃԖԞԪ','k':'ķĸкҗқҝҟҡӄԗԟ', 'L':'ĹĻĽĿŁḶḸḺḼᒺ','l':'ĺļľŀłӏḷḹḻḽ', 'M':'МӍᴍḾṀṂ','m':'ӎᵚᵯmṃṁḿ', 'N':'ŃŅŇŊЍИЙҊӢӤ','n':'ńņňŉŋҋᵰ', 'O':'ŌŎŐŒОЮѺӦ⥀⥁','o':'ōŏőœбоѻӧ', 'P':'РҎԖⰒⰐᑬᑭᑮᑶᑸ','p':'рҏԗᵱⱀᖰ', 'Q':'ҀԚⰢ','q':'ҁԛⱉᖳ', 'R':'ŔŖŘЯԄԆԖԘ℞ᖇ','r':'ŕŗřѓг', 'S':'ŚŜŞŠЅṦṨᔡ','s':'śŝşšѕᵴṩṧṥ', 'T':'ŢŤŦЂЃЋГТѢѢҬԎ','t':'ţťŧҭԏ', 'U':'ŨŪŬŮŰŲ⩂⩁⋃ᙈᙀ','u':'ũūŭůűų', 'V':'ѴѶṼṾⰜ⩢⩣⩛⩝⍱⋁ᐻ','v':'ѵѷṿṽⱌ⩡', 'W':'ŴѠѼѾԜ','w':'ŵѡѽѿԝ⍹', 'X':'ЖХҖӁӜӼӾԔ','x':'жхҗӂӝӽӿԕẋ', 'Y':'ŶŸЎУҮӮӰӲ','y':'ŷуўӯӱӳ', 'Z':'ŹŻŽᤏᤁ','z':'źżžᙇ', '.':'.․⁝⁞', '?':'?‽⁇⁈', '!':'!‽⁉', } 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" print(oddify(s)) input("Press enter when done:")

BaseConverter_py.py 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.

#!python # dozinal.py BASE = 12 PREC = 8 GLYPHS = {} VALUES = {} def compute_glyphs(): global GLYPHS, VALUES 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)): VALUES[GLYPHS[i]] = i 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 frac_parts.append(GLYPHS[part]) while whole > 0: mod = whole % BASE whole = whole // BASE whole_parts.append(GLYPHS[mod]) if len(whole_parts) == 0: whole_parts.append(GLYPHS[0]) if len(frac_parts) == 0: return sign + "".join(reversed(whole_parts)) return sign + "{}.{}".format("".join(reversed(whole_parts)), "".join(frac_parts)) def debase(string): if len(GLYPHS) != BASE: compute_glyphs() if string[0] == '-': negative = True string = string[1:] else: negative = False try: whole_parts, frac_parts = string.split(".") except: 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.")

Renamer.py 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.

# 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)

mcm_page.py 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 HIGHEST_PAGE_NUMBER = 3873 try: f = open('mcm_page_numbers.txt','r') maxpgraw = f.readline() raw = f.readline() f.close() 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): rem_pgs.append(i) else: print("Something went terribly wrong") except: rem_pgs = [i for i in range(1,HIGHEST_PAGE_NUMBER+1)] inchoice = "" 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': print("Saving") f = open('mcm_page_numbers.txt','w') f.write(str(HIGHEST_PAGE_NUMBER)+'\n') f.write(str(rem_pgs)) f.close() if inchoice == 'sc': break inchoice = input('Saved :') continue elif initial == 'p': print(rem_pgs) inchoice = input('Those are the currently loaded indicies :') continue idx = choice(range(len(rem_pgs))) chosen_page = rem_pgs.pop(idx) webbrowser.open('https://www.mcmaster.com/#{}'.format(chosen_page)) inchoice = input('Page {} queued:'.format(chosen_page))