|
295 | 295 | { |
296 | 296 | "data": { |
297 | 297 | "text/plain": [ |
298 | | - "<__main__._t at 0x7fe83401cca0>" |
| 298 | + "<__main__._t at 0x7fcf69d97d10>" |
299 | 299 | ] |
300 | 300 | }, |
301 | 301 | "execution_count": null, |
|
2170 | 2170 | { |
2171 | 2171 | "data": { |
2172 | 2172 | "text/plain": [ |
2173 | | - "['h', 'c', 'e', 'a', 'g', 'b', 'f', 'd']" |
| 2173 | + "['b', 'f', 'h', 'a', 'c', 'g', 'e', 'd']" |
2174 | 2174 | ] |
2175 | 2175 | }, |
2176 | 2176 | "execution_count": null, |
|
2889 | 2889 | "outputs": [], |
2890 | 2890 | "source": [ |
2891 | 2891 | "#export\n", |
2892 | | - "@patch\n", |
2893 | | - "def read(self:Path, size=-1, encoding='utf8'):\n", |
2894 | | - " \"Read the content of `fname`\"\n", |
2895 | | - " with self.open(encoding=encoding) as f: return f.read(size)" |
2896 | | - ] |
2897 | | - }, |
2898 | | - { |
2899 | | - "cell_type": "code", |
2900 | | - "execution_count": null, |
2901 | | - "metadata": {}, |
2902 | | - "outputs": [], |
2903 | | - "source": [ |
2904 | | - "#export\n", |
2905 | | - "@patch\n", |
2906 | | - "def write(self:Path, txt, encoding='utf8'):\n", |
2907 | | - " \"Write `txt` to `self`, creating directories as needed\"\n", |
2908 | | - " self.parent.mkdir(parents=True,exist_ok=True)\n", |
2909 | | - " with self.open('w', encoding=encoding) as f: f.write(txt)" |
2910 | | - ] |
2911 | | - }, |
2912 | | - { |
2913 | | - "cell_type": "code", |
2914 | | - "execution_count": null, |
2915 | | - "metadata": {}, |
2916 | | - "outputs": [], |
2917 | | - "source": [ |
2918 | | - "with tempfile.NamedTemporaryFile() as f:\n", |
2919 | | - " fn = Path(f.name)\n", |
2920 | | - " fn.write('t')\n", |
2921 | | - " t = fn.read()\n", |
2922 | | - " test_eq(t,'t')\n", |
2923 | | - " t = fn.readlines()\n", |
2924 | | - " test_eq(t,['t'])" |
2925 | | - ] |
2926 | | - }, |
2927 | | - { |
2928 | | - "cell_type": "code", |
2929 | | - "execution_count": null, |
2930 | | - "metadata": {}, |
2931 | | - "outputs": [], |
2932 | | - "source": [ |
2933 | | - "#export\n", |
2934 | | - "@patch\n", |
2935 | | - "def save(fn:Path, o):\n", |
| 2892 | + "def save_pickle(fn, o):\n", |
2936 | 2893 | " \"Save a pickle file, to a file name or opened file\"\n", |
| 2894 | + " fn = Path(fn)\n", |
2937 | 2895 | " if not isinstance(fn, io.IOBase): fn = open(fn,'wb')\n", |
2938 | 2896 | " try: pickle.dump(o, fn)\n", |
2939 | 2897 | " finally: fn.close()" |
|
2946 | 2904 | "outputs": [], |
2947 | 2905 | "source": [ |
2948 | 2906 | "#export\n", |
2949 | | - "@patch\n", |
2950 | | - "def load(fn:Path):\n", |
| 2907 | + "def load_pickle(fn):\n", |
2951 | 2908 | " \"Load a pickle file from a file name or opened file\"\n", |
| 2909 | + " fn = Path(fn)\n", |
2952 | 2910 | " if not isinstance(fn, io.IOBase): fn = open(fn,'rb')\n", |
2953 | 2911 | " try: return pickle.load(fn)\n", |
2954 | 2912 | " finally: fn.close()" |
|
2962 | 2920 | "source": [ |
2963 | 2921 | "with tempfile.NamedTemporaryFile() as f:\n", |
2964 | 2922 | " fn = Path(f.name)\n", |
2965 | | - " fn.save('t')\n", |
2966 | | - " t = fn.load()\n", |
| 2923 | + " save_pickle(fn, 't')\n", |
| 2924 | + " t = load_pickle(fn)\n", |
2967 | 2925 | "test_eq(t,'t')" |
2968 | 2926 | ] |
2969 | 2927 | }, |
|
3939 | 3897 | "name": "stdout", |
3940 | 3898 | "output_type": "stream", |
3941 | 3899 | "text": [ |
3942 | | - "0 2020-09-14 13:38:06.317966\n", |
3943 | | - "1 2020-09-14 13:38:06.568367\n", |
3944 | | - "2 2020-09-14 13:38:06.818557\n", |
3945 | | - "3 2020-09-14 13:38:07.069433\n", |
3946 | | - "4 2020-09-14 13:38:07.319457\n" |
| 3900 | + "0 2020-10-08 18:02:47.582440\n", |
| 3901 | + "1 2020-10-08 18:02:47.833442\n", |
| 3902 | + "2 2020-10-08 18:02:48.083059\n", |
| 3903 | + "3 2020-10-08 18:02:48.334451\n", |
| 3904 | + "4 2020-10-08 18:02:48.584491\n" |
3947 | 3905 | ] |
3948 | 3906 | } |
3949 | 3907 | ], |
|
0 commit comments