-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Simplify the unicode logic to avoid bare exception #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,11 @@ | |
| from os.path import expanduser, isfile | ||
| from datetime import datetime | ||
|
|
||
| try: | ||
| unicode # Python 2 | ||
| except NameError: | ||
| unicode = str # Python 3 | ||
|
|
||
|
|
||
| class KaggleApi(KaggleApi): | ||
| configPath = os.path.join(expanduser("~"),".kaggle") | ||
|
|
@@ -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) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
try/exceptseems a bit odd given the fact thatsixis already being installed insetup.py.kaggle-api/setup.py
Line 17 in 4625bc7
Is there any particular reason you didn't just import
sixand usesix.text_typein thestringfunction below?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.
It seems like the same output but you get to remove the
try/exceptat the top by simply replacingunicodewithsix.text_typeinside ofisinstance. Maybe I am missing something?Also, I agree with you that the name of this function should be changed to something other than
string.