README.rst 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. =========
  2. deepmerge
  3. =========
  4. .. image:: https://github.com/toumorokoshi/deepmerge/actions/workflows/python-package.yaml/badge.svg
  5. :target: https://github.com/toumorokoshi/deepmerge/actions/workflows/python-package.yaml
  6. A tools to handle merging of
  7. nested data structures in python.
  8. ------------
  9. Installation
  10. ------------
  11. deepmerge is available on `pypi <https://pypi.python.org/>`_:
  12. .. code-block:: bash
  13. pip install deepmerge
  14. -------
  15. Example
  16. -------
  17. **Generic Strategy**
  18. .. code-block:: python
  19. from deepmerge import always_merger
  20. base = {"foo": ["bar"]}
  21. next = {"foo": ["baz"]}
  22. expected_result = {'foo': ['bar', 'baz']}
  23. result = always_merger.merge(base, next)
  24. assert expected_result == result
  25. **Custom Strategy**
  26. .. code-block:: python
  27. from deepmerge import Merger
  28. my_merger = Merger(
  29. # pass in a list of tuple, with the
  30. # strategies you are looking to apply
  31. # to each type.
  32. [
  33. (list, ["append"]),
  34. (dict, ["merge"]),
  35. (set, ["union"])
  36. ],
  37. # next, choose the fallback strategies,
  38. # applied to all other types:
  39. ["override"],
  40. # finally, choose the strategies in
  41. # the case where the types conflict:
  42. ["override"]
  43. )
  44. base = {"foo": ["bar"]}
  45. next = {"bar": "baz"}
  46. my_merger.merge(base, next)
  47. assert base == {"foo": ["bar"], "bar": "baz"}
  48. You can also pass in your own merge functions, instead of a string.
  49. For more information, see the `docs <https://deepmerge.readthedocs.io/en/latest/>`_