iterobj#

class iterobj(obj, func=None, inplace=False, copy=False, leaf=False, atomic='default', rootkey='root', verbose=False, _trace=None, _output=None, *args, **kwargs)[source]#

Iterate over an object and apply a function to each node (item with or without children).

Can modify an object in-place, or return a value. See also sc.search() for a function to search through complex objects.

By default, lists, dictionaries, and objects are iterated over. For custom iteration options, see sc.IterObj().

Note: there are three different output possibilities, depending on the keywords:

  • inplace=False, copy=False (default): collate the output of the function into a flat dictionary, with keys corresponding to each node of the project

  • inplace=True, copy=False: modify the actual object in-place, such that the original object is modified

  • inplace=True, copy=True: make a deep copy of the object, modify that object, and return it (the original is unchanged)

Parameters:
  • obj (any) – the object to iterate over

  • func (function) – the function to apply; if None, return a dictionary of all leaf nodes in the object

  • inplace (bool) – whether to modify the object in place (else, collate the output of the functions)

  • copy (bool) – if modifying an object in place, whether to make a copy first

  • leaf (bool) – whether to apply the function only to leaf nodes of the object

  • atomic (list) – a list of known classes to treat as atomic (do not descend into); if ‘default’, use defaults (e.g. np.array, pd.DataFrame)

  • rootkey (str) – the key to list as the root of the object (default 'root')

  • verbose (bool) – whether to print progress.

  • _trace (list) – used internally for recursion

  • _output (list) – used internally for recursion

  • *args (list) – passed to func()

  • **kwargs (dict) – passed to func()

Examples:

data = dict(a=dict(x=[1,2,3], y=[4,5,6]), b=dict(foo='string', bar='other_string'))

# Search through an object
def check_int(obj):
    return isinstance(obj, int)

out = sc.iterobj(data, check_type)
print(out)


# Modify in place -- collapse mutliple short lines into one
def collapse(obj, maxlen):
    string = str(obj)
    if len(string) < maxlen:
        return string
    else:
        return obj

sc.printjson(data)
sc.iterobj(data, collapse, inplace=True, maxlen=10) # Note passing of keyword argument to function
sc.printjson(data)
New in version 3.0.0.
New in version 3.1.0: default func, renamed “twigs_only” to “leaf”, “atomic” argument
New in version 3.1.2: copy defaults to False; refactored into class
New in version 3.1.3: “rootkey” argument