Skip to content

Commit e4800c7

Browse files
committed
adding summarization function
1 parent eed7669 commit e4800c7

File tree

9 files changed

+40
-23
lines changed

9 files changed

+40
-23
lines changed

conftest.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ def nested_a_result():
4646
return json.load(the_file)
4747

4848

49+
@pytest.fixture(scope='function')
50+
def compounds():
51+
with open(os.path.join(FIXTURES_DIR, 'compounds.json')) as the_file:
52+
return json.load(the_file)
53+
54+
4955
@pytest.fixture(scope='class')
5056
def nested_a_affected_paths():
5157
return {

deepdiff/deephash.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
convert_item_or_items_into_set_else_none, get_doc,
1212
convert_item_or_items_into_compiled_regexes_else_none,
1313
get_id, type_is_subclass_of_type_group, type_in_type_group,
14-
number_to_string, datetime_normalize, KEY_TO_VAL_STR, short_repr,
14+
number_to_string, datetime_normalize, KEY_TO_VAL_STR,
1515
get_truncate_datetime, dict_, add_root_to_paths, PydanticBaseModel)
16+
17+
from deepdiff.summarize import summarize
1618
from deepdiff.base import Base
1719

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

320-
__str__ = __repr__
322+
def __str__(self):
323+
return str(self._get_objects_to_hashes_dict(extract_index=0))
321324

322325
def __bool__(self):
323326
return bool(self.hashes)

deepdiff/delta.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from deepdiff import DeepDiff
88
from deepdiff.serialization import pickle_load, pickle_dump
99
from deepdiff.helper import (
10-
strings, short_repr, numbers,
10+
strings, numbers,
1111
np_ndarray, np_array_factory, numpy_dtypes, get_doc,
1212
not_found, numpy_dtype_string_to_type, dict_,
1313
Opcode, FlatDeltaRow, UnkownValueCode, FlatDataAction,
@@ -20,7 +20,7 @@
2020
GET, GETATTR, parse_path, stringify_path,
2121
)
2222
from deepdiff.anyset import AnySet
23-
23+
from deepdiff.summarize import summarize
2424

2525
logger = logging.getLogger(__name__)
2626

@@ -165,7 +165,7 @@ def _deserializer(obj, safe_to_import=None):
165165
self.reset()
166166

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

170170
def reset(self):
171171
self.post_process_paths_to_convert = dict_()

deepdiff/model.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from collections.abc import Mapping
33
from copy import copy
44
from deepdiff.helper import (
5-
RemapDict, strings, short_repr, notpresent, get_type, numpy_numbers, np, literal_eval_extended,
5+
RemapDict, strings, notpresent, get_type, numpy_numbers, np, literal_eval_extended,
66
dict_, SetOrdered)
77
from deepdiff.path import stringify_element
88

@@ -580,12 +580,14 @@ def __init__(self,
580580

581581
def __repr__(self):
582582
if self.verbose_level:
583+
from deepdiff.summarize import summarize
584+
583585
if self.additional:
584-
additional_repr = short_repr(self.additional, max_length=35)
586+
additional_repr = summarize(self.additional, max_length=35)
585587
result = "<{} {}>".format(self.path(), additional_repr)
586588
else:
587-
t1_repr = short_repr(self.t1)
588-
t2_repr = short_repr(self.t2)
589+
t1_repr = summarize(self.t1, max_length=35)
590+
t2_repr = summarize(self.t2, max_length=35)
589591
result = "<{} t1:{}, t2:{}>".format(self.path(), t1_repr, t2_repr)
590592
else:
591593
result = "<{}>".format(self.path())
@@ -857,10 +859,12 @@ def __init__(self, parent, child, param=None):
857859
self.param = param
858860

859861
def __repr__(self):
862+
from deepdiff.summarize import summarize
863+
860864
name = "<{} parent:{}, child:{}, param:{}>"
861-
parent = short_repr(self.parent)
862-
child = short_repr(self.child)
863-
param = short_repr(self.param)
865+
parent = summarize(self.parent, max_length=35)
866+
child = summarize(self.child, max_length=35)
867+
param = summarize(self.param, max_length=15)
864868
return name.format(self.__class__.__name__, parent, child, param)
865869

866870
def get_param_repr(self, force=None):

deepdiff/serialization.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
SetOrdered,
2929
pydantic_base_model_type,
3030
PydanticBaseModel,
31+
NotPresent,
3132
)
3233
from deepdiff.model import DeltaResult
3334

