Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions kaggle/api/kaggle_api_extended.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
from os.path import expanduser, isfile
from datetime import datetime

try:
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.


class KaggleApi(KaggleApi):
configPath = os.path.join(expanduser("~"),".kaggle")
Expand Down Expand Up @@ -245,10 +250,4 @@ def printCsv(self, items, fields):
writer.writerow(i_fields)

def string(self, item):
try:
if isinstance(item, unicode):
return item
else:
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.