|
295 | 295 | { |
296 | 296 | "data": { |
297 | 297 | "text/plain": [ |
298 | | - "<__main__._t at 0x7f85ba48cfd0>" |
| 298 | + "<__main__._t at 0x7f574d90d990>" |
299 | 299 | ] |
300 | 300 | }, |
301 | 301 | "execution_count": null, |
|
2170 | 2170 | { |
2171 | 2171 | "data": { |
2172 | 2172 | "text/plain": [ |
2173 | | - "['e', 'g', 'f', 'b', 'c', 'a', 'd', 'h']" |
| 2173 | + "['d', 'b', 'h', 'a', 'g', 'c', 'e', 'f']" |
2174 | 2174 | ] |
2175 | 2175 | }, |
2176 | 2176 | "execution_count": null, |
|
2878 | 2878 | "#export\n", |
2879 | 2879 | "@patch\n", |
2880 | 2880 | "def readlines(self:Path, hint=-1, encoding='utf8'):\n", |
2881 | | - " \"Read the content of `fname`\"\n", |
| 2881 | + " \"Read the content of `self`\"\n", |
2882 | 2882 | " with self.open(encoding=encoding) as f: return f.readlines(hint)" |
2883 | 2883 | ] |
2884 | 2884 | }, |
|
2889 | 2889 | "outputs": [], |
2890 | 2890 | "source": [ |
2891 | 2891 | "#export\n", |
2892 | | - "def save_pickle(fn, o):\n", |
2893 | | - " \"Save a pickle file, to a file name or opened file\"\n", |
2894 | | - " fn = Path(fn)\n", |
2895 | | - " if not isinstance(fn, io.IOBase): fn = open(fn,'wb')\n", |
2896 | | - " try: pickle.dump(o, fn)\n", |
2897 | | - " finally: fn.close()" |
2898 | | - ] |
2899 | | - }, |
2900 | | - { |
2901 | | - "cell_type": "code", |
2902 | | - "execution_count": null, |
2903 | | - "metadata": {}, |
2904 | | - "outputs": [], |
2905 | | - "source": [ |
2906 | | - "#export\n", |
2907 | | - "def load_pickle(fn):\n", |
2908 | | - " \"Load a pickle file from a file name or opened file\"\n", |
2909 | | - " fn = Path(fn)\n", |
2910 | | - " if not isinstance(fn, io.IOBase): fn = open(fn,'rb')\n", |
2911 | | - " try: return pickle.load(fn)\n", |
2912 | | - " finally: fn.close()" |
2913 | | - ] |
2914 | | - }, |
2915 | | - { |
2916 | | - "cell_type": "code", |
2917 | | - "execution_count": null, |
2918 | | - "metadata": {}, |
2919 | | - "outputs": [], |
2920 | | - "source": [ |
2921 | | - "with tempfile.NamedTemporaryFile() as f:\n", |
2922 | | - " fn = Path(f.name)\n", |
2923 | | - " save_pickle(fn, 't')\n", |
2924 | | - " t = load_pickle(fn)\n", |
2925 | | - "test_eq(t,'t')" |
| 2892 | + "@patch\n", |
| 2893 | + "def mk_write(self:Path, data, encoding=None, errors=None, mode=511):\n", |
| 2894 | + " \"Make all parent dirs of `self`\"\n", |
| 2895 | + " self.parent.mkdir(exist_ok=True, parents=True, mode=mode)\n", |
| 2896 | + " self.write_text(data, encoding=encoding, errors=errors)" |
2926 | 2897 | ] |
2927 | 2898 | }, |
2928 | 2899 | { |
|
3009 | 2980 | "txt_files[0],ipy_files[0]" |
3010 | 2981 | ] |
3011 | 2982 | }, |
| 2983 | + { |
| 2984 | + "cell_type": "code", |
| 2985 | + "execution_count": null, |
| 2986 | + "metadata": {}, |
| 2987 | + "outputs": [], |
| 2988 | + "source": [ |
| 2989 | + "#export\n", |
| 2990 | + "def save_pickle(fn, o):\n", |
| 2991 | + " \"Save a pickle file, to a file name or opened file\"\n", |
| 2992 | + " fn = Path(fn)\n", |
| 2993 | + " if not isinstance(fn, io.IOBase): fn = open(fn,'wb')\n", |
| 2994 | + " try: pickle.dump(o, fn)\n", |
| 2995 | + " finally: fn.close()" |
| 2996 | + ] |
| 2997 | + }, |
| 2998 | + { |
| 2999 | + "cell_type": "code", |
| 3000 | + "execution_count": null, |
| 3001 | + "metadata": {}, |
| 3002 | + "outputs": [], |
| 3003 | + "source": [ |
| 3004 | + "#export\n", |
| 3005 | + "def load_pickle(fn):\n", |
| 3006 | + " \"Load a pickle file from a file name or opened file\"\n", |
| 3007 | + " fn = Path(fn)\n", |
| 3008 | + " if not isinstance(fn, io.IOBase): fn = open(fn,'rb')\n", |
| 3009 | + " try: return pickle.load(fn)\n", |
| 3010 | + " finally: fn.close()" |
| 3011 | + ] |
| 3012 | + }, |
| 3013 | + { |
| 3014 | + "cell_type": "code", |
| 3015 | + "execution_count": null, |
| 3016 | + "metadata": {}, |
| 3017 | + "outputs": [], |
| 3018 | + "source": [ |
| 3019 | + "with tempfile.NamedTemporaryFile() as f:\n", |
| 3020 | + " fn = Path(f.name)\n", |
| 3021 | + " save_pickle(fn, 't')\n", |
| 3022 | + " t = load_pickle(fn)\n", |
| 3023 | + "test_eq(t,'t')" |
| 3024 | + ] |
| 3025 | + }, |
3012 | 3026 | { |
3013 | 3027 | "cell_type": "code", |
3014 | 3028 | "execution_count": null, |
|
3017 | 3031 | "source": [ |
3018 | 3032 | "#hide\n", |
3019 | 3033 | "pkl = pickle.dumps(path)\n", |
3020 | | - "p2 =pickle.loads(pkl)\n", |
| 3034 | + "p2 = pickle.loads(pkl)\n", |
3021 | 3035 | "test_eq(path.ls()[0], p2.ls()[0])" |
3022 | 3036 | ] |
3023 | 3037 | }, |
|
0 commit comments