Skip to content

Commit bc12315

Browse files
committed
release
1 parent d53fb6c commit bc12315

File tree

4 files changed

+79
-51
lines changed

4 files changed

+79
-51
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
<!-- do not remove -->
44

5+
## 1.1.0
6+
7+
### Breaking Changes
8+
9+
- Remove `Path.{read,write}` and change `Path.{load,save}` to functions `load_pickle` and `save_pickle` ([#121](https://github.com/fastai/fastcore/issues/121))
10+
511
## 1.0.22
612

713
### New Features

fastcore/_nbdev.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,10 @@
107107
"using_attr": "02_utils.ipynb",
108108
"Self": "02_utils.ipynb",
109109
"Path.readlines": "02_utils.ipynb",
110+
"Path.mk_write": "02_utils.ipynb",
111+
"Path.ls": "02_utils.ipynb",
110112
"save_pickle": "02_utils.ipynb",
111113
"load_pickle": "02_utils.ipynb",
112-
"Path.ls": "02_utils.ipynb",
113114
"Path.__repr__": "02_utils.ipynb",
114115
"bunzip": "02_utils.ipynb",
115116
"join_path_file": "02_utils.ipynb",

fastcore/utils.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -506,9 +506,27 @@ def __getitem__(self,i): return self.__getattr__('__getitem__')(i)
506506
# Cell
507507
@patch
508508
def readlines(self:Path, hint=-1, encoding='utf8'):
509-
"Read the content of `fname`"
509+
"Read the content of `self`"
510510
with self.open(encoding=encoding) as f: return f.readlines(hint)
511511

512+
# Cell
513+
@patch
514+
def mk_write(self:Path, data, encoding=None, errors=None, mode=511):
515+
"Make all parent dirs of `self`"
516+
self.parent.mkdir(exist_ok=True, parents=True, mode=mode)
517+
self.write_text(data, encoding=encoding, errors=errors)
518+
519+
# Cell
520+
@patch
521+
def ls(self:Path, n_max=None, file_type=None, file_exts=None):
522+
"Contents of path as a list"
523+
extns=L(file_exts)
524+
if file_type: extns += L(k for k,v in mimetypes.types_map.items() if v.startswith(file_type+'/'))
525+
has_extns = len(extns)==0
526+
res = (o for o in self.iterdir() if has_extns or o.suffix in extns)
527+
if n_max is not None: res = itertools.islice(res, n_max)
528+
return L(res)
529+
512530
# Cell
513531
def save_pickle(fn, o):
514532
"Save a pickle file, to a file name or opened file"
@@ -525,17 +543,6 @@ def load_pickle(fn):
525543
try: return pickle.load(fn)
526544
finally: fn.close()
527545

528-
# Cell
529-
@patch
530-
def ls(self:Path, n_max=None, file_type=None, file_exts=None):
531-
"Contents of path as a list"
532-
extns=L(file_exts)
533-
if file_type: extns += L(k for k,v in mimetypes.types_map.items() if v.startswith(file_type+'/'))
534-
has_extns = len(extns)==0
535-
res = (o for o in self.iterdir() if has_extns or o.suffix in extns)
536-
if n_max is not None: res = itertools.islice(res, n_max)
537-
return L(res)
538-
539546
# Cell
540547
@patch
541548
def __repr__(self:Path):

nbs/02_utils.ipynb

Lines changed: 52 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@
295295
{
296296
"data": {
297297
"text/plain": [
298-
"<__main__._t at 0x7f85ba48cfd0>"
298+
"<__main__._t at 0x7f574d90d990>"
299299
]
300300
},
301301
"execution_count": null,
@@ -2170,7 +2170,7 @@
21702170
{
21712171
"data": {
21722172
"text/plain": [
2173-
"['e', 'g', 'f', 'b', 'c', 'a', 'd', 'h']"
2173+
"['d', 'b', 'h', 'a', 'g', 'c', 'e', 'f']"
21742174
]
21752175
},
21762176
"execution_count": null,
@@ -2878,7 +2878,7 @@
28782878
"#export\n",
28792879
"@patch\n",
28802880
"def readlines(self:Path, hint=-1, encoding='utf8'):\n",
2881-
" \"Read the content of `fname`\"\n",
2881+
" \"Read the content of `self`\"\n",
28822882
" with self.open(encoding=encoding) as f: return f.readlines(hint)"
28832883
]
28842884
},
@@ -2889,40 +2889,11 @@
28892889
"outputs": [],
28902890
"source": [
28912891
"#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)"
29262897
]
29272898
},
29282899
{
@@ -3009,6 +2980,49 @@
30092980
"txt_files[0],ipy_files[0]"
30102981
]
30112982
},
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+
},
30123026
{
30133027
"cell_type": "code",
30143028
"execution_count": null,
@@ -3017,7 +3031,7 @@
30173031
"source": [
30183032
"#hide\n",
30193033
"pkl = pickle.dumps(path)\n",
3020-
"p2 =pickle.loads(pkl)\n",
3034+
"p2 = pickle.loads(pkl)\n",
30213035
"test_eq(path.ls()[0], p2.ls()[0])"
30223036
]
30233037
},

0 commit comments

Comments
 (0)