Compatibility fix#124
Conversation
Line 126 gives ValueError when passing sequence. Using any() will allow for full compatibility.
Codecov Report
@@ Coverage Diff @@
## master #124 +/- ##
=======================================
Coverage 46.67% 46.67%
=======================================
Files 4 4
Lines 632 632
Branches 64 64
=======================================
Hits 295 295
Misses 322 322
Partials 15 15
Continue to review full report at Codecov.
|
|
That is interesting, maybe i start to understand your issues. You are using your own data objects and run in issues. |
|
Yes, I believe if you use a more general function to check if a list type is empty, you would increase the compatibility. |
|
We are talking about python iterables. There is at least one we don't want to iterate over its a string. But maybe all others are fine. So you should test for string and than you should try to iterate and if it fails ask for forgiveness and return the object as is. I am interested if you can make it work for your datatypes. |
|
Most important we need more tests for more values. This is very likely to cause issues. I am not sure but probably we loose parameters already here in our actual version and in your one too. So we have to improve the if statement for the values. For example (very uncomplete): |
|
I think I have a function that could handle what is trying to be done. def arg_is_iter(arg):
return '__iter__' in dir(arg)
def any_iter(arg):
if arg_is_iter(arg):
if 'any' in dir(arg):
#Object has built in method
return any_iter(arg.any())
#Call the method again to make sure the object is
#for sure iterable now
return any(arg)
else:
return bool(arg) |
|
Looking at this in hindsight, I sense our attempts to ask permission rather than forgiveness are biting us here. This is the current code: def enumerate_param(param, values):
if not any(values):
# if not values -> returns ValueError
return {}
if not isinstance(values, (list, tuple, set)):
# Coerces a single value to a list before continuing.
values = [values, ]
if not param.endswith('.'):
# Ensure this enumerated param ends in '.'
param += '.'
# Return final output: dict comprehension of the enumerated param and values.
return {
'{}{}'.format(param, idx+1): val
for idx, val in enumerate(values)
}I think we can clean this up for basically two cases:
End result: def enumerate_param(param, values):
# Coerce values to a list if it is not iterable or a string.
if isinstance(values, str):
values = [values,]
if "__iter__" not in dir(values) and "__getitem__" not in dir(values):
values = [values,]
# Ensure this enumerated param ends in '.'
if not param.endswith('.'):
param += '.'
output = {}
for idx, val in enumerate(values):
output['{}{}'.format(param, idx+1)] = val
return outputNo need for a shortcut test, no checking specifically for lists or sets or tuples, basic duck typing. |
GriceTurrble
left a comment
There was a problem hiding this comment.
Despite earlier discussion, this is a small change that has a tangible benefit in this use-case with seemingly little consequence. Accepting.
Thank you! Sorry it took so long.
* upstream/develop: Add Feeds utility to convert dict FeedOptions to string (python-amazon-mws#181) Ensure correct decoding of bytes (python-amazon-mws#168) Compatibility fix (python-amazon-mws#124) Implement a couple of function for the OutboundShipments API. (python-amazon-mws#96) Added OutboundShipments.create_fulfillment_order (python-amazon-mws#95) added UpdateReportAcknowledgements (python-amazon-mws#101) Update feeds.py (python-amazon-mws#166) added easyship api support for indian marketplace (python-amazon-mws#169) Moving Slack link up, removing gitter chat link add all current marketplaces and alphabetize by country code (python-amazon-mws#155) Updated slack invite link Slack invite link (python-amazon-mws#152) Slack invite link fix clean_params (python-amazon-mws#106) add in NL marketplace information Fix flake8 warnings Update develop branch CI + package metadata include mws.apis in setup.py Fix bug: Remove trailing comma
Line 126 gives ValueError when passing sequence. Using any() will allow for full compatibility.