Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ Re-design package and introduce large parts of the API (#90)
* notebook read write and dependencies * add dev * add meta api * add integrity * add repr to meta * title check in nb * fix_title * restructure store * lazily load infer_dependencies * add initialization function * add docstrings * change to google style * upd docs * autosummary * Clear cache * remove meta from docs * Add meta back to autosummary * 🚧 add a regular import to test * avoid loading meta on import * without metadata class * Suggestion on using autoclass instead of autosummary * renamings in meta * fix docs * further edits * add write to meta Co-authored-by: Alex Wolf <f.alexander.wolf@gmail.com>
- Loading branch information
Showing
20 changed files
with
433 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import secrets | ||
import string | ||
from datetime import datetime, timezone | ||
from typing import Mapping, Optional | ||
|
||
from pydantic import BaseModel, Extra | ||
|
||
from ._notebook import Notebook | ||
|
||
|
||
class MetaStore(BaseModel): | ||
"""The metadata stored in the notebook file.""" | ||
|
||
id: str | ||
time_init: str | ||
dependency: Optional[Mapping[str, str]] = None | ||
|
||
class Config: # noqa | ||
extra = Extra.allow | ||
|
||
|
||
def nbproject_id(): # rename to nbproject_id also in metadata slot? | ||
"""An 8-byte ID encoded as a 12-character base62 string.""" | ||
# https://github.com/laminlabs/notes/blob/main/2022-04-04-human-friendly-ids.ipynb | ||
base62 = string.digits + string.ascii_letters.swapcase() | ||
id = "".join(secrets.choice(base62) for i in range(12)) | ||
return id | ||
|
||
|
||
def initialize_metadata(nb: Optional[Notebook]) -> MetaStore: | ||
"""Initialize nbproject metadata. | ||
Args: | ||
nb: If a notebook is provided, also infer dependencies from the notebook. | ||
""" | ||
meta = MetaStore( | ||
id=nbproject_id(), time_init=datetime.now(timezone.utc).isoformat() | ||
) | ||
|
||
if nb is not None: | ||
from ._dependency import infer_dependencies | ||
|
||
meta.dependency = infer_dependencies(nb, pin_versions=True) | ||
|
||
return meta |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from typing import Optional | ||
|
||
from ._notebook import Notebook | ||
|
||
|
||
def check_integrity(nb: Notebook, ignore_code: Optional[str] = None) -> bool: | ||
"""Get current integrity status of the passed notebook. | ||
For `True` the code cells of the notebook must be executed consequently, i.e. | ||
execution count for each code cell should increase by one. | ||
Args: | ||
nb: The notebook to check. | ||
ignore_code: Ignore all cells which contain this code. | ||
""" | ||
cells = nb.cells | ||
|
||
integrity = True | ||
prev = 0 | ||
|
||
for cell in cells: | ||
if cell["cell_type"] != "code" or cell["source"] == []: | ||
continue | ||
|
||
if ignore_code is not None and ignore_code in "".join(cell["source"]): | ||
continue | ||
|
||
ccount = cell["execution_count"] | ||
if ccount is None or ccount - prev != 1: | ||
integrity = False | ||
break | ||
|
||
prev = ccount | ||
|
||
return integrity |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from pathlib import Path | ||
from typing import Union | ||
|
||
import orjson | ||
from pydantic import BaseModel | ||
|
||
|
||
class Notebook(BaseModel): | ||
metadata: dict | ||
nbformat: int | ||
nbformat_minor: int | ||
cells: list | ||
|
||
|
||
def read_notebook(filepath: Union[str, Path]) -> Notebook: | ||
"""Read a notebook from disk. | ||
Args: | ||
filepath: A path to the notebook to read. | ||
""" | ||
with open(filepath, "rb") as f: | ||
nb = orjson.loads(f.read()) | ||
|
||
return Notebook(**nb) | ||
|
||
|
||
def write_notebook(nb: Notebook, filepath: Union[str, Path]): | ||
"""Write the notebook to disk. | ||
Args: | ||
nb: Notebook to write. | ||
filepath: Path where to write the notebook. | ||
""" | ||
with open(filepath, "wb") as f: | ||
f.write(orjson.dumps(nb.dict())) |
Oops, something went wrong.