Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
ammounce committed Sep 3, 2024
1 parent ce61a60 commit a839008
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 18 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]"}]
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"
1 change: 1 addition & 0 deletions src/itemattribute/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .item_attribute import ItemAttribute
49 changes: 49 additions & 0 deletions src/itemattribute/item_attribute.py
Original file line number Diff line number Diff line change
@@ -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__

0 comments on commit a839008

Please sign in to comment.