From 7cce7aa3e4c6c7019a55530391b1761d33e5a04e Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Thu, 2 Aug 2018 23:00:10 +0200 Subject: [PATCH] Return helpers.TotalList instances on client.get_ methods --- readthedocs/telethon.rst | 9 +++++++++ telethon/client/bots.py | 2 +- telethon/client/chats.py | 10 ++++------ telethon/client/dialogs.py | 9 ++++----- telethon/client/messages.py | 9 ++++----- telethon/helpers.py | 23 +++++++++++++++++++++++ telethon/utils.py | 4 +--- 7 files changed, 46 insertions(+), 20 deletions(-) diff --git a/readthedocs/telethon.rst b/readthedocs/telethon.rst index dadf0810..e91a3ee8 100644 --- a/readthedocs/telethon.rst +++ b/readthedocs/telethon.rst @@ -27,6 +27,15 @@ telethon\.utils module :show-inheritance: +telethon\.helpers module +------------------------ + +.. automodule:: telethon.helpers + :members: + :undoc-members: + :show-inheritance: + + telethon\.events package ------------------------ diff --git a/telethon/client/bots.py b/telethon/client/bots.py index 8bef4c25..e8ac1a36 100644 --- a/telethon/client/bots.py +++ b/telethon/client/bots.py @@ -38,7 +38,7 @@ class BotMethods(UserMethods): geo_point=geo_point )) - # TODO Custom InlineResults(UserList) class with more information + # TODO Custom InlineResults(list) class with more information return [ custom.InlineResult(self, x, query_id=result.query_id) for x in result.results diff --git a/telethon/client/chats.py b/telethon/client/chats.py index 39d90617..5a5550f0 100644 --- a/telethon/client/chats.py +++ b/telethon/client/chats.py @@ -1,9 +1,7 @@ -from collections import UserList - from async_generator import async_generator, yield_ from .users import UserMethods -from .. import utils +from .. import utils, helpers from ..tl import types, functions @@ -169,12 +167,12 @@ class ChatMethods(UserMethods): async def get_participants(self, *args, **kwargs): """ - Same as :meth:`iter_participants`, but returns a list instead - with an additional ``.total`` attribute on the list. + Same as `iter_participants`, but returns a + `TotalList ` instead. """ total = [0] kwargs['_total'] = total - participants = UserList() + participants = helpers.TotalList() async for x in self.iter_participants(*args, **kwargs): participants.append(x) participants.total = total[0] diff --git a/telethon/client/dialogs.py b/telethon/client/dialogs.py index f04dbc01..fe92fa82 100644 --- a/telethon/client/dialogs.py +++ b/telethon/client/dialogs.py @@ -1,10 +1,9 @@ import itertools -from collections import UserList from async_generator import async_generator, yield_ from .users import UserMethods -from .. import utils +from .. import utils, helpers from ..tl import types, functions, custom @@ -124,12 +123,12 @@ class DialogMethods(UserMethods): async def get_dialogs(self, *args, **kwargs): """ - Same as :meth:`iter_dialogs`, but returns a list instead - with an additional ``.total`` attribute on the list. + Same as `iter_dialogs`, but returns a + `TotalList ` instead. """ total = [0] kwargs['_total'] = total - dialogs = UserList() + dialogs = helpers.TotalList() async for x in self.iter_dialogs(*args, **kwargs): dialogs.append(x) dialogs.total = total[0] diff --git a/telethon/client/messages.py b/telethon/client/messages.py index c3410f2b..77e2aa4a 100644 --- a/telethon/client/messages.py +++ b/telethon/client/messages.py @@ -2,14 +2,13 @@ import asyncio import itertools import logging import time -from collections import UserList from async_generator import async_generator, yield_ from .messageparse import MessageParseMethods from .uploads import UploadMethods from .buttons import ButtonMethods -from .. import utils +from .. import utils, helpers from ..tl import types, functions, custom __log__ = logging.getLogger(__name__) @@ -323,8 +322,8 @@ class MessageMethods(UploadMethods, ButtonMethods, MessageParseMethods): async def get_messages(self, *args, **kwargs): """ - Same as :meth:`iter_messages`, but returns a list instead - with an additional ``.total`` attribute on the list. + Same as `iter_messages`, but returns a + `TotalList ` instead. If the `limit` is not set, it will be 1 by default unless both `min_id` **and** `max_id` are set (as *named* arguments), in @@ -346,7 +345,7 @@ class MessageMethods(UploadMethods, ButtonMethods, MessageParseMethods): else: kwargs['limit'] = 1 - msgs = UserList() + msgs = helpers.TotalList() async for x in self.iter_messages(*args, **kwargs): msgs.append(x) msgs.total = total[0] diff --git a/telethon/helpers.py b/telethon/helpers.py index 9c3bb116..1452aad2 100644 --- a/telethon/helpers.py +++ b/telethon/helpers.py @@ -1,4 +1,5 @@ """Various helpers not related to the Telegram API itself""" +import collections import os import struct from hashlib import sha1, sha256 @@ -65,3 +66,25 @@ def get_password_hash(pw, current_salt): return sha256(pw_hash).digest() # endregion + +# region Custom Classes + +class TotalList(list): + """ + A list with an extra `total` property, which may not match its `len` + since the total represents the total amount of items *available* + somewhere else, not the items *in this list*. + """ + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.total = 0 + + def __str__(self): + return '[{}, total={}]'.format( + ', '.join(str(x) for x in self), self.total) + + def __repr__(self): + return '[{}, total={}]'.format( + ', '.join(repr(x) for x in self), self.total) + +# endregion diff --git a/telethon/utils.py b/telethon/utils.py index 9ace3845..befb2e07 100644 --- a/telethon/utils.py +++ b/telethon/utils.py @@ -10,7 +10,6 @@ import mimetypes import os import re import struct -from collections import UserList from mimetypes import guess_extension from types import GeneratorType @@ -603,8 +602,7 @@ def is_list_like(obj): enough. Things like ``open()`` are also iterable (and probably many other things), so just support the commonly known list-like objects. """ - return isinstance(obj, (list, tuple, set, dict, - UserList, GeneratorType)) + return isinstance(obj, (list, tuple, set, dict, GeneratorType)) def parse_phone(phone):