diff --git a/pirrtools/__init__.py b/pirrtools/__init__.py index c2fe443..3350a0b 100644 --- a/pirrtools/__init__.py +++ b/pirrtools/__init__.py @@ -90,7 +90,7 @@ def __get_pirc_file(): return None -def __load_pirc_file(): +def __load_pirc_file(verbose=False): """Loads the `.pirc` module from the `.pirc.py` file in the home directory and adds the specified paths to the system path.""" pirc_file = __get_pirc_file() @@ -98,19 +98,21 @@ def __load_pirc_file(): spec = __importlib_util.spec_from_file_location("pirc", pirc_file) pirc = __importlib_util.module_from_spec(spec) spec.loader.exec_module(pirc) - print(f"Loaded {pirc_file.stem} module from {pirc_file}") + if verbose: + print(f"Loaded {pirc_file.stem} module from {pirc_file}") if hasattr(pirc, "mypaths"): for path in pirc.mypaths: - addpath(path, verbose=True) + addpath(path, verbose=verbose) -def __load_matplotlib_inline(): +def __load_matplotlib_inline(verbose=False): """Loads the '%matplotlib inline' magic command in IPython if available.""" try: ipython = get_ipython() if ipython: ipython.run_line_magic("matplotlib", "inline") - print("Loaded '%matplotlib inline'") + if verbose: + print("Loaded '%matplotlib inline'") except ImportError: pass diff --git a/pirrtools/pandas.py b/pirrtools/pandas.py index f1bdeae..6d4b922 100644 --- a/pirrtools/pandas.py +++ b/pirrtools/pandas.py @@ -242,3 +242,35 @@ def to_cache(self, *args, **kwargs): reg_df("pirr")(UtilsAccessor) reg_ser("pirr")(UtilsAccessor) + + +def create_class(level: int): + class I_N: + def __init__(self, pandas_obj): + self._validate(pandas_obj) + self._obj = pandas_obj + self._grp = self._obj.groupby(level=level) + + @staticmethod + def _validate(obj): + if not isinstance(obj, (pd.DataFrame, pd.Series)): + raise AttributeError("The object must be a pandas DataFrame or Series.") + if level + 1 > obj.index.nlevels: + raise AttributeError( + f"The object must have at least {level + 1} index level(s)." + ) + + def __getattr__(self, name): + return getattr(self._grp, name) + + def __dir__(self): + return dir(self._grp) + + I_N.__name__ = f"I{level}" + return I_N + + +for i in range(0, 2): + cls = create_class(i) + reg_df(cls.__name__.lower())(cls) + reg_ser(cls.__name__.lower())(cls) diff --git a/pyproject.toml b/pyproject.toml index 749e2da..8d1baa9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "pirrtools" -version = "0.2.12" # Update this version number before you upload a new version to PyPI +version = "0.2.13" # Update this version number before you upload a new version to PyPI description = "Collection of tools I use in my projects" readme = "README.md" license = { text = "MIT" } diff --git a/setup.py b/setup.py index 7658ce2..ee5d86b 100644 --- a/setup.py +++ b/setup.py @@ -10,7 +10,7 @@ setup( name="pirrtools", - version="0.2.12", # Update this version number before releasing a new version + version="0.2.13", # Update this version number before releasing a new version description="Collection of tools I use in my projects", author="Sean Smith", author_email="pirsquared.pirr@gmail.com",