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
TimeDelta #148
TimeDelta #148
Conversation
The Travis failure is not caused by my code. Can I increase to 110 the line length? It's better. |
except (TypeError, AttributeError, ValueError): | ||
value = int(value) | ||
except (TypeError, ValueError): | ||
msg = '{0!r} cannot be interpreted as a valid period of time.'.format(value) |
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.
Can you please add a test that reaches this branch?
Looks good. Just a couple more tests, and this should be good to merge. |
The # ValueError
int('')
# TypeError
int([])
# OverflowError
field = fields.TimeDelta(fields.TimeDelta.DAYS)
field.deserialize(9999999999) |
Right, those branches are reached; could you also make assertions about the error messages? e.g. with pytest.raises(UnmarshallingError) as excinfo:
field.deserialize(in_value)
expected = ('{0!r} cannot be interpreted as '
'a valid period of time.').format(in_value)
assert expected in str(excinfo) |
Done |
return total_seconds(value) | ||
days = value.days | ||
seconds = days * 86400 + value.seconds | ||
microseconds = seconds * 10**6 + value.microseconds |
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.
I wonder if we can save unneeded computation here by only computing to the requested precision. Something like:
def _serialize(self, value, attr, obj):
try:
days = value.days
if self.precision == self.DAYS:
return days
else:
seconds = days * 86400 + value.seconds
if self.precision == self.SECONDS:
return seconds
else: # microseconds
return seconds * 10**6 + value.microseconds
except AttributeError:
msg = '{0!r} cannot be formatted as a timedelta.'.format()
raise MarshallingError(getattr(self, 'error', None) or msg)
Perhaps there is a more elegant way, but it would be good remove the extra computation.
I thought about it. The CPU cycles waste is really negligible (integer math). Saving them would mean uglier code, like the big |
And I'd be curious to benchmark them: full C speed integer math vs big |
Here's a quick-and-dirty comparison of serializing a bunch of |
LOL, you really have benchmarked them! However, this confirms that we don't have to worry about performance in this case and that we can avoid ugly code. The difference is negligible and probably within statistical error. Moreover, considering that you have tested with |
You should use |
Here are comparisons with each level of precision: http://nbviewer.ipython.org/gist/sloria/6229107fa5cea79c80a2. The try-if-else implementation is consistently faster. As expected, the difference is the largest with If you don't want to make the change, I'll go ahead and make it when I merge. |
ok, make the change when you merge |
#105