From 0a4c7ce064071c16b5333f4843cccd57d4f5f167 Mon Sep 17 00:00:00 2001 From: Matthieu Monsch <1216372+mtth@users.noreply.github.com> Date: Sat, 22 Jul 2023 08:35:15 -0700 Subject: [PATCH] Add import notebook allow empty option (#104) --- README.md | 2 +- opvious/__main__.py | 23 +++++++++++++---------- opvious/specifications/notebook.py | 7 ++++--- pyproject.toml | 2 +- 4 files changed, 19 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index ef35dca..ad2ee45 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/opvious/__main__.py b/opvious/__main__.py index a8b53ec..a37c61d 100644 --- a/opvious/__main__.py +++ b/opvious/__main__.py @@ -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 """ @@ -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: @@ -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) @@ -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"]) diff --git a/opvious/specifications/notebook.py b/opvious/specifications/notebook.py index 66068b4..db02b03 100644 --- a/opvious/specifications/notebook.py +++ b/opvious/specifications/notebook.py @@ -12,7 +12,7 @@ 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 @@ -20,6 +20,7 @@ def load_notebook_models( 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) @@ -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 @@ -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) diff --git a/pyproject.toml b/pyproject.toml index 7c4e62a..aa81fda 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 "] readme = "README.md"