Skip to content

Commit

Permalink
Merge pull request #86 from mahmoud/seq-snippet
Browse files Browse the repository at this point in the history
added Seq as another extension example
  • Loading branch information
mahmoud committed Feb 24, 2019
2 parents 057b1e5 + b5d66b1 commit f998924
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions docs/snippets.rst
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,32 @@ As an example, here is a lisp-style If expression custom spec type:
# {'yes': 1}
glom(0, If(bool, {'yes': T}, {'no': T}))
# {'no': 0}
Parellel Evaluation of Sub-Specs
--------------------------------

This is another example of a simple glom extension.
Sometimes it is convenient to execute multiple glom-specs
in parallel against a target, and get a sequence of their
results.

.. code-block:: python
class Seq(object):
def __init__(self, *subspecs):
self.subspecs = subspecs
def glomit(self, target, scope):
return [scope[glom](target, spec, scope) for spec in self.subspecs]
glom('1', Seq(float, int))
# [1.0, 1]
Without this extension, the simplest way to achieve the same result is
with a dict:

.. code-block:: python
glom('1', ({1: float, 2: int}, T.values()))

0 comments on commit f998924

Please sign in to comment.