Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
a little set of functions I've been using to create "glitchy" strings.
import random
def char_error(s):
t = ""
for ch in s:
if random.randrange(6) == 0:
t += chr(ord(ch)+random.choice([-1,1]))
t += ch
return t
def combine(s, t):
u = ""
for ch1, ch2 in zip(s, t):
fate = random.randrange(3)
if fate == 0:
u += chr((ord(ch1)+ord(ch2))/2)
elif fate == 1:
u += ch1
elif fate == 2:
u += ch2
return u
def moire(s, t):
u = ""
for ch1, ch2 in zip(s, t):
if random.randrange(2) == 0:
u += ch1
u += ch2
return u
def halfsies(s, t):
return s[:len(s)/2] + t[len(t)/2:]
def sonic_hedgehog(s, t):
return s[:random.randrange(len(s)/2, len(s))] + t[random.randrange(len(t)/2, len(t)):]
def surface(s, t):
s_seg_len = random.randrange(len(s)/2)
s_start = random.randrange(len(s)-s_seg_len)
t_seg_len = random.randrange(len(t)/2)
t_start = random.randrange(len(t)-t_seg_len)
return s[:s_start] + t[t_start:t_start+t_seg_len] + s[s_start+s_seg_len:]
def seg(s):
seg_len = random.randrange(len(s)/4, len(s)/2)
start = random.randrange(len(s)-seg_len)
return s[start:start+seg_len]
def stuck(s):
t = ""
tmp = None
for ch in s:
if tmp:
t += tmp
if random.randrange(2) == 0:
tmp = None
t += ch
if random.randrange(4) == 0:
tmp = ch
return t
def stuck_seg(s):
return ''.join([seg(s) for i in range(random.randrange(2,6))])
def repeat(s):
return ''.join([s for i in range(random.randrange(2,5))])
def rot13(s):
return s.encode('rot_13')
funcs = {
1: [char_error, char_error, seg, stuck, stuck, stuck_seg, stuck_seg, repeat, rot13],
2: [combine, moire, halfsies, halfsies, surface, surface, sonic_hedgehog]
def glitch(s_list):
strs = random.sample(s_list, 2)
if random.randrange(6) == 0:
strs[0] = random.choice(funcs[1])(strs[0])
if random.randrange(6) == 0:
strs[1] = random.choice(funcs[1])(strs[1])
s = random.choice(funcs[2])(strs[0], strs[1])
if random.randrange(5) == 0:
return random.choice(funcs[2])(s, glitch(s_list))
return s
if __name__ == '__main__':
import sys
strs = sys.argv[1:]
all_lines = list()
for line in sys.stdin:
line = line.strip()
if len(line) > 0:
for i in range(14):
print glitch(all_lines)

This comment has been minimized.

Copy link
Owner Author

@aparrish aparrish commented Feb 2, 2015

Example output, using KJV Genesis chapter 1 as input:

And the eae tof the deep. And the Spirit of God moved upon
the face of the waters.
And Ghe cvening gre t whalrsi gnw ev the ivird cayatur
thhae mniet ,nwhthe mheningere e oug third d y.
And God called the firmament Heaven. And theorning were the
third day.
EnBtWj lgiGnhn FGohtG tatl DtDn megren: ad tGe ditid
And led tae he lor t, hhs n hasfgood: andoG t dihiavd
tho live lfghm then ark ess.
And 3od epiiJ2gij3lDe e@jerGipnnn mj the giCmhDfdnh
And the evening and the morning w for meat: and ie the
fourth day.
And the earth was without form, and void; and darkness was
upon the face of the deep. And the Spirt it was good.
And God said, Let us make man in our image, after our
likeness: and let them have dominion over the fish of the
sea, and over the fowl of the air, and over the cattle, and
over all the earth, and over every creeping thing that
creeind: and God saw that it was good.
And tod marth we wrmhment, and aivivei the watersnehi w s
rp n the tac fofmthend er.mAthethatSpirihicf Ger mavove phe
fhr aaen :f nheitatars.
And God called the dry land Earth; and the gathering
together of the watm be for signs, and for seasons, and for
days, and years:
In the beginning God created thnd God divided the light
from the darkness.
And God said, Let there be heaven to give light upon the
earth: and it was so.
And God made the beast of the earth after his kind, and
cattle ere whose seed was in itself, after his kind: and
God saw that it was good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment