Skip to content

Commit

Permalink
[#27] Dynamic instance method definition for Python2/3
Browse files Browse the repository at this point in the history
  • Loading branch information
adetorcy committed Dec 8, 2017
1 parent b99f4c7 commit f297a61
Showing 1 changed file with 31 additions and 10 deletions.
41 changes: 31 additions & 10 deletions irods/message/property.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,18 @@ def __init__(self, length=None):
self.length = length
super(BinaryProperty, self).__init__()

def format(self, value):
if six.PY3 and not isinstance(value, bytes):
val = b64encode(value.encode('utf-8'))
else:
val = b64encode(value)
return val
if six.PY2:
def format(self, value):
return b64encode(value)

else:
# Python 3
def format(self, value):
if isinstance(value, bytes):
return b64encode(value)
else:
return b64encode(value.encode())


def parse(self, value):
val = b64decode(value)
Expand All @@ -76,10 +82,25 @@ def __init__(self, length=None):
self.length = length
super(StringProperty, self).__init__()

def format(self, value):
if six.PY2 and isinstance(value, unicode):
return value
return str(value)

if six.PY2:
def format(self, value):
if isinstance(value, str) or isinstance(value, unicode):
return value

return str(value)

else:
# Python 3
def format(self, value):
if isinstance(value, str):
return value

if isinstance(value, bytes):
return value.decode()

return str(value)


def parse(self, value):
return value
Expand Down

0 comments on commit f297a61

Please sign in to comment.