nd.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #
  2. # Copyright (c) 2021 Project CHIP Authors
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. """Nested dictionary utilities."""
  17. from typing import Any, Mapping, MutableMapping, Optional, Sequence
  18. Key = Sequence
  19. def get(nd: Optional[Mapping], keys: Key, default: Any = None) -> Any:
  20. """Get a value from a nested dictionary."""
  21. d: Any = nd
  22. while d and keys:
  23. d = d.get(keys[0])
  24. keys = keys[1:]
  25. if d is not None and d != {}:
  26. return d
  27. if default is not None:
  28. return default
  29. return d
  30. def put(nd: MutableMapping, keys: Key, value: Any) -> None:
  31. """Store a value in a nested dictionary."""
  32. while True:
  33. key = keys[0]
  34. keys = keys[1:]
  35. if not keys:
  36. break
  37. if key not in nd:
  38. nd[key] = {}
  39. nd = nd[key]
  40. nd[key] = value
  41. def store(nd: MutableMapping, keys: Key, value: Any, empty: Any, add) -> None:
  42. """Store a value in a nested dictionary where the leaves are containers."""
  43. while True:
  44. key = keys[0]
  45. keys = keys[1:]
  46. if not keys:
  47. break
  48. if key not in nd:
  49. nd[key] = {}
  50. nd = nd[key]
  51. if key not in nd:
  52. nd[key] = empty
  53. add(nd[key], value)
  54. def update(nd: MutableMapping, src: Mapping) -> None:
  55. """Update a nested dictionary."""
  56. for k, v in src.items():
  57. if k not in nd or nd[k] is None:
  58. nd[k] = v
  59. elif isinstance(nd[k], dict) and isinstance(v, dict):
  60. update(nd[k], v)
  61. elif isinstance(nd[k], list):
  62. if isinstance(v, list):
  63. nd[k] += v
  64. else:
  65. nd[k].append(v)
  66. elif type(nd[k]) == type(v):
  67. nd[k] = v
  68. else:
  69. raise TypeError(f"type mismatch {k},{v} was {nd[k]}")