X Tutup
Skip to content

Commit a86fc6c

Browse files
committed
Improving the design of existing Telegram classes
1 parent 1c45951 commit a86fc6c

20 files changed

+84
-461
lines changed

Makefile

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.PHONY: clean test lint help
1+
.PHONY: clean pep8 lint test
22

33
clean:
44
rm -fr build
@@ -7,13 +7,18 @@ clean:
77
find . -name '*.pyo' -exec rm -f {} \;
88
find . -name '*~' -exec rm -f {} \;
99

10+
pep8:
11+
flake8 telegram
12+
1013
lint:
11-
flake8 --doctests --max-complexity 10 telegram
14+
pylint -E telegram
1215

1316
test:
1417
@- $(foreach TEST, $(wildcard tests/test_*.py), python $(TEST))
1518

1619
help:
17-
@echo " clean remove unwanted stuff"
18-
@echo " lint check style with flake8"
19-
@echo " test run tests"
20+
@echo "Available targets:"
21+
@echo "- clean Clean up the source directory"
22+
@echo "- pep8 Check style with flake8"
23+
@echo "- lint Check style with pylint"
24+
@echo "- test Run tests"

telegram/audio.py

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -69,37 +69,4 @@ def de_json(data):
6969
if not data:
7070
return None
7171

72-
audio = dict()
73-
74-
# Required
75-
audio['file_id'] = data['file_id']
76-
audio['duration'] = data['duration']
77-
# Optionals
78-
audio['performer'] = data.get('performer')
79-
audio['title'] = data.get('title')
80-
audio['mime_type'] = data.get('mime_type')
81-
audio['file_size'] = data.get('file_size', 0)
82-
83-
return Audio(**audio)
84-
85-
def to_dict(self):
86-
"""
87-
Returns:
88-
dict:
89-
"""
90-
data = dict()
91-
92-
# Required
93-
data['file_id'] = self.file_id
94-
data['duration'] = self.duration
95-
# Optionals
96-
if self.performer:
97-
data['performer'] = self.performer
98-
if self.title:
99-
data['title'] = self.title
100-
if self.mime_type:
101-
data['mime_type'] = self.mime_type
102-
if self.file_size:
103-
data['file_size'] = self.file_size
104-
105-
return data
72+
return Audio(**data)

telegram/base.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"""Base class for Telegram Objects"""
2020

2121
import json
22-
from abc import ABCMeta, abstractmethod
22+
from abc import ABCMeta
2323

2424

2525
class TelegramObject(object):
@@ -51,10 +51,18 @@ def to_json(self):
5151
"""
5252
return json.dumps(self.to_dict())
5353

54-
@abstractmethod
5554
def to_dict(self):
5655
"""
5756
Returns:
5857
dict:
5958
"""
60-
return None
59+
data = dict()
60+
61+
for key, value in self.__dict__.iteritems():
62+
if value:
63+
if hasattr(value, 'to_dict'):
64+
data[key] = value.to_dict()
65+
else:
66+
data[key] = value
67+
68+
return data

telegram/contact.py

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -63,31 +63,4 @@ def de_json(data):
6363
if not data:
6464
return None
6565

66-
contact = dict()
67-
68-
# Required
69-
contact['phone_number'] = data['phone_number']
70-
contact['first_name'] = data['first_name']
71-
# Optionals
72-
contact['last_name'] = data.get('last_name')
73-
contact['user_id'] = data.get('user_id', 0)
74-
75-
return Contact(**contact)
76-
77-
def to_dict(self):
78-
"""
79-
Returns:
80-
dict:
81-
"""
82-
data = dict()
83-
84-
# Required
85-
data['phone_number'] = self.phone_number
86-
data['first_name'] = self.first_name
87-
# Optionals
88-
if self.last_name:
89-
data['last_name'] = self.last_name
90-
if self.user_id:
91-
data['user_id'] = self.user_id
92-
93-
return data
66+
return Contact(**data)

telegram/document.py

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -65,35 +65,4 @@ def de_json(data):
6565
if not data:
6666
return None
6767

68-
document = dict()
69-
70-
# Required
71-
document['file_id'] = data['file_id']
72-
# Optionals
73-
document['thumb'] = PhotoSize.de_json(data.get('thumb'))
74-
document['file_name'] = data.get('file_name')
75-
document['mime_type'] = data.get('mime_type')
76-
document['file_size'] = data.get('file_size', 0)
77-
78-
return Document(**document)
79-
80-
def to_dict(self):
81-
"""
82-
Returns:
83-
dict:
84-
"""
85-
data = dict()
86-
87-
# Required
88-
data['file_id'] = self.file_id
89-
# Optionals
90-
if self.thumb:
91-
data['thumb'] = self.thumb.to_dict()
92-
if self.file_name:
93-
data['file_name'] = self.file_name
94-
if self.mime_type:
95-
data['mime_type'] = self.mime_type
96-
if self.file_size:
97-
data['file_size'] = self.file_size
98-
99-
return data
68+
return Document(**data)

telegram/forcereply.py

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -53,26 +53,8 @@ def de_json(data):
5353
Returns:
5454
telegram.ForceReply:
5555
"""
56-
force_reply = dict()
56+
if not data:
57+
return None
5758

