From e254be8dfb49a3883aa7c56a0a43c49345af1de4 Mon Sep 17 00:00:00 2001
From: Padraig Gleeson
Date: Wed, 7 Feb 2024 17:25:52 +0000
Subject: [PATCH 1/4] Print differences
---
.github/workflows/ci.yml | 1 +
1 file changed, 1 insertion(+)
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 0e52956..e5d2c93 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -52,6 +52,7 @@ jobs:
run: |
cd examples
python document.py
+ git diff
cd test
python test.py
From 3746060c5b4a87294e8781d5b44a0b3ea716255a Mon Sep 17 00:00:00 2001
From: pgleeson
Date: Wed, 6 Mar 2024 15:26:40 +0000
Subject: [PATCH 2/4] Regenerated
---
docs/sphinx/source/api/Contributors.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/docs/sphinx/source/api/Contributors.md b/docs/sphinx/source/api/Contributors.md
index 36c90ea..ee65d18 100644
--- a/docs/sphinx/source/api/Contributors.md
+++ b/docs/sphinx/source/api/Contributors.md
@@ -3,7 +3,7 @@
# Modelspec contributors
This page list names and Github profiles of contributors to Modelspec, listed in no particular order.
-This page is generated periodically, most recently on 2024-02-07.
+This page is generated periodically, most recently on 2024-03-06.
- Padraig Gleeson ([@pgleeson](https://github.com/pgleeson))
- Manifest Chakalov ([@mqnifestkelvin](https://github.com/mqnifestkelvin))
From fd97461302c5251b612bf44078f37b6a90587b8c Mon Sep 17 00:00:00 2001
From: pgleeson
Date: Thu, 21 Mar 2024 09:43:16 +0000
Subject: [PATCH 3/4] Update an action, test using dev packages
---
.github/workflows/ci.yml | 2 +-
docs/sphinx/source/api/Contributors.md | 2 +-
test_all.sh | 4 ++--
3 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index e5d2c93..83ca0ab 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -14,7 +14,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
- - uses: pre-commit/action@v3.0.0
+ - uses: pre-commit/action@v3.0.1
with:
extra_args: --hook-stage manual --all-files
diff --git a/docs/sphinx/source/api/Contributors.md b/docs/sphinx/source/api/Contributors.md
index ee65d18..aaffb6b 100644
--- a/docs/sphinx/source/api/Contributors.md
+++ b/docs/sphinx/source/api/Contributors.md
@@ -3,7 +3,7 @@
# Modelspec contributors
This page list names and Github profiles of contributors to Modelspec, listed in no particular order.
-This page is generated periodically, most recently on 2024-03-06.
+This page is generated periodically, most recently on 2024-03-21.
- Padraig Gleeson ([@pgleeson](https://github.com/pgleeson))
- Manifest Chakalov ([@mqnifestkelvin](https://github.com/mqnifestkelvin))
diff --git a/test_all.sh b/test_all.sh
index f646a3e..e78ec14 100755
--- a/test_all.sh
+++ b/test_all.sh
@@ -1,9 +1,9 @@
#!/bin/bash
set -ex
-## Install
+## Install modelspec incl. dev dependencies
-pip install .
+pip install .[dev]
## Test main example
From bd315fd4effcdbbd39fe1c88ce8104017dc65e21 Mon Sep 17 00:00:00 2001
From: pgleeson
Date: Thu, 21 Mar 2024 10:02:34 +0000
Subject: [PATCH 4/4] Fix numerous flake highlighted issues (== -> is etc)
---
.github/workflows/ci.yml | 2 ++
docs/sphinx/source/api/examples/document.py | 6 ++--
examples/document.py | 6 ++--
examples/test/test.py | 4 +--
src/modelspec/base_types.py | 8 +----
src/modelspec/utils.py | 34 ++++++++++-----------
tests/test_base.py | 5 +--
7 files changed, 30 insertions(+), 35 deletions(-)
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 83ca0ab..b38197f 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -14,6 +14,8 @@ jobs:
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
+ with:
+ python-version: "3.10"
- uses: pre-commit/action@v3.0.1
with:
extra_args: --hook-stage manual --all-files
diff --git a/docs/sphinx/source/api/examples/document.py b/docs/sphinx/source/api/examples/document.py
index 578099c..83aa52e 100644
--- a/docs/sphinx/source/api/examples/document.py
+++ b/docs/sphinx/source/api/examples/document.py
@@ -3,6 +3,9 @@
from modelspec.base_types import Base
from typing import List
import sys
+import json
+import yaml
+import bson
# Example showing how to create a model of a document and use it to create/serialize instances
@@ -96,9 +99,6 @@ class Document(Base):
print("\n >> Generating specification in dict form...")
doc_dict = doc.generate_documentation(format="dict")
-import json
-import yaml
-import bson
with open("document.specification.json", "w") as d:
d.write(json.dumps(doc_dict, indent=4))
diff --git a/examples/document.py b/examples/document.py
index 578099c..83aa52e 100644
--- a/examples/document.py
+++ b/examples/document.py
@@ -3,6 +3,9 @@
from modelspec.base_types import Base
from typing import List
import sys
+import json
+import yaml
+import bson
# Example showing how to create a model of a document and use it to create/serialize instances
@@ -96,9 +99,6 @@ class Document(Base):
print("\n >> Generating specification in dict form...")
doc_dict = doc.generate_documentation(format="dict")
-import json
-import yaml
-import bson
with open("document.specification.json", "w") as d:
d.write(json.dumps(doc_dict, indent=4))
diff --git a/examples/test/test.py b/examples/test/test.py
index ce820a7..afa628b 100644
--- a/examples/test/test.py
+++ b/examples/test/test.py
@@ -1,10 +1,9 @@
import modelspec
from modelspec import field, instance_of, optional
from modelspec.base_types import Base
-from modelspec.utils import load_json
-from typing import List
from typing import Any
import sys
+import yaml
# Example testing multiple options...
@@ -104,7 +103,6 @@ class TopClass(Base):
print("\nGenerating specification in dict form")
doc_dict = tc.generate_documentation(format="dict")
-import yaml
print("Generating specification in YAML")
with open("test.specification.yaml", "w") as d:
diff --git a/src/modelspec/base_types.py b/src/modelspec/base_types.py
index 5dbe8f4..f9459a3 100644
--- a/src/modelspec/base_types.py
+++ b/src/modelspec/base_types.py
@@ -169,18 +169,13 @@ def from_yaml(cls, yaml_str: str) -> "Base":
@classmethod
def from_yaml_file(cls, yaml_file: str) -> "Base":
"""Instantiate an modelspec object from a file containing YAML"""
- return cls.from_dict(yaml.load(yaml_str, Loader=yaml.SafeLoader))
+ return cls.from_dict(yaml.load(yaml_file, Loader=yaml.SafeLoader))
@classmethod
def from_json(cls, json_str: str) -> "Base":
"""Instantiate an modelspec object from a JSON string"""
return cls.from_dict(json.loads(json_str))
- @classmethod
- def from_json_file(cls, json_file: str) -> "Base":
- """Instantiate an modelspec object from a file containing JSON"""
- return cls.from_dict(json.load(json_file))
-
@classmethod
def from_bson(cls, bson_str: str) -> "Base":
"""Instantiate an modelspec object from a BSON string"""
@@ -318,7 +313,6 @@ def to_xml_file(
filename: Optional[str] = None,
include_metadata: bool = True,
) -> str:
- from modelspec.utils import build_xml_element
if filename is None:
filename = f"{self.id}.xml"
diff --git a/src/modelspec/utils.py b/src/modelspec/utils.py
index 29f9275..af2ca29 100644
--- a/src/modelspec/utils.py
+++ b/src/modelspec/utils.py
@@ -14,7 +14,7 @@
from modelspec.base_types import EvaluableExpression
from random import Random
-from typing import Union, Dict
+from typing import Union
verbose = False
@@ -272,11 +272,11 @@ def build_xml_element(data, parent=None):
)
if attribute_value is not None:
if (
- type(attribute_value) == int
- or type(attribute_value) == float
- or type(attribute_value) == str
- or type(attribute_value) == bool
- or type(attribute_value) == list
+ type(attribute_value) is int
+ or type(attribute_value) is float
+ or type(attribute_value) is str
+ or type(attribute_value) is bool
+ or type(attribute_value) is list
):
parent.set(attribute_name, str(attribute_value))
elif type(attribute_value) == dict:
@@ -411,15 +411,15 @@ def _val_info(param_val):
pp = "%s" % param_val
pp = pp.replace("\n", "")
# pp+=' (TF %s %s)'%(param_val.shape,param_val.dtype)
- elif type(param_val) == tuple:
+ elif type(param_val) is tuple:
# If param_val is a tuple, recursively print its elements
# separated by commas and wrapped in parentheses
pp = "(" + ", ".join([_val_info(el) for el in param_val]) + ")"
else:
pp = "%s" % param_val
t = type(param_val)
- if not (t == int or t == float):
- pp += "(%s)" % (t if type(t) == str else t.__name__)
+ if not (t is int or t is float):
+ pp += "(%s)" % (t if type(t) is str else t.__name__)
return pp
@@ -476,13 +476,13 @@ def evaluate(
verbose,
)
try:
- if type(expr) == str and expr in parameters:
+ if type(expr) is str and expr in parameters:
expr = parameters[
expr
] # replace with the value in parameters & check whether it's float/int...
print_(" Using for that param: %s" % _val_info(expr), verbose)
- if type(expr) == str:
+ if type(expr) is str:
try:
if array_format == FORMAT_TENSORFLOW:
expr = tf.constant(int(expr))
@@ -498,14 +498,14 @@ def evaluate(
except:
pass
- if type(expr) == list:
+ if type(expr) is list:
print_(" Returning a list in format: %s" % array_format, verbose)
if array_format == FORMAT_TENSORFLOW:
return tf.constant(expr, dtype=tf.float64)
else:
return np.array(expr)
- if type(expr) == np.ndarray:
+ if type(expr) is np.ndarray:
print_(" Returning a numpy array in format: %s" % array_format, verbose)
if array_format == FORMAT_TENSORFLOW:
return tf.convert_to_tensor(expr, dtype=tf.float64)
@@ -538,9 +538,9 @@ def evaluate(
"The expression [%s] contains a random() call, but a random number generator (rng) must be supplied to the evaluate() call when this expression string is to be evaluated"
)
- if type(expr) == str and "math." in expr:
+ if type(expr) is str and "math." in expr:
parameters["math"] = math
- if type(expr) == str and "numpy." in expr:
+ if type(expr) is str and "numpy." in expr:
parameters["numpy"] = np
print_(
@@ -556,7 +556,7 @@ def evaluate(
verbose,
)
- if (type(v) == float or type(v) == str) and int(v) == v:
+ if (type(v) is float or type(v) is str) and int(v) == v:
print_(" Returning int: %s" % int(v), verbose)
@@ -583,7 +583,7 @@ def parse_list_like(list_str):
return [list_str]
elif isinstance(list_str, list):
return list_str
- elif type(list_str) == str:
+ elif type(list_str) is str:
try:
expr = int(list_str)
return [expr]
diff --git a/tests/test_base.py b/tests/test_base.py
index 3cd6fdd..3e3adbe 100644
--- a/tests/test_base.py
+++ b/tests/test_base.py
@@ -192,8 +192,9 @@ def test_save_load_json(tmp_path):
nety3 = NewNetwork.from_yaml_file(filenamey)
str_nety3 = str(nety3)
- # datax = load_xml(filenamex)
- # print_v("Loaded network specification from %s" % filenamex)
+ datax = load_xml(filenamex)
+ assert len(datax) > 0
+ print_v("Loaded network specification from %s" % filenamex)
# netx = NewNetwork.from_dict(datax)
# str_netx = str(netx)