Mercurial > repo
annotate bin/rot13 @ 11419:5455769afdd1
<oerjan> undo 11380
author | HackBot |
---|---|
date | Wed, 28 Feb 2018 04:08:06 +0000 |
parents | 6412d84cf279 |
children |
rev | line source |
---|---|
11112
6412d84cf279
<oerjan> sled bin/rot13//1c#!/hackenv/bin/shebang_args_or_input python
HackBot
parents:
11111
diff
changeset
|
1 #!/hackenv/bin/shebang_args_or_input python |
11111 | 2 |
3 import re, sys, unicodedata as U | |
4 def tr(c): | |
5 m = re.match(r'(.*\bLATIN\b.*\bLETTER )([A-Z])\b(.*)', U.name(c, '')) | |
6 if m: | |
7 p, q, r = m.groups() | |
8 n = ord(q) - ord('A') | |
9 try: | |
10 return U.lookup(p + chr(ord('A') + (n + 13) % 26) + r) | |
11 except KeyError: | |
12 return c | |
13 return c | |
14 | |
15 def tr2(c): | |
16 d = tr(c) | |
17 if d != c: | |
18 return d | |
19 else: | |
20 cs = [unichr(int(x, 16)) for x in U.decomposition(c).split()] | |
21 ds = [tr(x) for x in cs] | |
22 if cs != ds: | |
23 return u''.join(ds) | |
24 else: | |
25 return c | |
26 | |
27 for c in unicode(sys.stdin.read(), 'utf8'): | |
28 sys.stdout.write(tr2(c).encode('utf8')) | |
29 |