Merge pull request #85 from jh0ker/master

Decode Emoji byte strings into unicode strings if using Python 3
This commit is contained in:
Leandro Toledo 2015-11-10 11:55:19 -02:00
commit 1879cff82d
3 changed files with 78 additions and 0 deletions

View file

@ -13,6 +13,7 @@ The following wonderful people contributed directly or indirectly to this projec
- `ErgoZ Riftbit Vaper <https://github.com/ergoz>`_
- `franciscod <https://github.com/franciscod>`_
- `JASON0916 <https://github.com/JASON0916>`_
- `jh0ker <https://github.com/jh0ker>`_
- `JRoot3D <https://github.com/JRoot3D>`_
- `macrojames <https://github.com/macrojames>`_
- `njittam <https://github.com/njittam>`_

View file

@ -20,10 +20,43 @@
"""This module contains a object that represents an Emoji"""
import sys
def call_decode_byte_strings(cls):
"""
Calls the _decode_byte_strings function of the created class
Args:
cls (Class):
Returns:
Class:
"""
cls._decode_byte_strings()
return cls
@call_decode_byte_strings
class Emoji(object):
"""This object represents an Emoji."""
@classmethod
def _decode_byte_strings(cls):
"""
Decodes the Emojis into unicode strings if using Python 3
Args:
cls (Class):
Returns:
"""
if sys.version_info.major is 3:
emojis = filter(lambda attr : type(getattr(cls, attr)) is bytes,
dir(cls))
for var in emojis:
setattr(cls, var, getattr(cls, var).decode('utf-8'))
GRINNING_FACE_WITH_SMILING_EYES = b'\xF0\x9F\x98\x81'
FACE_WITH_TEARS_OF_JOY = b'\xF0\x9F\x98\x82'
SMILING_FACE_WITH_OPEN_MOUTH = b'\xF0\x9F\x98\x83'

44
tests/test_emoji.py Normal file
View file

@ -0,0 +1,44 @@
#!/usr/bin/env python
#
# A library that provides a Python interface to the Telegram Bot API
# Copyright (C) 2015 Leandro Toledo de Souza <leandrotoeldodesouza@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains a object that represents Tests for Telegram Emoji"""
import os
import unittest
import sys
sys.path.append('.')
import telegram
from telegram.emoji import Emoji
from tests.base import BaseTest
class EmojiTest(BaseTest, unittest.TestCase):
"""This object represents Tests for Telegram Emoji."""
def test_emoji(self):
"""Test Emoji class"""
print('Testing Emoji class')
for attr in dir(Emoji):
if attr[0] != '_': # TODO: dirty way to filter out functions
self.assertTrue(type(getattr(Emoji, attr)) is str)
if __name__ == '__main__':
unittest.main()