pg.object_utils.transform

Accessible via pg.object_utils.transform.

transform(value, transform_fn, root_path=None, inplace=True)[source]

Bottom-up (post-order) transform a (maybe) hierarchical value.

Transform on value is in-place unless transform_fn returns a different instance.

Example:

def _remove_int(path, value):
  del path
  if isinstance(value, int):
    return pg.MISSING_VALUE
  return value

inputs = {
    'a': {
        'b': 1,
        'c': [1, 'bar', 2, 3],
        'd': 'foo'
    },
    'e': 'bar',
    'f': 4
}
output = pg.object_utils.transform(inputs, _remove_int)
assert output == {
    'a': {
        'c': ['bar'],
        'd': 'foo',
    },
    'e': 'bar'
})
Return type:

Any

Parameters:
  • value – Any python value type. If value is a list of dict, transformation will occur recursively.

  • transform_fn – Transform function in signature (path, value) -> new value If new value is MISSING_VALUE, key will be deleted.

  • root_path – KeyPath of the root.

  • inplace – If True, perform transformation in place.

Returns:

Transformed value.