-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreplace_x.py
50 lines (36 loc) · 1.16 KB
/
replace_x.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python3
"""Replace all code letters with X.
From ITT 2016 - Kevlin Henney - Seven Ineffective
Coding Habits of Many Programmers (https://youtu.be/ZsHMHukIlJY?t=1260),
Code samples are shown and then replaced with all X's
to show identation and alignement.
This script removes all punctuations and replaces
all characteters with X's.
"""
import re
import sys
import string
punctuations = string.punctuation
punctuations = r"[{}]".format(punctuations)
PATTERNS_REPLACEMENTS = ((r'\w', 'X',), (punctuations, '',),)
def read_text():
"""Read file, if no file provided, use a sample."""
try:
file_ = sys.argv[1]
with open(file_, 'r') as f:
text = f.read()
except IndexError:
text = """lambda x: x+1"""
return text
def replacer(pattern, replace, text):
"""Substitution wrapper from the re module."""
return re.sub(pattern, replace, text)
def main():
"""Replace input with X's."""
text = read_text()
new_text = text
for pattern, replace in PATTERNS_REPLACEMENTS:
new_text = replacer(pattern, replace, new_text)
return (new_text, text)
if __name__ == "__main__":
print(main()[0])