1
0
mirror of https://github.com/eternnoir/pyTelegramBotAPI.git synced 2023-08-10 21:12:57 +03:00

PEP 8 clean-up.

This commit is contained in:
pieter 2015-07-01 22:34:40 +02:00
parent a03769adde
commit 2f8d1ad4ed
4 changed files with 19 additions and 17 deletions

View File

@ -1,7 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import print_function from __future__ import print_function
import json
import time import time
import threading import threading
@ -209,4 +208,3 @@ class TeleBot:
:return: :return:
""" """
return apihelper.send_chat_action(self.token, chat_id, action) return apihelper.send_chat_action(self.token, chat_id, action)

View File

@ -54,6 +54,7 @@ def get_updates(token, offset=None):
else: else:
return _make_request(token, method_url) return _make_request(token, method_url)
def get_user_profile_photos(token, user_id, offset=None, limit=None): def get_user_profile_photos(token, user_id, offset=None, limit=None):
method_url = r'getUserProfilePhotos' method_url = r'getUserProfilePhotos'
payload = {'user_id': user_id} payload = {'user_id': user_id}

View File

@ -22,6 +22,7 @@ ForceReply
import json import json
class JsonSerializable: class JsonSerializable:
""" """
Subclasses of this class are guaranteed to be able to be converted to JSON format. Subclasses of this class are guaranteed to be able to be converted to JSON format.
@ -36,13 +37,14 @@ class JsonSerializable:
""" """
raise NotImplementedError raise NotImplementedError
class JsonDeserializable: class JsonDeserializable:
""" """
Subclasses of this class are guaranteed to be able to be created from a json-style dict or json formatted string. Subclasses of this class are guaranteed to be able to be created from a json-style dict or json formatted string.
All subclasses of this class must override de_json. All subclasses of this class must override de_json.
""" """
@classmethod @classmethod
def de_json(self, json_type): def de_json(cls, json_type):
""" """
Returns an instance of this class from the given json dict or string. Returns an instance of this class from the given json dict or string.
@ -52,7 +54,7 @@ class JsonDeserializable:
raise NotImplementedError raise NotImplementedError
@staticmethod @staticmethod
def check_json(self, json_type): def check_json(json_type):
""" """
Checks whether json_type is a dict or a string. If it is already a dict, it is returned as-is. Checks whether json_type is a dict or a string. If it is already a dict, it is returned as-is.
If it is not, it is converted to a dict by means of json.loads(json_type) If it is not, it is converted to a dict by means of json.loads(json_type)
@ -115,37 +117,37 @@ class Message(JsonDeserializable):
opts['text'] = obj['text'] opts['text'] = obj['text']
content_type = 'text' content_type = 'text'
if 'audio' in obj: if 'audio' in obj:
opts['audio'] = Audio.de_json(json.dumps(obj['audio'])) opts['audio'] = Audio.de_json(obj['audio'])
content_type = 'audio' content_type = 'audio'
if 'document' in obj: if 'document' in obj:
opts['document'] = Document.de_json(json.dumps(obj['document'])) opts['document'] = Document.de_json(obj['document'])
content_type = 'document' content_type = 'document'
if 'photo' in obj: if 'photo' in obj:
opts['photo'] = Message.parse_photo(obj['photo']) opts['photo'] = Message.parse_photo(obj['photo'])
content_type = 'photo' content_type = 'photo'
if 'sticker' in obj: if 'sticker' in obj:
opts['sticker'] = Sticker.de_json(json.dumps(obj['sticker'])) opts['sticker'] = Sticker.de_json(obj['sticker'])
content_type = 'sticker' content_type = 'sticker'
if 'video' in obj: if 'video' in obj:
opts['video'] = Video.de_json(json.dumps(obj['video'])) opts['video'] = Video.de_json(obj['video'])
content_type = 'video' content_type = 'video'
if 'location' in obj: if 'location' in obj:
opts['location'] = Location.de_json(json.dumps(obj['location'])) opts['location'] = Location.de_json(obj['location'])
content_type = 'location' content_type = 'location'
return Message(message_id, from_user, date, chat, content_type, opts) return Message(message_id, from_user, date, chat, content_type, opts)
@classmethod @classmethod
def parse_chat(cls, chat): def parse_chat(cls, chat):
if 'first_name' not in chat: if 'first_name' not in chat:
return GroupChat.de_json(json.dumps(chat)) return GroupChat.de_json(chat)
else: else:
return User.de_json(json.dumps(chat)) return User.de_json(chat)
@classmethod @classmethod
def parse_photo(cls, photo_size_array): def parse_photo(cls, photo_size_array):
ret = [] ret = []
for ps in photo_size_array: for ps in photo_size_array:
ret.append(PhotoSize.de_json(json.dumps(ps))) ret.append(PhotoSize.de_json(ps))
return ret return ret
def __init__(self, message_id, from_user, date, chat, content_type, options): def __init__(self, message_id, from_user, date, chat, content_type, options):
@ -205,7 +207,7 @@ class Document(JsonDeserializable):
file_id = obj['file_id'] file_id = obj['file_id']
thumb = None thumb = None
if 'file_id' in obj['thumb']: if 'file_id' in obj['thumb']:
thumb = PhotoSize.de_json(json.dumps(obj['thumb'])) thumb = PhotoSize.de_json(obj['thumb'])
file_name = None file_name = None
mime_type = None mime_type = None
file_size = None file_size = None
@ -232,7 +234,7 @@ class Sticker(JsonDeserializable):
file_id = obj['file_id'] file_id = obj['file_id']
width = obj['width'] width = obj['width']
height = obj['height'] height = obj['height']
thumb = PhotoSize.de_json(json.dumps(obj['thumb'])) thumb = PhotoSize.de_json(obj['thumb'])
file_size = None file_size = None
if 'file_size' in obj: if 'file_size' in obj:
file_size = obj['file_size'] file_size = obj['file_size']
@ -255,7 +257,7 @@ class Video(JsonDeserializable):
height = obj['height'] height = obj['height']
duration = obj['duration'] duration = obj['duration']
if 'file_id' in obj['thumb']: if 'file_id' in obj['thumb']:
thumb = PhotoSize.de_json(json.dumps(obj['thumb'])) thumb = PhotoSize.de_json(obj['thumb'])
caption = None caption = None
mime_type = None mime_type = None
file_size = None file_size = None
@ -304,7 +306,7 @@ class UserProfilePhotos(JsonDeserializable):
def de_json(cls, json_string): def de_json(cls, json_string):
obj = cls.check_json(json_string) obj = cls.check_json(json_string)
total_count = obj['total_count'] total_count = obj['total_count']
photos = [[PhotoSize.de_json(json.dumps(y)) for y in x] for x in obj['photos']] photos = [[PhotoSize.de_json(y) for y in x] for x in obj['photos']]
return UserProfilePhotos(total_count, photos) return UserProfilePhotos(total_count, photos)
def __init__(self, total_count, photos): def __init__(self, total_count, photos):

