1
- import ipdb
2
1
from functools import wraps
3
2
from pandas .api .extensions import register_series_accessor , register_dataframe_accessor
4
3
import inspect
5
- from . import stack_counter
4
+ from contextlib import nullcontext
6
5
7
- start_method_call = None
6
+ cb_notify_dataframe_method_call = None
7
+ cb_notify_series_method_call = None
8
+ stack_counter_context = None
8
9
9
10
def register_dataframe_method (method ):
10
11
"""Register a function as a method attached to the Pandas DataFrame.
@@ -29,24 +30,18 @@ def __init__(self, pandas_obj):
29
30
30
31
@wraps (method )
31
32
def __call__ (self , * args , ** kwargs ):
32
- with stack_counter .global_scf .get_sc () as sc :
33
- #ipdb.set_trace()
34
-
33
+ global stack_counter_context_manager
34
+ with stack_counter_context .get_sc () if stack_counter_context else nullcontext () as sc :
35
35
method_call_obj = None
36
- global start_method_call
37
- stack_depth = sc .scf .level
38
- if stack_depth == 1 :
39
- if start_method_call :
40
- #ipdb.set_trace()
41
- method_call_obj = start_method_call (self ._obj , method .__name__ , method_signature , args , kwargs , stack_depth )
42
- ret = method (self ._obj , * args , ** kwargs )
43
- elif stack_depth == 2 and method .__name__ == 'copy' :
44
- #ipdb.set_trace()
45
- if start_method_call :
46
- method_call_obj = start_method_call (self ._obj , method .__name__ , method_signature , args , kwargs , stack_depth )
47
- ret = method (self ._obj , * args , ** kwargs )
48
- else :
49
- ret = method (self ._obj , * args , ** kwargs )
36
+ global cb_notify_dataframe_method_call
37
+ if cb_notify_dataframe_method_call :
38
+ stack_depth = sc .scf .level
39
+ method_call_obj = cb_notify_dataframe_method_call (self ._obj , method .__name__ , method_signature , args , kwargs , stack_depth )
40
+ if method_call_obj :
41
+ new_args , new_kwargs = method_call_obj .handle_start_method_call ()
42
+ args = new_args [1 :]; kwargs = new_kwargs
43
+
44
+ ret = method (self ._obj , * args , ** kwargs )
50
45
51
46
if method_call_obj :
52
47
method_call_obj .handle_end_method_call (ret )
@@ -74,13 +69,16 @@ def __init__(self, pandas_obj):
74
69
75
70
@wraps (method )
76
71
def __call__ (self , * args , ** kwargs ):
77
- with stack_counter .global_scf .get_sc () as sc :
72
+ global stack_counter_context_manager
73
+ with stack_counter_context .get_sc () if stack_counter_context else nullcontext () as sc :
78
74
method_call_obj = None
79
- stack_depth = sc .scf .level
80
- if stack_depth <= 2 :
81
- global start_method_call
82
- if start_method_call :
83
- method_call_obj = start_method_call (self ._obj , method .__name__ , method_signature , args , kwargs , stack_depth )
75
+ global cb_notify_series_method_call
76
+ if cb_notify_series_method_call :
77
+ stack_depth = sc .scf .level
78
+ method_call_obj = cb_notify_series_method_call (self ._obj , method .__name__ , method_signature , args , kwargs , stack_depth )
79
+ if method_call_obj :
80
+ new_args , new_kwargs = method_call_obj .handle_start_method_call ()
81
+ args = new_args [1 :]; kwargs = new_kwargs
84
82
85
83
ret = method (self ._obj , * args , ** kwargs )
86
84
0 commit comments