Skip to content

Commit

Permalink
adding summarization function
Browse files Browse the repository at this point in the history
  • Loading branch information
seperman committed Mar 5, 2025
1 parent eed7669 commit e4800c7
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 23 deletions.
6 changes: 6 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ def nested_a_result():
return json.load(the_file)


@pytest.fixture(scope='function')
def compounds():
with open(os.path.join(FIXTURES_DIR, 'compounds.json')) as the_file:
return json.load(the_file)


@pytest.fixture(scope='class')
def nested_a_affected_paths():
return {
Expand Down
9 changes: 6 additions & 3 deletions deepdiff/deephash.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
convert_item_or_items_into_set_else_none, get_doc,
convert_item_or_items_into_compiled_regexes_else_none,
get_id, type_is_subclass_of_type_group, type_in_type_group,
number_to_string, datetime_normalize, KEY_TO_VAL_STR, short_repr,
number_to_string, datetime_normalize, KEY_TO_VAL_STR,
get_truncate_datetime, dict_, add_root_to_paths, PydanticBaseModel)

from deepdiff.summarize import summarize
from deepdiff.base import Base

try:
Expand Down Expand Up @@ -315,9 +317,10 @@ def __repr__(self):
"""
Hide the counts since it will be confusing to see them when they are hidden everywhere else.
"""
return short_repr(self._get_objects_to_hashes_dict(extract_index=0), max_length=500)
return summarize(self._get_objects_to_hashes_dict(extract_index=0), max_length=500)

__str__ = __repr__
def __str__(self):
return str(self._get_objects_to_hashes_dict(extract_index=0))

def __bool__(self):
return bool(self.hashes)
Expand Down
6 changes: 3 additions & 3 deletions deepdiff/delta.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from deepdiff import DeepDiff
from deepdiff.serialization import pickle_load, pickle_dump
from deepdiff.helper import (
strings, short_repr, numbers,
strings, numbers,
np_ndarray, np_array_factory, numpy_dtypes, get_doc,
not_found, numpy_dtype_string_to_type, dict_,
Opcode, FlatDeltaRow, UnkownValueCode, FlatDataAction,
Expand All @@ -20,7 +20,7 @@
GET, GETATTR, parse_path, stringify_path,
)
from deepdiff.anyset import AnySet

from deepdiff.summarize import summarize

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -165,7 +165,7 @@ def _deserializer(obj, safe_to_import=None):
self.reset()

def __repr__(self):
return "<Delta: {}>".format(short_repr(self.diff, max_length=100))
return "<Delta: {}>".format(summarize(self.diff, max_length=100))

def reset(self):
self.post_process_paths_to_convert = dict_()
Expand Down
18 changes: 11 additions & 7 deletions deepdiff/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from collections.abc import Mapping
from copy import copy
from deepdiff.helper import (
RemapDict, strings, short_repr, notpresent, get_type, numpy_numbers, np, literal_eval_extended,
RemapDict, strings, notpresent, get_type, numpy_numbers, np, literal_eval_extended,
dict_, SetOrdered)
from deepdiff.path import stringify_element

Expand Down Expand Up @@ -580,12 +580,14 @@ def __init__(self,

def __repr__(self):
if self.verbose_level:
from deepdiff.summarize import summarize

if self.additional:
additional_repr = short_repr(self.additional, max_length=35)
additional_repr = summarize(self.additional, max_length=35)
result = "<{} {}>".format(self.path(), additional_repr)
else:
t1_repr = short_repr(self.t1)
t2_repr = short_repr(self.t2)
t1_repr = summarize(self.t1, max_length=35)
t2_repr = summarize(self.t2, max_length=35)
result = "<{} t1:{}, t2:{}>".format(self.path(), t1_repr, t2_repr)
else:
result = "<{}>".format(self.path())
Expand Down Expand Up @@ -857,10 +859,12 @@ def __init__(self, parent, child, param=None):
self.param = param

def __repr__(self):
from deepdiff.summarize import summarize

name = "<{} parent:{}, child:{}, param:{}>"
parent = short_repr(self.parent)
child = short_repr(self.child)
param = short_repr(self.param)
parent = summarize(self.parent, max_length=35)
child = summarize(self.child, max_length=35)
param = summarize(self.param, max_length=15)
return name.format(self.__class__.__name__, parent, child, param)

def get_param_repr(self, force=None):
Expand Down
2 changes: 2 additions & 0 deletions deepdiff/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
SetOrdered,
pydantic_base_model_type,
PydanticBaseModel,
NotPresent,
)
from deepdiff.model import DeltaResult

