From a4dcf39a171aaed987788a927d59cdb7f0107994 Mon Sep 17 00:00:00 2001 From: Vladimir Magamedov Date: Thu, 13 Mar 2014 10:42:39 +0200 Subject: [PATCH] Added ability to easily pipe functions after initial function definition using "|" operator. --- sqlconstruct.py | 3 +++ tests.py | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/sqlconstruct.py b/sqlconstruct.py index 68a8c35..3c710c3 100644 --- a/sqlconstruct.py +++ b/sqlconstruct.py @@ -487,6 +487,9 @@ class _Processable(object): def __processor__(self, scope): raise NotImplementedError + def __or__(self, value): + return apply_(value, [self]) + def _get_value_processor(scope, value): if isinstance(value, ColumnElement): diff --git a/tests.py b/tests.py index 5eb4537..ff55852 100644 --- a/tests.py +++ b/tests.py @@ -384,6 +384,16 @@ def test_defined_calls(self): (1 + 2 + 3, 'foo' + 'bar' + 'baz'), ) + def test_pipe_operator(self): + struct = Construct({ + 'a_name_hash': apply_(capitalize, [self.a_cls.name]) | hash, + }) + + s1, s2 = self.session.query(struct).order_by(self.a_cls.name).all() + + self.assertEqual(s1.a_name_hash, hash('A1')) + self.assertEqual(s2.a_name_hash, hash('A2')) + def test_query_count(self): query = self.session.query( Construct({'a_id': self.a_cls.id,