Skip to content

Commit b1d27bf

Browse files
committed
fixes #121
1 parent 46ad915 commit b1d27bf

File tree

3 files changed

+23
-79
lines changed

3 files changed

+23
-79
lines changed

fastcore/_nbdev.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,8 @@
107107
"using_attr": "02_utils.ipynb",
108108
"Self": "02_utils.ipynb",
109109
"Path.readlines": "02_utils.ipynb",
110-
"Path.read": "02_utils.ipynb",
111-
"Path.write": "02_utils.ipynb",
112-
"Path.save": "02_utils.ipynb",
113-
"Path.load": "02_utils.ipynb",
110+
"save_pickle": "02_utils.ipynb",
111+
"load_pickle": "02_utils.ipynb",
114112
"Path.ls": "02_utils.ipynb",
115113
"Path.__repr__": "02_utils.ipynb",
116114
"remove_patches_path": "02_utils.ipynb",

fastcore/utils.py

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
'Float', 'tuplify', 'detuplify', 'replicate', 'uniqueify', 'setify', 'merge', 'is_listy', 'range_of',
88
'groupby', 'last_index', 'shufflish', 'IterLen', 'ReindexCollection', 'num_methods', 'rnum_methods',
99
'inum_methods', 'fastuple', 'trace', 'compose', 'maps', 'partialler', 'mapped', 'instantiate', 'using_attr',
10-
'Self', 'Self', 'remove_patches_path', 'bunzip', 'join_path_file', 'urlread', 'urljson', 'run', 'do_request',
11-
'sort_by_run', 'PrettyString', 'round_multiple', 'even_mults', 'num_cpus', 'add_props', 'ContextManagers',
12-
'set_num_threads', 'ProcessPoolExecutor', 'ThreadPoolExecutor', 'parallel', 'run_procs', 'parallel_gen']
10+
'Self', 'Self', 'save_pickle', 'load_pickle', 'remove_patches_path', 'bunzip', 'join_path_file', 'urlread',
11+
'urljson', 'run', 'do_request', 'sort_by_run', 'PrettyString', 'round_multiple', 'even_mults', 'num_cpus',
12+
'add_props', 'ContextManagers', 'set_num_threads', 'ProcessPoolExecutor', 'ThreadPoolExecutor', 'parallel',
13+
'run_procs', 'parallel_gen']
1314

1415
# Cell
1516
from .imports import *
@@ -509,30 +510,17 @@ def readlines(self:Path, hint=-1, encoding='utf8'):
509510
with self.open(encoding=encoding) as f: return f.readlines(hint)
510511

511512
# Cell
512-
@patch
513-
def read(self:Path, size=-1, encoding='utf8'):
514-
"Read the content of `fname`"
515-
with self.open(encoding=encoding) as f: return f.read(size)
516-
517-
# Cell
518-
@patch
519-
def write(self:Path, txt, encoding='utf8'):
520-
"Write `txt` to `self`, creating directories as needed"
521-
self.parent.mkdir(parents=True,exist_ok=True)
522-
with self.open('w', encoding=encoding) as f: f.write(txt)
523-
524-
# Cell
525-
@patch
526-
def save(fn:Path, o):
513+
def save_pickle(fn, o):
527514
"Save a pickle file, to a file name or opened file"
515+
fn = Path(fn)
528516
if not isinstance(fn, io.IOBase): fn = open(fn,'wb')
529517
try: pickle.dump(o, fn)
530518
finally: fn.close()
531519

532520
# Cell
533-
@patch
534-
def load(fn:Path):
521+
def load_pickle(fn):
535522
"Load a pickle file from a file name or opened file"
523+
fn = Path(fn)
536524
if not isinstance(fn, io.IOBase): fn = open(fn,'rb')
537525
try: return pickle.load(fn)
538526
finally: fn.close()

nbs/02_utils.ipynb

Lines changed: 13 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@
295295
{
296296
"data": {
297297
"text/plain": [
298-
"<__main__._t at 0x7fe83401cca0>"
298+
"<__main__._t at 0x7fcf69d97d10>"
299299
]
300300
},
301301
"execution_count": null,
@@ -2170,7 +2170,7 @@
21702170
{
21712171
"data": {
21722172
"text/plain": [
2173-
"['h', 'c', 'e', 'a', 'g', 'b', 'f', 'd']"
2173+
"['b', 'f', 'h', 'a', 'c', 'g', 'e', 'd']"
21742174
]
21752175
},
21762176
"execution_count": null,
@@ -2889,51 +2889,9 @@
28892889
"outputs": [],
28902890
"source": [
28912891
"#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",
29362893
" \"Save a pickle file, to a file name or opened file\"\n",
2894+
" fn = Path(fn)\n",
29372895
" if not isinstance(fn, io.IOBase): fn = open(fn,'wb')\n",
29382896
" try: pickle.dump(o, fn)\n",
29392897
" finally: fn.close()"
@@ -2946,9 +2904,9 @@
29462904
"outputs": [],
29472905
"source": [
29482906
"#export\n",
2949-
"@patch\n",
2950-
"def load(fn:Path):\n",
2907+
"def load_pickle(fn):\n",
29512908
" \"Load a pickle file from a file name or opened file\"\n",
2909+
" fn = Path(fn)\n",
29522910
" if not isinstance(fn, io.IOBase): fn = open(fn,'rb')\n",
29532911
" try: return pickle.load(fn)\n",
29542912
" finally: fn.close()"
@@ -2962,8 +2920,8 @@
29622920
"source": [
29632921
"with tempfile.NamedTemporaryFile() as f:\n",
29642922
" 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",
29672925
"test_eq(t,'t')"
29682926
]
29692927
},
@@ -3939,11 +3897,11 @@
39393897
"name": "stdout",
39403898
"output_type": "stream",
39413899
"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"
39473905
]
39483906
}
39493907
],

0 commit comments

Comments
 (0)