deepdiff icon indicating copy to clipboard operation
deepdiff copied to clipboard

Show only the diff for larger texts in diff.pretty()

Open lucas-av7 opened this issue 4 years ago • 2 comments

I have some keys in my dicts with values coming from larger text files, if I change only one word and use the diff.pretty() it shows the whole old text and the whole new text. I would like to have a parameter to show only the diff.

If I just print(diff) it shows to me that there is a diff parameter inside the values_changed. So I guess that is possible to add this new parameter to diff.pretty() something like diff.pretty(show_diff=True) and if diff is present it will show only the diff, if not it will use the default.

If you know another way that I can achieve this, please let me know

lucas-av7 avatar Oct 05 '21 14:10 lucas-av7

I was able to get this doing that:

def pretty_print_diff(diff):
    type_t1 = get_type(diff.t1).__name__
    type_t2 = get_type(diff.t2).__name__

    val_t1 = '"{}"'.format(str(diff.t1)) if type_t1 == "str" else str(diff.t1)
    val_t2 = '"{}"'.format(str(diff.t2)) if type_t2 == "str" else str(diff.t2)

    diff_path = diff.path(root='root')

    # I added it
    if(diff.additional):
        diff = diff.additional['diff']
        return f"Value of {diff_path} changed. Check the diff:\n{diff}"
    ####

    return PRETTY_FORM_TEXTS.get(diff.report_type, "").format(
        diff_path=diff_path,
        type_t1=type_t1,
        type_t2=type_t2,
        val_t1=val_t1,
        val_t2=val_t2)

lucas-av7 avatar Oct 05 '21 14:10 lucas-av7

Ok I think we can introduce the verbose_level parameter to the pretty print too.

seperman avatar Oct 12 '21 23:10 seperman