Skip to content

Commit

Permalink
ENH: dispatch(Field, collections.Mapping) for compute_up
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe Jevnik committed Apr 1, 2016
1 parent 6961844 commit 00d772a
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 2 deletions.
1 change: 1 addition & 0 deletions blaze/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import warnings
from .expr import (
Symbol,
broadcast_collect,
by,
cast,
coalesce,
Expand Down
3 changes: 2 additions & 1 deletion blaze/compute/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"""
from __future__ import absolute_import, division, print_function

from collections import Mapping
import itertools
import numbers
import fnmatch
Expand Down Expand Up @@ -768,7 +769,7 @@ def compute_up(expr, seq, **kwargs):
raise NotImplementedError("Only 1d slices supported")


@dispatch(Field, dict)
@dispatch(Field, Mapping)
def compute_up(expr, data, **kwargs):
return data[expr._name]

Expand Down
23 changes: 23 additions & 0 deletions blaze/compute/tests/test_python_compute.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from __future__ import absolute_import, division, print_function
from collections import Mapping

import math
import itertools
import operator
import pytest
from datetime import datetime, date
import datashape
from datashape.py2help import mappingproxy
from collections import Iterator, Iterable

import blaze
Expand Down Expand Up @@ -834,6 +836,27 @@ def test_compute_field_on_dicts():
assert compute(s.x, {s: d}) == [1, 2, 3]


class _MapProxy(Mapping): # pragma: no cover
def __init__(self, mapping):
self._map = mapping

def __getitem__(self, key):
return self._map[key]

def __iter__(self):
return iter(self._map)

def __len__(self):
return len(self._map)


@pytest.mark.parametrize('cls', (_MapProxy, mappingproxy))
def test_comute_on_mapping(cls):
s = symbol('s', '{x: 3 * int64, y: 3 * float32}')
d = cls({'x': [1, 2, 3], 'y': [4.0, 5.0, 6.0]})
assert compute(s.x, {s: d}, return_type='native') == [1, 2, 3]


def test_truncate():
s = symbol('x', 'real')
assert compute(s.truncate(20), 154) == 140
Expand Down
2 changes: 1 addition & 1 deletion blaze/expr/broadcast.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from .strings import Like
from .datetime import DateTime

__all__ = ['broadcast', 'Broadcast', 'scalar_symbols']
__all__ = ['broadcast', 'Broadcast', 'scalar_symbols', 'broadcast_collect']


def broadcast(expr, leaves, scalars=None):
Expand Down
2 changes: 2 additions & 0 deletions docs/source/whatsnew/0.10.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ Improved Backends

* Adds :class:`~blaze.expr.math.greatest` and :class:`~blaze.expr.math.least`
support to the sql backend (:issue:`1428`).
* Generalize ``Field`` to support :class:`collections.Mapping` object
(:issue:`1467`).

Experimental Features
~~~~~~~~~~~~~~~~~~~~~
Expand Down

0 comments on commit 00d772a

Please sign in to comment.