From ce852de9a8cbc4e5c3f7485037c5e91ddb3c2bb4 Mon Sep 17 00:00:00 2001 From: JASON0916 Date: Fri, 17 Jul 2015 22:53:54 +0800 Subject: [PATCH] base class for telegram class --- telegram/audio.py | 6 ++---- telegram/contact.py | 8 +++----- telegram/document.py | 8 +++----- telegram/forcereply.py | 5 +---- telegram/groupchat.py | 8 +++----- telegram/location.py | 8 +++----- telegram/message.py | 8 +++----- telegram/photosize.py | 8 +++----- telegram/replymarkup.py | 8 +++----- telegram/sticker.py | 8 +++----- telegram/telegram_boject_base.py | 26 ++++++++++++++++++++++++++ telegram/update.py | 8 +++----- telegram/user.py | 8 +++----- telegram/userprofilephotos.py | 8 +++----- telegram/video.py | 8 +++----- 15 files changed, 65 insertions(+), 68 deletions(-) create mode 100644 telegram/telegram_boject_base.py diff --git a/telegram/audio.py b/telegram/audio.py index 6595a099e..17ba6c184 100644 --- a/telegram/audio.py +++ b/telegram/audio.py @@ -2,9 +2,10 @@ import json +from .telegram_boject_base import Base -class Audio(object): +class Audio(Base): def __init__(self, file_id, duration, @@ -30,6 +31,3 @@ class Audio(object): if self.file_size: json_data['file_size'] = self.file_size return json.dumps(json_data) - - def __str__(self): - return self.to_json() diff --git a/telegram/contact.py b/telegram/contact.py index fdde8230d..dd7edb768 100644 --- a/telegram/contact.py +++ b/telegram/contact.py @@ -2,9 +2,10 @@ import json +from .telegram_boject_base import Base -class Contact(object): +class Contact(Base): def __init__(self, phone_number, first_name, @@ -29,7 +30,4 @@ class Contact(object): json_data['last_name'] = self.last_name if self.user_id: json_data['user_id'] = self.user_id - return json.dumps(json_data) - - def __str__(self): - return self.to_json() + return json.dumps(json_data) \ No newline at end of file diff --git a/telegram/document.py b/telegram/document.py index f058a3e9c..65acf1611 100644 --- a/telegram/document.py +++ b/telegram/document.py @@ -2,9 +2,10 @@ import json +from .telegram_boject_base import Base -class Document(object): +class Document(Base): def __init__(self, file_id, thumb, @@ -40,7 +41,4 @@ class Document(object): json_data['mime_type'] = self.mime_type if self.file_size: json_data['file_size'] = self.file_size - return json.dumps(json_data) - - def __str__(self): - return self.to_json() + return json.dumps(json_data) \ No newline at end of file diff --git a/telegram/forcereply.py b/telegram/forcereply.py index 0dca6fb4d..e531f371b 100644 --- a/telegram/forcereply.py +++ b/telegram/forcereply.py @@ -21,7 +21,4 @@ class ForceReply(ReplyMarkup): json_data = {'force_reply': self.force_reply} if self.selective: json_data['selective'] = self.selective - return json.dumps(json_data) - - def __str__(self): - return self.to_json() + return json.dumps(json_data) \ No newline at end of file diff --git a/telegram/groupchat.py b/telegram/groupchat.py index 7408aebfa..aa6d707e5 100644 --- a/telegram/groupchat.py +++ b/telegram/groupchat.py @@ -2,9 +2,10 @@ import json +from .telegram_boject_base import Base -class GroupChat(object): +class GroupChat(Base): def __init__(self, id, title): @@ -19,7 +20,4 @@ class GroupChat(object): def to_json(self): json_data = {'id': self.id, 'title': self.title} - return json.dumps(json_data) - - def __str__(self): - return self.to_json() + return json.dumps(json_data) \ No newline at end of file diff --git a/telegram/location.py b/telegram/location.py index 5148101a2..66de52d82 100644 --- a/telegram/location.py +++ b/telegram/location.py @@ -2,9 +2,10 @@ import json +from .telegram_boject_base import Base -class Location(object): +class Location(Base): def __init__(self, longitude, latitude): @@ -19,7 +20,4 @@ class Location(object): def to_json(self): json_data = {'longitude': self.longitude, 'latitude': self.latitude} - return json.dumps(json_data) - - def __str__(self): - return self.to_json() + return json.dumps(json_data) \ No newline at end of file diff --git a/telegram/message.py b/telegram/message.py index 3668b20ce..62295ad31 100644 --- a/telegram/message.py +++ b/telegram/message.py @@ -2,9 +2,10 @@ import json +from .telegram_boject_base import Base -class Message(object): +class Message(Base): def __init__(self, message_id, from_user, @@ -202,7 +203,4 @@ class Message(object): json_data['delete_chat_photo'] = self.delete_chat_photo if self.group_chat_created: json_data['group_chat_created'] = self.group_chat_created - return json.dumps(json_data) - - def __str__(self): - return self.to_json() + return json.dumps(json_data) \ No newline at end of file diff --git a/telegram/photosize.py b/telegram/photosize.py index 174f9d650..5ee587ab7 100644 --- a/telegram/photosize.py +++ b/telegram/photosize.py @@ -2,9 +2,10 @@ import json +from .telegram_boject_base import Base -class PhotoSize(object): +class PhotoSize(Base): def __init__(self, file_id, width, @@ -28,7 +29,4 @@ class PhotoSize(object): 'height': self.height} if self.file_size: json_data['file_size'] = self.file_size - return json.dumps(json_data) - - def __str__(self): - return self.to_json() + return json.dumps(json_data) \ No newline at end of file diff --git a/telegram/replymarkup.py b/telegram/replymarkup.py index b582a5e66..de6992a89 100644 --- a/telegram/replymarkup.py +++ b/telegram/replymarkup.py @@ -1,9 +1,7 @@ #!/usr/bin/env python +from .telegram_boject_base import Base -class ReplyMarkup(object): +class ReplyMarkup(Base): def to_json(self): - raise NotImplementedError - - def __str__(self): - return self.to_json() + raise NotImplementedError \ No newline at end of file diff --git a/telegram/sticker.py b/telegram/sticker.py index 84464bf17..536dcbe97 100644 --- a/telegram/sticker.py +++ b/telegram/sticker.py @@ -2,9 +2,10 @@ import json +from .telegram_boject_base import Base -class Sticker(object): +class Sticker(Base): def __init__(self, file_id, width, @@ -38,7 +39,4 @@ class Sticker(object): 'thumb': self.thumb.to_json()} if self.file_size: json_data['file_size'] = self.file_size - return json.dumps(json_data) - - def __str__(self): - return self.to_json() + return json.dumps(json_data) \ No newline at end of file diff --git a/telegram/telegram_boject_base.py b/telegram/telegram_boject_base.py new file mode 100644 index 000000000..764f48cc2 --- /dev/null +++ b/telegram/telegram_boject_base.py @@ -0,0 +1,26 @@ +from abc import ABCMeta, abstractmethod +from telegram import TelegramError + + +class Base(object): + + """Base class for most telegram object""" + + __metaclass__ = ABCMeta + + def __str__(self): + return self.to_json() + + def __getitem__(self, item): + try: + return self.__dict__[item] + except KeyError as e: + raise TelegramError(str(e)) + + @staticmethod + def de_json(data): + pass + + @abstractmethod + def to_json(self): + pass \ No newline at end of file diff --git a/telegram/update.py b/telegram/update.py index 2c7cfb675..daf85ad45 100644 --- a/telegram/update.py +++ b/telegram/update.py @@ -2,9 +2,10 @@ import json +from .telegram_boject_base import Base -class Update(object): +class Update(Base): def __init__(self, update_id, message=None): @@ -26,7 +27,4 @@ class Update(object): json_data = {'update_id': self.update_id} if self.message: json_data['message'] = self.message.to_json() - return json.dumps(json_data) - - def __str__(self): - return self.to_json() + return json.dumps(json_data) \ No newline at end of file diff --git a/telegram/user.py b/telegram/user.py index bcd1e4bb4..d8e61412e 100644 --- a/telegram/user.py +++ b/telegram/user.py @@ -2,9 +2,10 @@ import json +from .telegram_boject_base import Base -class User(object): +class User(Base): def __init__(self, id, first_name, @@ -37,7 +38,4 @@ class User(object): json_data['last_name'] = self.last_name if self.username: json_data['username'] = self.username - return json.dumps(json_data) - - def __str__(self): - return self.to_json() + return json.dumps(json_data) \ No newline at end of file diff --git a/telegram/userprofilephotos.py b/telegram/userprofilephotos.py index d304ff11a..c583585e4 100644 --- a/telegram/userprofilephotos.py +++ b/telegram/userprofilephotos.py @@ -2,9 +2,10 @@ import json +from .telegram_boject_base import Base -class UserProfilePhotos(object): +class UserProfilePhotos(Base): def __init__(self, total_count, photos): @@ -32,7 +33,4 @@ class UserProfilePhotos(object): json_data['photos'] = [] for photo in self.photos: json_data['photos'].append([x.to_json() for x in photo]) - return json.dumps(json_data) - - def __str__(self): - return self.to_json() + return json.dumps(json_data) \ No newline at end of file diff --git a/telegram/video.py b/telegram/video.py index fd589e2d5..3689bdd98 100644 --- a/telegram/video.py +++ b/telegram/video.py @@ -2,9 +2,10 @@ import json +from .telegram_boject_base import Base -class Video(object): +class Video(Base): def __init__(self, file_id, width, @@ -52,7 +53,4 @@ class Video(object): json_data['file_size'] = self.file_size if self.caption: json_data['caption'] = self.caption - return json.dumps(json_data) - - def __str__(self): - return self.to_json() + return json.dumps(json_data) \ No newline at end of file