Skip to content

Commit

Permalink
Added a structures section to house use data structures and subclasses
Browse files Browse the repository at this point in the history
  • Loading branch information
pi-r-squared committed Jul 22, 2024
1 parent 0ecb1ab commit 22d9e6d
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 5 deletions.
20 changes: 17 additions & 3 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
Copyright (c) 2024, Sean S. Smith

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
4 changes: 4 additions & 0 deletions pirrtools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
- __load_matplotlib_inline(): Loads the '%matplotlib inline' magic command in IPython if
available.
Classes:
- AttrDict: A dictionary-like object that allows attribute access to its items.
Usage:
- To add a path to the system path, use the addpath() function.
- To reload a module or class, use the reload_entity() function.
Expand All @@ -30,6 +33,7 @@
import types as __types
from IPython import get_ipython
from .pandas import load_cache
from .structures import AttrDict


__HOME = __pathlib.Path.home().absolute()
Expand Down
4 changes: 4 additions & 0 deletions pirrtools/structures/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"""Moduel for structures.
"""

from .attrdict import AttrDict
50 changes: 50 additions & 0 deletions pirrtools/structures/attrdict.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""A dictionary that allows access to its keys as attributes."""


class AttrDict(dict):
"""A dictionary that allows access to its keys as attributes.
Example:
>>> d = AttrDict({'a': 1, 'b': 2})
>>> d.a
1
"""

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Convert nested dictionaries to AttrDict instances
for key, value in self.items():
if isinstance(value, dict):
self[key] = AttrDict(value)

def __getattr__(self, item):
try:
return super().__getitem__(item)
except KeyError as exc:
raise AttributeError(
f"'AttrDict' object has no attribute '{item}'"
) from exc

def __setattr__(self, key, value):
self[key] = value

def __delattr__(self, item):
try:
del self[item]
except KeyError as exc:
raise AttributeError(
f"'AttrDict' object has no attribute '{item}'"
) from exc

def __dir__(self):
return list(self.keys()) + super().__dir__()

def __getitem__(self, key):
return super().setdefault(key, type(self)())

def __setitem__(self, key, value):
if isinstance(value, dict):
value = type(self)(value)
super().__setitem__(key, value)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "pirrtools"
version = "0.2.4" # Update this version number before you upload a new version to PyPI
version = "0.2.5" # 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" }
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

setup(
name="pirrtools",
version="0.2.4", # Update this version number before releasing a new version
version="0.2.5", # Update this version number before releasing a new version
description="Collection of tools I use in my projects",
author="Sean Smith",
author_email="[email protected]",
Expand Down

0 comments on commit 22d9e6d

Please sign in to comment.