Name | magicattr JSON |
Version |
0.1.6
JSON |
| download |
home_page | https://github.com/frmdstryr/magicattr |
Summary | A getattr and setattr that works on nested objects, lists, dicts, and any combination thereof without resorting to eval |
upload_time | 2022-01-25 16:56:47 |
maintainer | |
docs_url | None |
author | frmdstryr |
requires_python | |
license | MIT |
keywords |
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# Magicattr
[![status](https://github.com/frmdstryr/magicattr/actions/workflows/ci.yml/badge.svg)](https://github.com/frmdstryr/magicattr/actions)
[![codecov](https://codecov.io/gh/frmdstryr/magicattr/branch/master/graph/badge.svg)](https://codecov.io/gh/frmdstryr/magicattr)
A getattr and setattr that works on nested objects, lists,
dictionaries, and any combination thereof without resorting to eval.
It differs from getattr and setattr in that it retains the failure cause
instead of always raising an AttributeError.
### Example
Say we have a person class as follows:
```python
class Person:
settings = {
'autosave': True,
'style': {
'height': 30,
'width': 200
},
'themes': ['light', 'dark']
}
def __init__(self, name, age, friends):
self.name = name
self.age = age
self.friends = friends
bob = Person(name="Bob", age=31, friends=[])
jill = Person(name="Jill", age=29, friends=[bob])
jack = Person(name="Jack", age=28, friends=[bob, jill])
```
With magicattr we can do this
```python
# Nothing new
assert magicattr.get(bob, 'age') == 31
# Lists
assert magicattr.get(jill, 'friends[0].name') == 'Bob'
assert magicattr.get(jack, 'friends[-1].age') == 29
# Dict lookups
assert magicattr.get(jack, 'settings["style"]["width"]') == 200
# Combination of lookups
assert magicattr.get(jack, 'settings["themes"][-2]') == 'light'
assert magicattr.get(jack, 'friends[-1].settings["themes"][1]') == 'dark'
# Setattr
magicattr.set(bob, 'settings["style"]["width"]', 400)
assert magicattr.get(bob, 'settings["style"]["width"]') == 400
# Nested objects
magicattr.set(bob, 'friends', [jack, jill])
assert magicattr.get(jack, 'friends[0].friends[0]') == jack
magicattr.set(jill, 'friends[0].age', 32)
assert bob.age == 32
```
You can also delete like this too.
```python
# Deletion
magicattr.delete(jill, 'friends[0]')
assert len(jill.friends) == 0
magicattr.delete(jill, 'age')
assert not hasattr(jill, 'age')
magicattr.delete(bob, 'friends[0].age')
assert not hasattr(jack, 'age')
```
What if someone tries to mess with you?
```python
# Unsupported
with pytest.raises(NotImplementedError) as e:
magicattr.get(bob, 'friends[0+1]')
with pytest.raises(SyntaxError) as e:
magicattr.get(bob, 'friends[')
with pytest.raises(ValueError) as e:
magicattr.get(bob, 'friends = [1,1]')
# Nice try, function calls are not allowed
with pytest.raises(ValueError):
magicattr.get(bob, 'friends.pop(0)')
```
Did I miss anything? Let me know!
#### What it can't do?
Slicing, expressions, function calls, append/pop from lists, eval stuff, etc...
#### How does it work?
Parses the attr string into an ast node and manually evaluates it.
### Installing
`pip install magicattr`
### License
MIT
Hope it helps, cheers!
Raw data
{
"_id": null,
"home_page": "https://github.com/frmdstryr/magicattr",
"name": "magicattr",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "",
"author": "frmdstryr",
"author_email": "frmdstryr@gmail.com",
"download_url": "",
"platform": "",
"description": "# Magicattr\n\n[![status](https://github.com/frmdstryr/magicattr/actions/workflows/ci.yml/badge.svg)](https://github.com/frmdstryr/magicattr/actions)\n[![codecov](https://codecov.io/gh/frmdstryr/magicattr/branch/master/graph/badge.svg)](https://codecov.io/gh/frmdstryr/magicattr)\n\n\nA getattr and setattr that works on nested objects, lists,\ndictionaries, and any combination thereof without resorting to eval.\n\nIt differs from getattr and setattr in that it retains the failure cause\ninstead of always raising an AttributeError.\n\n\n\n### Example\n\nSay we have a person class as follows:\n\n```python\n\n\nclass Person:\n settings = {\n 'autosave': True,\n 'style': {\n 'height': 30,\n 'width': 200\n },\n 'themes': ['light', 'dark']\n }\n def __init__(self, name, age, friends):\n self.name = name\n self.age = age\n self.friends = friends\n\n\nbob = Person(name=\"Bob\", age=31, friends=[])\njill = Person(name=\"Jill\", age=29, friends=[bob])\njack = Person(name=\"Jack\", age=28, friends=[bob, jill])\n\n```\n\nWith magicattr we can do this\n\n```python\n\n# Nothing new\nassert magicattr.get(bob, 'age') == 31\n\n# Lists\nassert magicattr.get(jill, 'friends[0].name') == 'Bob'\nassert magicattr.get(jack, 'friends[-1].age') == 29\n\n# Dict lookups\nassert magicattr.get(jack, 'settings[\"style\"][\"width\"]') == 200\n\n# Combination of lookups\nassert magicattr.get(jack, 'settings[\"themes\"][-2]') == 'light'\nassert magicattr.get(jack, 'friends[-1].settings[\"themes\"][1]') == 'dark'\n\n# Setattr\nmagicattr.set(bob, 'settings[\"style\"][\"width\"]', 400)\nassert magicattr.get(bob, 'settings[\"style\"][\"width\"]') == 400\n\n# Nested objects\nmagicattr.set(bob, 'friends', [jack, jill])\nassert magicattr.get(jack, 'friends[0].friends[0]') == jack\n\nmagicattr.set(jill, 'friends[0].age', 32)\nassert bob.age == 32\n```\n\nYou can also delete like this too.\n\n```python\n\n# Deletion\nmagicattr.delete(jill, 'friends[0]')\nassert len(jill.friends) == 0\n\nmagicattr.delete(jill, 'age')\nassert not hasattr(jill, 'age')\n\nmagicattr.delete(bob, 'friends[0].age')\nassert not hasattr(jack, 'age')\n\n```\n\nWhat if someone tries to mess with you?\n\n```python\n\n# Unsupported\nwith pytest.raises(NotImplementedError) as e:\n magicattr.get(bob, 'friends[0+1]')\n\nwith pytest.raises(SyntaxError) as e:\n magicattr.get(bob, 'friends[')\n\nwith pytest.raises(ValueError) as e:\n magicattr.get(bob, 'friends = [1,1]')\n\n# Nice try, function calls are not allowed\nwith pytest.raises(ValueError):\n magicattr.get(bob, 'friends.pop(0)')\n\n```\n\nDid I miss anything? Let me know!\n\n\n\n#### What it can't do?\n\nSlicing, expressions, function calls, append/pop from lists, eval stuff, etc...\n\n#### How does it work?\n\nParses the attr string into an ast node and manually evaluates it.\n\n\n### Installing\n\n`pip install magicattr`\n\n\n### License\n\nMIT\n\nHope it helps, cheers!\n\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A getattr and setattr that works on nested objects, lists, dicts, and any combination thereof without resorting to eval",
"version": "0.1.6",
"project_urls": {
"Homepage": "https://github.com/frmdstryr/magicattr"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "2a7e76b7e0c391bee7e9273725c29c8fe41c4df62a215ce58aa8e3518baee0bb",
"md5": "66e1b94aa6bd099eec112aa3d934ebc8",
"sha256": "d96b18ee45b5ee83b09c17e15d3459a64de62d538808c2f71182777dd9dbbbdf"
},
"downloads": -1,
"filename": "magicattr-0.1.6-py2.py3-none-any.whl",
"has_sig": false,
"md5_digest": "66e1b94aa6bd099eec112aa3d934ebc8",
"packagetype": "bdist_wheel",
"python_version": "py2.py3",
"requires_python": null,
"size": 4664,
"upload_time": "2022-01-25T16:56:47",
"upload_time_iso_8601": "2022-01-25T16:56:47.074382Z",
"url": "https://files.pythonhosted.org/packages/2a/7e/76b7e0c391bee7e9273725c29c8fe41c4df62a215ce58aa8e3518baee0bb/magicattr-0.1.6-py2.py3-none-any.whl",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2022-01-25 16:56:47",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "frmdstryr",
"github_project": "magicattr",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "magicattr"
}