Package: python-plyr
Plyr: computing on nested containersplyr[/plaɪ'ə/], derived fromapplier, is a python C-extension, that implements amap-like logic, which computes a specified function on the lower-most non-container data of arbitrarily nestedbuilt-inpython containers, i.e. dicts, lists, tuples. It automatically unpacks nested containers in order to call the same function on their underlying non-container objects and then reassembles the structures. See the docstring ofplyr.applyfor details.plyrhappens to coincide with a similarly named library forRstatistical computations language, which streamlines dataframe and vector/matrix transformations.SetupThe package can be installed frompip:pipinstallpython-plyrThe development environment could be initialized with the following commands:# create a dedicated envcondacreate-nplyr"python>=3.7"pippytesttwine\&&condaactivateplyr\&&pipinstallbuild# build and installpipinstall-e.-vvthe Essential ExampleBelow we provide an example, which, we hope, illustrates the casesplyrmight be useful in.importplyrfromcollectionsimportnamedtuplent=namedtuple("nt","u,v")# add the leaf data in a pair of nested objectsplyr.apply(lambdau,v:u+v,[{"a":(1,2,3),"z":3.1415},nt([1,"u"],"abc")],[{"a":(4,6,8),"z":2.7128},nt([4,"v"],"xyz")],# _star=True,  # (default) call fn(d1, d2, **kwargs))# output: [{'a': (5, 8, 11), 'z': 5.8543}, nt(u=[5, 'uv'], v='abcxyz')]# join strings in a pair of tuplesplyr.apply(" -->> ".join,("abc","uvw","xyz"),("123","456","789"),_star=False,# call fn((d1, d2,), **kwargs))# output: ('abc -->> 123', 'uvw -->> 456', 'xyz -->> 789')By default.applyperforms safety checks to ensure identical structure if multiple nested objects are given. If the arguments have identical structure by design, then these integrity checks may be turned off by specifying_safe=False. Please refer to the docs ofplyr.apply.plyr.raggedis a special version of.applywhich implements leaf broadcasting semantics. When processing multiple nested objects it allows one structure to subsume the other structures: any intermediate leaf datais broadcasted deeper into the hierarchyof the other nested structures. Please refer to./doc/mapping_structures.ipynbfor details.Serializing and deserializingSerialization and deserialization of nested objects can be done by.flattenand.unflatten. The following snippet shows how to represent a given nested object as a flat list and then undo the process.importplyro=[1,(2,3),{"a":(4,5),"z":{"a":6}},7]flat,skel=plyr.flatten(o)asserto==plyr.unflatten(flat,skel)flat# output: ([1, 2, 3, 4, 5, 6, 7]This next example demonstrates how to unpack a stream of data into nested objects.importplyrstream,_=iter(range(13)),Nonestruct=({"foo":_,"bar":[_,_]},_)objects=[plyr.unflatten(stream,struct)for_inrange(3)]The following example illustrates how to compute the a functon over a nested object andinvertthe structure (see.iapply):importplyrdeffunc(x,y):return{"z":x*y},-y# `inverted` shall nests the original structure of `o` _under_ func's#  return-value structureflat,struct=plyr.flatapply(func,(2,{"a":1},),(4,{"a":3},))inverted=plyr.apply(plyr.unflatten,*flat,_star=False,struct=struct)inverted# output: ({'z': (8, {'a': 3})}, (-4, {'a': -3}))Here's how one might broadcast a value across the structure of a specified nested object.importplyrstruct={"foo":{"bar":1},"baz":(2,3)}const="abc"broadcast=plyr.apply(lambdax:const,struct)broadcast# output: {'foo': {'bar': 'abc'}, 'baz': ('abc', 'abc')}Other ExamplesBelow we perform something fancy withnumpy. Specifically, we stack outputs from some experiments (dicts of arrays), to get the standard deviation between the results.importplyrimportnumpyasnp# some computationsdefexperiment(j):returndict(a=float(np.random.normal()),u=np.random.normal(size=(5,2))*0.1,z=np.random.normal(size=(2,5))*10,)# run 10 replications of an experimentresults=[experiment(j)forjinrange(10)]# stack and analyze the results (np.stack needs an iterable argument)res=plyr.apply(np.stack,*results,axis=0,_star=False)# get the shapesshapes=plyr.apply(lambdax:x.shape,res)# compute the std along the replication axisplyr.apply(np.std,res,axis=0)You may notice that.applyis veryunsophisticated: it applies the specified function to the leaf data regardless of its type, and every dict, list, or tuple isalwaystreated as a nested container.