未验证 提交 bdd135d4 编写于 作者: R Rohan R Bharadwaj 提交者: GitHub

Split base85.py into functions, Add doctests (#5746)

* Update base16.py

* Rename base64_encoding.py to base64.py

* Split into functions, Add doctests

* Update base16.py
上级 24731b07
import base64
def encode_to_b16(inp: str) -> bytes:
def base16_encode(inp: str) -> bytes:
"""
Encodes a given utf-8 string into base-16.
>>> encode_to_b16('Hello World!')
>>> base16_encode('Hello World!')
b'48656C6C6F20576F726C6421'
>>> encode_to_b16('HELLO WORLD!')
>>> base16_encode('HELLO WORLD!')
b'48454C4C4F20574F524C4421'
>>> encode_to_b16('')
>>> base16_encode('')
b''
"""
# encode the input into a bytes-like object and then encode b16encode that
return base64.b16encode(inp.encode("utf-8"))
def decode_from_b16(b16encoded: bytes) -> str:
def base16_decode(b16encoded: bytes) -> str:
"""
Decodes from base-16 to a utf-8 string.
>>> decode_from_b16(b'48656C6C6F20576F726C6421')
>>> base16_decode(b'48656C6C6F20576F726C6421')
'Hello World!'
>>> decode_from_b16(b'48454C4C4F20574F524C4421')
>>> base16_decode(b'48454C4C4F20574F524C4421')
'HELLO WORLD!'
>>> decode_from_b16(b'')
>>> base16_decode(b'')
''
"""
# b16decode the input into bytes and decode that into a human readable string
......
import base64
def main() -> None:
inp = input("->")
encoded = inp.encode("utf-8") # encoded the input (we need a bytes like object)
a85encoded = base64.a85encode(encoded) # a85encoded the encoded string
print(a85encoded)
print(base64.a85decode(a85encoded).decode("utf-8")) # decoded it
def base85_encode(string: str) -> bytes:
"""
>>> base85_encode("")
b''
>>> base85_encode("12345")
b'0etOA2#'
>>> base85_encode("base 85")
b'@UX=h+?24'
"""
# encoded the input to a bytes-like object and then a85encode that
return base64.a85encode(string.encode("utf-8"))
def base85_decode(a85encoded: bytes) -> str:
"""
>>> base85_decode(b"")
''
>>> base85_decode(b"0etOA2#")
'12345'
>>> base85_decode(b"@UX=h+?24")
'base 85'
"""
# a85decode the input into bytes and decode that into a human readable string
return base64.a85decode(a85encoded).decode("utf-8")
if __name__ == "__main__":
main()
import doctest
doctest.testmod()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册