def wrap_data(key, value, name, version): msg = {} if (_.is_object(value) and value is not None): msg = value else: msg[key] = value _.set_(msg, "__head__.plugin_name", name) _.set_(msg, "__head__.plugin_version", version) return msg
def flatten_dict(obj, delim='.'): '''Missing pydash method to flatten dict''' nobj = {} for key, val in obj.items(): if ps.is_dict(val) and not ps.is_empty(val): strip = flatten_dict(val, delim) for k, v in strip.items(): nobj[key + delim + k] = v elif ps.is_list(val) and not ps.is_empty(val) and ps.is_dict(val[0]): for idx, v in enumerate(val): nobj[key + delim + str(idx)] = v if ps.is_object(v): nobj = flatten_dict(nobj, delim) else: nobj[key] = val return nobj
def map_values_deep(obj, iteratee=None, property_path=NoValue): """ Map all non-object values in `obj` with return values from `iteratee`. The iteratee is invoked with two arguments: ``(obj_value, property_path)`` where ``property_path`` contains the list of path keys corresponding to the path of ``obj_value``. Args: obj (list|dict): Object to map. iteratee (callable): Iteratee applied to each value. Returns: mixed: The modified object. Warning: `obj` is modified in place. Example: >>> x = {'a': 1, 'b': {'c': 2}} >>> y = map_values_deep(x, lambda val: val * 2) >>> y == {'a': 2, 'b': {'c': 4}} True >>> z = map_values_deep(x, lambda val, props: props) >>> z == {'a': ['a'], 'b': {'c': ['b', 'c']}} True .. versionadded: 2.2.0 .. versionchanged:: 3.0.0 Allow iteratees to accept partial arguments. .. versionchanged:: 4.0.0 Renamed from ``deep_map_values`` to ``map_values_deep``. """ properties = to_path(property_path) if pyd.is_object(obj): def deep_iteratee(value, key): return map_values_deep(value, iteratee, pyd.flatten([properties, key])) return assign(obj, map_values(obj, deep_iteratee)) else: return callit(iteratee, obj, properties)
def map_values_deep(obj, iteratee=None, property_path=NoValue): """Map all non-object values in `obj` with return values from `iteratee`. The iteratee is invoked with two arguments: ``(obj_value, property_path)`` where ``property_path`` contains the list of path keys corresponding to the path of ``obj_value``. Args: obj (list|dict): Object to map. iteratee (function): Iteratee applied to each value. Returns: mixed: The modified object. Warning: `obj` is modified in place. Example: >>> x = {'a': 1, 'b': {'c': 2}} >>> y = map_values_deep(x, lambda val: val * 2) >>> y == {'a': 2, 'b': {'c': 4}} True >>> z = map_values_deep(x, lambda val, props: props) >>> z == {'a': ['a'], 'b': {'c': ['b', 'c']}} True .. versionadded: 2.2.0 .. versionchanged:: 3.0.0 Allow iteratees to accept partial arguments. .. versionchanged:: 4.0.0 Renamed from ``deep_map_values`` to ``map_values_deep``. """ properties = to_path(property_path) if pyd.is_object(obj): deep_iteratee = ( lambda value, key: map_values_deep(value, iteratee, pyd.flatten([properties, key]))) return assign(obj, map_values(obj, deep_iteratee)) else: return callit(iteratee, obj, properties)
def deep_map_values(obj, callback=None, property_path=NoValue): """Map all non-object values in `obj` with return values from `callback`. The callback is invoked with two arguments: ``(obj_value, property_path)`` where ``property_path`` contains the list of path keys corresponding to the path of ``obj_value``. Args: obj (list|dict): Object to map. callback (callable): Callback applied to each value. Returns: mixed: The modified object. Warning: `obj` is modified in place. Example: >>> x = {'a': 1, 'b': {'c': 2}} >>> y = deep_map_values(x, lambda val: val * 2) >>> y == {'a': 2, 'b': {'c': 4}} True >>> z = deep_map_values(x, lambda val, props: props) >>> z == {'a': ['a'], 'b': {'c': ['b', 'c']}} True .. versionadded: 2.2.0 .. versionchanged:: 3.0.0 Allow callbacks to accept partial arguments. """ properties = path_keys(property_path) if pyd.is_object(obj): deep_callback = ( lambda value, key: deep_map_values(value, callback, pyd.flatten([properties, key]))) return pyd.extend(obj, map_values(obj, deep_callback)) else: return call_callback(callback, obj, properties)
def deep_map_values(obj, callback=None, property_path=NoValue): """Map all non-object values in `obj` with return values from `callback`. The callback is invoked with two arguments: ``(obj_value, property_path)`` where ``property_path`` contains the list of path keys corresponding to the path of ``obj_value``. Args: obj (list|dict): Object to map. callback (function): Callback applied to each value. Returns: mixed: The modified object. Warning: `obj` is modified in place. Example: >>> x = {'a': 1, 'b': {'c': 2}} >>> y = deep_map_values(x, lambda val: val * 2) >>> y == {'a': 2, 'b': {'c': 4}} True >>> z = deep_map_values(x, lambda val, props: props) >>> z == {'a': ['a'], 'b': {'c': ['b', 'c']}} True .. versionadded: 2.2.0 .. versionchanged:: 3.0.0 Allow callbacks to accept partial arguments. """ properties = path_keys(property_path) if pyd.is_object(obj): deep_callback = ( lambda value, key: deep_map_values(value, callback, pyd.flatten([properties, key]))) return pyd.extend(obj, map_values(obj, deep_callback)) else: return callit(callback, obj, properties)
def test_is_object(case, expected): assert _.is_object(case) == expected