@@ -601,6 +602,7 @@ def _serialize_tuple(value):
601602
np_ndarray: lambda x: x.tolist(),
602603
tuple: _serialize_tuple,
603604
Mapping: dict,
605+
NotPresent: str,
604606
}
605607

606608
if PydanticBaseModel is not pydantic_base_model_type:

tests/test_cache.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def test_cache_deeply_nested_a2(self, nested_a_t1, nested_a_t2, nested_a_result)
5656
# 'MAX DIFF LIMIT REACHED': False
5757
# }
5858
# assert expected_stats == stats
59+
import pytest; pytest.set_trace()
5960
assert nested_a_result == diff
6061
diff_of_diff = DeepDiff(nested_a_result, diff.to_dict(), ignore_order=False)
6162
assert not diff_of_diff

tests/test_delta.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,9 @@ def test_delta_repr(self):
170170
diff = DeepDiff(t1, t2)
171171
delta = Delta(diff)
172172
options = {
173-
"<Delta: {'iterable_item_added': {'root[2]': 3, 'root[3]': 5}}>",
174-
"<Delta: {'iterable_item_added': {'root[3]': 5, 'root[2]': 3}}>"}
173+
'<Delta: {"iterable_item_added":{"root[2]":3,"root[3]":5}}>',
174+
'<Delta: {"iterable_item_added":{"root[3]":5,"root[2]":3}}>',
175+
}
175176
assert repr(delta) in options
176177

177178
def test_get_elem_and_compare_to_old_value(self):

tests/test_hash.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def test_get_hash_by_obj_is_the_same_as_by_obj_get_id(self):
5656
def test_deephash_repr(self):
5757
obj = "a"
5858
result = DeepHash(obj)
59-
assert "{'a': '980410da9522db17c3ab8743541f192a5ab27772a6154dbc7795ee909e653a5c'}" == repr(result)
59+
assert '{"a":"980410da9522db17c3ab8743541f192a5ab27772a6154dbc7795ee909e653a5c"}' == repr(result)
6060

6161
def test_deephash_values(self):
6262
obj = "a"

tests/test_model.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def test_a(self):
8181
class TestDiffLevel:
8282
def setup_class(cls):
8383
# Test data
84-
cls.custom1 = CustomClass(a='very long text here', b=37)
84+
cls.custom1 = CustomClass(a='very long text here, much longer than you can ever imagine. The longest text here.', b=37)
8585
cls.custom2 = CustomClass(a=313, b=37)
8686
cls.t1 = {42: 'answer', 'vegan': 'for life', 1337: cls.custom1}
8787
cls.t2 = {
@@ -257,7 +257,7 @@ def test_repr_long(self):
257257
item_repr = repr(self.lowest)
258258
finally:
259259
self.lowest.verbose_level = level
260-
assert item_repr == "<root[1337].a t1:'very long t...', t2:313>"
260+
assert item_repr == '<root[1337].a t1:"very long text here, much lon....", t2:313>'
261261

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

271271
def test_repetition_attribute_and_repr(self):
272272
t1 = [1, 1]
@@ -275,7 +275,7 @@ def test_repetition_attribute_and_repr(self):
275275
node = DiffLevel(t1, t2)
276276
node.additional['repetition'] = some_repetition
277277
assert node.repetition == some_repetition
278-
assert repr(node) == "<root {'repetition': 'some repetition'}>"
278+
assert repr(node) == '<root {"repetition":"some repetition"}>'
279279

280280

281281
class TestChildRelationship:
@@ -286,14 +286,14 @@ def test_create_invalid_klass(self):
286286
def test_rel_repr_short(self):
287287
rel = WorkingChildRelationship(parent="that parent", child="this child", param="some param")
288288
rel_repr = repr(rel)
289-
expected = "<WorkingChildRelationship parent:'that parent', child:'this child', param:'some param'>"
289+
expected = '<WorkingChildRelationship parent:"that parent", child:"this child", param:"some param">'
290290
assert rel_repr == expected
291291

292292
def test_rel_repr_long(self):
293293
rel = WorkingChildRelationship(
294-
parent="that parent who has a long path",
294+
parent="that parent who has a long path, still going on. Yes, a very long path indeed.",
295295
child="this child",
296296
param="some param")
297297
rel_repr = repr(rel)
298-
expected = "<WorkingChildRelationship parent:'that parent...', child:'this child', param:'some param'>"
298+
expected = '<WorkingChildRelationship parent:"that parent who has a long pa....", child:"this child", param:"some param">'
299299
assert rel_repr == expected

0 commit comments

Comments
 (0)