Skip to content

Commit f8c2b19

Browse files
authored
refactor: remove lazy-loader dependency and simplify imports (#32)
- Removed the lazy-loader dependency from environment.yml and setup.py - Simplified the import statements in pandas_flavor/init.py by directly importing the required methods - Updated the all variable in pandas_flavor/init.py to include the imported methods - Cleaned up setup.py by removing unnecessary comments and adding docstrings to methods - Add docstrings to bring up docstring coverage - Update repo with darglint configuration file
1 parent 5b1ab54 commit f8c2b19

File tree

6 files changed

+96
-44
lines changed

6 files changed

+96
-44
lines changed

.darglint

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[darglint]
2+
docstring_style=google

docs/tracing_ext-demo.py

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,105 @@
1+
"""Tracing extension demo."""
2+
import time
3+
14
import pandas as pd
5+
26
import pandas_flavor as pf
3-
import time
47

58

69
@pf.register_dataframe_method
710
def my_method(df: pd.DataFrame) -> pd.DataFrame:
11+
"""Transpose the DataFrame.
12+
13+
Args:
14+
df: The DataFrame to transpose.
15+
16+
Returns:
17+
The transposed DataFrame.
18+
"""
819
print("my_method called")
920
return df.transpose()
1021

1122

1223
@pf.register_dataframe_method
1324
def another_method(df: pd.DataFrame, new_col_d) -> pd.DataFrame:
25+
"""Adds a new column to the DataFrame.
26+
27+
Args:
28+
df (DataFrame): The DataFrame to add the column to.
29+
new_col_d (dict): A dictionary of column names and values.
30+
31+
Returns:
32+
DataFrame: The DataFrame with the new column.
33+
"""
1434
print("another_method called")
1535
for col, v in new_col_d.items():
1636
df[col] = v
1737
return df
1838

1939

2040
class tracer:
41+
"""A simple tracer for method calls."""
42+
2143
@staticmethod
2244
def create_tracer(*args):
45+
"""Creates a tracer.
46+
47+
Args:
48+
*args: Variable length argument list for the tracer.
49+
50+
Returns:
51+
The created tracer.
52+
"""
2353
return tracer()
2454

2555
def __init__(self):
56+
"""Initialize the tracer."""
2657
self.method_name = None
2758
self.start_ts = None
2859
self.end_ts = None
2960

3061
def __enter__(self):
62+
"""Enter the tracer.
63+
64+
Returns:
65+
The tracer.
66+
"""
3167
return self
3268

3369
def handle_start_method_call(
3470
self, method_name, method_signature, method_args, method_kwagrs
3571
):
72+
"""Handle the start of a method call.
73+
74+
Args:
75+
method_name: The name of the method.
76+
method_signature: The signature of the method.
77+
method_args: The arguments of the method.
78+
method_kwagrs: The keyword arguments of the method.
79+
80+
Returns:
81+
The arguments and keyword arguments of the method.
82+
"""
3683
self.method_name = method_name
3784
self.start_ts = time.time()
3885
return method_args, method_kwagrs
3986

4087
def handle_end_method_call(self, ret):
88+
"""Handle the end of a method call.
89+
90+
Args:
91+
ret: The return value of the method.
92+
"""
4193
self.end_ts = time.time()
4294

4395
def __exit__(self, exc_type, value, traceback):
96+
"""Exit the tracer.
97+
98+
Args:
99+
exc_type: The type of the exception.
100+
value: The value of the exception.
101+
traceback: The traceback of the exception.
102+
"""
44103
call_dur = self.end_ts - self.start_ts
45104
print(f"method {self.method_name} took {call_dur} secs to execute")
46105

environment.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@ dependencies:
77
- xarray
88
- pip
99
- pre-commit
10-
- pytest # for tests
10+
- pytest # for tests
1111
- black # code formatting
1212
- twine # for uploading to PyPI
1313
- bump2version
1414
- build # for building package distributions
1515
- pip:
16-
- lazy-loader
1716
- tuna

pandas_flavor/__init__.py

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,20 @@
11
"""Top-level API for pandas-flavor."""
2-
import lazy_loader as lazy
3-
4-
__getattr__, __dir__, __all__ = lazy.attach(
5-
__name__,
6-
submod_attrs={
7-
"register": [
8-
"register_series_method",
9-
"register_series_accessor",
10-
"register_dataframe_method",
11-
"register_dataframe_accessor",
12-
],
13-
"xarray": [
14-
"register_xarray_dataarray_method",
15-
"register_xarray_dataset_method",
16-
],
17-
},
2+
from .register import (
3+
register_dataframe_accessor,
4+
register_dataframe_method,
5+
register_series_accessor,
6+
register_series_method,
7+
)
8+
from .xarray import (
9+
register_xarray_dataarray_method,
10+
register_xarray_dataset_method,
1811
)
19-
# from .register import (
20-
# register_series_method,
21-
# register_series_accessor,
22-
# register_dataframe_method,
23-
# register_dataframe_accessor,
24-
# )
25-
# from .xarray import (
26-
# register_xarray_dataarray_method,
27-
# register_xarray_dataset_method,
28-
# )
2912

30-
# __all__ = [
31-
# "register_series_method",
32-
# "register_series_accessor",
33-
# "register_dataframe_method",
34-
# "register_dataframe_accessor",
35-
# "register_xarray_dataarray_method",
36-
# "register_xarray_dataset_method",
37-
# ]
13+
__all__ = [
14+
"register_series_method",
15+
"register_series_accessor",
16+
"register_dataframe_method",
17+
"register_dataframe_accessor",
18+
"register_xarray_dataarray_method",
19+
"register_xarray_dataset_method",
20+
]

pandas_flavor/register.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"""Register functions as methods of Pandas DataFrame and Series."""
12
from functools import wraps
23
from pandas.api.extensions import (
34
register_series_accessor,
@@ -179,6 +180,16 @@ def register_series_method(method):
179180
method_signature = inspect.signature(method)
180181

181182
def inner(*args, **kwargs):
183+
"""Inner function to register the method.
184+
185+
Args:
186+
*args: The arguments to pass to the registered method.
187+
**kwargs: The keyword arguments to pass to the registered method.
188+
189+
Returns:
190+
method: The original method.
191+
"""
192+
182193
class AccessorMethod(object):
183194
"""Series Accessor method class."""
184195

setup.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
#!/usr/bin/env python
2-
# -*- coding: utf-8 -*-
3-
4-
# Note: To use the 'upload' functionality of this file, you must:
5-
# $ pip install twine
6-
1+
"""Setup script."""
72
import io
83
import os
94
import sys
@@ -19,7 +14,7 @@
1914
AUTHOR = "Zach Sailer"
2015

2116
# What packages are required for this module to be executed?
22-
REQUIRED = ["pandas>=0.23", "xarray", "lazy-loader>=0.1"]
17+
REQUIRED = ["pandas>=0.23", "xarray"]
2318

2419
# The rest you shouldn't have to touch too much :)
2520
# ------------------------------------------------
@@ -58,12 +53,15 @@ def status(s: str):
5853
print("\033[1m{0}\033[0m".format(s))
5954

6055
def initialize_options(self):
56+
"""Initialize options."""
6157
pass
6258

6359
def finalize_options(self):
60+
"""Finalize options."""
6461
pass
6562

6663
def run(self):
64+
"""Build and publish the package."""
6765
try:
6866
self.status("Removing previous builds…")
6967
rmtree(os.path.join(here, "dist"))

0 commit comments

Comments
 (0)