Skip to content

Commit a7116f0

Browse files
committed
Added new codec: braille
1 parent 838e5fc commit a7116f0

4 files changed

Lines changed: 123 additions & 1 deletion

File tree

codext/VERSION.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.1.1
1+
1.2.0

codext/languages/braille.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# -*- coding: UTF-8 -*-
2+
"""Braille Codec - braille content encoding.
3+
4+
This codec:
5+
- en/decodes strings from str to str
6+
- en/decodes strings from bytes to bytes
7+
- decodes file content to str (read)
8+
- encodes file content from str to bytes (write)
9+
"""
10+
from codext.__common__ import *
11+
12+
13+
if PY3:
14+
ENCMAP = {
15+
# digits
16+
'0': '⠴', '1': '⠂', '2': '⠆', '3': '⠒', '4': '⠲', '5': '⠢', '6': '⠖',
17+
'7': '⠶', '8': '⠦', '9': '⠔',
18+
# letters
19+
'a': '⠁', 'b': '⠃', 'c': '⠉', 'd': '⠙', 'e': '⠑', 'f': '⠋', 'g': '⠛',
20+
'h': '⠓', 'i': '⠊', 'j': '⠚', 'k': '⠅', 'l': '⠇', 'm': '⠍', 'n': '⠝',
21+
'o': '⠕', 'p': '⠏', 'q': '⠟', 'r': '⠗', 's': '⠎', 't': '⠞', 'u': '⠥',
22+
'v': '⠧', 'w': '⠺', 'x': '⠭', 'y': '⠽', 'z': '⠵',
23+
# punctuation
24+
' ': '⠀', '!': '⠮', '"': '⠐', '#': '⠼', '$': '⠫', '%': '⠩', '&': '⠯',
25+
':': '⠱', ';': '⠰', '<': '⠣', '=': '⠿', '>': '⠜', '?': '⠹', '@': '⠈',
26+
"'": '⠄', '(': '⠷', ')': '⠾', '*': '⠡', '+': '⠬', ',': '⠠', '-': '⠤',
27+
'.': '⠨', '/': '⠌', '[': '⠪', '\\': '⠳', ']': '⠻', '^': '⠘',
28+
'_': '⠸',
29+
}
30+
DECMAP = {v: k for k, v in ENCMAP.items()}
31+
REPLACE_CHAR = "?"
32+
33+
34+
class BrailleError(ValueError):
35+
pass
36+
37+
38+
class BrailleDecodeError(BrailleError):
39+
pass
40+
41+
42+
class BrailleEncodeError(BrailleError):
43+
pass
44+
45+
46+
def braille_encode(text, errors="strict"):
47+
r = ""
48+
for i, c in enumerate(ensure_str(text)):
49+
try:
50+
r += ENCMAP[c]
51+
except KeyError:
52+
if errors == "strict":
53+
raise BrailleEncodeError("'braille' codec can't encode "
54+
"character '{}' in position {}"
55+
.format(c, i))
56+
elif errors == "replace":
57+
r += REPLACE_CHAR
58+
elif errors == "ignore":
59+
continue
60+
else:
61+
raise ValueError("Unsupported error handling {}"
62+
.format(errors))
63+
return r, len(text)
64+
65+
66+
def braille_decode(text, errors="strict"):
67+
r = ""
68+
for i, c in enumerate(ensure_str(text)):
69+
try:
70+
r += DECMAP[c]
71+
except KeyError:
72+
if errors == "strict":
73+
raise BrailleDecodeError("'braille' codec can't decode "
74+
"character '{}' in position {}"
75+
.format(c, i))
76+
elif errors == "replace":
77+
r += REPLACE_CHAR
78+
elif errors == "ignore":
79+
continue
80+
else:
81+
raise ValueError("Unsupported error handling {}"
82+
.format(errors))
83+
return r, len(text)
84+
85+
86+
add("braille", braille_encode, braille_decode)

docs/encodings.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,23 @@ This encoding relies on the `base64` library and is only supported in Python 3.
2727

2828
-----
2929

30+
### Braille
31+
32+
It supports letters, digits and some special characters.
33+
34+
**Codec** | **Conversions** | **Aliases** | **Comment**
35+
:---: | :---: | --- | ---
36+
`braille` | braille <-> text | none | Python 3 only
37+
38+
```python
39+
>>> codext.encode("this is a test", "braille")
40+
'⠞⠓⠊⠎⠀⠊⠎⠀⠁⠀⠞⠑⠎⠞'
41+
>>> codext.decode("⠞⠓⠊⠎⠀⠊⠎⠀⠁⠀⠞⠑⠎⠞", "braille")
42+
'this is a test'
43+
```
44+
45+
-----
46+
3047
### DNA
3148

3249
This implements the 8 methods of ATGC nucleotides following the rule of complementary pairing, according the literature about coding and computing of DNA sequences.

tests/test_braille.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env python
2+
# -*- coding: UTF-8 -*-
3+
"""Braille codec tests.
4+
5+
"""
6+
from unittest import TestCase
7+
8+
from codext.__common__ import *
9+
10+
11+
if PY3:
12+
class TestCodecBraille(TestCase):
13+
def test_codec_braille(self):
14+
STR = "this is a test"
15+
BRA = "⠞⠓⠊⠎⠀⠊⠎⠀⠁⠀⠞⠑⠎⠞"
16+
self.assertEqual(codecs.encode(STR, "braille"), BRA)
17+
self.assertEqual(codecs.encode(b(STR), "braille"), b(BRA))
18+
self.assertEqual(codecs.decode(BRA, "braille"), STR)
19+
self.assertEqual(codecs.decode(b(BRA), "braille"), b(STR))

0 commit comments

Comments
 (0)