Skip to content

Commit

Permalink
Use custom_dict instead of regular dict
Browse files Browse the repository at this point in the history
  • Loading branch information
has2k1 committed Oct 1, 2017
1 parent 85a43b9 commit 46c0ada
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 23 deletions.
3 changes: 2 additions & 1 deletion plydata/dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
# works for multiple data type sources.

"""
Verb implementations for a :class:`dict`
Verb implementations for a :class:`custom_dict`
"""


def define(verb):
verb.data = verb.data.copy()
env = verb.env.with_outer_namespace(verb.data)
for col, expr in zip(verb.new_columns, verb.expressions):
if isinstance(expr, str):
Expand Down
11 changes: 7 additions & 4 deletions plydata/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@

from .eval import EvalEnvironment
from .grouped_datatypes import GroupedDataFrame
from .utils import temporary_attr
from .utils import temporary_attr, custom_dict


type_lookup = {
pd.DataFrame: import_module('.dataframe', __package__),
GroupedDataFrame: import_module('.dataframe', __package__),
dict: import_module('.dict', __package__)
custom_dict: import_module('.dict', __package__)
}

DATASTORE_TYPES = tuple(type_lookup.keys())


# We use this for "single dispatch" instead of maybe
# functools.singledispatch because when piping the datatype
Expand Down Expand Up @@ -53,7 +56,7 @@ class OptionalSingleDataFrameArgument(type):
"""
def __call__(cls, *args, **kwargs):
# When we have data we can proceed with the computation
if len(args) and isinstance(args[0], pd.DataFrame):
if len(args) and isinstance(args[0], DATASTORE_TYPES):
return args[0] >> super().__call__(*args[1:], **kwargs)
else:
return super().__call__(*args, **kwargs)
Expand Down Expand Up @@ -86,7 +89,7 @@ def __rrshift__(self, other):
Overload the >> operator
"""
if self.data is None:
if isinstance(other, (pd.DataFrame, dict)):
if isinstance(other, DATASTORE_TYPES):
self.data = other
else:
msg = "Unknown type of data {}"
Expand Down
25 changes: 13 additions & 12 deletions plydata/tests/test_dict.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
import numpy as np

from plydata import define
from plydata.utils import custom_dict


def test_define():
x = np.array([1, 2, 3])
y = np.array([4, 5, 6])
d = {'x': x}
d = custom_dict({'x': x})

# No args
d >> define()
assert len(d) == 1

# All types of args
d >> define(('x*2', 'x*2'),
('x*3', 'x*3'),
x_sq='x**2',
x_cumsum='np.cumsum(x)',
y=y)
result = d >> define(('x*2', 'x*2'),
('x*3', 'x*3'),
x_sq='x**2',
x_cumsum='np.cumsum(x)',
y=y)

assert len(d) == 6
assert all(d['x*2'] == x*2)
assert all(d['x*3'] == x*3)
assert all(d['x_sq'] == x**2)
assert all(d['x_cumsum'] == np.cumsum(x))
assert all(d['y'] == y)
assert len(result) == 6
assert all(result['x*2'] == x*2)
assert all(result['x*3'] == x*3)
assert all(result['x_sq'] == x**2)
assert all(result['x_cumsum'] == np.cumsum(x))
assert all(result['y'] == y)
14 changes: 8 additions & 6 deletions plydata/tests/test_operators.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
import pandas as pd
import pytest
from plydata import define

from plydata.operators import get_verb_function


def test_get_verb_function():
class dict_subclass(dict):
class dataframe_subclass(pd.DataFrame):
pass

data = dict()
data2 = dict_subclass()
data = pd.DataFrame()
data2 = dataframe_subclass()

# No error
func1 = get_verb_function(data, 'define')
func2 = get_verb_function(data2, 'define')
assert func1 is func2

with pytest.raises(TypeError):
get_verb_function(data, 'arrange')
get_verb_function(data, 'unknown_verb')


def test_DataOperator():
s = {1, 2, 3}
data = {'x': [1, 2, 3], 'y': [1, 2, 3]}
s = {1, 2, 3} # unrecognized datastore
data = pd.DataFrame({'x': [1, 2, 3],
'y': [1, 2, 3]})

with pytest.raises(TypeError):
s >> define(z='x')
Expand Down
10 changes: 10 additions & 0 deletions plydata/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,13 @@ class y
return env.namespace[name]
except KeyError:
raise NameError("No data named {!r} found".format(name))


class custom_dict(dict):
"""
Dict datastore for conflict testing purposes
Using a regular dict creates conflicts with verbs
whose first parameter can be a dict
"""
pass

0 comments on commit 46c0ada

Please sign in to comment.