Skip to content

Commit

Permalink
ENH: Check against collections.Mapping instead of dict
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe Jevnik committed Apr 1, 2016
1 parent 6d7b4e5 commit 94a2950
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 23 deletions.
9 changes: 2 additions & 7 deletions blaze/compute/core.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import absolute_import, division, print_function

from collections import defaultdict, Iterator
from collections import defaultdict, Iterator, Mapping
from datetime import date, datetime, timedelta
import itertools
import numbers
Expand Down Expand Up @@ -378,7 +378,7 @@ def swap_resources_into_scope(expr, scope):
return expr, new_scope


@dispatch(Expr, dict)
@dispatch(Expr, Mapping)
def compute(expr, d, return_type=no_default, **kwargs):
"""Compute expression against data sources.
Expand Down Expand Up @@ -451,11 +451,6 @@ def compute(expr, d, return_type=no_default, **kwargs):
return result


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


@compute_up.register(Join, object, object)
def join_dataframe_to_selectable(expr, lhs, rhs, scope=None, **kwargs):
lexpr, rexpr = expr._leaves()
Expand Down
2 changes: 1 addition & 1 deletion blaze/compute/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def pre_compute(expr, seq, scope=None, **kwargs):
first = next(iter(seq))
except StopIteration:
return []
if isinstance(first, dict):
if isinstance(first, Mapping):
leaf = expr._leaves()[0]
return pluck(leaf.fields, seq)
else:
Expand Down
9 changes: 4 additions & 5 deletions blaze/expr/core.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
from __future__ import absolute_import, division, print_function

from collections import Mapping
import datetime
import numbers
import inspect

from pprint import pformat
from functools import reduce, partial

import numpy as np
import toolz
from toolz import unique, concat, first
import pandas as pd

from ..compatibility import _strtypes
from ..dispatch import dispatch
Expand Down Expand Up @@ -365,12 +364,12 @@ def subs(o, d):
return _subs(o, d)


@dispatch((tuple, list), dict)
@dispatch((tuple, list), Mapping)
def _subs(o, d):
return type(o)([subs(arg, d) for arg in o])


@dispatch(Node, dict)
@dispatch(Node, Mapping)
def _subs(o, d):
"""
Expand All @@ -383,7 +382,7 @@ def _subs(o, d):
return type(o)(*newargs)


@dispatch(object, dict)
@dispatch(object, Mapping)
def _subs(o, d):
""" Private dispatched version of ``subs``
Expand Down
13 changes: 6 additions & 7 deletions blaze/expr/expressions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import absolute_import, division, print_function

from collections import Mapping
from keyword import iskeyword
import re

Expand Down Expand Up @@ -323,7 +324,7 @@ def __init__(self, name, dshape, token=0):
self._hash = None

def __repr__(self):
fmt = "<`{}` symbol; dshape='{}'>"
fmt = "<`{}` symbol; dshape='{}'>"
return fmt.format(self._name, sanitized_dshape(self.dshape))

def __str__(self):
Expand All @@ -339,7 +340,7 @@ def symbol(name, dshape, token=None):
return Symbol(name, dshape, token=token or 0)


@dispatch(Symbol, dict)
@dispatch(Symbol, Mapping)
def _subs(o, d):
""" Subs symbols using symbol function
Expand Down Expand Up @@ -485,7 +486,6 @@ def sliceit(child, index):
return s



class Slice(Expr):
"""Elements `start` until `stop`. On many backends, a `step` parameter
is also allowed.
Expand Down Expand Up @@ -671,9 +671,8 @@ def __str__(self):

@copydoc(ReLabel)
def relabel(child, labels=None, **kwargs):
labels = labels or dict()
labels = toolz.merge(labels, kwargs)
labels = dict((k, v) for k, v in labels.items() if k != v)
labels = {k: v
for k, v in toolz.merge(labels or {}, kwargs).items() if k != v}
label_keys = set(labels)
fields = child.fields
if not label_keys.issubset(fields):
Expand All @@ -682,7 +681,7 @@ def relabel(child, labels=None, **kwargs):
', '.join(map(repr, non_existent_fields)))
if not labels:
return child
if isinstance(labels, dict): # Turn dict into tuples
if isinstance(labels, Mapping): # Turn dict into tuples
labels = tuple(sorted(labels.items()))
if isscalar(child.dshape.measure):
if child._name == labels[0][0]:
Expand Down
5 changes: 2 additions & 3 deletions blaze/interactive.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import absolute_import, division, print_function

import sys
from collections import Iterator
from collections import Iterator, Mapping
import decimal
import datetime
from functools import reduce, partial
Expand Down Expand Up @@ -182,7 +181,7 @@ def data(data_source, dshape=None, name=None, fields=None, schema=None, **kwargs
return _Data(data_source, ds, name)


@dispatch(_Data, dict)
@dispatch(_Data, Mapping)
def _subs(o, d):
return o

Expand Down

0 comments on commit 94a2950

Please sign in to comment.