Created
June 15, 2020 20:28
-
-
Save kain88-de/04e614de5fbbe29ffd5085141e919341 to your computer and use it in GitHub Desktop.
file serialization test
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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import io\n", | |
"import os" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"class FileIOPicklable(io.FileIO):\n", | |
" def __init__(self, name):\n", | |
" super().__init__(name, mode='r')\n", | |
"\n", | |
" def __getstate__(self):\n", | |
" return self.name, self.tell()\n", | |
"\n", | |
" def __setstate__(self, args):\n", | |
" name = args[0]\n", | |
" super().__init__(name, mode='r')\n", | |
" self.seek(args[1])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def stdopen():\n", | |
" raw = io.FileIO('Untitled.ipynb')\n", | |
" file = io.TextIOWrapper(raw)\n", | |
" lines = file.readlines()\n", | |
" file.close()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"74 µs ± 938 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n" | |
] | |
} | |
], | |
"source": [ | |
"%timeit stdopen()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def standard_open():\n", | |
" file = open('Untitled.ipynb')\n", | |
" liens = file.readlines()\n", | |
" file.close()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"80.9 µs ± 441 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n" | |
] | |
} | |
], | |
"source": [ | |
"%timeit standard_open()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def pickle_open():\n", | |
" raw = FileIOPicklable('Untitled.ipynb')\n", | |
" file = io.TextIOWrapper(raw)\n", | |
" lines = file.readlines()\n", | |
" file.close()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"79.8 µs ± 1.09 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n" | |
] | |
} | |
], | |
"source": [ | |
"%timeit pickle_open()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"class TextIOPicklable(io.TextIOWrapper):\n", | |
" def __init__(self, raw):\n", | |
" super().__init__(raw)\n", | |
" self.raw_class = raw.__class__\n", | |
"\n", | |
" def __getstate__(self):\n", | |
" return self.raw_class, self.name, self.tell()\n", | |
"\n", | |
" def __setstate__(self, args):\n", | |
" raw_class = args[0]\n", | |
" name = args[1]\n", | |
" # raw_class is used for further expansion this functionality to\n", | |
" # GZip files, which also requires a text wrapper.\n", | |
" raw = raw_class(name)\n", | |
" super().__init__(raw)\n", | |
" self.seek(args[2])\n", | |
" \n", | |
"class BufferIOPicklable(io.BufferedReader):\n", | |
" def __init__(self, raw):\n", | |
" super().__init__(raw)\n", | |
" self.raw_class = raw.__class__\n", | |
"\n", | |
" def __getstate__(self):\n", | |
" return self.raw_class, self.name, self.tell()\n", | |
"\n", | |
" def __setstate__(self, args):\n", | |
" raw_class = args[0]\n", | |
" name = args[1]\n", | |
" raw = raw_class(name)\n", | |
" super().__init__(raw)\n", | |
" self.seek(args[2])\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def pickle_open2():\n", | |
" raw = FileIOPicklable('Untitled.ipynb')\n", | |
" file = TextIOPicklable(raw)\n", | |
" lines = file.readlines()\n", | |
" file.close()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"133 µs ± 5.33 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n" | |
] | |
} | |
], | |
"source": [ | |
"%timeit pickle_open2()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def pickle_open3():\n", | |
" raw = FileIOPicklable('Untitled.ipynb')\n", | |
" file = TextIOPicklable(BufferIOPicklable(raw))\n", | |
" lines = file.readlines()\n", | |
" file.close()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"153 µs ± 9.6 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n" | |
] | |
} | |
], | |
"source": [ | |
"%timeit pickle_open3()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"class NonWrapper():\n", | |
" def __init__(self, name, mode='r'):\n", | |
" self._name = name\n", | |
" self._mode = mode\n", | |
" self._fh = open(name, mode)\n", | |
" \n", | |
" def __getstate__(self):\n", | |
" return self._name, self._mode, self._fh.tell()\n", | |
" \n", | |
" def __setstate__(self, args):\n", | |
" self._name = args[0]\n", | |
" self._mode = args[1]\n", | |
" self._fh = open(self._name, self._mode)\n", | |
" self._fh.seek(args[2])\n", | |
" \n", | |
" def readlines(self):\n", | |
" return self._fh.readlines()\n", | |
" \n", | |
" def close(self):\n", | |
" self._fh.close()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def my_open():\n", | |
" file = NonWrapper('Untitled.ipynb')\n", | |
" lines = file.readlines()\n", | |
" file.close()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"91.2 µs ± 6.51 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n" | |
] | |
} | |
], | |
"source": [ | |
"%timeit my_open()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.6.7" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment