Skip to content

Commit

Permalink
Merge pull request #191 from mahmoud/pipe-spec
Browse files Browse the repository at this point in the history
pipe spec
  • Loading branch information
mahmoud committed Aug 12, 2020
2 parents e8cd3b8 + 240e309 commit 3cdf4e7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions glom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
Let, # backwards compat 2020-07
Coalesce,
Inspect,
Pipe,
GlomError,
BadSpec,
PathAccessError,
Expand Down
20 changes: 20 additions & 0 deletions glom/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1779,6 +1779,26 @@ def _handle_tuple(target, spec, scope):
return res


class Pipe(object):
"""Evaluate specs one after the other, passing the result of
the previous evaluation in as the target of the next spec:
>>> glom({'a': {'b': -5}}, Pipe('a', 'b', abs))
5
Same behavior as ``Auto(tuple(steps))``, but useful for explicit
usage in other modes.
"""
def __init__(self, *steps):
self.steps = steps

def glomit(self, target, scope):
return _handle_tuple(target, self.steps, scope)

def __repr__(self):
return self.__class__.__name__ + bbrepr(self.steps)


class TargetRegistry(object):
'''
responsible for registration of target types for iteration
Expand Down
7 changes: 6 additions & 1 deletion glom/test/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pytest

from glom import glom, SKIP, STOP, Path, Inspect, Coalesce, CoalesceError, Val, Call, T, S, Invoke, Spec, Ref
from glom import Auto, Fill, Iter, A, Vars, Val, Literal, GlomError
from glom import Auto, Fill, Iter, A, Vars, Val, Literal, GlomError, Pipe

import glom.core as glom_core
from glom.core import UP, ROOT, bbformat, bbrepr
Expand Down Expand Up @@ -424,6 +424,11 @@ def test_ref():
glom(etree, etree2tuples)


def test_pipe():
assert glom(1, Pipe("__class__", "__name__", str.upper)) == 'INT'
assert repr(Pipe(1, Pipe([2], dict))) == 'Pipe(1, Pipe([2], dict))'


_IS_PYPY = '__pypy__' in sys.builtin_module_names
@pytest.mark.skipif(_IS_PYPY, reason='pypy othertype.__repr__ is never object.__repr__')
def test_api_repr():
Expand Down

0 comments on commit 3cdf4e7

Please sign in to comment.