GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — deepdiff/deepdiff/diff.py at master · van-ess0/deepdiff Expand file tree/ Copy pathdiff.pyMore file actions executable file ·729 lines (624 loc) · 30.3 KB / Copy pathdiff.pyFile metadata and controlsexecutable file ·729 lines (624 loc) · 30.3 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 #!/usr/bin/env python # In order to run the docstrings: # python3 -m deepdiff.diff # You might need to run it many times since dictionaries come in different orders # every time you run the docstrings. # However the docstring expects it in a specific order in order to pass! import difflib import logging import json import warnings from itertools import zip_longest from collections.abc import Mapping, Iterable from ordered_set import OrderedSet from deepdiff.helper import (strings, bytes_type, numbers, ListItemRemovedOrAdded, notpresent, IndexedHash, Verbose, unprocessed, json_convertor_default, add_to_frozen_set, convert_item_or_items_into_set_else_none, get_type, convert_item_or_items_into_compiled_regexes_else_none, type_is_subclass_of_type_group, type_in_type_group, get_doc, number_to_string, KEY_TO_VAL_STR) from deepdiff.model import RemapDict, ResultDict, TextResult, TreeResult, DiffLevel from deepdiff.model import DictRelationship, AttributeRelationship from deepdiff.model import SubscriptableIterableRelationship, NonSubscriptableIterableRelationship, SetRelationship from deepdiff.deephash import DeepHash, BoolObj from deepdiff.base import Base logger = logging.getLogger(__name__) warnings.simplefilter('once', DeprecationWarning) try: import jsonpickle except ImportError: jsonpickle = None logger.info('jsonpickle is not installed. The to_json_pickle and from_json_pickle functions will not work.' 'If you dont need those functions, there is nothing to do.') TREE_VIEW = 'tree' TEXT_VIEW = 'text' doc = get_doc('diff_doc.rst') class DeepDiff(ResultDict, Base): __doc__ = doc def __init__(self, t1, t2, ignore_order=False, report_repetition=False, significant_digits=None, number_format_notation="f", exclude_paths=None, exclude_regex_paths=None, exclude_types=None, ignore_type_in_groups=None, ignore_string_type_changes=False, ignore_numeric_type_changes=False, ignore_type_subclasses=False, ignore_string_case=False, exclude_obj_callback=None, number_to_string_func=None, ignore_nan_inequality=False, verbose_level=1, view=TEXT_VIEW, hasher=None, **kwargs): if kwargs: raise ValueError(( "The following parameter(s) are not valid: %s\n" "The valid parameters are ignore_order, report_repetition, significant_digits, " "number_format_notation, exclude_paths, exclude_types, exclude_regex_paths, ignore_type_in_groups, " "ignore_string_type_changes, ignore_numeric_type_changes, ignore_type_subclasses, " "ignore_nan_inequality, number_to_string_func, verbose_level, view, and hasher.") % ', '.join(kwargs.keys())) self.ignore_order = ignore_order self.ignore_type_in_groups = self.get_ignore_types_in_groups( ignore_type_in_groups=ignore_type_in_groups, ignore_string_type_changes=ignore_string_type_changes, ignore_numeric_type_changes=ignore_numeric_type_changes, ignore_type_subclasses=ignore_type_subclasses) self.report_repetition = report_repetition self.exclude_paths = convert_item_or_items_into_set_else_none(exclude_paths) self.exclude_regex_paths = convert_item_or_items_into_compiled_regexes_else_none(exclude_regex_paths) self.exclude_types = set(exclude_types) if exclude_types else None self.exclude_types_tuple = tuple(exclude_types) if exclude_types else None # we need tuple for checking isinstance self.ignore_string_type_changes = ignore_string_type_changes self.ignore_numeric_type_changes = ignore_numeric_type_changes self.ignore_type_subclasses = ignore_type_subclasses self.type_check_func = type_is_subclass_of_type_group if ignore_type_subclasses else type_in_type_group self.ignore_string_case = ignore_string_case self.exclude_obj_callback = exclude_obj_callback self.number_to_string = number_to_string_func or number_to_string self.ignore_nan_inequality = ignore_nan_inequality self.hashes = {} self.hasher = hasher self.significant_digits = self.get_significant_digits(significant_digits, ignore_numeric_type_changes) self.number_format_notation = number_format_notation self.tree = TreeResult() Verbose.level = verbose_level root = DiffLevel(t1, t2) self.__diff(root, parents_ids=frozenset({id(t1)})) self.tree.cleanup() self.view = view view_results = self._get_view_results(view) self.update(view_results) def _get_view_results(self, view): """ Get the results based on the view """ if view == TREE_VIEW: result = self.tree else: result = TextResult(tree_results=self.tree) result.cleanup() # clean up text-style result dictionary return result # TODO: adding adding functionality # def __add__(self, other): # if isinstance(other, DeepDiff): # result = deepcopy(self) # result.update(other) # else: # result = deepcopy(other) # for key in REPORT_KEYS: # if key in self: # getattr(self, "_do_{}".format(key))(result) # return result # __radd__ = __add__ # def _do_iterable_item_added(self, result): # for item in self['iterable_item_added']: # pass def __report_result(self, report_type, level): """ Add a detected change to the reference-style result dictionary. report_type will be added to level. (We'll create the text-style report from there later.) :param report_type: A well defined string key describing the type of change. Examples: "set_item_added", "values_changed" :param parent: A DiffLevel object describing the objects in question in their before-change and after-change object structure. :rtype: None """ if not self.__skip_this(level): level.report_type = report_type self.tree[report_type].add(level) @staticmethod def __dict_from_slots(object): def unmangle(attribute): if attribute.startswith('__') and attribute != '__weakref__': return '_{type}{attribute}'.format( type=type(object).__name__, attribute=attribute ) return attribute all_slots = [] if isinstance(object, type): mro = object.__mro__ # pragma: no cover. I have not been able to write a test for this case. But we still check for it. else: mro = object.__class__.__mro__ for type_in_mro in mro: slots = getattr(type_in_mro, '__slots__', None) if slots: if isinstance(slots, strings): all_slots.append(slots) else: all_slots.extend(slots) return {i: getattr(object, unmangle(i)) for i in all_slots} def __diff_obj(self, level, parents_ids=frozenset({}), is_namedtuple=False): """Difference of 2 objects""" try: if is_namedtuple: t1 = level.t1._asdict() t2 = level.t2._asdict() else: t1 = level.t1.__dict__ t2 = level.t2.__dict__ except AttributeError: try: t1 = self.__dict_from_slots(level.t1) t2 = self.__dict_from_slots(level.t2) except AttributeError: self.__report_result('unprocessed', level) return self.__diff_dict( level, parents_ids, print_as_attribute=True, override=True, override_t1=t1, override_t2=t2) def __skip_this(self, level): """ Check whether this comparison should be skipped because one of the objects to compare meets exclusion criteria. :rtype: bool """ skip = False if self.exclude_paths and level.path() in self.exclude_paths: skip = True elif self.exclude_regex_paths and any( [exclude_regex_path.search(level.path()) for exclude_regex_path in self.exclude_regex_paths]): skip = True elif self.exclude_types_tuple and \ (isinstance(level.t1, self.exclude_types_tuple) or isinstance(level.t2, self.exclude_types_tuple)): skip = True elif self.exclude_obj_callback and \ (self.exclude_obj_callback(level.t1, level.path()) or self.exclude_obj_callback(level.t2, level.path())): skip = True return skip def __get_clean_to_keys_mapping(self, keys, level): result = {} for key in keys: if self.ignore_string_type_changes and isinstance(key, bytes): clean_key = key.decode('utf-8') elif isinstance(key, numbers): type_ = "number" if self.ignore_numeric_type_changes else key.__class__.__name__ clean_key = self.number_to_string(key, significant_digits=self.significant_digits, number_format_notation=self.number_format_notation) clean_key = KEY_TO_VAL_STR.format(type_, clean_key) else: clean_key = key if clean_key in result: logger.warning(('{} and {} in {} become the same key when ignore_numeric_type_changes' 'or ignore_numeric_type_changes are set to be true.').format( key, result[clean_key], level.path())) else: result[clean_key] = key return result def __diff_dict(self, level, parents_ids=frozenset({}), print_as_attribute=False, override=False, override_t1=None, override_t2=None): """Difference of 2 dictionaries""" if override: # for special stuff like custom objects and named tuples we receive preprocessed t1 and t2 # but must not spoil the chain (=level) with it t1 = override_t1 t2 = override_t2 else: t1 = level.t1 t2 = level.t2 if print_as_attribute: item_added_key = "attribute_added" item_removed_key = "attribute_removed" rel_class = AttributeRelationship else: item_added_key = "dictionary_item_added" item_removed_key = "dictionary_item_removed" rel_class = DictRelationship t1_keys = set(t1.keys()) t2_keys = set(t2.keys()) if self.ignore_string_type_changes or self.ignore_numeric_type_changes: t1_clean_to_keys = self.__get_clean_to_keys_mapping(keys=t1_keys, level=level) t2_clean_to_keys = self.__get_clean_to_keys_mapping(keys=t2_keys, level=level) t1_keys = set(t1_clean_to_keys.keys()) t2_keys = set(t2_clean_to_keys.keys()) else: t1_clean_to_keys = t2_clean_to_keys = None t_keys_intersect = t2_keys.intersection(t1_keys) t_keys_added = t2_keys - t_keys_intersect t_keys_removed = t1_keys - t_keys_intersect for key in t_keys_added: key = t2_clean_to_keys[key] if t2_clean_to_keys else key change_level = level.branch_deeper( notpresent, t2[key], child_relationship_class=rel_class, child_relationship_param=key) self.__report_result(item_added_key, change_level) for key in t_keys_removed: key = t1_clean_to_keys[key] if t1_clean_to_keys else key change_level = level.branch_deeper( t1[key], notpresent, child_relationship_class=rel_class, child_relationship_param=key) self.__report_result(item_removed_key, change_level) for key in t_keys_intersect: # key present in both dicts - need to compare values key1 = t1_clean_to_keys[key] if t1_clean_to_keys else key key2 = t2_clean_to_keys[key] if t2_clean_to_keys else key item_id = id(t1[key1]) if parents_ids and item_id in parents_ids: continue parents_ids_added = add_to_frozen_set(parents_ids, item_id) # Go one level deeper next_level = level.branch_deeper( t1[key1], t2[key2], child_relationship_class=rel_class, child_relationship_param=key) self.__diff(next_level, parents_ids_added) def __diff_set(self, level): """Difference of sets""" t1_hashtable = self.__create_hashtable(level.t1, level) t2_hashtable = self.__create_hashtable(level.t2, level) t1_hashes = set(t1_hashtable.keys()) t2_hashes = set(t2_hashtable.keys()) hashes_added = t2_hashes - t1_hashes hashes_removed = t1_hashes - t2_hashes items_added = [t2_hashtable[i].item for i in hashes_added] items_removed = [t1_hashtable[i].item for i in hashes_removed] for item in items_added: change_level = level.branch_deeper( notpresent, item, child_relationship_class=SetRelationship) self.__report_result('set_item_added', change_level) for item in items_removed: change_level = level.branch_deeper( item, notpresent, child_relationship_class=SetRelationship) self.__report_result('set_item_removed', change_level) @staticmethod def __iterables_subscriptable(t1, t2): try: if getattr(t1, '__getitem__') and getattr(t2, '__getitem__'): return True else: # pragma: no cover return False # should never happen except AttributeError: return False def __diff_iterable(self, level, parents_ids=frozenset({})): """Difference of iterables""" # We're handling both subscriptable and non-subscriptable iterables. Which one is it? subscriptable = self.__iterables_subscriptable(level.t1, level.t2) if subscriptable: child_relationship_class = SubscriptableIterableRelationship else: child_relationship_class = NonSubscriptableIterableRelationship for i, (x, y) in enumerate( zip_longest( level.t1, level.t2, fillvalue=ListItemRemovedOrAdded)): if y is ListItemRemovedOrAdded: # item removed completely change_level = level.branch_deeper( x, notpresent, child_relationship_class=child_relationship_class, child_relationship_param=i) self.__report_result('iterable_item_removed', change_level) elif x is ListItemRemovedOrAdded: # new item added change_level = level.branch_deeper( notpresent, y, child_relationship_class=child_relationship_class, child_relationship_param=i) self.__report_result('iterable_item_added', change_level) else: # check if item value has changed item_id = id(x) if parents_ids and item_id in parents_ids: continue parents_ids_added = add_to_frozen_set(parents_ids, item_id) # Go one level deeper next_level = level.branch_deeper( x, y, child_relationship_class=child_relationship_class, child_relationship_param=i) self.__diff(next_level, parents_ids_added) def __diff_str(self, level): """Compare strings""" if self.ignore_string_case: level.t1 = level.t1.lower() level.t2 = level.t2.lower() if type(level.t1) == type(level.t2) and level.t1 == level.t2: return # do we add a diff for convenience? do_diff = True t1_str = level.t1 t2_str = level.t2 if isinstance(level.t1, bytes_type): try: t1_str = level.t1.decode('ascii') except UnicodeDecodeError: do_diff = False if isinstance(level.t2, bytes_type): try: t2_str = level.t2.decode('ascii') except UnicodeDecodeError: do_diff = False if t1_str == t2_str: return if do_diff: if '\n' in t1_str or '\n' in t2_str: diff = difflib.unified_diff( t1_str.splitlines(), t2_str.splitlines(), lineterm='') diff = list(diff) if diff: level.additional['diff'] = '\n'.join(diff) self.__report_result('values_changed', level) def __diff_tuple(self, level, parents_ids): # Checking to see if it has _fields. Which probably means it is a named # tuple. try: level.t1._asdict # It must be a normal tuple except AttributeError: self.__diff_iterable(level, parents_ids) # We assume it is a namedtuple then else: self.__diff_obj(level, parents_ids, is_namedtuple=True) def _add_hash(self, hashes, item_hash, item, i): if item_hash in hashes: hashes[item_hash].indexes.append(i) else: hashes[item_hash] = IndexedHash(indexes=[i], item=item) def __create_hashtable(self, t, level): """Create hashtable of {item_hash: (indexes, item)}""" hashes = {} for (i, item) in enumerate(t): try: parent = "{}[{}]".format(level.path(), i) hashes_all = DeepHash(item, hashes=self.hashes, exclude_types=self.exclude_types, exclude_paths=self.exclude_paths, exclude_regex_paths=self.exclude_regex_paths, hasher=self.hasher, ignore_repetition=not self.report_repetition, significant_digits=self.significant_digits, number_format_notation=self.number_format_notation, ignore_string_type_changes=self.ignore_string_type_changes, ignore_numeric_type_changes=self.ignore_numeric_type_changes, ignore_type_in_groups=self.ignore_type_in_groups, ignore_type_subclasses=self.ignore_type_subclasses, ignore_string_case=self.ignore_string_case, number_to_string_func=self.number_to_string, exclude_obj_callback=self.exclude_obj_callback, parent=parent, apply_hash=False, ) key = item if item is True: key = BoolObj.TRUE elif item is False: key = BoolObj.FALSE item_hash = hashes_all[key] except Exception as e: # pragma: no cover logger.error("Can not produce a hash for %s." "Not counting this object.\n %s" % (level.path(), e)) else: if item_hash is unprocessed: # pragma: no cover logger.warning("Item %s was not processed while hashing " "thus not counting this object." % level.path()) else: self._add_hash(hashes=hashes, item_hash=item_hash, item=item, i=i) return hashes def __diff_iterable_with_deephash(self, level): """Diff of unhashable iterables. Only used when ignoring the order.""" t1_hashtable = self.__create_hashtable(level.t1, level) t2_hashtable = self.__create_hashtable(level.t2, level) t1_hashes = set(t1_hashtable.keys()) t2_hashes = set(t2_hashtable.keys()) hashes_added = t2_hashes - t1_hashes hashes_removed = t1_hashes - t2_hashes if self.report_repetition: for hash_value in hashes_added: for i in t2_hashtable[hash_value].indexes: change_level = level.branch_deeper( notpresent, t2_hashtable[hash_value].item, child_relationship_class=SubscriptableIterableRelationship, # TODO: that might be a lie! child_relationship_param=i ) # TODO: what is this value exactly? self.__report_result('iterable_item_added', change_level) for hash_value in hashes_removed: for i in t1_hashtable[hash_value].indexes: change_level = level.branch_deeper( t1_hashtable[hash_value].item, notpresent, child_relationship_class=SubscriptableIterableRelationship, # TODO: that might be a lie! child_relationship_param=i) self.__report_result('iterable_item_removed', change_level) items_intersect = t2_hashes.intersection(t1_hashes) for hash_value in items_intersect: t1_indexes = t1_hashtable[hash_value].indexes t2_indexes = t2_hashtable[hash_value].indexes t1_indexes_len = len(t1_indexes) t2_indexes_len = len(t2_indexes) if t1_indexes_len != t2_indexes_len: # this is a repetition change! # create "change" entry, keep current level untouched to handle further changes repetition_change_level = level.branch_deeper( t1_hashtable[hash_value].item, t2_hashtable[hash_value].item, # nb: those are equal! child_relationship_class=SubscriptableIterableRelationship, # TODO: that might be a lie! child_relationship_param=t1_hashtable[hash_value] .indexes[0]) repetition_change_level.additional['repetition'] = RemapDict( old_repeat=t1_indexes_len, new_repeat=t2_indexes_len, old_indexes=t1_indexes, new_indexes=t2_indexes) self.__report_result('repetition_change', repetition_change_level) else: for hash_value in hashes_added: change_level = level.branch_deeper( notpresent, t2_hashtable[hash_value].item, child_relationship_class=SubscriptableIterableRelationship, # TODO: that might be a lie! child_relationship_param=t2_hashtable[hash_value].indexes[ 0]) # TODO: what is this value exactly? self.__report_result('iterable_item_added', change_level) for hash_value in hashes_removed: change_level = level.branch_deeper( t1_hashtable[hash_value].item, notpresent, child_relationship_class=SubscriptableIterableRelationship, # TODO: that might be a lie! child_relationship_param=t1_hashtable[hash_value].indexes[ 0]) self.__report_result('iterable_item_removed', change_level) def __diff_numbers(self, level): """Diff Numbers""" t1_type = "number" if self.ignore_numeric_type_changes else level.t1.__class__.__name__ t2_type = "number" if self.ignore_numeric_type_changes else level.t2.__class__.__name__ if self.significant_digits is None: if level.t1 != level.t2: self.__report_result('values_changed', level) else: # Bernhard10: I use string formatting for comparison, to be consistent with usecases where # data is read from files that were previousely written from python and # to be consistent with on-screen representation of numbers. # Other options would be abs(t1-t2)<10**-self.significant_digits # or math.is_close (python3.5+) # Note that abs(3.25-3.251) = 0.0009999999999998899 < 0.001 # Note also that "{:.3f}".format(1.1135) = 1.113, but "{:.3f}".format(1.11351) = 1.114 # For Decimals, format seems to round 2.5 to 2 and 3.5 to 4 (to closest even number) t1_s = self.number_to_string(level.t1, significant_digits=self.significant_digits, number_format_notation=self.number_format_notation) t2_s = self.number_to_string(level.t2, significant_digits=self.significant_digits, number_format_notation=self.number_format_notation) t1_s = KEY_TO_VAL_STR.format(t1_type, t1_s) t2_s = KEY_TO_VAL_STR.format(t2_type, t2_s) if t1_s != t2_s: self.__report_result('values_changed', level) def __diff_types(self, level): """Diff types""" level.report_type = 'type_changes' self.__report_result('type_changes', level) def __diff(self, level, parents_ids=frozenset({})): """The main diff method""" if level.t1 is level.t2: return if self.__skip_this(level): return if get_type(level.t1) != get_type(level.t2): report_type_change = True for type_group in self.ignore_type_in_groups: if self.type_check_func(level.t1, type_group) and self.type_check_func(level.t2, type_group): report_type_change = False break if report_type_change: self.__diff_types(level) return if self.ignore_nan_inequality and isinstance(level.t1, float) and str(level.t1) == str(level.t2) == 'nan': return if isinstance(level.t1, strings): self.__diff_str(level) elif isinstance(level.t1, numbers): self.__diff_numbers(level) elif isinstance(level.t1, Mapping): self.__diff_dict(level, parents_ids) elif isinstance(level.t1, tuple): self.__diff_tuple(level, parents_ids) elif isinstance(level.t1, (set, frozenset, OrderedSet)): self.__diff_set(level) elif isinstance(level.t1, Iterable): if self.ignore_order: self.__diff_iterable_with_deephash(level) else: self.__diff_iterable(level, parents_ids) else: self.__diff_obj(level, parents_ids) def to_json_pickle(self): """ Get the json pickle of the diff object. Unless you need all the attributes and functionality of DeepDiff, running to_json() is the safer option that json pickle. """ if jsonpickle: copied = self.copy() return jsonpickle.encode(copied) else: logger.error('jsonpickle library needs to be installed in order to run to_json_pickle') @classmethod def from_json_pickle(cls, value): """ Load DeepDiff object with all the bells and whistles from the json pickle dump. Note that json pickle dump comes from to_json_pickle """ if jsonpickle: return jsonpickle.decode(value) else: logger.error('jsonpickle library needs to be installed in order to run from_json_pickle') def to_json(self, default_mapping=None): """ Dump json of the text view. **Parameters** default_mapping : default_mapping, dictionary(optional), a dictionary of mapping of different types to json types. by default DeepDiff converts certain data types. For example Decimals into floats so they can be exported into json. If you have a certain object type that the json serializer can not serialize it, please pass the appropriate type conversion through this dictionary. **Example** Serialize custom objects >>> class A: ... pass ... >>> class B: ... pass ... >>> t1 = A() >>> t2 = B() >>> ddiff = DeepDiff(t1, t2) >>> ddiff.to_json() TypeError: We do not know how to convert <__main__.A object at 0x10648> of type <class '__main__.A'> for json serialization. Please pass the default_mapping parameter with proper mapping of the object to a basic python type. >>> default_mapping = {A: lambda x: 'obj A', B: lambda x: 'obj B'} >>> ddiff.to_json(default_mapping=default_mapping) '{"type_changes": {"root": {"old_type": "A", "new_type": "B", "old_value": "obj A", "new_value": "obj B"}}}' """ return json.dumps(self.to_dict(), default=json_convertor_default(default_mapping=default_mapping)) def to_dict(self): """ Dump dictionary of the text view. It does not matter which view you are currently in. It will give you the dictionary of the text view. """ if self.view == TREE_VIEW: result = dict(self._get_view_results(view=TEXT_VIEW)) else: result = dict(self) return result if __name__ == "__main__": # pragma: no cover import doctest doctest.testmod() |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |