From 7e4a16742fd0c4950405c8da9b699b1aee7de548 Mon Sep 17 00:00:00 2001 From: jrycw Date: Thu, 30 May 2024 07:21:48 +0800 Subject: [PATCH] Code review change --- docs/_quarto.yml | 7 ++- great_tables/_pipe.py | 113 ++---------------------------------------- great_tables/gt.py | 3 +- tests/test_pipe.py | 40 --------------- 4 files changed, 7 insertions(+), 156 deletions(-) diff --git a/docs/_quarto.yml b/docs/_quarto.yml index f9abf1597..3bcba3131 100644 --- a/docs/_quarto.yml +++ b/docs/_quarto.yml @@ -174,14 +174,13 @@ quartodoc: contents: - GT.save - GT.as_raw_html - - title: Pipelines + - title: Pipeline desc: > Sometimes, you might want to programmatically manipulate the table while still benefiting - from the chained API that **Great Tables** offers. `pipe()` and `pipes()` are designed - to tackle this issue. + from the chained API that **Great Tables** offers. `pipe()` is designed to tackle this + issue. contents: - GT.pipe - - GT.pipes - title: Value formatting functions desc: > If you have single values (or lists of them) in need of formatting, we have a set of diff --git a/great_tables/_pipe.py b/great_tables/_pipe.py index 51e727196..1ac939e81 100644 --- a/great_tables/_pipe.py +++ b/great_tables/_pipe.py @@ -33,10 +33,10 @@ def pipe(self: "GT", func: Callable[..., "GT"], *args: Any, **kwargs: Any) -> "G Examples: ------ - Let's use the "name", "land_area_km2," and "density_2021" columns of the `towny` dataset to + Let's use the `name`, `land_area_km2`, and `density_2021` columns of the `towny` dataset to create a table. First, we'll demonstrate using two consecutive calls to the `.tab_style()` - method to highlight the maximum value of the "land_area_km2" column with "lightgray" and the - maximum value of the "density_2021" column with "lightblue". + method to highlight the maximum value of the `land_area_km2` column with `"lightgray"` and the + maximum value of the `density_2021` column with `"lightblue"`. ```{python} import polars as pl @@ -94,110 +94,3 @@ def tbl_style(gtbl: GT, columns: list[str], colors: list[str]) -> GT: ``` """ return func(self, *args, **kwargs) - - -def pipes(self: "GT", *funcs: Callable["GT", "GT"] | list[Callable["GT", "GT"]]) -> "GT": - """ - Provide a structured way to chain functions for a GT object. - - This function accepts multiple functions, each of which receives a GT object and returns a GT - object. This allows users to easily integrate functions into the chained API offered by - **Great Tables**. It serves as a helper function for chaining multiple functions at once. - - Parameters - ---------- - *funcs - Multiple functions or a list of functions, each receiving a GT object and returning a GT - object. - - Returns - ------- - gt - A GT object. - - Examples: - ------ - Let's use the "name", "land_area_km2," and "density_2021" columns of the `towny` dataset to - create a table. First, we'll demonstrate using two consecutive calls to the `.tab_style()` - method to highlight the maximum value of the "land_area_km2" column with "lightgray" and the - maximum value of the "density_2021" column with "lightblue". - - ```{python} - import polars as pl - from great_tables import GT, loc, style - from great_tables.data import towny - - - towny_mini = pl.from_pandas(towny).head(10) - - ( - GT( - towny_mini[["name", "land_area_km2", "density_2021"]], - rowname_col="name", - ) - .tab_style( - style=style.fill(color="lightgray"), - locations=loc.body( - columns="land_area_km2", - rows=pl.col("land_area_km2").eq(pl.col("land_area_km2").max()), - ), - ) - .tab_style( - style=style.fill(color="lightblue"), - locations=loc.body( - columns="density_2021", - rows=pl.col("density_2021").eq(pl.col("density_2021").max()), - ), - ) - ) - ``` - - Next, we'll demonstrate achieving the same result using the `.pipes()` method to - programmatically style each column. You might find leveraging `partial` to bind the other - parameters in advance handy. - - ```{python} - from functools import partial - - - columns = ["land_area_km2", "density_2021"] - colors = ["lightgray", "lightblue"] - - - def tbl_style(gtbl: GT, column: str, color: str) -> GT: - return gtbl.tab_style( - style=style.fill(color=color), - locations=loc.body(columns=column, rows=pl.col(column).eq(pl.col(column).max())), - ) - - - ( - GT( - towny_mini[["name", "land_area_km2", "density_2021"]], - rowname_col="name", - ).pipes( - *[partial(tbl_style, column=column, color=color) - for column, color in zip(columns, colors)] - ) - ) - ``` - - Alternatively, you can collect all the functions in a list like this: - - ```{python} - ( - GT( - towny_mini[["name", "land_area_km2", "density_2021"]], - rowname_col="name", - ).pipes( - [partial(tbl_style, column=column, color=color) - for column, color in zip(columns, colors)] - ) - ) - ``` - """ - if isinstance(funcs[0], list) and len(funcs) == 1: - funcs = funcs[0] - for func in funcs: - self = pipe(self, func) - return self diff --git a/great_tables/gt.py b/great_tables/gt.py index 9c9b53e70..c5e44f02c 100644 --- a/great_tables/gt.py +++ b/great_tables/gt.py @@ -11,7 +11,7 @@ from great_tables._boxhead import cols_align, cols_label from great_tables._data_color import data_color from great_tables._export import as_raw_html, save -from great_tables._pipe import pipe, pipes +from great_tables._pipe import pipe from great_tables._formats import ( fmt, fmt_bytes, @@ -257,7 +257,6 @@ def __init__( as_raw_html = as_raw_html pipe = pipe - pipes = pipes # ----- diff --git a/tests/test_pipe.py b/tests/test_pipe.py index f904cb694..613ab1a5b 100644 --- a/tests/test_pipe.py +++ b/tests/test_pipe.py @@ -1,6 +1,5 @@ import polars as pl from great_tables import GT, loc, style -from functools import partial def test_pipe(): @@ -36,42 +35,3 @@ def tbl_style(gtbl: GT, columns: list[str], colors: list[str]) -> GT: gt3 = GT(df).pipe(tbl_style, columns=columns, colors=colors) # check **kwargs assert gt1._styles == gt2._styles == gt3._styles - - -def test_pipes(): - columns = ["x", "y"] - colors = ["lightgray", "lightblue"] - df = pl.DataFrame(dict(zip(columns, [[1, 2, 3], [3, 2, 1]]))) - - gt1 = ( - GT(df) - .tab_style( - style=style.fill(color=colors[0]), - locations=loc.body( - columns=columns[0], rows=pl.col(columns[0]).eq(pl.col(columns[0]).max()) - ), - ) - .tab_style( - style=style.fill(color=colors[1]), - locations=loc.body( - columns=columns[1], rows=pl.col(columns[1]).eq(pl.col(columns[1]).max()) - ), - ) - ) - - def tbl_style(gtbl: GT, column: str, color: str) -> GT: - gtbl = gtbl.tab_style( - style=style.fill(color=color), - locations=loc.body(columns=column, rows=pl.col(column).eq(pl.col(column).max())), - ) - return gtbl - - gt2 = GT(df).pipes( - *[partial(tbl_style, column=column, color=color) for column, color in zip(columns, colors)] - ) - - gt3 = GT(df).pipes( - [partial(tbl_style, column=column, color=color) for column, color in zip(columns, colors)] - ) - - assert gt1._styles == gt2._styles == gt3._styles