Skip to content

Commit 0b9e0da

Browse files
committed
single callback to create method call object which is also with context, getting simpler code
1 parent e6821c1 commit 0b9e0da

File tree

1 file changed

+12
-20
lines changed

1 file changed

+12
-20
lines changed

pandas_flavor/register.py

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
import inspect
44
from contextlib import nullcontext
55

6-
cb_notify_dataframe_method_call = None
7-
cb_notify_series_method_call = None
86
cb_create_call_stack_context_manager = None
97

108
def register_dataframe_method(method):
@@ -31,18 +29,15 @@ def __init__(self, pandas_obj):
3129
@wraps(method)
3230
def __call__(self, *args, **kwargs):
3331
global cb_create_call_stack_context_manager
34-
with cb_create_call_stack_context_manager(method.__name__) if cb_create_call_stack_context_manager else nullcontext():
35-
method_call_obj = None
36-
global cb_notify_dataframe_method_call
37-
if cb_notify_dataframe_method_call:
38-
method_call_obj = cb_notify_dataframe_method_call(self._obj, method.__name__, method_signature, args, kwargs)
39-
if method_call_obj:
40-
new_args, new_kwargs = method_call_obj.handle_start_method_call()
41-
args = new_args[1:]; kwargs = new_kwargs
32+
method_call_obj = cb_create_call_stack_context_manager(method.__name__) if cb_create_call_stack_context_manager else nullcontext()
33+
with method_call_obj:
34+
if not isinstance(method_call_obj, nullcontext):
35+
new_args, new_kwargs = method_call_obj.handle_start_method_call(self._obj, method.__name__, method_signature, args, kwargs)
36+
args = new_args[1:]; kwargs = new_kwargs
4237

4338
ret = method(self._obj, *args, **kwargs)
4439

45-
if method_call_obj:
40+
if not isinstance(method_call_obj, nullcontext):
4641
method_call_obj.handle_end_method_call(ret)
4742

4843
return ret
@@ -69,18 +64,15 @@ def __init__(self, pandas_obj):
6964
@wraps(method)
7065
def __call__(self, *args, **kwargs):
7166
global cb_create_call_stack_context_manager
72-
with cb_create_call_stack_context_manager(method.__name__) if cb_create_call_stack_context_manager else nullcontext():
73-
method_call_obj = None
74-
global cb_notify_series_method_call
75-
if cb_notify_series_method_call:
76-
method_call_obj = cb_notify_series_method_call(self._obj, method.__name__, method_signature, args, kwargs)
77-
if method_call_obj:
78-
new_args, new_kwargs = method_call_obj.handle_start_method_call()
79-
args = new_args[1:]; kwargs = new_kwargs
67+
method_call_obj = cb_create_call_stack_context_manager(method.__name__) if cb_create_call_stack_context_manager else nullcontext()
68+
with method_call_obj:
69+
if not isinstance(method_call_obj, nullcontext):
70+
new_args, new_kwargs = method_call_obj.handle_start_method_call(self._obj, method.__name__, method_signature, args, kwargs)
71+
args = new_args[1:]; kwargs = new_kwargs
8072

8173
ret = method(self._obj, *args, **kwargs)
8274

83-
if method_call_obj:
75+
if not isinstance(method_call_obj, nullcontext):
8476
method_call_obj.handle_end_method_call(ret)
8577

8678
return ret

0 commit comments

Comments
 (0)