Skip to content

Instantly share code, notes, and snippets.

@elazarl
Last active May 16, 2019
Embed
What would you like to do?
replace UUID with distinguishable emojis
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""uuid4sym - replace UUID version 4 to an easily distinguishable symbol
When looking at a long log file with some UUID4 uuids, you'll have a hard time
distinguishing between two UUIDs.
Both 26cb1b84-7748-11e9-9437-2bf0efaa1b2e and 2725e8de-7748-11e9-a9c8-f3e5d10ab3d6
looks similar to the human eye.
uuid4sym would replace each UUID with a symbol which is easier to distinguish.
uuid4sym replaces the first 50 UUIDs with animals emojis which are not similar.
The next UUIDs will be replaced with UXXX with a ascending serial number and
different color.
You can disable emojis with -e/--no-emojis, and disable colors with -C/--no-colors
"""
import re
import sys
import optparse
COLORS = range(31, 38)
ESC = '\033['
def color(i, txt):
'color returns txt surrounded by terminal escape which colors it with color i'
return '{e}{i}m{txt}{e}0m'.format(e=ESC,
i=COLORS[0] + i % len(COLORS),
txt=txt)
class Animal(object):
'Animal will replace a given UUID, with the same emoji'
animals = 'πŸ­πŸΏπŸ¦”πŸ¦‡πŸπŸ₯πŸ¦πŸ§πŸΉπŸ˜πŸ¦πŸ°πŸ¦πŸ…πŸ¦›πŸ¦πŸ πŸ‘πŸ¦πŸšπŸŒπŸ¦žπŸ¦…πŸ¦‰πŸ¦œπŸ¦’πŸ¦šπŸ‡πŸ¦—πŸŽ β™˜β™žπŸ½πŸΎπŸ‘£πŸ’πŸ¦€πŸˆπŸ€πŸ¦‹πŸπŸžπŸœπŸ›πŸ•·πŸ•ΈπŸ¦‚πŸ¦ŽπŸ¦ŸπŸ¦ πŸ™πŸŒ°πŸŒ±πŸŒ½πŸ¨'
def __init__(self, colors=True, emojis=True):
self.dict = {}
self.i = 0
self.colors = colors
if not emojis:
self.animals = ''
def __repr__(self):
return 'Animal(%d UUIDs)'%self.i
def __str__(self):
return 'Animal(%s)'%self.dict
def size(self):
'size returns the number of different UUIDs stored'
return self.i
def get(self, uuid):
'get accepts UUID and replaced it with always the same symbol'
animal = self.dict.get(uuid)
if not animal:
if self.i >= len(self.animals):
index = self.i - len(self.animals)
text = 'U%03d' % index
if self.colors:
animal = color(index, text)
else:
animal = text
else:
animal = self.animals[self.i]
self.i += 1
self.dict[uuid] = animal
return animal
def main():
'main function, reads from stdin, replaces UUIDv4 with symbols'
parser = optparse.OptionParser(usage="usage: %prog [options]")
parser.add_option("-C",
"--no-colors",
action="store_true",
default=False,
dest='no_colors',
help='do not use terminal colors')
parser.add_option("-e",
"--no-emojis",
action="store_true",
default=False,
dest='no_emojis',
help='do not replace uuid with utf-8 emojis')
options, args = parser.parse_args()
if args:
sys.stderr.write("No args are allowed")
return
animals = Animal(colors=not options.no_colors,
emojis=not options.no_emojis)
uuid_re = re.compile('[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}')
try:
while True:
line = sys.stdin.readline()
if not line:
break
last_i = 0
for match in uuid_re.finditer(line):
start, end = match.span()
sys.stdout.write(line[last_i:start])
sys.stdout.write(animals.get(match.group(0)))
last_i = end
sys.stdout.write(line[last_i:])
sys.stdout.flush()
except KeyboardInterrupt:
sys.stderr.write('Exit by Ctrl-C')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment