Skip to content

Commit f044e4f

Browse files
committed
Adde new codec: resistor
1 parent 25d6f8b commit f044e4f

4 files changed

Lines changed: 36 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ This library extends the native `codecs` library and provides some new encodings
2727
`nokia3310` | Nokia 3310 keystrokes <-> text | uses "`-`" as a separator for encoding, "`-`" or "`_`" or whitespace for decoding
2828
`ordinals` | Ordinals <-> text | dummy character ordinals conversion
2929
`radio` | Radio <-> text | aka NATO or radio phonetic alphabet
30+
`resistor` | Resistor <-> text | aka resistor color codes
3031
`rot-N` | ROT(N) <-> text | aka Caesar cipher (N belongs to [1,25])
3132
`url` | URL <-> text | aka URL encoding
3233
`xor-N` | XOR(N) <-> text | XOR with a single byte (N belongs to [1,255])

codext/stegano/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# -*- coding: UTF-8 -*-
22
from .leetspeak import *
33
from .nokia import *
4+
from .resistor import *
45
from .whitespace import *

codext/stegano/resistor.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# -*- coding: UTF-8 -*-
2+
"""Resistor Codec - resistor color codes 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 ..__common__ import *
11+
12+
13+
ENCMAP = {i: "\033[48;5;%dm \033[0;00m" % c for i, c in zip("0123456789", [232, 130, 1, 214, 11, 2, 4, 129, 245, 231])}
14+
ENCMAP[' '] = "/"
15+
16+
17+
add_map("resistor", ENCMAP, pattern=r"^resistors?(?:[-_]color(?:[-_]code)?)?$")

docs/encodings.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,23 @@ This is also known as the [NATO phonetic alphabet](https://en.wikipedia.org/wiki
187187

188188
-----
189189

190+
### Resistor Color Codes
191+
192+
This uses the [electronic color code](https://en.wikipedia.org/wiki/Electronic_color_code#Resistor_color-coding) to encode digits, displaying colors in the terminal with ANSI color codes.
193+
194+
**Codec** | **Conversions** | **Aliases** | **Comment**
195+
:---: | :---: | --- | ---
196+
`resistor` | Resistor <-> text | `resistors-color`, `resistor_color_code` | visually, it only works in a terminal supporting ANSI color codes
197+
198+
```python
199+
>>> codext.encode("1234", "resistor")
200+
'\x1b[48;5;130m \x1b[0;00m\x1b[48;5;1m \x1b[0;00m\x1b[48;5;214m \x1b[0;00m\x1b[48;5;11m \x1b[0;00m'
201+
>>> codext.decode("\x1b[48;5;130m \x1b[0;00m\x1b[48;5;1m \x1b[0;00m\x1b[48;5;214m \x1b[0;00m\x1b[48;5;11m \x1b[0;00m", "resistors_color")
202+
'1234'
203+
```
204+
205+
-----
206+
190207
### URL
191208

192209
This handles URL encoding, regardless of the case when decoding and with no error.

0 commit comments

Comments
 (0)