From a83ec3290497e3b8a9359d626efb31e767ec79cb Mon Sep 17 00:00:00 2001 From: Dinis Cruz Date: Tue, 21 Jan 2025 22:50:50 +0000 Subject: [PATCH] misc refactoring and code clean --- .../mgraph/schemas/Schema__MGraph__Node.py | 1 + .../json/actions/MGraph__Json__Obj_Factory.py | 22 -------- .../MGraph__Json__Obj_Factory__Models.py | 6 +++ .../MGraph__Json__Obj_Factory__Schemas.py | 8 +++ .../exporters/MGraph__Export__Json__Dot.py | 2 +- .../json/models/Model__MGraph__Json__Node.py | 2 + .../test_Perf_Test__MGraph_Json.py | 0 ...t__bug__performance__MGraph__Json__Load.py | 0 .../actions/test_MGraph__Json__Obj_Factory.py | 52 ------------------- .../test_MGraph__Json__Obj_Factory__Models.py | 10 ++++ ...test_MGraph__Json__Obj_Factory__Schemas.py | 42 +++++++++++---- 11 files changed, 61 insertions(+), 84 deletions(-) delete mode 100644 mgraph_ai/providers/json/actions/MGraph__Json__Obj_Factory.py rename tests/unit/providers/json/{utils => _performance}/test_Perf_Test__MGraph_Json.py (100%) rename tests/unit/providers/json/{utils => _performance}/test__bug__performance__MGraph__Json__Load.py (100%) delete mode 100644 tests/unit/providers/json/actions/test_MGraph__Json__Obj_Factory.py diff --git a/mgraph_ai/mgraph/schemas/Schema__MGraph__Node.py b/mgraph_ai/mgraph/schemas/Schema__MGraph__Node.py index 889eff5..b357818 100644 --- a/mgraph_ai/mgraph/schemas/Schema__MGraph__Node.py +++ b/mgraph_ai/mgraph/schemas/Schema__MGraph__Node.py @@ -7,3 +7,4 @@ class Schema__MGraph__Node(Type_Safe): node_data : Schema__MGraph__Node__Data node_id : Obj_Id node_type : Type['Schema__MGraph__Node'] + diff --git a/mgraph_ai/providers/json/actions/MGraph__Json__Obj_Factory.py b/mgraph_ai/providers/json/actions/MGraph__Json__Obj_Factory.py deleted file mode 100644 index 665514b..0000000 --- a/mgraph_ai/providers/json/actions/MGraph__Json__Obj_Factory.py +++ /dev/null @@ -1,22 +0,0 @@ -from mgraph_ai.mgraph.schemas.Schema__MGraph__Node__Data import Schema__MGraph__Node__Data -from mgraph_ai.providers.json.schemas.Schema__MGraph__Json__Node import Schema__MGraph__Json__Node -from osbot_utils.helpers.Obj_Id import Obj_Id -from osbot_utils.type_safe.Type_Safe import Type_Safe - - -class MGraph__Json__Obj_Factory(Type_Safe): - - def create__Schema__MGraph__Node__Data(self): - return object.__new__(Schema__MGraph__Node__Data) - - def create__Schema__MGraph__Json__Node(self): - node_data = self.create__Schema__MGraph__Node__Data() - schema_node = object.__new__(Schema__MGraph__Json__Node) - schema_node_dict = dict(node_data = node_data , - node_id = Obj_Id() , - node_type = Schema__MGraph__Json__Node) - object.__setattr__(schema_node, '__dict__', schema_node_dict) - - return schema_node - - diff --git a/mgraph_ai/providers/json/actions/MGraph__Json__Obj_Factory__Models.py b/mgraph_ai/providers/json/actions/MGraph__Json__Obj_Factory__Models.py index 00599c0..82959e5 100644 --- a/mgraph_ai/providers/json/actions/MGraph__Json__Obj_Factory__Models.py +++ b/mgraph_ai/providers/json/actions/MGraph__Json__Obj_Factory__Models.py @@ -23,6 +23,12 @@ def create__Model__MGraph__Json__Types(self): object.__setattr__(types, '__dict__', types_dict) return types + def create__Model__MGraph__Json__Node(self, data=None): + node = object.__new__(Model__MGraph__Json__Node) + node_dict = dict(data = data or self.json_schema_factory.create__Schema__MGraph__Json__Node()) + object.__setattr__(node, '__dict__', node_dict) + return node + def create__Model__MGraph__Json__Node__Value(self): node = object.__new__(Model__MGraph__Json__Node__Value) node_dict = dict(data = self.json_schema_factory.create__Schema__MGraph__Json__Node__Value()) diff --git a/mgraph_ai/providers/json/actions/MGraph__Json__Obj_Factory__Schemas.py b/mgraph_ai/providers/json/actions/MGraph__Json__Obj_Factory__Schemas.py index 5b5430f..3767f87 100644 --- a/mgraph_ai/providers/json/actions/MGraph__Json__Obj_Factory__Schemas.py +++ b/mgraph_ai/providers/json/actions/MGraph__Json__Obj_Factory__Schemas.py @@ -19,6 +19,14 @@ def __init__(self): super().__init__() self.schema_factory = MGraph__Obj_Factory__Schemas() + def create__Schema__MGraph__Json__Node(self): + node = object.__new__(Schema__MGraph__Json__Node) + node_dict = dict(node_data = self.schema_factory.create__Schema__MGraph__Node__Data(), + node_id = self.schema_factory.create__Schema__MGraph__Node().node_id, + node_type = Schema__MGraph__Json__Node) + object.__setattr__(node, '__dict__', node_dict) + return node + def create__Schema__MGraph__Json__Node__Value__Data(self): data = object.__new__(Schema__MGraph__Json__Node__Value__Data) data_dict = dict(value = None, diff --git a/mgraph_ai/providers/json/actions/exporters/MGraph__Export__Json__Dot.py b/mgraph_ai/providers/json/actions/exporters/MGraph__Export__Json__Dot.py index 332d8cd..540296a 100644 --- a/mgraph_ai/providers/json/actions/exporters/MGraph__Export__Json__Dot.py +++ b/mgraph_ai/providers/json/actions/exporters/MGraph__Export__Json__Dot.py @@ -51,7 +51,7 @@ def format_value(self, value: Any) -> str: # if isinstance(value, str): # dot specific escaping escaped = value.replace('"', '\\"') return f'{escaped}' - return f'"{value}"' + #return f'"{value}"' def process_node(self, node: Any) -> str: # Process a node and generate DOT representation node_type = self.get_node_type(node) diff --git a/mgraph_ai/providers/json/models/Model__MGraph__Json__Node.py b/mgraph_ai/providers/json/models/Model__MGraph__Json__Node.py index 5af6427..3bbcafa 100644 --- a/mgraph_ai/providers/json/models/Model__MGraph__Json__Node.py +++ b/mgraph_ai/providers/json/models/Model__MGraph__Json__Node.py @@ -4,3 +4,5 @@ class Model__MGraph__Json__Node(Model__MGraph__Node): # Base model class for JSON nodes data: Schema__MGraph__Json__Node + def __init__(self, **kwargs): + super().__init__(**kwargs) \ No newline at end of file diff --git a/tests/unit/providers/json/utils/test_Perf_Test__MGraph_Json.py b/tests/unit/providers/json/_performance/test_Perf_Test__MGraph_Json.py similarity index 100% rename from tests/unit/providers/json/utils/test_Perf_Test__MGraph_Json.py rename to tests/unit/providers/json/_performance/test_Perf_Test__MGraph_Json.py diff --git a/tests/unit/providers/json/utils/test__bug__performance__MGraph__Json__Load.py b/tests/unit/providers/json/_performance/test__bug__performance__MGraph__Json__Load.py similarity index 100% rename from tests/unit/providers/json/utils/test__bug__performance__MGraph__Json__Load.py rename to tests/unit/providers/json/_performance/test__bug__performance__MGraph__Json__Load.py diff --git a/tests/unit/providers/json/actions/test_MGraph__Json__Obj_Factory.py b/tests/unit/providers/json/actions/test_MGraph__Json__Obj_Factory.py deleted file mode 100644 index 8c6180b..0000000 --- a/tests/unit/providers/json/actions/test_MGraph__Json__Obj_Factory.py +++ /dev/null @@ -1,52 +0,0 @@ -from unittest import TestCase -from osbot_utils.helpers.trace.Trace_Call import trace_calls -from osbot_utils.helpers.Obj_Id import Obj_Id -from osbot_utils.utils.Objects import __, type_full_name -from osbot_utils.testing.performance.Performance_Measure__Session import Performance_Measure__Session -from mgraph_ai.providers.json.actions.MGraph__Json__Obj_Factory import MGraph__Json__Obj_Factory -from mgraph_ai.mgraph.schemas.Schema__MGraph__Node__Data import Schema__MGraph__Node__Data -from mgraph_ai.providers.json.schemas.Schema__MGraph__Json__Node import Schema__MGraph__Json__Node - - -class test_MGraph__Json__Obj_Factory(TestCase): - - @classmethod - def setUpClass(cls): - cls.assert_enabled = True - cls.session = Performance_Measure__Session(assert_enabled=cls.assert_enabled) - cls.obj_factory = MGraph__Json__Obj_Factory() - - def test_create__Schema__MGraph__Node__Data(self): - schema_node_data = self.obj_factory.create__Schema__MGraph__Node__Data() - with schema_node_data as _: - assert type(_) is Schema__MGraph__Node__Data - assert _.obj() == __() - - def test_create__Schema__MGraph__Json__Node(self): - schema_json_node = self.obj_factory.create__Schema__MGraph__Json__Node() - with schema_json_node as _: - assert type(_) is Schema__MGraph__Json__Node - assert type(_.node_data) is Schema__MGraph__Node__Data - assert type(_.node_id ) is Obj_Id - assert type(_.node_type) is type - assert _.obj() == __(node_data = __() , - node_id = _.node_id , - node_type = type_full_name(Schema__MGraph__Json__Node)) - - - - def test__perf__Schema__MGraph__Json__Node(self): - with self.session as _: - print() - print() - _.measure(self.obj_factory.create__Schema__MGraph__Node__Data).print().assert_time__less_than(200) - _.measure(self.obj_factory.create__Schema__MGraph__Json__Node).print().assert_time__less_than(1000) - # _.measure(Schema__MGraph__Json__Node ).print(34) - # _.measure(Obj_Id ).print(34) - - -# @trace_calls( include = ['*'], - # show_duration = True , - # duration_padding= 120 , - # show_class = True , - # ) \ No newline at end of file diff --git a/tests/unit/providers/json/actions/test_MGraph__Json__Obj_Factory__Models.py b/tests/unit/providers/json/actions/test_MGraph__Json__Obj_Factory__Models.py index 17f74e4..46dbe51 100644 --- a/tests/unit/providers/json/actions/test_MGraph__Json__Obj_Factory__Models.py +++ b/tests/unit/providers/json/actions/test_MGraph__Json__Obj_Factory__Models.py @@ -35,6 +35,7 @@ def test_create_all_objects(self): print() _.padding = 50 _.measure(self.models_factory.create__Model__MGraph__Json__Types ).print().assert_time__less_than(500) + _.measure(self.models_factory.create__Model__MGraph__Json__Node ).print().assert_time__less_than(3000) _.measure(self.models_factory.create__Model__MGraph__Json__Node__Value ).print().assert_time__less_than(3000) _.measure(self.models_factory.create__Model__MGraph__Json__Node__Property ).print().assert_time__less_than(3000) _.measure(self.models_factory.create__Model__MGraph__Json__Node__Dict ).print().assert_time__less_than(3000) @@ -59,6 +60,15 @@ def test_create__Model__MGraph__Json__Types(self): assert _.obj() == __(node_model_type = type_full_name(Model__MGraph__Json__Node), edge_model_type = type_full_name(Model__MGraph__Json__Edge)) + def test_create__Model__MGraph__Json__Node(self): + node = self.models_factory.create__Model__MGraph__Json__Node() + with node as _: + assert type(_) is Model__MGraph__Json__Node + assert type(_.data) is Schema__MGraph__Json__Node + assert _.obj() == __(data = __(node_data = __(), + node_id = _.data.node_id, + node_type = type_full_name(Schema__MGraph__Json__Node))) + def test_create__Model__MGraph__Json__Node__Value(self): node = self.models_factory.create__Model__MGraph__Json__Node__Value() with node as _: diff --git a/tests/unit/providers/json/actions/test_MGraph__Json__Obj_Factory__Schemas.py b/tests/unit/providers/json/actions/test_MGraph__Json__Obj_Factory__Schemas.py index ec6324b..043d8c8 100644 --- a/tests/unit/providers/json/actions/test_MGraph__Json__Obj_Factory__Schemas.py +++ b/tests/unit/providers/json/actions/test_MGraph__Json__Obj_Factory__Schemas.py @@ -1,9 +1,12 @@ from unittest import TestCase +from mgraph_ai.mgraph.schemas.Schema__MGraph__Node import Schema__MGraph__Node +from mgraph_ai.mgraph.schemas.Schema__MGraph__Node__Data import Schema__MGraph__Node__Data from mgraph_ai.providers.json.schemas.Schema__MGraph__Json__Node import Schema__MGraph__Json__Node from mgraph_ai.providers.json.schemas.Schema__MGraph__Json__Node__Property__Data import Schema__MGraph__Json__Node__Property__Data from mgraph_ai.providers.json.schemas.Schema__MGraph__Json__Node__Value__Data import Schema__MGraph__Json__Node__Value__Data from osbot_utils.testing.performance.Performance_Measure__Session import Performance_Measure__Session -from osbot_utils.utils.Objects import __, type_full_name +from osbot_utils.type_safe.Type_Safe import Type_Safe +from osbot_utils.utils.Objects import __, type_full_name, base_types from mgraph_ai.providers.json.actions.MGraph__Json__Obj_Factory__Schemas import MGraph__Json__Obj_Factory__Schemas from mgraph_ai.providers.json.schemas.Schema__MGraph__Json__Edge import Schema__MGraph__Json__Edge from mgraph_ai.providers.json.schemas.Schema__MGraph__Json__Graph import Schema__MGraph__Json__Graph @@ -28,17 +31,38 @@ def test_create_all_objects(self): print() print() _.padding = 50 - _.measure(self.json_factory.create__Schema__MGraph__Json__Node__Value__Data ).print().assert_time__less_than(300) + _.measure(self.json_factory.create__Schema__MGraph__Json__Node ).print().assert_time__less_than(300) + _.measure(Schema__MGraph__Json__Node ).print().assert_time__less_than(300) + print() + _.measure(self.json_factory.create__Schema__MGraph__Json__Node__Value__Data ).print().assert_time__less_than(300) + _.measure(Schema__MGraph__Json__Node__Value__Data ).print().assert_time__less_than(300) + print() _.measure(self.json_factory.create__Schema__MGraph__Json__Node__Property__Data).print().assert_time__less_than(300) - _.measure(self.json_factory.create__Schema__MGraph__Json__Node__Value ).print().assert_time__less_than(1000) - _.measure(self.json_factory.create__Schema__MGraph__Json__Node__Property ).print().assert_time__less_than(1000) - _.measure(self.json_factory.create__Schema__MGraph__Json__Node__Dict ).print().assert_time__less_than(1000) - _.measure(self.json_factory.create__Schema__MGraph__Json__Node__List ).print().assert_time__less_than(1000) - _.measure(self.json_factory.create__Schema__MGraph__Json__Edge ).print().assert_time__less_than(2000) - _.measure(self.json_factory.create__Schema__MGraph__Json__Graph__Data ).print().assert_time__less_than(300) - _.measure(self.json_factory.create__Schema__MGraph__Json__Types ).print().assert_time__less_than(500) + _.measure(Schema__MGraph__Json__Node__Property__Data ).print().assert_time__less_than(300) + print() + _.measure(self.json_factory.create__Schema__MGraph__Json__Node__Value ).print().assert_time__less_than(1000) + _.measure(Schema__MGraph__Json__Node__Value ).print().assert_time__less_than(300) + print() + _.measure(self.json_factory.create__Schema__MGraph__Json__Node__Property ).print().assert_time__less_than(1000) + _.measure(Schema__MGraph__Json__Node__Property ).print().assert_time__less_than(300) + print() + _.measure(self.json_factory.create__Schema__MGraph__Json__Node__Dict ).print().assert_time__less_than(1000) + _.measure(self.json_factory.create__Schema__MGraph__Json__Node__List ).print().assert_time__less_than(1000) + _.measure(self.json_factory.create__Schema__MGraph__Json__Edge ).print().assert_time__less_than(2000) + _.measure(self.json_factory.create__Schema__MGraph__Json__Graph__Data ).print().assert_time__less_than(300) + _.measure(self.json_factory.create__Schema__MGraph__Json__Types ).print().assert_time__less_than(500) _.measure(self.json_factory.create__Schema__MGraph__Json__Graph ).print().assert_time__less_than(3000) + def test_create__Schema__MGraph__Json__Node(self): + node = self.json_factory.create__Schema__MGraph__Json__Node() + with node as _: + assert type(_) is Schema__MGraph__Json__Node + assert type(_.node_data) is Schema__MGraph__Node__Data + assert base_types(_) == [Schema__MGraph__Node, Type_Safe, object] + assert _.obj() == __(node_data = __(), + node_id = _.node_id, + node_type = type_full_name(Schema__MGraph__Json__Node)) + def test_create__Schema__MGraph__Json__Node__Value__Data(self): data = self.json_factory.create__Schema__MGraph__Json__Node__Value__Data() with data as _: