Skip to content

Commit

Permalink
encoding.to_binary and to_unicode now transform other datatypes into …
Browse files Browse the repository at this point in the history
…that as well, so an int becomes a string in requested format. Fixes issue #32, and so closes issue #31
  • Loading branch information
luckydonald committed Jul 2, 2015
1 parent 90aaaab commit b179974
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 24 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

#Changelog.

## Version 0.4.1c: ##
- Bug Fix: encoding.to_binary and to_unicode now transform other datatypes (like int) into that type as well. Fixes [issue #32](https://github.com/luckydonald/pytg/issues/32), and that closes [issue 31](https://github.com/luckydonald/pytg/issues/31)

## Version 0.4.1b: ##
- added ```Receiver.queued_messages()```, showing how many messages are waiting in the queue.

Expand Down
2 changes: 1 addition & 1 deletion pytg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


__all__ = ["receiver", "sender", "Telegram"]
VERSION = "0.4.1b"
VERSION = "0.4.1c"

class Telegram(object):
"""
Expand Down
50 changes: 27 additions & 23 deletions pytg/encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,37 @@
import sys

if sys.version < '3': # python 2.7
text_type = unicode
binary_type = str
native_type = binary_type
long_int = long
def to_native(x):
return to_binary(x)
text_type = unicode
binary_type = str
native_type = binary_type
long_int = long
def to_native(x):
return to_binary(x)
else: # python 3
text_type = str
binary_type = bytes
native_type = text_type
long_int = int
def to_native(x):
return to_unicode(x)
text_type = str
binary_type = bytes
native_type = text_type
long_int = int
def to_native(x):
return to_unicode(x)


def to_binary(x):
if isinstance(x, text_type):
return x.encode("utf-8")
else:
return x
def to_binary(x, force=False):

This comment has been minimized.

Copy link
@luckydonald

luckydonald Sep 10, 2015

Author Owner

What does that force do anyway?

if isinstance(x, text_type):
return x.encode("utf-8")
elif isinstance(x, binary_type):
return x
else:
return to_binary(str(x))


def to_unicode(x):
if isinstance(x, binary_type):
if x == b'\\':
return u"\\"
return x.decode("utf-8")
else:
return x
if isinstance(x, binary_type):
if x == b'\\':
return u"\\"
return x.decode("utf-8")
elif isinstance(x, text_type):
return x
else:
return to_unicode(str(x))

0 comments on commit b179974

Please sign in to comment.