_transformations.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import re
  2. import six
  3. try:
  4. from inspect import Parameter, signature
  5. except ImportError:
  6. signature = None
  7. try:
  8. from inspect import getfullargspec as getargspec
  9. except ImportError:
  10. from inspect import getargspec
  11. _EMPTY_SENTINEL = object()
  12. def inc(x):
  13. """ Add one to the current value """
  14. return x + 1
  15. def dec(x):
  16. """ Subtract one from the current value """
  17. return x - 1
  18. def discard(evolver, key):
  19. """ Discard the element and returns a structure without the discarded elements """
  20. try:
  21. del evolver[key]
  22. except KeyError:
  23. pass
  24. # Matchers
  25. def rex(expr):
  26. """ Regular expression matcher to use together with transform functions """
  27. r = re.compile(expr)
  28. return lambda key: isinstance(key, six.string_types) and r.match(key)
  29. def ny(_):
  30. """ Matcher that matches any value """
  31. return True
  32. # Support functions
  33. def _chunks(l, n):
  34. for i in range(0, len(l), n):
  35. yield l[i:i + n]
  36. def transform(structure, transformations):
  37. r = structure
  38. for path, command in _chunks(transformations, 2):
  39. r = _do_to_path(r, path, command)
  40. return r
  41. def _do_to_path(structure, path, command):
  42. if not path:
  43. return command(structure) if callable(command) else command
  44. kvs = _get_keys_and_values(structure, path[0])
  45. return _update_structure(structure, kvs, path[1:], command)
  46. def _items(structure):
  47. try:
  48. return structure.items()
  49. except AttributeError:
  50. # Support wider range of structures by adding a transform_items() or similar?
  51. return list(enumerate(structure))
  52. def _get(structure, key, default):
  53. try:
  54. if hasattr(structure, '__getitem__'):
  55. return structure[key]
  56. return getattr(structure, key)
  57. except (IndexError, KeyError):
  58. return default
  59. def _get_keys_and_values(structure, key_spec):
  60. if callable(key_spec):
  61. # Support predicates as callable objects in the path
  62. arity = _get_arity(key_spec)
  63. if arity == 1:
  64. # Unary predicates are called with the "key" of the path
  65. # - eg a key in a mapping, an index in a sequence.
  66. return [(k, v) for k, v in _items(structure) if key_spec(k)]
  67. elif arity == 2:
  68. # Binary predicates are called with the key and the corresponding
  69. # value.
  70. return [(k, v) for k, v in _items(structure) if key_spec(k, v)]
  71. else:
  72. # Other arities are an error.
  73. raise ValueError(
  74. "callable in transform path must take 1 or 2 arguments"
  75. )
  76. # Non-callables are used as-is as a key.
  77. return [(key_spec, _get(structure, key_spec, _EMPTY_SENTINEL))]
  78. if signature is None:
  79. def _get_arity(f):
  80. argspec = getargspec(f)
  81. return len(argspec.args) - len(argspec.defaults or ())
  82. else:
  83. def _get_arity(f):
  84. return sum(
  85. 1
  86. for p
  87. in signature(f).parameters.values()
  88. if p.default is Parameter.empty
  89. and p.kind in (Parameter.POSITIONAL_ONLY, Parameter.POSITIONAL_OR_KEYWORD)
  90. )
  91. def _update_structure(structure, kvs, path, command):
  92. from pyrsistent._pmap import pmap
  93. e = structure.evolver()
  94. if not path and command is discard:
  95. # Do this in reverse to avoid index problems with vectors. See #92.
  96. for k, v in reversed(kvs):
  97. discard(e, k)
  98. else:
  99. for k, v in kvs:
  100. is_empty = False
  101. if v is _EMPTY_SENTINEL:
  102. # Allow expansion of structure but make sure to cover the case
  103. # when an empty pmap is added as leaf node. See #154.
  104. is_empty = True
  105. v = pmap()
  106. result = _do_to_path(v, path, command)
  107. if result is not v or is_empty:
  108. e[k] = result
  109. return e.persistent()