View File

@ -82,8 +82,9 @@ def test_json_Message_Location():
assert msg.location.latitude == 26.090577 assert msg.location.latitude == 26.090577
assert msg.content_type == 'location' assert msg.content_type == 'location'
def test_json_UserProfilePhotos(): def test_json_UserProfilePhotos():
json_string = r'{"total_count":1,"photos":[[{"file_id":"AgADAgADqacxG6wpRwABvEB6fpeIcKS4HAIkAATZH_SpyZjzIwdVAAIC","file_size":6150,"width":160,"height":160},{"file_id":"AgADAgADqacxG6wpRwABvEB6fpeIcKS4HAIkAATOiTNi_YoJMghVAAIC","file_size":13363,"width":320,"height":320},{"file_id":"AgADAgADqacxG6wpRwABvEB6fpeIcKS4HAIkAAQW4DyFv0-lhglVAAIC","file_size":28347,"width":640,"height":640},{"file_id":"AgADAgADqacxG6wpRwABvEB6fpeIcKS4HAIkAAT50RvJCg0GQApVAAIC","file_size":33953,"width":800,"height":800}]]}' json_string = r'{"total_count":1,"photos":[[{"file_id":"AgADAgADqacxG6wpRwABvEB6fpeIcKS4HAIkAATZH_SpyZjzIwdVAAIC","file_size":6150,"width":160,"height":160},{"file_id":"AgADAgADqacxG6wpRwABvEB6fpeIcKS4HAIkAATOiTNi_YoJMghVAAIC","file_size":13363,"width":320,"height":320},{"file_id":"AgADAgADqacxG6wpRwABvEB6fpeIcKS4HAIkAAQW4DyFv0-lhglVAAIC","file_size":28347,"width":640,"height":640},{"file_id":"AgADAgADqacxG6wpRwABvEB6fpeIcKS4HAIkAAT50RvJCg0GQApVAAIC","file_size":33953,"width":800,"height":800}]]}'
upp = types.UserProfilePhotos.de_json(json_string) upp = types.UserProfilePhotos.de_json(json_string)
assert upp.photos[0][0].width == 160 assert upp.photos[0][0].width == 160
assert upp.photos[0][-1].height == 800 assert upp.photos[0][-1].height == 800