black + remove unused imports

This commit is contained in:
Peter Justin 2019-04-08 10:03:24 +02:00
parent 93de151025
commit d8fe34e5bc
6 changed files with 179 additions and 117 deletions

View file

@ -27,16 +27,13 @@ __license__ = "GPLv3"
import base64
import itertools
from typing import List, Iterator, Tuple, Union
from functools import reduce
from typing import IO, Iterator, List, Tuple, Union
from PIL import Image
ENCODINGS = {
'UTF-8': 8,
'UTF-32LE': 32
}
ENCODINGS = {"UTF-8": 8, "UTF-32LE": 32}
def a2bits(chars: str) -> str:
"""Converts a string to its bits representation as a string of 0's and 1's.
@ -44,9 +41,12 @@ def a2bits(chars: str) -> str:
>>> a2bits("Hello World!")
'010010000110010101101100011011000110111100100000010101110110111101110010011011000110010000100001'
"""
return bin(reduce(lambda x, y : (x<<8)+y, (ord(c) for c in chars), 1))[3:]
return bin(reduce(lambda x, y: (x << 8) + y, (ord(c) for c in chars), 1))[
3:
]
def a2bits_list(chars: str, encoding: str ='UTF-8') -> List[str]:
def a2bits_list(chars: str, encoding: str = "UTF-8") -> List[str]:
"""Convert a string to its bits representation as a list of 0's and 1's.
>>> a2bits_list("Hello World!")
@ -65,20 +65,24 @@ def a2bits_list(chars: str, encoding: str ='UTF-8') -> List[str]:
>>> "".join(a2bits_list("Hello World!"))
'010010000110010101101100011011000110111100100000010101110110111101110010011011000110010000100001'
"""
return [bin(ord(x))[2:].rjust(ENCODINGS[encoding],"0") for x in chars]
return [bin(ord(x))[2:].rjust(ENCODINGS[encoding], "0") for x in chars]
def bs(s: int) -> str:
"""Converts an int to its bits representation as a string of 0's and 1's.
"""
return str(s) if s<=1 else bs(s>>1) + str(s&1)
return str(s) if s <= 1 else bs(s >> 1) + str(s & 1)
def setlsb(component: int, bit: str) -> int:
"""Set Least Significant Bit of a colour component.
"""
return component & ~1 | int(bit)
def n_at_a_time(items: List[int], n: int, fillvalue: str) \
-> Iterator[Tuple[Union[int, str]]]:
def n_at_a_time(
items: List[int], n: int, fillvalue: str
) -> Iterator[Tuple[Union[int, str]]]:
"""Returns an iterator which groups n items at a time.
Any final partial tuple will be padded with the fillvalue
@ -88,6 +92,7 @@ def n_at_a_time(items: List[int], n: int, fillvalue: str) \
it = iter(items)
return itertools.zip_longest(*[it] * n, fillvalue=fillvalue)
def binary2base64(binary_file: str) -> str:
"""Convert a binary file (OGG, executable, etc.) to a
printable string.
@ -97,12 +102,14 @@ def binary2base64(binary_file: str) -> str:
encoded_string = base64.b64encode(bin_file.read())
return encoded_string.decode()
def base642binary(b64_fname: str) -> bytes:
"""Convert a printable string to a binary file.
"""
b64_fname += '==='
b64_fname += "==="
return base64.b64decode(b64_fname)
def open_image(fname_or_instance: Union[str, IO[bytes]]):
"""Opens a Image and returns it.