FireBot/overrides.py

43 lines
1.2 KiB
Python
Raw Normal View History

2023-11-05 01:37:35 +00:00
#!/usr/bin/python3
from builtins import bytes as bbytes
from typing import TypeVar, Type, Any
_T = TypeVar("_T")
class bytes(bbytes):
"""Local override of builtin bytes class to add "lazy_decode"
2023-11-08 03:23:59 +00:00
bytes(iterable_of_ints) -> bytes
bytes(string, encoding[, errors]) -> bytes
bytes(bytes_or_buffer) -> immutable copy of bytes_or_buffer
bytes(int) -> bytes object of size given by the parameter initialized with null bytes
bytes() -> empty bytes object
2023-11-08 03:23:59 +00:00
Construct an immutable array of bytes from:
- an iterable yielding integers in range(256)
- a text string encoded using the specified encoding
- any object implementing the buffer API.
- an integer
"""
2023-11-05 01:37:35 +00:00
def __new__(
cls: Type[_T],
thing: Any = None,
encoding: str = "UTF-8",
errors: str = "strict",
) -> _T:
if type(thing) == str:
cls.value = super().__new__(cls, thing, encoding, errors)
elif thing == None:
cls.value = super().__new__(cls)
elif thing != None:
cls.value = super().__new__(cls, thing)
else:
raise AttributeError("wtf")
return cls.value
@classmethod
def lazy_decode(self):
return str(self.value)[2:-1]