From b072121e201bf1d89be6a71fd3492c3c43b9b479 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=2E=20Fernando=20S=C3=A1nchez?= Date: Thu, 2 Feb 2017 04:46:35 +0100 Subject: [PATCH] Added Model string representation This should help with performance issues with models that have large private variables. --- senpy/VERSION | 2 +- senpy/models.py | 6 +++++- tests/test_models.py | 11 +++++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/senpy/VERSION b/senpy/VERSION index c2de2e8..142f9ea 100644 --- a/senpy/VERSION +++ b/senpy/VERSION @@ -1 +1 @@ -0.7.0-dev1 +0.7.0-dev2 diff --git a/senpy/models.py b/senpy/models.py index 348ed87..e6ca1f5 100644 --- a/senpy/models.py +++ b/senpy/models.py @@ -147,6 +147,9 @@ class SenpyMixin(object): obj = obj.jsonld() jsonschema.validate(obj, self.schema) + def __str__(self): + return str(self.to_JSON()) + class SenpyModel(SenpyMixin, dict): @@ -198,7 +201,8 @@ class SenpyModel(SenpyMixin, dict): def _plain_dict(self): d = {k: v for (k, v) in self.items() if k[0] != "_"} - d["@id"] = d.pop('id') + if 'id' in d: + d["@id"] = d.pop('id') return d diff --git a/tests/test_models.py b/tests/test_models.py index 7460274..a21b240 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -100,5 +100,16 @@ class ModelsTest(TestCase): logging.debug(c) p.validate() + def test_str(self): + """The string representation shouldn't include private variables""" + r = Results() + p = SenpyPlugin({"name": "STR test", "version": 0}) + p._testing = 0 + s = str(p) + assert "_testing" not in s + r.analysis.append(p) + s = str(r) + assert "_testing" not in s + def test_frame_response(self): pass