Skip to content

Commit 1c625ab

Browse files
committed
moved logic from pandas_flavor
1 parent 88c17ee commit 1c625ab

File tree

2 files changed

+24
-26
lines changed

2 files changed

+24
-26
lines changed

pandas_flavor/register.py

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import ipdb
21
from functools import wraps
32
from pandas.api.extensions import register_series_accessor, register_dataframe_accessor
43
import inspect
5-
from . import stack_counter
4+
from contextlib import nullcontext
65

7-
start_method_call = None
6+
cb_notify_dataframe_method_call = None
7+
cb_notify_series_method_call = None
8+
stack_counter_context = None
89

910
def register_dataframe_method(method):
1011
"""Register a function as a method attached to the Pandas DataFrame.
@@ -29,24 +30,18 @@ def __init__(self, pandas_obj):
2930

3031
@wraps(method)
3132
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:
3535
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)
5045

5146
if method_call_obj:
5247
method_call_obj.handle_end_method_call(ret)
@@ -74,13 +69,16 @@ def __init__(self, pandas_obj):
7469

7570
@wraps(method)
7671
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:
7874
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
8482

8583
ret = method(self._obj, *args, **kwargs)
8684

File renamed without changes.

0 commit comments

Comments
 (0)