Skip to content

Commit cdc4b30

Browse files
authored
Merge pull request #492 from AaronDMarasco/dev
Add print() option
2 parents 111a2eb + 5f22bd2 commit cdc4b30

File tree

5 files changed

+74
-2
lines changed

5 files changed

+74
-2
lines changed

AUTHORS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,4 @@ Authors in order of the timeline of their contributions:
6464
- [dtorres-sf](https://github.com/dtorres-sf) for fixing iterable moved items when iterable_compare_func is used.
6565
- [Florian Finkernagel](https://github.com/TyberiusPrime) for pandas and polars support.
6666
- Mathis Chenuet [artemisart](https://github.com/artemisart) for fixing slots classes comparison.
67+
- [Aaron D. Marasco](https://github.com/AaronDMarasco) added `prefix` option to `pretty()`

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
- v8-0-1
55
- Bugfix. Numpy should be optional.
6+
- Added `prefix` option to `pretty()`
67

78
- v8-0-0
89

deepdiff/serialization.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ def _to_delta_dict(self, directed=True, report_repetition_required=True, always_
296296

297297
return deepcopy(dict(result))
298298

299-
def pretty(self):
299+
def pretty(self, prefix=None):
300300
"""
301301
The pretty human readable string output for the diff object
302302
regardless of what view was used to generate the diff.
@@ -310,12 +310,16 @@ def pretty(self):
310310
Item root[1] removed from set.
311311
"""
312312
result = []
313+
if prefix is None:
314+
prefix = ''
313315
keys = sorted(self.tree.keys()) # sorting keys to guarantee constant order across python versions.
314316
for key in keys:
315317
for item_key in self.tree[key]:
316318
result += [pretty_print_diff(item_key)]
317319

318-
return '\n'.join(result)
320+
if callable(prefix):
321+
return "\n".join(f"{prefix(diff=self)}{r}" for r in result)
322+
return "\n".join(f"{prefix}{r}" for r in result)
319323

320324

321325
class _RestrictedUnpickler(pickle.Unpickler):

docs/view.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,29 @@ Use the pretty method for human readable output. This is regardless of what view
299299
Item root[4] removed from set.
300300
Item root[1] removed from set.
301301

302+
The pretty method has an optional parameter ``prefix`` that allows a prefix string before every output line (*e.g.* for logging):
303+
>>> from deepdiff import DeepDiff
304+
>>> t1={1,2,4}
305+
>>> t2={2,3}
306+
>>> print(DeepDiff(t1, t2).pretty(prefix='Diff: '))
307+
Diff: Item root[3] added to set.
308+
Diff: Item root[4] removed from set.
309+
Diff: Item root[1] removed from set.
310+
311+
The ``prefix`` may also be a callable function. This function must accept ``**kwargs``; as of this version, the only parameter is ``diff`` but the signature allows for future expansion.
312+
The ``diff`` given will be the ``DeepDiff`` that ``pretty`` was called on; this allows interesting capabilities such as:
313+
>>> from deepdiff import DeepDiff
314+
>>> t1={1,2,4}
315+
>>> t2={2,3}
316+
>>> def callback(**kwargs):
317+
... """Helper function using a hidden variable on the diff that tracks which count prints next"""
318+
... kwargs['diff']._diff_count = 1 + getattr(kwargs['diff'], '_diff_count', 0)
319+
... return f"Diff #{kwargs['diff']._diff_count}: "
320+
...
321+
>>> print(DeepDiff(t1, t2).pretty(prefix=callback))
322+
Diff #1: Item root[3] added to set.
323+
Diff #2: Item root[4] removed from set.
324+
Diff #3: Item root[1] removed from set.
302325

303326

304327
Text view vs. Tree view vs. vs. pretty() method

tests/test_serialization.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,49 @@ def test_pretty_form_method(self, expected, verbose_level):
330330
result = ddiff.pretty()
331331
assert result == expected
332332

333+
@pytest.mark.parametrize("expected, verbose_level",
334+
(
335+
('\t\tItem root[5] added to dictionary.'
336+
'\n\t\tItem root[3] removed from dictionary.'
337+
'\n\t\tType of root[2] changed from int to str and value changed from 2 to "b".'
338+
'\n\t\tValue of root[4] changed from 4 to 5.', 0),
339+
('\t\tItem root[5] (5) added to dictionary.'
340+
'\n\t\tItem root[3] (3) removed from dictionary.'
341+
'\n\t\tType of root[2] changed from int to str and value changed from 2 to "b".'
342+
'\n\t\tValue of root[4] changed from 4 to 5.', 2),
343+
), ids=("verbose=0", "verbose=2")
344+
)
345+
def test_pretty_form_method_prefixed_simple(self, expected, verbose_level):
346+
t1 = {2: 2, 3: 3, 4: 4}
347+
t2 = {2: 'b', 4: 5, 5: 5}
348+
ddiff = DeepDiff(t1, t2, verbose_level=verbose_level)
349+
result = ddiff.pretty(prefix="\t\t")
350+
assert result == expected
351+
352+
@pytest.mark.parametrize("expected, verbose_level",
353+
(
354+
('Diff #1: Item root[5] added to dictionary.'
355+
'\nDiff #2: Item root[3] removed from dictionary.'
356+
'\nDiff #3: Type of root[2] changed from int to str and value changed from 2 to "b".'
357+
'\nDiff #4: Value of root[4] changed from 4 to 5.', 0),
358+
('Diff #1: Item root[5] (5) added to dictionary.'
359+
'\nDiff #2: Item root[3] (3) removed from dictionary.'
360+
'\nDiff #3: Type of root[2] changed from int to str and value changed from 2 to "b".'
361+
'\nDiff #4: Value of root[4] changed from 4 to 5.', 2),
362+
), ids=("verbose=0", "verbose=2")
363+
)
364+
def test_pretty_form_method_prefixed_callback(self, expected, verbose_level):
365+
def prefix_callback(**kwargs):
366+
"""Helper function using a hidden variable on the diff that tracks which count prints next"""
367+
kwargs['diff']._diff_count = 1 + getattr(kwargs['diff'], '_diff_count', 0)
368+
return f"Diff #{kwargs['diff']._diff_count}: "
369+
370+
t1 = {2: 2, 3: 3, 4: 4}
371+
t2 = {2: 'b', 4: 5, 5: 5}
372+
ddiff = DeepDiff(t1, t2, verbose_level=verbose_level)
373+
result = ddiff.pretty(prefix=prefix_callback)
374+
assert result == expected
375+
333376
@pytest.mark.parametrize('test_num, value, func_to_convert_back', [
334377
(1, {'10': None}, None),
335378
(2, {"type_changes": {"root": {"old_type": None, "new_type": list, "new_value": ["你好", 2, 3, 5]}}}, None),

0 commit comments

Comments
 (0)