Beispiel #1
0
    def test_basic(self):
        data = (
            [1, 2],
            [3, 4],
        )

        self.assertEqual((1, 3), pluck(data, 0))

        self.assertEqual((2, 4), pluck(data, max))
Beispiel #2
0
 def test_null_values(self):
     data = {None: [1, 2], 'b': [3, 4], 'c': [None, 5]}
     self.assertEqual({
         None: 1,
         'b': 3,
         'c': None,
     }, pluck(data, 0))
Beispiel #3
0
    def test_slice(self):
        data = [
            [1, 2, 3],
            [4, 5, 6],
        ]

        self.assertEqual([[2, 3], [5, 6]], pluck(data, slice(1, 3)))
Beispiel #4
0
    def test_basic(self):
        data = {
            Coordinate(x=1, y=2),
            Coordinate(x=2, y=4),
            Coordinate(x=3, y=6),
        }

        self.assertEqual({1, 2, 3}, pluck(data, 'x'))
Beispiel #5
0
    def test_fn(self):
        # build-in
        data = [
            [1],
            [1, 2],
            [1, 2, 3],
        ]
        self.assertEqual([1, 2, 3], pluck(data, max))

        # user function
        def double(val):
            return val * 2

        self.assertEqual(
            [2, 4, 6],
            pluck([1, 2, 3], double),
        )

        # lambda
        self.assertEqual([2, 4, 6], pluck([1, 2, 3], lambda x: x * 2))
Beispiel #6
0
    def test_indicies(self):
        data = {
            'a': [ 0, 1 ],
            'b': [ 5, 6 ],
        }

        self.assertEqual(
            {
                'a': 1,
                'b': 6,
            },
            pluck(data, 1)
        )
Beispiel #7
0
    def test_basic(self):
        data = {
            'a': { 'k': 1, 'v': 2 },
            'b': { 'k': 3, 'v': 4 },
            'c': { 'k': 5, 'v': 6 },
        }

        self.assertEqual(
            {
                'a': 2,
                'b': 4,
                'c': 6,
            },
            pluck(data, 'v')
        )
Beispiel #8
0
    def test_basic(self):
        data = [
            {
                'x': 1,
                'y': 2
            },
            {
                'x': 2,
                'y': 4
            },
            {
                'x': 3,
                'y': 6
            },
        ]

        self.assertEqual([1, 2, 3], pluck(data, 'x'))
Beispiel #9
0
 def test_null(self):
     self.assertEqual(None, pluck(None, None))
     self.assertEqual(None, pluck(None, 123))
Beispiel #10
0
 def test_empty(self):
     self.assertEqual([], pluck([], 'k'))
     self.assertEqual({}, pluck({}, 'k'))
     self.assertEqual(set(), pluck(set(), 'k'))
Beispiel #11
0
 def test_null_handle(self):
     data = [1, 2, 3]
     self.assertEqual([None, None, None], pluck(data, None))
Beispiel #12
0
 def test_indicies(self):
     data = [
         [0, 1, 2],
         [5, 6, 7],
     ]
     self.assertEqual([0, 5], pluck(data, 0))
Beispiel #13
0
from pluckit import pluck
from rekey import rekey
from sqlalchemy.orm.query import Query

from .result_list import ResultList

# monkey patch SQLAlchemy Query object
Query.pluck = lambda self, handle: pluck(self, handle)
Query.rekey = lambda self, key, handle=None: rekey(self, key, handle)

# all() should return a pluckable/rekeyable
Query.__sa_all = Query.all
Query.all = lambda self: ResultList(self.__sa_all())


def where(self, *args, **kwargs):
    query = self
    entity = self._entities[0].entity_zero.entity

    for field, value in kwargs.items():
        attr = getattr(entity, field)

        if isinstance(value, (list, tuple, set)):
            query = query.filter(attr.in_(value))
        else:
            query = query.filter(attr == value)

    if len(args) > 0:
        query = query.filter(*args)

    return query