Skip to content
This repository was archived by the owner on Mar 30, 2023. It is now read-only.

Commit cfef02a

Browse files
author
Jason Costello
committed
Adding support for Definition from dictionary
1 parent 4155986 commit cfef02a

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

hypervector/resources/core/definition.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,16 @@ def list(cls):
4545
return [cls.from_response(definition) for definition in response]
4646

4747
@classmethod
48-
def new(cls, definition_file, project_uuid=None):
48+
def new(cls, definition, project_uuid=None):
4949
endpoint = f"{hypervector.API_BASE}/{cls.resource_name}/add"
50+
51+
if isinstance(definition, dict):
52+
definition_json = definition
53+
else:
54+
definition_json = _parse_definition_from_json_file(definition)
55+
5056
data = {
51-
"definition": _parse_definition_from_json_file(definition_file),
57+
"definition": definition_json,
5258
"project_uuid": project_uuid
5359
}
5460
response = requests.post(endpoint, json=data, headers=cls.get_headers()).json()

tests/test_definition_api_resource.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,36 @@ def test_definition_list(mocked_resources, mocked_responses):
2626
assert retrieved_definition.definition_uuid == definition.definition_uuid
2727

2828

29-
def test_definition_new(mocked_resources):
29+
def test_definition_new_from_dict(mocked_resources):
30+
project, _, _, _ = mocked_resources
31+
32+
definition_dict = {
33+
"definition_name": "Test definition",
34+
"features": [
35+
{
36+
"type": "float",
37+
"distribution": {
38+
"type": "gaussian",
39+
"mu": 1E-2,
40+
"sigma": 1.5
41+
}
42+
}
43+
]
44+
}
45+
46+
definition = hypervector.Definition.new(
47+
definition=definition_dict,
48+
project_uuid=project.project_uuid
49+
)
50+
51+
assert isinstance(definition, hypervector.Definition)
52+
53+
54+
def test_definition_new_from_file(mocked_resources):
3055
project, _, _, _ = mocked_resources
3156

3257
definition = hypervector.Definition.new(
33-
definition_file=get_resource_path("hyperdef.json"),
58+
definition=get_resource_path("hyperdef.json"),
3459
project_uuid=project.project_uuid
3560
)
3661

tests/util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def mocked_resources(mocked_responses):
4343
)
4444

4545
definition = hypervector.Definition.new(
46-
definition_file=get_resource_path("hyperdef.json"),
46+
definition=get_resource_path("hyperdef.json"),
4747
project_uuid=str(uuid.uuid4())
4848
)
4949

0 commit comments

Comments
 (0)