Skip to content

Commit

Permalink
MAINT: deprecate alt.curry and alt.pipe
Browse files Browse the repository at this point in the history
  • Loading branch information
jakevdp committed Mar 23, 2020
1 parent 0b29fb9 commit af4148d
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 23 deletions.
4 changes: 2 additions & 2 deletions altair/_magics.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import IPython
from IPython.core import magic_arguments
import pandas as pd
from toolz import pipe
from toolz import curried

from altair.vegalite import v3 as vegalite_v3
from altair.vegalite import v4 as vegalite_v4
Expand Down Expand Up @@ -46,7 +46,7 @@ def _prepare_data(data, data_transformers):
if data is None or isinstance(data, dict):
return data
elif isinstance(data, pd.DataFrame):
return pipe(data, data_transformers.get())
return curried.pipe(data, data_transformers.get())
elif isinstance(data, str):
return {"url": data}
else:
Expand Down
41 changes: 35 additions & 6 deletions altair/utils/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
import os
import random
import hashlib
import warnings

import pandas as pd
from toolz.curried import curry, pipe # noqa
from toolz import curried
from typing import Callable

from .core import sanitize_dataframe
from .core import sanitize_geo_interface
from .deprecation import AltairDeprecationWarning
from .plugin_registry import PluginRegistry


Expand Down Expand Up @@ -55,7 +57,7 @@ class MaxRowsError(Exception):
pass


