File tree 7 files changed +219
-155
lines changed
7 files changed +219
-155
lines changed Original file line number Diff line number Diff line change @@ -42,6 +42,7 @@ This gets transformed into:
42
42
def signum (x : pl.Expr) -> pl.Expr:
43
43
return pl.when(x > 0 ).then(1 ).otherwise(pl.when(x < 0 ).then(- 1 ).otherwise(0 ))
44
44
```
45
+
45
46
### Handling Multiple Statements
46
47
47
48
polarIFy can also handle multiple statements like:
@@ -125,6 +126,38 @@ print(result)
125
126
# └─────┴─────────┘
126
127
```
127
128
129
+ ### Displaying the transpiled polars expression
130
+
131
+ You can also display the transpiled polars expression by calling the ` transform_func_to_new_source ` method:
132
+
133
+ ``` python
134
+ from polarify import transform_func_to_new_source
135
+
136
+ def signum (x ):
137
+ s = 0
138
+ if x > 0 :
139
+ s = 1
140
+ elif x < 0 :
141
+ s = - 1
142
+ return s
143
+
144
+
145
+ print (f " Original function: \n { inspect.getsource(signum)} " )
146
+ # Original function:
147
+ # def signum(x):
148
+ # s = 0
149
+ # if x > 0:
150
+ # s = 1
151
+ # elif x < 0:
152
+ # s = -1
153
+ # return s
154
+ print (f " Transformed function: \n { transform_func_to_new_source(signum)} " )
155
+ # Transformed function:
156
+ # def signum_polarified(x):
157
+ # import polars as pl
158
+ # return pl.when(x > 0).then(1).otherwise(pl.when(x < 0).then(-1).otherwise(0))
159
+ ```
160
+
128
161
TODO: complicated example with nested functions
129
162
130
163
## ⚙️ How It Works
Original file line number Diff line number Diff line change 2
2
# https://github.com/prefix-dev/pixi/issues/79
3
3
[project ]
4
4
name = " polarify"
5
- version = " 0.1.1 "
5
+ version = " 0.1.2 "
6
6
description = " Simplifying conditional Polars Expressions with Python 🐍 🐻❄️"
7
7
authors = [
" Bela Stoyan <[email protected] >" ,
" Pavel Zwerschke <[email protected] >" ]
8
8
channels = [" conda-forge" ]
Original file line number Diff line number Diff line change 2
2
import inspect
3
3
from functools import wraps
4
4
5
- from .main import parse_body
5
+ from .main import parse_body , transform_tree_into_expr
6
6
7
7
8
8
def transform_func_to_new_source (func ) -> str :
9
9
source = inspect .getsource (func )
10
10
tree = ast .parse (source )
11
11
func_def : ast .FunctionDef = tree .body [0 ] # type: ignore
12
- expr = parse_body (func_def .body )
12
+ root_node = parse_body (func_def .body )
13
+
14
+ expr = transform_tree_into_expr (root_node )
13
15
14
16
# Replace the body of the function with the parsed expr
15
17
# Also import polars as pl since this is used in the generated code
You can’t perform that action at this time.
0 commit comments