diff --git a/pyrogram/api/core/__init__.py b/pyrogram/api/core/__init__.py index daba6b7c..bf596446 100644 --- a/pyrogram/api/core/__init__.py +++ b/pyrogram/api/core/__init__.py @@ -19,6 +19,7 @@ from .future_salt import FutureSalt from .future_salts import FutureSalts from .gzip_packed import GzipPacked +from .list import List from .message import Message from .msg_container import MsgContainer from .object import Object diff --git a/pyrogram/api/core/list.py b/pyrogram/api/core/list.py new file mode 100644 index 00000000..1f55dc45 --- /dev/null +++ b/pyrogram/api/core/list.py @@ -0,0 +1,26 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-2019 Dan Tès +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + +from .object import Object + + +class List(list, Object): + __slots__ = [] + + def __repr__(self): + return "pyrogram.api.core.List([{}])".format(",".join(Object.__str__(i) for i in self)) diff --git a/pyrogram/api/core/object.py b/pyrogram/api/core/object.py index ace7a59a..9d1a4852 100644 --- a/pyrogram/api/core/object.py +++ b/pyrogram/api/core/object.py @@ -17,7 +17,6 @@ # along with Pyrogram. If not, see . from collections import OrderedDict -from datetime import datetime from io import BytesIO from json import dumps @@ -36,32 +35,22 @@ class Object: def write(self, *args) -> bytes: pass - def __eq__(self, other: "Object") -> bool: - for attr in self.__slots__: - try: - if getattr(self, attr) != getattr(other, attr): - return False - except AttributeError: - return False + @staticmethod + def default(obj: "Object"): + if isinstance(obj, bytes): + return repr(obj) - return True + return OrderedDict( + [("_", obj.QUALNAME)] + + [ + (attr, getattr(obj, attr)) + for attr in obj.__slots__ + if getattr(obj, attr) is not None + ] + ) def __str__(self) -> str: - def default(obj: Object): - try: - return OrderedDict( - [("_", obj.QUALNAME)] - + [(attr, getattr(obj, attr)) - for attr in obj.__slots__ - if getattr(obj, attr) is not None] - ) - except AttributeError: - if isinstance(obj, datetime): - return obj.strftime("%d-%b-%Y %H:%M:%S") - else: - return repr(obj) - - return dumps(self, indent=4, default=default, ensure_ascii=False) + return dumps(self, indent=4, default=Object.default, ensure_ascii=False) def __repr__(self) -> str: return "pyrogram.api.{}({})".format( @@ -73,6 +62,16 @@ class Object: ) ) + def __eq__(self, other: "Object") -> bool: + for attr in self.__slots__: + try: + if getattr(self, attr) != getattr(other, attr): + return False + except AttributeError: + return False + + return True + def __len__(self) -> int: return len(self.write()) diff --git a/pyrogram/api/core/primitives/vector.py b/pyrogram/api/core/primitives/vector.py index cd24ec35..720486f2 100644 --- a/pyrogram/api/core/primitives/vector.py +++ b/pyrogram/api/core/primitives/vector.py @@ -19,6 +19,7 @@ from io import BytesIO from . import Int +from ..list import List from ..object import Object @@ -37,11 +38,11 @@ class Vector(Object): @staticmethod def read(b: BytesIO, t: Object = None) -> list: - return [ + return List( t.read(b) if t else Vector._read(b) for _ in range(Int.read(b)) - ] + ) def __new__(cls, value: list, t: Object = None) -> bytes: return b"".join(