Skip to content

Commit

Permalink
fixes #344
Browse files Browse the repository at this point in the history
  • Loading branch information
jph00 committed Aug 8, 2021
1 parent b5479a4 commit e85025f
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
1 change: 1 addition & 0 deletions fastcore/_nbdev.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@
"Path.readlines": "03_xtras.ipynb",
"Path.read_json": "03_xtras.ipynb",
"Path.mk_write": "03_xtras.ipynb",
"Path.relpath": "03_xtras.ipynb",
"Path.ls": "03_xtras.ipynb",
"Path.__repr__": "03_xtras.ipynb",
"Path.delete": "03_xtras.ipynb",
Expand Down
25 changes: 25 additions & 0 deletions fastcore/xtras.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,25 @@ def maybe_open(f, mode='r', **kwargs):
with open(f, mode, **kwargs) as f: yield f
else: yield f

# Cell
def _jpg_size(f):
size,ftype = 2,0
while not 0xc0 <= ftype <= 0xcf:
f.seek(size, 1)
byte = f.read(1)
while ord(byte) == 0xff: byte = f.read(1)
ftype = ord(byte)
size = struct.unpack('>H', f.read(2))[0] - 2
f.seek(1, 1) # `precision'
h,w = struct.unpack('>HH', f.read(4))
return w,h

def _gif_size(f): return struct.unpack('<HH', head[6:10])

def _png_size(f):
assert struct.unpack('>i', head[4:8])[0]==0x0d0a1a0a
return struct.unpack('>ii', head[16:24])

# Cell
def image_size(fn):
"Tuple of (w,h) for png, gif, or jpg; `None` otherwise"
Expand Down Expand Up @@ -239,6 +258,12 @@ def mk_write(self:Path, data, encoding=None, errors=None, mode=511):
self.parent.mkdir(exist_ok=True, parents=True, mode=mode)
self.write_text(data, encoding=encoding, errors=errors)

# Cell
@patch
def relpath(self:Path, start=None):
"Same as `os.path.relpath`, but returns a `Path`, and resolves symlinks"
return Path(os.path.relpath(self.resolve(), Path(start).resolve()))

# Cell
@patch
def ls(self:Path, n_max=None, file_type=None, file_exts=None):
Expand Down
58 changes: 57 additions & 1 deletion nbs/03_xtras.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@
{
"data": {
"text/plain": [
"['b', 'a', 'g', 'h', 'f', 'e', 'c', 'd']"
"['e', 'd', 'c', 'h', 'f', 'a', 'b', 'g']"
]
},
"execution_count": null,
Expand Down Expand Up @@ -848,6 +848,7 @@
"metadata": {},
"outputs": [],
"source": [
"#export\n",
"def _jpg_size(f):\n",
" size,ftype = 2,0\n",
" while not 0xc0 <= ftype <= 0xcf:\n",
Expand Down Expand Up @@ -1353,6 +1354,60 @@
" self.write_text(data, encoding=encoding, errors=errors)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#export\n",
"@patch\n",
"def relpath(self:Path, start=None):\n",
" \"Same as `os.path.relpath`, but returns a `Path`, and resolves symlinks\"\n",
" return Path(os.path.relpath(self.resolve(), Path(start).resolve()))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Path('/home/jhoward/git/fastcore/fastcore')"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Path('../fastcore/').resolve()\n",
"p"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Path('../fastcore')"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"p.relpath(Path('.'))"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -2127,6 +2182,7 @@
"Converted 03b_net.ipynb.\n",
"Converted 04_dispatch.ipynb.\n",
"Converted 05_transform.ipynb.\n",
"Converted 06_docments.ipynb.\n",
"Converted 07_meta.ipynb.\n",
"Converted 08_script.ipynb.\n",
"Converted index.ipynb.\n",
Expand Down

0 comments on commit e85025f

Please sign in to comment.