Refactor Poll.

Move PollAnswer into poll.py and rename it to PollOption
This commit is contained in:
Dan 2018-12-23 13:28:53 +01:00
parent f8de518f6b
commit 2b568afd2a
5 changed files with 25 additions and 47 deletions

View File

@ -32,7 +32,7 @@ from .client.types import (
Location, Message, MessageEntity, Dialog, Dialogs, Photo, PhotoSize, Sticker, User, UserStatus,
UserProfilePhotos, Venue, Animation, Video, VideoNote, Voice, CallbackQuery, Messages, ForceReply,
InlineKeyboardButton, InlineKeyboardMarkup, KeyboardButton, ReplyKeyboardMarkup, ReplyKeyboardRemove,
Poll, PollAnswer
Poll
)
from .client import (
Client, ChatAction, ParseMode, Emoji,

View File

@ -31,7 +31,7 @@ from .input_media import (
from .messages_and_media import (
Audio, Contact, Document, Animation, Location, Photo, PhotoSize,
Sticker, Venue, Video, VideoNote, Voice, UserProfilePhotos,
Message, Messages, MessageEntity, Poll, PollAnswer
Message, Messages, MessageEntity, Poll
)
from .user_and_chats import (
Chat, ChatMember, ChatMembers, ChatPhoto,

View File

@ -27,7 +27,6 @@ from .messages import Messages
from .photo import Photo
from .photo_size import PhotoSize
from .poll import Poll
from .poll_answer import PollAnswer
from .sticker import Sticker
from .user_profile_photos import UserProfilePhotos
from .venue import Venue

View File

@ -20,10 +20,21 @@ from typing import List
import pyrogram
from pyrogram.api import types
from .poll_answer import PollAnswer
from ..pyrogram_type import PyrogramType
class PollOption(PyrogramType):
def __init__(self,
*,
client: "pyrogram.client.ext.BaseClient",
text: str,
voters: int):
super().__init__(client)
self.text = text
self.voters = voters
class Poll(PyrogramType):
def __init__(self,
*,
@ -31,16 +42,16 @@ class Poll(PyrogramType):
id: int,
closed: bool,
question: str,
answers: List[PollAnswer],
answer_chosen: int = None,
options: List[PollOption],
option_chosen: int = None,
total_voters: int):
super().__init__(client)
self.id = id
self.closed = closed
self.question = question
self.answers = answers
self.answer_chosen = answer_chosen
self.options = options
self.option_chosen = option_chosen
self.total_voters = total_voters
@staticmethod
@ -48,21 +59,21 @@ class Poll(PyrogramType):
poll = media_poll.poll
results = media_poll.results.results
total_voters = media_poll.results.total_voters
answer_chosen = None
option_chosen = None
answers = []
options = []
for i, answer in enumerate(poll.answers):
voters = None
voters = 0
if results:
result = results[i]
voters = result.voters
if result.chosen:
answer_chosen = i
option_chosen = i
answers.append(PollAnswer(
options.append(PollOption(
text=answer.text,
voters=voters,
client=client
@ -72,8 +83,8 @@ class Poll(PyrogramType):
id=poll.id,
closed=poll.closed,
question=poll.question,
answers=answers,
answer_chosen=answer_chosen,
options=options,
option_chosen=option_chosen,
total_voters=total_voters,
client=client
)

View File

@ -1,32 +0,0 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-2018 Dan Tès <https://github.com/delivrance>
#
# 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 <http://www.gnu.org/licenses/>.
import pyrogram
from ..pyrogram_type import PyrogramType
class PollAnswer(PyrogramType):
def __init__(self,
*,
client: "pyrogram.client.ext.BaseClient",
text: str,
voters: int = None):
super().__init__(client)
self.text = text
self.voters = voters