Expand Down Expand Up @@ -601,6 +602,7 @@ def _serialize_tuple(value):
np_ndarray: lambda x: x.tolist(),
tuple: _serialize_tuple,
Mapping: dict,
NotPresent: str,
}

if PydanticBaseModel is not pydantic_base_model_type:
Expand Down
1 change: 1 addition & 0 deletions tests/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def test_cache_deeply_nested_a2(self, nested_a_t1, nested_a_t2, nested_a_result)
# 'MAX DIFF LIMIT REACHED': False
# }
# assert expected_stats == stats
import pytest; pytest.set_trace()
assert nested_a_result == diff
diff_of_diff = DeepDiff(nested_a_result, diff.to_dict(), ignore_order=False)
assert not diff_of_diff
Expand Down
5 changes: 3 additions & 2 deletions tests/test_delta.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,9 @@ def test_delta_repr(self):
diff = DeepDiff(t1, t2)
delta = Delta(diff)
options = {
"<Delta: {'iterable_item_added': {'root[2]': 3, 'root[3]': 5}}>",
"<Delta: {'iterable_item_added': {'root[3]': 5, 'root[2]': 3}}>"}
'<Delta: {"iterable_item_added":{"root[2]":3,"root[3]":5}}>',
'<Delta: {"iterable_item_added":{"root[3]":5,"root[2]":3}}>',
}
assert repr(delta) in options

def test_get_elem_and_compare_to_old_value(self):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def test_get_hash_by_obj_is_the_same_as_by_obj_get_id(self):
def test_deephash_repr(self):
obj = "a"
result = DeepHash(obj)
assert "{'a': '980410da9522db17c3ab8743541f192a5ab27772a6154dbc7795ee909e653a5c'}" == repr(result)
assert '{"a":"980410da9522db17c3ab8743541f192a5ab27772a6154dbc7795ee909e653a5c"}' == repr(result)

def test_deephash_values(self):
obj = "a"
Expand Down
14 changes: 7 additions & 7 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def test_a(self):
class TestDiffLevel:
def setup_class(cls):
# Test data
cls.custom1 = CustomClass(a='very long text here', b=37)
cls.custom1 = CustomClass(a='very long text here, much longer than you can ever imagine. The longest text here.', b=37)
cls.custom2 = CustomClass(a=313, b=37)
cls.t1 = {42: 'answer', 'vegan': 'for life', 1337: cls.custom1}
cls.t2 = {
Expand Down Expand Up @@ -257,7 +257,7 @@ def test_repr_long(self):
item_repr = repr(self.lowest)
finally:
self.lowest.verbose_level = level
assert item_repr == "<root[1337].a t1:'very long t...', t2:313>"
assert item_repr == '<root[1337].a t1:"very long text here, much lon....", t2:313>'

def test_repr_very_long(self):
level = self.lowest.verbose_level
Expand All @@ -266,7 +266,7 @@ def test_repr_very_long(self):
item_repr = repr(self.lowest)
finally:
self.lowest.verbose_level = level
assert item_repr == "<root[1337].a t1:'very long t...', t2:313>"
assert item_repr == '<root[1337].a t1:"very long text here, much lon....", t2:313>'

def test_repetition_attribute_and_repr(self):
t1 = [1, 1]
Expand All @@ -275,7 +275,7 @@ def test_repetition_attribute_and_repr(self):
node = DiffLevel(t1, t2)
node.additional['repetition'] = some_repetition
assert node.repetition == some_repetition
assert repr(node) == "<root {'repetition': 'some repetition'}>"
assert repr(node) == '<root {"repetition":"some repetition"}>'


class TestChildRelationship:
Expand All @@ -286,14 +286,14 @@ def test_create_invalid_klass(self):
def test_rel_repr_short(self):
rel = WorkingChildRelationship(parent="that parent", child="this child", param="some param")
rel_repr = repr(rel)
expected = "<WorkingChildRelationship parent:'that parent', child:'this child', param:'some param'>"
expected = '<WorkingChildRelationship parent:"that parent", child:"this child", param:"some param">'
assert rel_repr == expected

def test_rel_repr_long(self):
rel = WorkingChildRelationship(
parent="that parent who has a long path",
parent="that parent who has a long path, still going on. Yes, a very long path indeed.",
child="this child",
param="some param")
rel_repr = repr(rel)
expected = "<WorkingChildRelationship parent:'that parent...', child:'this child', param:'some param'>"
expected = '<WorkingChildRelationship parent:"that parent who has a long pa....", child:"this child", param:"some param">'
assert rel_repr == expected

0 comments on commit e4800c7

Please sign in to comment.