Skip to content

Commit 3e6c6da

Browse files
committed
initial version of handling pandas method call
1 parent f930814 commit 3e6c6da

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

pandas_flavor/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
"""Version number."""
2-
__version__ = "0.3.0"
2+
__version__ = "0.4.0"

pandas_flavor/register.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from functools import wraps
22
from pandas.api.extensions import register_series_accessor, register_dataframe_accessor
3+
from . import stack_counter
34

5+
handle_pandas_method_call = None
46

57
def register_dataframe_method(method):
68
"""Register a function as a method attached to the Pandas DataFrame.
@@ -23,7 +25,15 @@ def __init__(self, pandas_obj):
2325

2426
@wraps(method)
2527
def __call__(self, *args, **kwargs):
26-
return method(self._obj, *args, **kwargs)
28+
with stack_counter.global_scf.get_sc() as sc:
29+
#ipdb.set_trace()
30+
ret = method(self._obj, *args, **kwargs)
31+
if sc.scf.level == 1:
32+
#print("pf dataframe __call__")
33+
global handle_pandas_method_call
34+
if handle_pandas_method_call:
35+
handle_pandas_method_call(self._obj, method.__name__, args, kwargs, ret)
36+
return ret
2737

2838
register_dataframe_accessor(method.__name__)(AccessorMethod)
2939

pandas_flavor/stack_counter.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class StackCounter:
2+
def __init__(self, scf):
3+
self.scf = scf
4+
5+
def __enter__(self):
6+
#print("StackCounter:__enter__", id(self))
7+
self.scf.level += 1
8+
return self
9+
10+
def __exit__(self, type, value, traceback):
11+
#print("StackCounter:__exit__", id(self))
12+
self.scf.level -= 1
13+
14+
class SCF:
15+
def __init__(self):
16+
self.level = 0
17+
18+
def get_sc(self):
19+
return StackCounter(self)
20+
21+
global_scf = SCF()

0 commit comments

Comments
 (0)