From d75b394837a6b2c71dd55a9638bc896cb9ac0dad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20tom=20W=C3=B6rden?= <h.tomwoerden@indiscale.com> Date: Fri, 25 Nov 2022 10:45:37 +0100 Subject: [PATCH] ENH: add nice string representation for identifiables --- src/caoscrawler/identifiable.py | 8 ++++++++ unittests/test_identifiable.py | 5 +++++ 2 files changed, 13 insertions(+) diff --git a/src/caoscrawler/identifiable.py b/src/caoscrawler/identifiable.py index 5b2b2152..0db566bf 100644 --- a/src/caoscrawler/identifiable.py +++ b/src/caoscrawler/identifiable.py @@ -22,6 +22,7 @@ from __future__ import annotations import caosdb as db from datetime import datetime +import json from hashlib import sha256 from typing import Union @@ -127,3 +128,10 @@ class Identifiable(): return True else: return False + + def __repr__(self): + pstring = json.dumps(self.properties) + return (f"{self.__class__.__name__} for RT {self.record_type}: id={self.record_id}; " + f"name={self.name}\n\tpath={self.path}\n" + f"\tproperties:\n{pstring}\n" + f"\tbackrefs:\n{self.backrefs}") diff --git a/unittests/test_identifiable.py b/unittests/test_identifiable.py index c06ef0ec..d1bc275f 100644 --- a/unittests/test_identifiable.py +++ b/unittests/test_identifiable.py @@ -65,6 +65,11 @@ def test_name(): Identifiable(properties={"Name": 'li'}) +def test_repr(): + # only test that something meaningful is returned + assert 'properties' str(Identifiable(name="A", record_type="B")) + + def test_equality(): assert Identifiable( record_id=12, properties={"a": 0}) == Identifiable(record_id=12, properties={"a": 1}) -- GitLab