Skip to content

Commit 3a959a8

Browse files
committed
introduced start and end method call phase explicitly
1 parent 69d45e1 commit 3a959a8

File tree

1 file changed

+22
-32
lines changed

1 file changed

+22
-32
lines changed

pandas_flavor/register.py

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,9 @@
11
import ipdb
22
from functools import wraps
3-
import inspect
43
from pandas.api.extensions import register_series_accessor, register_dataframe_accessor
54
from . import stack_counter
65

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
177

188
def register_dataframe_method(method):
199
"""Register a function as a method attached to the Pandas DataFrame.
@@ -35,25 +25,21 @@ def __init__(self, pandas_obj):
3525
self._obj = pandas_obj
3626

3727
@wraps(method)
38-
def __call__(self, *args, **orig_kwargs):
28+
def __call__(self, *args, **kwargs):
3929
with stack_counter.global_scf.get_sc() as sc:
4030
#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+
5038
ret = method(self._obj, *args, **kwargs)
5139

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+
5743
return ret
5844

5945
register_dataframe_accessor(method.__name__)(AccessorMethod)
@@ -76,13 +62,17 @@ def __init__(self, pandas_obj):
7662
@wraps(method)
7763
def __call__(self, *args, **kwargs):
7864
with stack_counter.global_scf.get_sc() as sc:
79-
ret = method(self._obj, *args, **kwargs)
65+
method_call_obj = None
8066
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+
8676
return ret
8777

8878
register_series_accessor(method.__name__)(AccessorMethod)

0 commit comments

Comments
 (0)