X Tutup
Skip to content

Commit 1879cff

Browse files
committed
Merge pull request python-telegram-bot#85 from jh0ker/master
Decode Emoji byte strings into unicode strings if using Python 3
2 parents cd5e805 + a6c12ad commit 1879cff

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ The following wonderful people contributed directly or indirectly to this projec
1313
- `ErgoZ Riftbit Vaper <https://github.com/ergoz>`_
1414
- `franciscod <https://github.com/franciscod>`_
1515
- `JASON0916 <https://github.com/JASON0916>`_
16+
- `jh0ker <https://github.com/jh0ker>`_
1617
- `JRoot3D <https://github.com/JRoot3D>`_
1718
- `macrojames <https://github.com/macrojames>`_
1819
- `njittam <https://github.com/njittam>`_

telegram/emoji.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,43 @@
2020

2121
"""This module contains a object that represents an Emoji"""
2222

23+
import sys
2324

25+
def call_decode_byte_strings(cls):
26+
"""
27+
Calls the _decode_byte_strings function of the created class
28+
29+
Args:
30+
cls (Class):
31+
32+
Returns:
33+
Class:
34+
"""
35+
36+
cls._decode_byte_strings()
37+
return cls
38+
39+
@call_decode_byte_strings
2440
class Emoji(object):
2541
"""This object represents an Emoji."""
2642

43+
@classmethod
44+
def _decode_byte_strings(cls):
45+
"""
46+
Decodes the Emojis into unicode strings if using Python 3
47+
48+
Args:
49+
cls (Class):
50+
51+
Returns:
52+
"""
53+
54+
if sys.version_info.major is 3:
55+
emojis = filter(lambda attr : type(getattr(cls, attr)) is bytes,
56+
dir(cls))
57+
for var in emojis:
58+
setattr(cls, var, getattr(cls, var).decode('utf-8'))
59+
2760
GRINNING_FACE_WITH_SMILING_EYES = b'\xF0\x9F\x98\x81'
2861
FACE_WITH_TEARS_OF_JOY = b'\xF0\x9F\x98\x82'
2962
SMILING_FACE_WITH_OPEN_MOUTH = b'\xF0\x9F\x98\x83'

tests/test_emoji.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env python
2+
#
3+
# A library that provides a Python interface to the Telegram Bot API
4+
# Copyright (C) 2015 Leandro Toledo de Souza <leandrotoeldodesouza@gmail.com>
5+
#
6+
# This program is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU General Public License as published by
8+
# the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# This program is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU General Public License
17+
# along with this program. If not, see [http://www.gnu.org/licenses/].
18+
19+
"""This module contains a object that represents Tests for Telegram Emoji"""
20+
21+
import os
22+
import unittest
23+
import sys
24+
sys.path.append('.')
25+
26+
import telegram
27+
from telegram.emoji import Emoji
28+
from tests.base import BaseTest
29+
30+
31+
class EmojiTest(BaseTest, unittest.TestCase):
32+
"""This object represents Tests for Telegram Emoji."""
33+
34+
def test_emoji(self):
35+
"""Test Emoji class"""
36+
print('Testing Emoji class')
37+
38+
for attr in dir(Emoji):
39+
if attr[0] != '_': # TODO: dirty way to filter out functions
40+
self.assertTrue(type(getattr(Emoji, attr)) is str)
41+
42+
43+
if __name__ == '__main__':
44+
unittest.main()

0 commit comments

Comments
 (0)
X Tutup