Skip to content

Commit

Permalink
refactor, avoid multiple json load and dump
Browse files Browse the repository at this point in the history
  • Loading branch information
zmezei committed Jul 6, 2023
1 parent f552b91 commit 0b2a1ff
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
25 changes: 14 additions & 11 deletions src/uagents/models.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import hashlib
from typing import Type, Union, Dict, Any
import json
import copy
from pydantic import BaseModel


class Model(BaseModel):
_schema_no_descr = None
_schema_no_descriptions = None

@staticmethod
def remove_descriptions(schema: Dict[str, Dict[str, str]]):
fields_with_descr = []
if not "properties" in schema:
return

fields_with_descr = []
for field_name, field_props in schema["properties"].items():
if "description" in field_props:
fields_with_descr.append(field_name)

for field_name in fields_with_descr:
del schema["properties"][field_name]["description"]

Expand All @@ -24,21 +25,23 @@ def remove_descriptions(schema: Dict[str, Dict[str, str]]):
Model.remove_descriptions(definition)

@classmethod
def schema_no_descr(cls) -> Dict[str, Any]:
if cls._schema_no_descr is None:
schema = json.loads(cls.schema_json(indent=None, sort_keys=True))
def schema_no_descriptions(cls) -> Dict[str, Any]:
if cls._schema_no_descriptions is None:
schema = copy.deepcopy(cls.schema())
Model.remove_descriptions(schema)
cls._schema_no_descr = schema
return cls._schema_no_descr
cls._schema_no_descriptions = schema
return cls._schema_no_descriptions

@classmethod
def schema_json_no_descr(cls) -> str:
return json.dumps(cls.schema_no_descr())
def schema_json_no_descriptions(cls) -> str:
return json.dumps(cls.schema_no_descriptions(), indent=None, sort_keys=True)

@staticmethod
def build_schema_digest(model: Union["Model", Type["Model"]]) -> str:
digest = (
hashlib.sha256(model.schema_json_no_descr().encode("utf-8")).digest().hex()
hashlib.sha256(model.schema_json_no_descriptions().encode("utf-8"))
.digest()
.hex()
)
return f"model:{digest}"

Expand Down
2 changes: 1 addition & 1 deletion src/uagents/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def manifest(self) -> Dict[str, Any]:

for schema_digest, model in all_models.items():
manifest["models"].append(
{"digest": schema_digest, "schema": model.schema_no_descr()}
{"digest": schema_digest, "schema": model.schema_no_descriptions()}
)

for request, responses in self._replies.items():
Expand Down
6 changes: 3 additions & 3 deletions tests/test_field_descr.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Message(Model):

self.assertEqual(
Message.schema_json(indent=None, sort_keys=True),
Message.schema_json_no_descr(),
Message.schema_json_no_descriptions(),
)

class Message(Model):
Expand All @@ -50,7 +50,7 @@ class Message(Model):

self.assertNotEqual(
Message.schema_json(indent=None, sort_keys=True),
Message.schema_json_no_descr(),
Message.schema_json_no_descriptions(),
)

class MessageArgs(Model):
Expand All @@ -63,7 +63,7 @@ class Message(Model):

self.assertNotEqual(
Message.schema_json(indent=None, sort_keys=True),
Message.schema_json_no_descr(),
Message.schema_json_no_descriptions(),
)

def test_model_digest(self):
Expand Down

0 comments on commit 0b2a1ff

Please sign in to comment.