Skip to content

Instantly share code, notes, and snippets.

@pelf
Last active June 16, 2020 15:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pelf/9ae46949afd6afc8994ba13e7ea79ab4 to your computer and use it in GitHub Desktop.
Save pelf/9ae46949afd6afc8994ba13e7ea79ab4 to your computer and use it in GitHub Desktop.
Sublime plugin for anonymising bank statement data
import sublime, sublime_plugin, random, string, re
class AnonymiseCommand(sublime_plugin.TextCommand):
def replacement(self, s):
# Figure out which chars to use
chars = []
if re.search("[a-z]", s):
chars += string.ascii_lowercase
if re.search("[A-Z]", s):
chars += string.ascii_uppercase
if re.search("\s", s):
chars.append(' ')
if re.search("\d", s):
chars += string.digits
# Generate random text with same length
return ''.join([random.choice(chars) for n in range(len(s))])
def replacement2(self, s):
s2 = ''
for char in s:
if re.match("[a-z]", char):
s2 += random.choice(string.ascii_lowercase)
elif re.match("[A-Z]", char):
s2 += random.choice(string.ascii_uppercase)
elif re.match("\d", char):
s2 += random.choice(string.digits)
else:
s2 += char
return s2
def run(self, edit):
view = self.view
replacements = {}
# Get all the current selections
for region in view.sel():
if not region.empty():
# Get the selected text
s = view.substr(region)
# Check if we have replaced this text on a previous selection
if s in replacements:
# Use previous replacement text
s2 = replacements[s]
else:
# Generate new replacement text
s2 = self.replacement2(s)
replacements[s] = s2
# Replace the current selection
view.replace(edit, region, s2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment