Skip to content

Commit a89225f

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 9b46595 commit a89225f

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ df.row_by_value('x', 10)
123123

124124
## Registered methods tracing
125125

126-
The pandas_flavor 0.5.0 release introduced [tracing of the registered method calls](/docs/tracing_ext.md). Now it is possible to add additional run-time logic around registered method execution which can be used for some support tasks. This extention was introduced
126+
The pandas_flavor 0.5.0 release introduced [tracing of the registered method calls](/docs/tracing_ext.md). Now it is possible to add additional run-time logic around registered method execution which can be used for some support tasks. This extention was introduced
127127
to allow visualization of [pyjanitor](https://github.com/pyjanitor-devs/pyjanitor) method chains as implemented in [pyjviz](https://github.com/pyjanitor-devs/pyjviz)
128128

129129

docs/tracing_ext-demo.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,37 @@
22
import pandas_flavor as pf
33
import time
44

5+
56
@pf.register_dataframe_method
67
def my_method(df: pd.DataFrame) -> pd.DataFrame:
78
print("my_method called")
89
return df.transpose()
910

11+
1012
@pf.register_dataframe_method
1113
def another_method(df: pd.DataFrame, new_col_d) -> pd.DataFrame:
1214
print("another_method called")
1315
for col, v in new_col_d.items():
1416
df[col] = v
1517
return df
1618

19+
1720
class tracer:
1821
@staticmethod
1922
def create_tracer(*args):
2023
return tracer()
21-
24+
2225
def __init__(self):
2326
self.method_name = None
2427
self.start_ts = None
2528
self.end_ts = None
26-
29+
2730
def __enter__(self):
2831
return self
29-
30-
def handle_start_method_call(self,
31-
method_name,
32-
method_signature,
33-
method_args,
34-
method_kwagrs):
32+
33+
def handle_start_method_call(
34+
self, method_name, method_signature, method_args, method_kwagrs
35+
):
3536
self.method_name = method_name
3637
self.start_ts = time.time()
3738
return method_args, method_kwagrs
@@ -40,10 +41,12 @@ def handle_end_method_call(self, ret):
4041
self.end_ts = time.time()
4142

4243
def __exit__(self, exc_type, value, traceback):
43-
print(f"method {self.method_name} took {self.end_ts - self.start_ts} secs to execute")
44+
print(
45+
f"method {self.method_name} took {self.end_ts - self.start_ts} secs to execute"
46+
)
4447

4548

4649
pf.register.method_call_ctx_factory = tracer.create_tracer
4750
s_df = pd.DataFrame([[i + j for i in range(10)] for j in range(10)])
48-
res_df = s_df.my_method().another_method({'new_col': 'new value'})
51+
res_df = s_df.my_method().another_method({"new_col": "new value"})
4952
print(res_df)

docs/tracing_ext.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,27 @@ to allow the methods registered via `pandas_flavors` to be traced.
66
Default value of `method_call_ctx_factory` is None.
77

88
Starting version 0.5.0 `pandas_flavor` implements the way to pass registered method name, signature and parameters to
9-
be handled by user-defined object when the call is made. To allow this the user of pandas_flavor must set
9+
be handled by user-defined object when the call is made. To allow this the user of pandas_flavor must set
1010
`method_call_ctx_factory` to refer to function with signature `(method_name: str, method_args: list, method_kwargs: dict) -> tracing_ctx`.
1111
`tracing_ctx` should be class which implements methods with signatures as below:
1212

1313
```python
1414
class tracing_ctx:
1515
def __enter__(self) -> None: pass
1616
def __exit__(self, type, value, traceback): -> None: pass
17-
def handle_start_method_call(self, method_name: str,
17+
def handle_start_method_call(self, method_name: str,
1818
method_signature: inspect.Signature,
1919
method_args: list,
2020
method_kwargs: dict) -> (list, dict): pass
2121
def handle_end_method_call(self, method_ret: object) -> None: pass
2222
```
2323

2424
During method call `pandas_flavor` will create object of class `tracing_ctx` then will use that object to enter *with* code block.
25-
`handle_start_method_call` and `handle_end_method_call` will be called before and after actual method call. The input arguments
25+
`handle_start_method_call` and `handle_end_method_call` will be called before and after actual method call. The input arguments
2626
and return object of actual method call will be passed to corresponding `tracing_ctx` method.
2727

2828
So `handle_start_method_call` and `handle_end_method_call` will have the chance to implement required tracing logic.
29-
Aslo, __exit__ method will be called if any exception would occur. This way it is possible to handle the situation of
29+
Aslo, __exit__ method will be called if any exception would occur. This way it is possible to handle the situation of
3030
actual method ends by raising exception.
3131

3232
The example of tracer class implementation and factory function registration is given in [tracing_ext-demo.py](/docs/tracing_ext-demo.py)

0 commit comments

Comments
 (0)