58-
# Required
59-
force_reply['force_reply'] = data['force_reply']
60-
# Optionals
61-
force_reply['selective'] = data.get('selective', False)
62-
63-
return ForceReply(**force_reply)
64-
65-
def to_dict(self):
66-
"""
67-
Returns:
68-
dict:
69-
"""
70-
data = dict()
71-
72-
# Required
73-
data['force_reply'] = self.force_reply
74-
# Optionals
75-
if self.selective:
76-
data['selective'] = self.selective
77-
78-
return data
59+
return ForceReply(**data)
60+

telegram/groupchat.py

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -53,23 +53,4 @@ def de_json(data):
5353
if not data:
5454
return None
5555

56-
groupchat = dict()
57-
58-
# Required
59-
groupchat['id'] = data['id']
60-
groupchat['title'] = data['title']
61-
62-
return GroupChat(**groupchat)
63-
64-
def to_dict(self):
65-
"""
66-
Returns:
67-
dict:
68-
"""
69-
data = dict()
70-
71-
# Required
72-
data['id'] = self.id
73-
data['title'] = self.title
74-
75-
return data
56+
return GroupChat(**data)

telegram/location.py

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -52,23 +52,4 @@ def de_json(data):
5252
if not data:
5353
return None
5454

55-
location = dict()
56-
57-
# Required
58-
location['longitude'] = data['longitude']
59-
location['latitude'] = data['latitude']
60-
61-
return Location(**location)
62-
63-
def to_dict(self):
64-
"""
65-
Returns:
66-
dict:
67-
"""
68-
data = dict()
69-
70-
# Required
71-
data['longitude'] = self.longitude
72-
data['latitude'] = self.latitude
73-
74-
return data
55+
return Location(**data)

telegram/message.py

Lines changed: 23 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -134,110 +134,60 @@ def de_json(data):
134134
if not data:
135135
return None
136136

