Created
April 12, 2022 10:08
-
-
Save valderman/dff50397db00bace86860dd12f6eaab9 to your computer and use it in GitHub Desktop.
Communicate on GitHub PRs using only emoji
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python3 | |
from typing import Callable, TypeVar, TextIO, Iterable | |
T = TypeVar("T") | |
chars = ["π", "π", "π", "π", "π", "π§‘", "π", "π"] | |
def chunk(size: int, xs: list[T]) -> Iterable[list[T]]: | |
for ix in range(0, len(xs), size): | |
yield xs[ix : ix+size] | |
def thumbs(s: str) -> str: | |
def bits(b: bytes) -> list[bool]: | |
return [bool(byte & (1 << bit)) for byte in b for bit in range(8)] | |
def digit(bits: list[bool]) -> int: | |
return sum([1 << ix if bit else 0 for (bit, ix) in zip(bits, range(3))]) | |
def digits(s: str) -> list[int]: | |
b = bytes(s, "utf-8") | |
return list(map(digit, chunk(3, bits(b)))) | |
def char(digit: int) -> str: | |
return chars[digit] | |
return ''.join(map(char, digits(s))) | |
def unthumbs(s: str) -> str: | |
def digits(s: str) -> list[int]: | |
return [chars.index(c) for c in s] | |
def bits(digit: int) -> list[bool]: | |
return [bool(digit & (1 << bit)) for bit in range(3)] | |
def byte(bits: list[bool]) -> int: | |
return sum([1 << ix if bit else 0 for (bit, ix) in zip(bits, range(8))]) | |
allBits = [bit for digit in digits(s.strip()) for bit in bits(digit)] | |
allBytes = [byte(b) for b in chunk(8, allBits)] | |
return ''.join([chr(byte) for byte in allBytes]) | |
def translate(tr: Callable[[str], str], stdin: TextIO) -> None: | |
for line in stdin: | |
print(tr(line)) | |
if __name__ == "__main__": | |
from sys import argv, stdin | |
tr = unthumbs if len(argv) > 1 and argv[1] == "-r" else thumbs | |
translate(tr, stdin) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment