1
1
import ipdb
2
2
from functools import wraps
3
- import inspect
4
3
from pandas .api .extensions import register_series_accessor , register_dataframe_accessor
5
4
from . import stack_counter
6
5
7
- handle_pandas_method_call = None
8
-
9
- class LambdaCall :
10
- def __init__ (self , func ):
11
- self .func = func
12
- self .ret = None
13
-
14
- def __call__ (self , * args , ** kwargs ):
15
- self .ret = self .func (* args , ** kwargs )
16
- return self .ret
6
+ start_method_call = None
17
7
18
8
def register_dataframe_method (method ):
19
9
"""Register a function as a method attached to the Pandas DataFrame.
@@ -35,25 +25,21 @@ def __init__(self, pandas_obj):
35
25
self ._obj = pandas_obj
36
26
37
27
@wraps (method )
38
- def __call__ (self , * args , ** orig_kwargs ):
28
+ def __call__ (self , * args , ** kwargs ):
39
29
with stack_counter .global_scf .get_sc () as sc :
40
30
#ipdb.set_trace()
41
- kwargs = {}
42
- for k , arg in orig_kwargs .items ():
43
- if method .__name__ == 'assign' and inspect .isfunction (arg ):
44
- print ('got got that' )
45
- #ipdb.set_trace()
46
- kwargs [k ] = LambdaCall (arg )
47
- else :
48
- kwargs [k ] = arg
49
-
31
+
32
+ method_call_obj = None
33
+ if sc .scf .level == 1 :
34
+ global start_method_call
35
+ if start_method_call :
36
+ method_call_obj = start_method_call (self ._obj , method .__name__ , args , kwargs )
37
+
50
38
ret = method (self ._obj , * args , ** kwargs )
51
39
52
- if sc .scf .level == 1 :
53
- #print("pf dataframe __call__")
54
- global handle_pandas_method_call
55
- if handle_pandas_method_call :
56
- handle_pandas_method_call (self ._obj , method .__name__ , args , kwargs , ret )
40
+ if method_call_obj :
41
+ method_call_obj .handle_end_method_call (ret )
42
+
57
43
return ret
58
44
59
45
register_dataframe_accessor (method .__name__ )(AccessorMethod )
@@ -76,13 +62,17 @@ def __init__(self, pandas_obj):
76
62
@wraps (method )
77
63
def __call__ (self , * args , ** kwargs ):
78
64
with stack_counter .global_scf .get_sc () as sc :
79
- ret = method ( self . _obj , * args , ** kwargs )
65
+ method_call_obj = None
80
66
if sc .scf .level <= 2 :
81
- #ipdb.set_trace()
82
- print ("pf series __call__" )
83
- global handle_pandas_method_call
84
- if handle_pandas_method_call :
85
- handle_pandas_method_call (self ._obj , method .__name__ , args , kwargs , ret )
67
+ global start_method_call
68
+ if start_method_call :
69
+ method_call_obj = start_method_call (self ._obj , method .__name__ , args , kwargs )
70
+
71
+ ret = method (self ._obj , * args , ** kwargs )
72
+
73
+ if method_call_obj :
74
+ method_call_obj .handle_end_method_call (ret )
75
+
86
76
return ret
87
77
88
78
register_series_accessor (method .__name__ )(AccessorMethod )
0 commit comments