|
| 1 | +With `codext`, the `codecs` library has multiple new encodings in addition to [the native ones](https://docs.python.org/3.8/library/codecs.html#standard-encodings), like presented hereafter. |
| 2 | + |
| 3 | +Unless explicitely specified, each codec supports writing to and reading from a file. |
| 4 | + |
| 5 | +!!! warning "Lossy conversion" |
| 6 | + |
| 7 | + Some encodings are lossy, meaning that it is not always possible to decode back to the exact start string. This should be considered especially when chaining codecs. |
| 8 | + |
| 9 | +----- |
| 10 | + |
| 11 | +### Baudot |
| 12 | + |
| 13 | +It supports various formats such as CCITT-1 and CCITT-2, ITA1 and ITA2, and some others. |
| 14 | + |
| 15 | +**Codec** | **Conversions** | **Aliases** | **Comment** |
| 16 | +:---: | :---: | --- | --- |
| 17 | +`baudot` | text <-> text | Baudot code bits | `baudot-ccitt1`, `baudot_ccitt2_lsb`, ... | supports CCITT-1, CCITT-2, EU/FR, ITA1, ITA2, MTK-2 (Python3 only), UK, ... |
| 18 | +`baudot-spaced` | text <-> Baudot code groups of bits | `baudot-spaced-ita1_lsb`, `baudot_spaced_ita2_msb`, ... | groups of 5 bits are whitespace-separated |
| 19 | +`baudot-tape` | text <-> Baudot code tape | `baudot-tape-mtk2`, `baudot_tape_murray`, ... | outputs a string that looks like a perforated tape |
| 20 | + |
| 21 | +!!! note "LSB / MSB" |
| 22 | + |
| 23 | + "`_lsb`" or "`_msb`" can be specified in the codec name to set the bits order. If not specified, it defaults to MSB. |
| 24 | + |
| 25 | + |
| 26 | +```python |
| 27 | +>>> codext.encode("12345", "baudot-fr") |
| 28 | +'010000000100010001000010100111' |
| 29 | +>>> codext.decode("010000000100010001000010100111", "baudot-fr") |
| 30 | +'12345' |
| 31 | +``` |
| 32 | + |
| 33 | +```python |
| 34 | +>>> codext.encode("TEST", "baudot-spaced_uk") |
| 35 | +'10101 00010 10100 10101' |
| 36 | +>>> codext.decode("10101 00010 10100 10101", "baudot-spaced_uk") |
| 37 | +'TEST' |
| 38 | +``` |
| 39 | + |
| 40 | +```python |
| 41 | +>>> s = codext.encode("HELLO WORLD!", "baudot-tape_ita2") |
| 42 | +>>> print(s) |
| 43 | +***.** |
| 44 | +* *. |
| 45 | + . * |
| 46 | +* .* |
| 47 | +* .* |
| 48 | +** . |
| 49 | + *. |
| 50 | +* .** |
| 51 | +** . |
| 52 | + * .* |
| 53 | +* .* |
| 54 | + * . * |
| 55 | +** .** |
| 56 | + **. * |
| 57 | +>>> codext.decode(s, "baudot-tape_ita2") |
| 58 | +'HELLO WORLD!' |
| 59 | +``` |
| 60 | + |
| 61 | +----- |
| 62 | + |
| 63 | +### Excess-3 Code |
| 64 | + |
| 65 | +Also called *Stibitz code*, it converts letters to ordinals, left-pads with zeros and then applies Excess-3 (Stibitz) code to get groups of 4 bits that are finally reassembled into bytes. |
| 66 | + |
| 67 | +**Codec** | **Conversions** | **Aliases** | **Comment** |
| 68 | +:---: | :---: | --- | --- |
| 69 | +`excess3` | text <-> XS3 encoded text | `excess-3`, `xs3`, `stibitz` | |
| 70 | + |
| 71 | +```python |
| 72 | +>>> codext.encode("This is a test!", "excess-3") |
| 73 | +';t7C\x84H6T8D\x83e<£eD\x944D\x84I6`' |
| 74 | +>>> codext.decode(";t7C\x84H6T8D\x83e<£eD\x944D\x84I6`", "stibitz") |
| 75 | +'This is a test!' |
| 76 | +``` |
| 77 | + |
| 78 | +----- |
| 79 | + |
| 80 | +### Gray Code |
| 81 | + |
| 82 | +Also called *reflected binary code*, it implements the Gray code applied to characters while converted to bytes. |
| 83 | + |
| 84 | +**Codec** | **Conversions** | **Aliases** | **Comment** |
| 85 | +:---: | :---: | --- | --- |
| 86 | +`gray` | text <-> gray encoded text | `reflected-bin`, `reflected_binary` | |
| 87 | + |
| 88 | +```python |
| 89 | +>>> codext.encode("this is a test", "gray") |
| 90 | +'N\\]J0]J0Q0NWJN' |
| 91 | +>>> codext.decode("N\\]J0]J0Q0NWJN", "gray") |
| 92 | +'this is a test' |
| 93 | +>>> codext.encode("THIS IS A TEST", "gray") |
| 94 | +'~lmz0mz0a0~gz~' |
| 95 | +>>> codext.decode("~lmz0mz0a0~gz~", "gray") |
| 96 | +'THIS IS A TEST' |
| 97 | +``` |
| 98 | + |
0 commit comments