mirror of
https://github.com/aljazceru/bitfinex-api-py.git
synced 2025-12-19 06:44:22 +01:00
Apply black to all python files (bfxapi/**/*.py).
This commit is contained in:
@@ -2,6 +2,7 @@ from typing import Any, Dict, Generic, Iterable, List, Tuple, Type, TypeVar, cas
|
||||
|
||||
T = TypeVar("T", bound="_Type")
|
||||
|
||||
|
||||
def compose(*decorators):
|
||||
def wrapper(function):
|
||||
for decorator in reversed(decorators):
|
||||
@@ -10,30 +11,37 @@ def compose(*decorators):
|
||||
|
||||
return wrapper
|
||||
|
||||
|
||||
def partial(cls):
|
||||
def __init__(self, **kwargs):
|
||||
for annotation in self.__annotations__.keys():
|
||||
if annotation not in kwargs:
|
||||
self.__setattr__(annotation, None)
|
||||
else: self.__setattr__(annotation, kwargs[annotation])
|
||||
else:
|
||||
self.__setattr__(annotation, kwargs[annotation])
|
||||
|
||||
kwargs.pop(annotation, None)
|
||||
|
||||
if len(kwargs) != 0:
|
||||
raise TypeError(f"{cls.__name__}.__init__() got an unexpected keyword argument '{list(kwargs.keys())[0]}'")
|
||||
raise TypeError(
|
||||
f"{cls.__name__}.__init__() got an unexpected keyword argument '{list(kwargs.keys())[0]}'"
|
||||
)
|
||||
|
||||
cls.__init__ = __init__
|
||||
|
||||
return cls
|
||||
|
||||
|
||||
class _Type:
|
||||
"""
|
||||
Base class for any dataclass serializable by the _Serializer generic class.
|
||||
"""
|
||||
|
||||
|
||||
class _Serializer(Generic[T]):
|
||||
def __init__(self, name: str, klass: Type[_Type], labels: List[str],
|
||||
*, flat: bool = False):
|
||||
def __init__(
|
||||
self, name: str, klass: Type[_Type], labels: List[str], *, flat: bool = False
|
||||
):
|
||||
self.name, self.klass, self.__labels, self.__flat = name, klass, labels, flat
|
||||
|
||||
def _serialize(self, *args: Any) -> Iterable[Tuple[str, Any]]:
|
||||
@@ -41,8 +49,10 @@ class _Serializer(Generic[T]):
|
||||
args = tuple(_Serializer.__flatten(list(args)))
|
||||
|
||||
if len(self.__labels) > len(args):
|
||||
raise AssertionError(f"{self.name} -> <labels> and <*args> " \
|
||||
"arguments should contain the same amount of elements.")
|
||||
raise AssertionError(
|
||||
f"{self.name} -> <labels> and <*args> "
|
||||
"arguments should contain the same amount of elements."
|
||||
)
|
||||
|
||||
for index, label in enumerate(self.__labels):
|
||||
if label != "_PLACEHOLDER":
|
||||
@@ -52,7 +62,7 @@ class _Serializer(Generic[T]):
|
||||
return cast(T, self.klass(**dict(self._serialize(*values))))
|
||||
|
||||
def get_labels(self) -> List[str]:
|
||||
return [ label for label in self.__labels if label != "_PLACEHOLDER" ]
|
||||
return [label for label in self.__labels if label != "_PLACEHOLDER"]
|
||||
|
||||
@classmethod
|
||||
def __flatten(cls, array: List[Any]) -> List[Any]:
|
||||
@@ -64,10 +74,17 @@ class _Serializer(Generic[T]):
|
||||
|
||||
return array[:1] + cls.__flatten(array[1:])
|
||||
|
||||
|
||||
class _RecursiveSerializer(_Serializer, Generic[T]):
|
||||
def __init__(self, name: str, klass: Type[_Type], labels: List[str],
|
||||
*, serializers: Dict[str, _Serializer[Any]],
|
||||
flat: bool = False):
|
||||
def __init__(
|
||||
self,
|
||||
name: str,
|
||||
klass: Type[_Type],
|
||||
labels: List[str],
|
||||
*,
|
||||
serializers: Dict[str, _Serializer[Any]],
|
||||
flat: bool = False,
|
||||
):
|
||||
super().__init__(name, klass, labels, flat=flat)
|
||||
|
||||
self.serializers = serializers
|
||||
@@ -81,15 +98,21 @@ class _RecursiveSerializer(_Serializer, Generic[T]):
|
||||
|
||||
return cast(T, self.klass(**serialization))
|
||||
|
||||
def generate_labeler_serializer(name: str, klass: Type[T], labels: List[str],
|
||||
*, flat: bool = False
|
||||
) -> _Serializer[T]:
|
||||
return _Serializer[T](name, klass, labels, \
|
||||
flat=flat)
|
||||
|
||||
def generate_recursive_serializer(name: str, klass: Type[T], labels: List[str],
|
||||
*, serializers: Dict[str, _Serializer[Any]],
|
||||
flat: bool = False
|
||||
) -> _RecursiveSerializer[T]:
|
||||
return _RecursiveSerializer[T](name, klass, labels, \
|
||||
serializers=serializers, flat=flat)
|
||||
def generate_labeler_serializer(
|
||||
name: str, klass: Type[T], labels: List[str], *, flat: bool = False
|
||||
) -> _Serializer[T]:
|
||||
return _Serializer[T](name, klass, labels, flat=flat)
|
||||
|
||||
|
||||
def generate_recursive_serializer(
|
||||
name: str,
|
||||
klass: Type[T],
|
||||
labels: List[str],
|
||||
*,
|
||||
serializers: Dict[str, _Serializer[Any]],
|
||||
flat: bool = False,
|
||||
) -> _RecursiveSerializer[T]:
|
||||
return _RecursiveSerializer[T](
|
||||
name, klass, labels, serializers=serializers, flat=flat
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user