Skip to content

Commit 19ddf5e

Browse files
committed
Improve repr string representation of SummaryCounters
1 parent 9816ca0 commit 19ddf5e

File tree

3 files changed

+572
-19
lines changed

3 files changed

+572
-19
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ See also https://github.com/neo4j/neo4j-python-driver/wiki for a full changelog.
193193
- The driver incorrectly applied a timeout hint received from the server to both read and write I/O operations.
194194
It is now only applied to read I/O operations.
195195
In turn, a new configuration option `connection_write_timeout` with a default value of `30 seconds` is introduced.
196-
196+
- Adjust `repr` string representation of `SummaryCounters` to conform with Python's recommendations.
197197

198198
## Version 5.28
199199
- Since the types of `Relationship`s are tied to the `Graph` object they belong to, fixing `pickle` support for graph types means that `Relationship`s with the same name will have a different type after `deepcopy`ing or pickling and unpickling them or their graph.

src/neo4j/_work/summary.py

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,28 @@ def status_precedence(status: GqlStatusObject) -> int:
330330
return self._gql_status_objects
331331

332332

333+
_COUNTER_KEY_TO_ATTR_NAME = {
334+
"nodes-created": "nodes_created",
335+
"nodes-deleted": "nodes_deleted",
336+
"relationships-created": "relationships_created",
337+
"relationships-deleted": "relationships_deleted",
338+
"properties-set": "properties_set",
339+
"labels-added": "labels_added",
340+
"labels-removed": "labels_removed",
341+
"indexes-added": "indexes_added",
342+
"indexes-removed": "indexes_removed",
343+
"constraints-added": "constraints_added",
344+
"constraints-removed": "constraints_removed",
345+
"system-updates": "system_updates",
346+
"contains-updates": "_contains_updates",
347+
"contains-system-updates": "_contains_system_updates",
348+
}
349+
350+
_COUNTER_ATTR_NAME_TO_KEY = {
351+
v: k for k, v in _COUNTER_KEY_TO_ATTR_NAME.items()
352+
}
353+
354+
333355
class SummaryCounters:
334356
"""Contains counters for various operations that a query triggered."""
335357

@@ -373,29 +395,33 @@ class SummaryCounters:
373395
_contains_system_updates = None
374396

375397
def __init__(self, statistics) -> None:
376-
key_to_attr_name = {
377-
"nodes-created": "nodes_created",
378-
"nodes-deleted": "nodes_deleted",
379-
"relationships-created": "relationships_created",
380-
"relationships-deleted": "relationships_deleted",
381-
"properties-set": "properties_set",
382-
"labels-added": "labels_added",
383-
"labels-removed": "labels_removed",
384-
"indexes-added": "indexes_added",
385-
"indexes-removed": "indexes_removed",
386-
"constraints-added": "constraints_added",
387-
"constraints-removed": "constraints_removed",
388-
"system-updates": "system_updates",
389-
"contains-updates": "_contains_updates",
390-
"contains-system-updates": "_contains_system_updates",
391-
}
392398
for key, value in dict(statistics).items():
393-
attr_name = key_to_attr_name.get(key)
399+
attr_name = _COUNTER_KEY_TO_ATTR_NAME.get(key)
394400
if attr_name:
395401
setattr(self, attr_name, value)
396402

397403
def __repr__(self) -> str:
398-
return repr(vars(self))
404+
statistics = {
405+
_COUNTER_ATTR_NAME_TO_KEY[k]: v
406+
for k, v in vars(self).items()
407+
if k in _COUNTER_ATTR_NAME_TO_KEY
408+
}
409+
return f"{self.__class__.__name__}({statistics!r})"
410+
411+
def __str__(self) -> str:
412+
attrs = []
413+
for k, v in vars(self).items():
414+
if k.startswith("_"): # hide private attributes
415+
continue
416+
if hasattr(self.__class__, k) and getattr(self.__class__, k) == v:
417+
# hide default values
418+
continue
419+
attrs.append(f"{k}: {v}")
420+
attrs.append(f"contains_updates: {self.contains_updates}")
421+
attrs.append(
422+
f"contains_system_updates: {self.contains_system_updates}"
423+
)
424+
return f"SummaryCounters{{{', '.join(attrs)}}}"
399425

400426
@property
401427
def contains_updates(self) -> bool:

0 commit comments

Comments
 (0)