137-
message = dict()
138-
139-
# Required
140-
message['message_id'] = data['message_id']
141-
message['from_user'] = User.de_json(data['from'])
142-
message['date'] = datetime.fromtimestamp(data['date'])
143-
# Optionals
137+
data['from_user'] = User.de_json(data['from'])
138+
data['date'] = datetime.fromtimestamp(data['date'])
144139
if 'first_name' in data.get('chat', ''):
145-
message['chat'] = User.de_json(data.get('chat'))
140+
data['chat'] = User.de_json(data.get('chat'))
146141
elif 'title' in data.get('chat', ''):
147-
message['chat'] = GroupChat.de_json(data.get('chat'))
148-
message['forward_from'] = \
142+
data['chat'] = GroupChat.de_json(data.get('chat'))
143+
data['forward_from'] = \
149144
User.de_json(data.get('forward_from'))
150-
message['forward_date'] = \
145+
data['forward_date'] = \
151146
Message._fromtimestamp(data.get('forward_date'))
152-
message['reply_to_message'] = \
147+
data['reply_to_message'] = \
153148
Message.de_json(data.get('reply_to_message'))
154-
message['text'] = \
155-
data.get('text')
156-
message['audio'] = \
149+
data['audio'] = \
157150
Audio.de_json(data.get('audio'))
158-
message['document'] = \
151+
data['document'] = \
159152
Document.de_json(data.get('document'))
160-
message['photo'] = \
161-
[PhotoSize.de_json(x) for x in data.get('photo', [])]
162-
message['sticker'] = \
153+
data['photo'] = \
154+
PhotoSize.de_list(data.get('photo'))
155+
data['sticker'] = \
163156
Sticker.de_json(data.get('sticker'))
164-
message['video'] = \
157+
data['video'] = \
165158
Video.de_json(data.get('video'))
166-
message['voice'] = \
159+
data['voice'] = \
167160
Voice.de_json(data.get('voice'))
168-
message['caption'] = \
169-
data.get('caption')
170-
message['contact'] = \
161+
data['contact'] = \
171162
Contact.de_json(data.get('contact'))
172-
message['location'] = \
163+
data['location'] = \
173164
Location.de_json(data.get('location'))
174-
message['new_chat_participant'] = \
165+
data['new_chat_participant'] = \
175166
User.de_json(data.get('new_chat_participant'))
176-
message['left_chat_participant'] = \
167+
data['left_chat_participant'] = \
177168
User.de_json(data.get('left_chat_participant'))
178-
message['new_chat_title'] = \
179-
data.get('new_chat_title')
180-
message['new_chat_photo'] = \
181-
[PhotoSize.de_json(x) for x in data.get('new_chat_photo', [])]
182-
message['delete_chat_photo'] = \
183-
data.get('delete_chat_photo')
184-
message['group_chat_created'] = \
185-
data.get('group_chat_created')
169+
data['new_chat_photo'] = \
170+
PhotoSize.de_list(data.get('new_chat_photo'))
186171

187-
return Message(**message)
172+
return Message(**data)
188173

189174
def to_dict(self):
190175
"""
191176
Returns:
192177
dict:
193178
"""
194-
data = dict()
179+
data = super(Message, self).to_dict()
195180

196181
# Required
197-
data['message_id'] = self.message_id
198-
data['from'] = self.from_user.to_dict()
182+
data['from'] = data.pop('from_user')
199183
data['date'] = self._totimestamp(self.date)
200-
data['chat'] = self.chat.to_dict()
201184
# Optionals
202-
if self.forward_from:
203-
data['forward_from'] = self.forward_from.to_dict()
204185
if self.forward_date:
205186
data['forward_date'] = self._totimestamp(self.forward_date)
206-
if self.reply_to_message:
207-
data['reply_to_message'] = self.reply_to_message.to_dict()
208-
if self.text:
209-
data['text'] = self.text
210-
if self.audio:
211-
data['audio'] = self.audio.to_dict()
212-
if self.document:
213-
data['document'] = self.document.to_dict()
214187
if self.photo:
215188
data['photo'] = [p.to_dict() for p in self.photo]
216-
if self.sticker:
217-
data['sticker'] = self.sticker.to_dict()
218-
if self.video:
219-
data['video'] = self.video.to_dict()
220-
if self.voice:
221-
data['voice'] = self.voice.to_dict()
222-
if self.caption:
223-
data['caption'] = self.caption
224-
if self.contact:
225-
data['contact'] = self.contact.to_dict()
226-
if self.location:
227-
data['location'] = self.location.to_dict()
228-
if self.new_chat_participant:
229-
data['new_chat_participant'] = self.new_chat_participant.to_dict()
230-
if self.left_chat_participant:
231-
data['left_chat_participant'] = \
232-
self.left_chat_participant.to_dict()
233-
if self.new_chat_title:
234-
data['new_chat_title'] = self.new_chat_title
235189
if self.new_chat_photo:
236190
data['new_chat_photo'] = [p.to_dict() for p in self.new_chat_photo]
237-
if self.delete_chat_photo:
238-
data['delete_chat_photo'] = self.delete_chat_photo
239-
if self.group_chat_created:
240-
data['group_chat_created'] = self.group_chat_created
241191

242192
return data
243193

0 commit comments

Comments
 (0)
X Tutup