-
-
Save sloria/5c1b845a6ec59c2404d0 to your computer and use it in GitHub Desktop.
This file contains 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
{ | |
"metadata": { | |
"name": "", | |
"signature": "sha256:33783e38f8bc2e4a21ed947b69ed746c9b3a0a14ef2809d2c1bc501d951c516a" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"from datetime import timedelta\n", | |
"from marshmallow.fields import Field\n", | |
"\n", | |
"class TimeDelta1(Field):\n", | |
" DAYS = 'days'\n", | |
" SECONDS = 'seconds'\n", | |
" MICROSECONDS = 'microseconds'\n", | |
"\n", | |
" def __init__(self, precision='seconds', error=None, **kwargs):\n", | |
" precision = precision.lower()\n", | |
" units = (self.DAYS, self.SECONDS, self.MICROSECONDS)\n", | |
"\n", | |
" if precision not in units:\n", | |
" msg = 'The precision must be \"{0}\", \"{1}\" or \"{2}\".'.format(*units)\n", | |
" raise ValueError(msg)\n", | |
"\n", | |
" self.precision = precision\n", | |
" super(TimeDelta1, self).__init__(error=error, **kwargs)\n", | |
"\n", | |
" def _serialize(self, value, attr, obj):\n", | |
" try:\n", | |
" days = value.days\n", | |
" seconds = days * 86400 + value.seconds\n", | |
" microseconds = seconds * 10**6 + value.microseconds\n", | |
" except AttributeError:\n", | |
" msg = '{0!r} cannot be formatted as a timedelta.'.format(value)\n", | |
" raise MarshallingError(getattr(self, 'error', None) or msg)\n", | |
"\n", | |
" return locals()[self.precision]\n", | |
" \n", | |
"class TimeDelta2(Field):\n", | |
" DAYS = 'days'\n", | |
" SECONDS = 'seconds'\n", | |
" MICROSECONDS = 'microseconds'\n", | |
"\n", | |
" def __init__(self, precision='seconds', error=None, **kwargs):\n", | |
" precision = precision.lower()\n", | |
" units = (self.DAYS, self.SECONDS, self.MICROSECONDS)\n", | |
"\n", | |
" if precision not in units:\n", | |
" msg = 'The precision must be \"{0}\", \"{1}\" or \"{2}\".'.format(*units)\n", | |
" raise ValueError(msg)\n", | |
"\n", | |
" self.precision = precision\n", | |
" super(TimeDelta2, self).__init__(error=error, **kwargs)\n", | |
"\n", | |
" def _serialize(self, value, attr, obj):\n", | |
" try:\n", | |
" days = value.days\n", | |
" if self.precision == self.DAYS:\n", | |
" return days\n", | |
" else:\n", | |
" seconds = days * 86400 + value.seconds\n", | |
" if self.precision == self.SECONDS:\n", | |
" return seconds\n", | |
" else: # microseconds\n", | |
" return seconds * 10**6 + value.microseconds\n", | |
" except AttributeError:\n", | |
" msg = '{0!r} cannot be formatted as a timedelta.'.format()\n", | |
" raise MarshallingError(getattr(self, 'error', None) or msg)\n" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 1 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def seconds1(): \n", | |
" f = TimeDelta1(precision='microseconds')\n", | |
" for i in range(999999):\n", | |
" obj = {'foo': timedelta(microseconds=i)}\n", | |
" f.serialize('foo', obj)\n", | |
" \n", | |
"def seconds2():\n", | |
" f = TimeDelta2(precision='microseconds')\n", | |
" for i in range(999999):\n", | |
" obj = {'foo': timedelta(microseconds=i)}\n", | |
" f.serialize('foo', obj)\n", | |
"\n" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 2 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%timeit seconds1()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"1 loops, best of 3: 5.99 s per loop\n" | |
] | |
} | |
], | |
"prompt_number": 3 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%timeit seconds2()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"1 loops, best of 3: 5.35 s per loop\n" | |
] | |
} | |
], | |
"prompt_number": 4 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 10 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment