Skip to content

Commit 19341e7

Browse files
committed
Add --latest-only option to jcache cache list
Fixes #32
1 parent 986ee85 commit 19341e7

File tree

4 files changed

+67
-23
lines changed

4 files changed

+67
-23
lines changed

README.md

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ to come ...
4141

4242
## Example CLI usage
4343

44-
<!-- This section was auto-generated on 2020-03-12 17:11 by: /Users/cjs14/GitHub/jupyter-cache/tests/make_cli_readme.py -->
44+
<!-- This section was auto-generated on 2020-03-12 17:31 by: /Users/cjs14/GitHub/jupyter-cache/tests/make_cli_readme.py -->
4545

4646
From the checked-out repository folder:
4747

@@ -141,12 +141,14 @@ You can remove cached records by their ID.
141141
$ jcache cache list
142142
ID Origin URI Created Accessed
143143
---- ------------------------------------- ---------------- ----------------
144-
5 tests/notebooks/external_output.ipynb 2020-03-12 17:11 2020-03-12 17:11
145-
4 tests/notebooks/complex_outputs.ipynb 2020-03-12 17:11 2020-03-12 17:11
146-
3 tests/notebooks/basic_unrun.ipynb 2020-03-12 17:11 2020-03-12 17:11
147-
2 tests/notebooks/basic_failing.ipynb 2020-03-12 17:11 2020-03-12 17:11
144+
5 tests/notebooks/external_output.ipynb 2020-03-12 17:31 2020-03-12 17:31
145+
4 tests/notebooks/complex_outputs.ipynb 2020-03-12 17:31 2020-03-12 17:31
146+
3 tests/notebooks/basic_unrun.ipynb 2020-03-12 17:31 2020-03-12 17:31
147+
2 tests/notebooks/basic_failing.ipynb 2020-03-12 17:31 2020-03-12 17:31
148148
```
149149

150+
Tip: Use the `--latest-only` option, to only show the latest versions of cached notebooks.
151+
150152
You can also cache notebooks with artefacts
151153
(external outputs of the notebook execution).
152154

@@ -164,8 +166,8 @@ Show a full description of a cached notebook by referring to its ID
164166
$ jcache cache show 6
165167
ID: 6
166168
Origin URI: ../tests/notebooks/basic.ipynb
167-
Created: 2020-03-12 17:11
168-
Accessed: 2020-03-12 17:11
169+
Created: 2020-03-12 17:31
170+
Accessed: 2020-03-12 17:31
169171
Hashkey: 818f3412b998fcf4fe9ca3cca11a3fc3
170172
Artifacts:
171173
- artifact_folder/artifact.txt
@@ -265,11 +267,11 @@ Success!
265267
$ jcache stage list
266268
ID URI Created Assets Cache ID
267269
---- ------------------------------------- ---------------- -------- ----------
268-
5 tests/notebooks/external_output.ipynb 2020-03-12 17:11 0 5
269-
4 tests/notebooks/complex_outputs.ipynb 2020-03-12 17:11 0
270-
3 tests/notebooks/basic_unrun.ipynb 2020-03-12 17:11 0 6
271-
2 tests/notebooks/basic_failing.ipynb 2020-03-12 17:11 0 2
272-
1 tests/notebooks/basic.ipynb 2020-03-12 17:11 0 6
270+
5 tests/notebooks/external_output.ipynb 2020-03-12 17:31 0 5
271+
4 tests/notebooks/complex_outputs.ipynb 2020-03-12 17:31 0
272+
3 tests/notebooks/basic_unrun.ipynb 2020-03-12 17:31 0 6
273+
2 tests/notebooks/basic_failing.ipynb 2020-03-12 17:31 0 2
274+
1 tests/notebooks/basic.ipynb 2020-03-12 17:31 0 6
273275
```
274276

275277
You can remove a staged notebook by its URI or ID:
@@ -315,10 +317,10 @@ that are inside the notebook folder, and data supplied by the executor.
315317
$ jcache stage list
316318
ID URI Created Assets Cache ID
317319
---- ------------------------------------- ---------------- -------- ----------
318-
5 tests/notebooks/external_output.ipynb 2020-03-12 17:11 0 5
319-
3 tests/notebooks/basic_unrun.ipynb 2020-03-12 17:11 0 6
320-
2 tests/notebooks/basic_failing.ipynb 2020-03-12 17:11 0
321-
1 tests/notebooks/basic.ipynb 2020-03-12 17:11 0 6
320+
5 tests/notebooks/external_output.ipynb 2020-03-12 17:31 0 5
321+
3 tests/notebooks/basic_unrun.ipynb 2020-03-12 17:31 0 6
322+
2 tests/notebooks/basic_failing.ipynb 2020-03-12 17:31 0
323+
1 tests/notebooks/basic.ipynb 2020-03-12 17:31 0 6
322324
```
323325

324326
Execution data (such as execution time) will be stored in the cache record:
@@ -327,11 +329,11 @@ Execution data (such as execution time) will be stored in the cache record:
327329
$ jcache cache show 6
328330
ID: 6
329331
Origin URI: ../tests/notebooks/basic_unrun.ipynb
330-
Created: 2020-03-12 17:11
331-
Accessed: 2020-03-12 17:11
332+
Created: 2020-03-12 17:31
333+
Accessed: 2020-03-12 17:31
332334
Hashkey: 818f3412b998fcf4fe9ca3cca11a3fc3
333335
Data:
334-
execution_seconds: 1.1132317770000002
336+
execution_seconds: 1.0559415130000005
335337

336338
```
337339

@@ -341,7 +343,7 @@ Failed notebooks will not be cached, but the exception traceback will be added t
341343
$ jcache stage show 2
342344
ID: 2
343345
URI: ../tests/notebooks/basic_failing.ipynb
344-
Created: 2020-03-12 17:11
346+
Created: 2020-03-12 17:31
345347
Failed Last Execution!
346348
Traceback (most recent call last):
347349
File "../jupyter_cache/executors/basic.py", line 152, in execute
@@ -396,7 +398,7 @@ Success!
396398
$ jcache stage show 1
397399
ID: 1
398400
URI: ../tests/notebooks/basic.ipynb
399-
Created: 2020-03-12 17:11
401+
Created: 2020-03-12 17:31
400402
Cache ID: 6
401403
Assets:
402404
- ../tests/notebooks/artifact_folder/artifact.txt

jupyter_cache/cli/commands/cmd_cache.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,15 @@ def format_cache_record(record, hashkeys, path_length):
2828

2929
@cmnd_cache.command("list")
3030
@options.CACHE_PATH
31-
@click.option("-h", "--hashkeys", is_flag=True, help="Whether to show hashkeys.")
31+
@click.option(
32+
"-l",
33+
"--latest-only",
34+
is_flag=True,
35+
help="Show only the most recent record per origin URI.",
36+
)
37+
@click.option("-h", "--hashkeys", is_flag=True, help="Show the hashkey of notebook.")
3238
@options.PATH_LENGTH
33-
def list_caches(cache_path, hashkeys, path_length):
39+
def list_caches(cache_path, latest_only, hashkeys, path_length):
3440
"""List cached notebook records in the cache."""
3541
import tabulate
3642

@@ -39,6 +45,15 @@ def list_caches(cache_path, hashkeys, path_length):
3945
if not records:
4046
click.secho("No Cached Notebooks", fg="blue")
4147
# TODO optionally list number of artifacts
48+
if latest_only:
49+
latest_records = {}
50+
for record in records:
51+
if record.uri not in latest_records:
52+
latest_records[record.uri] = record
53+
continue
54+
if latest_records[record.uri].created < record.created:
55+
latest_records[record.uri] = record
56+
records = list(latest_records.values())
4257
click.echo(
4358
tabulate.tabulate(
4459
[

tests/make_cli_readme.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ def main():
9494
)
9595
)
9696
strings.append(get_string(cmd_cache.list_caches, cache_name))
97+
strings.append(
98+
"Tip: Use the `--latest-only` option, "
99+
"to only show the latest versions of cached notebooks."
100+
)
97101
strings.append(
98102
dedent(
99103
"""\

tests/test_cli.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,29 @@ def test_list_caches(tmp_path):
3939
assert "basic.ipynb" in result.output.strip(), result.output
4040

4141

42+
def test_list_caches_latest_only(tmp_path):
43+
db = JupyterCacheBase(str(tmp_path))
44+
db.cache_notebook_file(
45+
path=os.path.join(NB_PATH, "basic.ipynb"),
46+
uri="basic.ipynb",
47+
check_validity=False,
48+
)
49+
db.cache_notebook_file(
50+
path=os.path.join(NB_PATH, "complex_outputs.ipynb"),
51+
uri="basic.ipynb",
52+
check_validity=False,
53+
)
54+
runner = CliRunner()
55+
result = runner.invoke(cmd_cache.list_caches, ["-p", tmp_path])
56+
assert result.exception is None, result.output
57+
assert result.exit_code == 0, result.output
58+
assert len(result.output.strip().splitlines()) == 4, result.output
59+
result = runner.invoke(cmd_cache.list_caches, ["-p", tmp_path, "--latest-only"])
60+
assert result.exception is None, result.output
61+
assert result.exit_code == 0, result.output
62+
assert len(result.output.strip().splitlines()) == 3, result.output
63+
64+
4265
def test_cache_with_artifact(tmp_path):
4366
JupyterCacheBase(str(tmp_path))
4467
nb_path = os.path.join(NB_PATH, "basic.ipynb")

0 commit comments

Comments
 (0)