@curry
@curried.curry
def limit_rows(data, max_rows=5000):
"""Raise MaxRowsError if the data model has more than max_rows.
Expand Down Expand Up @@ -84,7 +86,7 @@ def limit_rows(data, max_rows=5000):
return data


@curry
@curried.curry
def sample(data, n=None, frac=None):
"""Reduce the size of the data model by sampling without replacement."""
check_data_type(data)
Expand All @@ -98,7 +100,7 @@ def sample(data, n=None, frac=None):
return {"values": values}


@curry
@curried.curry
def to_json(
data,
prefix="altair-data",
Expand All @@ -117,7 +119,7 @@ def to_json(
return {"url": os.path.join(urlpath, filename), "format": {"type": "json"}}


@curry
@curried.curry
def to_csv(
data,
prefix="altair-data",
Expand All @@ -134,7 +136,7 @@ def to_csv(
return {"url": os.path.join(urlpath, filename), "format": {"type": "csv"}}


@curry
@curried.curry
def to_values(data):
"""Replace a DataFrame by a data model with values."""
check_data_type(data)
Expand Down Expand Up @@ -213,3 +215,30 @@ def _data_to_csv_string(data):
raise NotImplementedError(
"to_csv only works with data expressed as " "a DataFrame or as a dict"
)


def pipe(data, *funcs):
"""
Pipe a value through a sequence of functions
Deprecated: use toolz.curried.pipe() instead.
"""
warnings.warn(
"alt.pipe() is deprecated, and will be removed in a future release. "
"Use toolz.curried.pipe() instead.",
AltairDeprecationWarning,
)
return curried.pipe(data, *funcs)


def curry(*args, **kwargs):
""" Curry a callable function
Deprecated: use toolz.curried.curry() instead.
"""
warnings.warn(
"alt.curry() is deprecated, and will be removed in a future release. "
"Use toolz.curried.curry() instead.",
AltairDeprecationWarning,
)
return curried.curry(*args, **kwargs)
4 changes: 2 additions & 2 deletions altair/utils/tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import pytest
import pandas as pd
from toolz import pipe


from ..data import limit_rows, MaxRowsError, sample, pipe, to_values, to_json, to_csv
from ..data import limit_rows, MaxRowsError, sample, to_values, to_json, to_csv


def _create_dataframe(N):
Expand Down
10 changes: 6 additions & 4 deletions altair/vega/data.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import pandas as pd
from toolz.curried import curry, pipe
from toolz import curried
from ..utils.core import sanitize_dataframe
from ..utils.data import (
MaxRowsError,
curry,
pipe,
sample,
to_csv,
to_json,
Expand All @@ -11,7 +13,7 @@
)


@curry
@curried.curry
def limit_rows(data, max_rows=5000):
"""Raise MaxRowsError if the data model has more than max_rows."""
if not isinstance(data, (list, pd.DataFrame)):
Expand All @@ -25,9 +27,9 @@ def limit_rows(data, max_rows=5000):
return data


@curry
@curried.curry
def default_data_transformer(data):
return pipe(data, limit_rows, to_values)
return curried.pipe(data, limit_rows, to_values)


__all__ = (
Expand Down
8 changes: 5 additions & 3 deletions altair/vegalite/data.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from toolz.curried import curry, pipe
from toolz import curried
from ..utils.core import sanitize_dataframe
from ..utils.data import (
MaxRowsError,
curry,
limit_rows,
pipe,
sample,
to_csv,
to_json,
Expand All @@ -12,9 +14,9 @@
from ..utils.data import DataTransformerRegistry as _DataTransformerRegistry


@curry
@curried.curry
def default_data_transformer(data, max_rows=5000):
return pipe(data, limit_rows(max_rows=max_rows), to_values)
return curried.pipe(data, limit_rows(max_rows=max_rows), to_values)


class DataTransformerRegistry(_DataTransformerRegistry):
Expand Down
5 changes: 3 additions & 2 deletions altair/vegalite/v3/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
import json
import jsonschema
import pandas as pd
from toolz.curried import pipe as _pipe

from .schema import core, channels, mixins, Undefined, SCHEMA_URL

from .data import data_transformers, pipe
from .data import data_transformers
from ... import utils, expr
from .display import renderers, VEGALITE_VERSION, VEGAEMBED_VERSION, VEGA_VERSION
from .theme import themes
Expand Down Expand Up @@ -80,7 +81,7 @@ def _prepare_data(data, context=None):

# convert dataframes or objects with __geo_interface__ to dict
if isinstance(data, pd.DataFrame) or hasattr(data, "__geo_interface__"):
data = pipe(data, data_transformers.get())
data = _pipe(data, data_transformers.get())

# convert string input to a URLData
if isinstance(data, str):
Expand Down
5 changes: 3 additions & 2 deletions altair/vegalite/v4/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
import json
import jsonschema
import pandas as pd
from toolz.curried import pipe as _pipe

from .schema import core, channels, mixins, Undefined, SCHEMA_URL

from .data import data_transformers, pipe
from .data import data_transformers
from ... import utils, expr
from .display import renderers, VEGALITE_VERSION, VEGAEMBED_VERSION, VEGA_VERSION
from .theme import themes
Expand Down Expand Up @@ -80,7 +81,7 @@ def _prepare_data(data, context=None):

# convert dataframes or objects with __geo_interface__ to dict
if isinstance(data, pd.DataFrame) or hasattr(data, "__geo_interface__"):
data = pipe(data, data_transformers.get())
data = _pipe(data, data_transformers.get())

# convert string input to a URLData
if isinstance(data, str):
Expand Down
6 changes: 4 additions & 2 deletions doc/user_guide/data_transformers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ Piping

Multiple data transformers can be piped together using ``pipe``::

from altair import pipe, limit_rows, to_values
from altair import limit_rows, to_values
from toolz.curried import pipe
pipe(data, limit_rows(10000), to_values)

Managing data transformers
Expand Down Expand Up @@ -182,11 +183,12 @@ custom data transformer that stores all JSON files in separate directory.::

import os
import altair as alt
from toolz.curried import pipe


def json_dir(data, data_dir='altairdata'):
os.makedirs(data_dir, exist_ok=True)
return alt.pipe(data, alt.to_json(filename=data_dir + '/{prefix}-{hash}.{extension}') )
return pipe(data, alt.to_json(filename=data_dir + '/{prefix}-{hash}.{extension}') )


alt.data_transformers.register('json_dir', json_dir)
Expand Down

0 comments on commit af4148d

Please sign in to comment.