Skip to content

Commit

Permalink
Add import notebook allow empty option (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtth authored Jul 22, 2023
1 parent b231506 commit 0a4c7ce
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class BinPacking(om.Model):

@om.objective
def minimize_bins_used(self):
return self.used.total()
return om.total(self.used(b) for b in self.bins)
```

Auto-generated specification:
Expand Down
23 changes: 13 additions & 10 deletions opvious/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,21 @@
(https://www.npmjs.com/package/opvious-cli) for additional operations.
Usage:
{_COMMAND} register-notebook PATH [MODEL] [-dn NAME] [-t TAGS]
{_COMMAND} register-notebook PATH [MODEL]
[-dn NAME] [-t TAGS] [--allow-empty]
{_COMMAND} register-sources GLOB [-dn NAME] [-t TAGS]
{_COMMAND} (-h | --help)
{_COMMAND} --version
Options:
--allow-empty Do not throw an error if no models were found in a
notebook
-d, --dry-run Validate the specification but do not store it on the
server.
server
-n, --name NAME Formulation name. By default this name is inferred
from the file's name, omitting the extension.
from the file's name, omitting the extension
-t, --tags TAGS Comma-separated list of tags. By default only the
`latest` tag is added.
`latest` tag is added
--version Show SDK version
-h, --help Show this message
"""
Expand Down Expand Up @@ -73,10 +76,11 @@ async def _handle(self, spec: LocalSpecification, name: str) -> None:
async def handle_notebook(
self,
path: str,
model_name: Optional[str] = None,
name: Optional[str] = None,
model_name: Optional[str],
name: Optional[str],
allow_empty: bool,
) -> None:
sn = load_notebook_models(path)
sn = load_notebook_models(path, allow_empty=allow_empty)
if model_name is None:
model_names = list(sn.__dict__.keys())
if not self._dry_run and len(model_names) != 1:
Expand All @@ -89,9 +93,7 @@ async def handle_notebook(
model = getattr(sn, model_name)
await self._handle(model.specification(), name)

async def handle_sources(
self, glob: str, name: Optional[str] = None
) -> None:
async def handle_sources(self, glob: str, name: Optional[str]) -> None:
if name is None:
name = _default_name(glob)
spec = LocalSpecification.globs(glob)
Expand All @@ -111,6 +113,7 @@ async def _run(args: Mapping[str, Any]) -> None:
args["PATH"],
model_name=args["MODEL"],
name=args["--name"],
allow_empty=args["--allow-empty"],
)
elif args["register-sources"]:
await handler.handle_sources(args["GLOB"], name=args["--name"])
Expand Down
7 changes: 4 additions & 3 deletions opvious/specifications/notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@


def load_notebook_models(
path: str, root: Optional[str] = None
path: str, root: Optional[str] = None, allow_empty=False
) -> types.SimpleNamespace:
"""Loads all models from a notebook
Args:
path: Path to the notebook, relative to `root` if present otherwise CWD
root: Root path. If set to a file, its parent directory will be used
(convenient for use with `__file__`).
allow_empty: Do not throw an error if no models were found
"""
if root:
root = os.path.realpath(root)
Expand All @@ -34,6 +35,8 @@ def load_notebook_models(
t.start()
t.join()

if not ns.__dict__ and not allow_empty:
raise Exception(f"No models found in {path}")
return ns


Expand Down Expand Up @@ -92,6 +95,4 @@ def code(self, raw):
if isinstance(value, Model):
count += 1
setattr(ns, attr, value)
if not count:
raise Exception("No models found")
_logger.debug("Loaded %s model(s) from %s.", count, path)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "opvious"
version = "0.17.0rc1"
version = "0.17.0rc2"
description = "Opvious Python SDK"
authors = ["Opvious Engineering <[email protected]>"]
readme = "README.md"
Expand Down

0 comments on commit 0a4c7ce

Please sign in to comment.