diff --git a/.gitignore b/.gitignore index 82f9275..f2a0259 100644 --- a/.gitignore +++ b/.gitignore @@ -160,3 +160,6 @@ cython_debug/ # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ + +.DS_Store +test.ipynb \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..7d29425 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,18 @@ +[build-system] +requires = ["setuptools >= 61.0"] +build-backend = "setuptools.build_meta" + +[project] + +name = "itemattribute" +version = "0.0.1" +dependencies = [] +requires-python = ">= 3.7" +authors = [{name = "Andy Mounce", email = "ammounce@gmail.com"}] +description = "Python class that allows dict item syntax for attribute access" +readme = {file = "README.txt", content-type = "text/markdown"} +license = {file = "LICENSE"} + +[project.urls] +Homepage = "https://github.com/ammounce/itemattribute" +Issues = "https://github.com/ammounce/itemattribute/issues" \ No newline at end of file diff --git a/src/itemattribute/__init__.py b/src/itemattribute/__init__.py new file mode 100644 index 0000000..ddec175 --- /dev/null +++ b/src/itemattribute/__init__.py @@ -0,0 +1 @@ +from .item_attribute import ItemAttribute diff --git a/src/itemattribute/item_attribute.py b/src/itemattribute/item_attribute.py new file mode 100644 index 0000000..d300de2 --- /dev/null +++ b/src/itemattribute/item_attribute.py @@ -0,0 +1,49 @@ + + +class ItemAttribute(object): + ''' + Class that has properties which can be called like dictionary + items + + Parameters + ---------- + dictionary : dict + Dictionary object, defaults to None, contains initial + attributes for ItemAttribute instance. + + ''' + + def __init__(self, dictionary=None): + if dictionary is not None: + for k in dictionary.keys(): + self[k] = dictionary[k] + + __getitem__ = object.__getattribute__ + __setitem__ = object.__setattr__ + __delitem__ = object.__delattr__ + + def keys(self): + ''' + Returns a list of keys. + ''' + return self.__dict__.keys() + + def values(self): + ''' + Returns a list of values. + ''' + return self.__dict__.values() + + def items(self): + ''' + Returns a list of key:value pairs. + ''' + return self.__dict__.items() + + def __contains__(self, item): + ''' + Overloads the `key in object` syntax to check if + `key in obj.__dict__` + ''' + + return item in self.__dict__