Skip to content

Conversation

@cclauss
Copy link

@cclauss cclauss commented Feb 1, 2018

No description provided.

unicode # Python 2
except NameError:
unicode = str # Python 3

Copy link

@delirious-lettuce delirious-lettuce Feb 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The try/except seems a bit odd given the fact that six is already being installed in setup.py.

install_requires = ['urllib3 >= 1.15', 'six >= 1.10', 'certifi', 'python-dateutil'],

Is there any particular reason you didn't just import six and use six.text_type in the string function below?

Copy link
Author

@cclauss cclauss Feb 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@delirious-lettuce Good to bump into you again on the interwebs... Perhaps your approach is acceptable to the maintainers and is clearly much cleaner but it slightly changes the logic in Python 2. To my mind, the current code reads: if item is unicode then return it, otherwise return str(item). This means that in Python 2 string(42) returns ‘42’, not u’42’. While type('42') == type(u'42') is True in Python 3, it is False in Python 2.

I hope the maintainers find your approach acceptable because then we could just eliminate string() which is currently masking the Python standard library string module.

Copy link

@delirious-lettuce delirious-lettuce Feb 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cclauss ,

Nice to bump into you again as well... but I'm not sure I understand your comment.

To my mind, the current code reads: if item is unicode then return it, otherwise return str(item). This means that in Python 2 string(42) returns ‘42’, not u’42’. While type('42') == type(u'42') is True in Python 3, it is False in Python 2.

>>> import six
... 
... try:
...     unicode  # Python 2
... except NameError:
...     unicode = str  # Python 3
... 
... 
... def string(item):
...     try:
...         if isinstance(item, unicode):
...             return item
...         else:
...             return str(item)
...     except:
...         return str(item)
... 
... 
... def string_six(item):
...     return item if isinstance(item, six.text_type) else str(item)
... 

# Python 2.7.14 (default, Dec 12 2017, 12:40:29) 
# [GCC 5.4.0 20160609] on linux2
>>> string(42) == string_six(42)
True
>>> string(u'some unicode') == string_six(u'some unicode')
True

# Python 3.6.3 (default, Dec 12 2017, 12:53:45) 
# [GCC 5.4.0 20160609] on linux
>>> string(42) == string_six(42)
True
>>> string(u'some unicode') == string_six(u'some unicode')
True

It seems like the same output but you get to remove the try/except at the top by simply replacing unicode with six.text_type inside of isinstance. Maybe I am missing something?

Also, I agree with you that the name of this function should be changed to something other than string.

return str(item)
except:
return str(item)
return item if isinstance(item, unicode) else str(item)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@timoboz timoboz merged commit d294d09 into Kaggle:master Mar 20, 2018
@cclauss cclauss deleted the patch-2 branch March 20, 2018 05:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants