From d4b6b723d9bf32df3c6402544a483de1415a8c40 Mon Sep 17 00:00:00 2001 From: Glyph Date: Sat, 12 Sep 2015 13:25:23 -0700 Subject: [PATCH 1/2] support for incrementally transcoding a stream --- alphaglyph.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/alphaglyph.py b/alphaglyph.py index b0d88b7..1a3735d 100644 --- a/alphaglyph.py +++ b/alphaglyph.py @@ -61,3 +61,25 @@ def iter_transcode(s): def transcode(s): return ''.join([a for a in iter_transcode(s)]) + + +def transcode_stream(instream, outstream): + buf = b'' + while True: + octet = instream.read(1) + buf += octet + try: + text = buf.decode('utf-8') + except UnicodeDecodeError: + pass + else: + buf = b'' + outstream.write(transcode(text)) + outstream.flush() + if not octet: + break + + +if __name__ == '__main__': + import sys + transcode_stream(sys.stdin, sys.stdout) From 26470d851a15d8c45e2f748f50d62c3d50902085 Mon Sep 17 00:00:00 2001 From: Glyph Date: Sat, 12 Sep 2015 13:25:31 -0700 Subject: [PATCH 2/2] reverse mapping as well as forward mapping make encoding and decoding the same --- alphaglyph.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/alphaglyph.py b/alphaglyph.py index 1a3735d..d8209a0 100644 --- a/alphaglyph.py +++ b/alphaglyph.py @@ -50,6 +50,9 @@ CODEX = { 'x': (u'\u03C9', 'omega') } +for latin_letter, (greek_letter, name) in CODEX.items(): + CODEX[greek_letter] = (latin_letter, latin_letter) + def iter_transcode(s):