Skip to content

Commit

Permalink
Merge pull request #262 from jacebrowning/dict-attributes
Browse files Browse the repository at this point in the history
Add support for accessing Dict keys as attributes
  • Loading branch information
jacebrowning committed Apr 17, 2022
2 parents 54117f6 + b8a4c16 commit 02da937
Show file tree
Hide file tree
Showing 10 changed files with 653 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release Notes

## 1.4 (beta)

- Added support for accessing `Dict` keys as attributes.

## 1.3 (2022-04-09)

- Added support for paths relative to the user's home directory.
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ ci: format check test mkdocs ## Run all tasks that determine CI status
dev: install ## Continuously run all CI tasks when files chanage
poetry run ptw

.PHONY: shell
shell: install
poetry run ipython --ipython-dir=notebooks

.PHONY: demo
demo: install
poetry run nbstripout notebooks/*.ipynb
Expand Down
12 changes: 12 additions & 0 deletions datafiles/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ def to_yaml(cls, representer, node):
class Dict(dict):
"""Patchable `dict` type."""

def __getattr__(self, name):
try:
return self[name]
except KeyError:
return super().__getattribute__(name)

def __setattr__(self, name, value):
if name == "datafile":
super().__setattr__(name, value)
else:
self[name] = value

@classmethod
def to_yaml(cls, representer, node):
return representer.represent_dict(node)
2 changes: 1 addition & 1 deletion docs/types/containers.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ from typing import Dict, Optional
| `foobar: Dict[str, int]` | `foobar = None` | `foobar: {}` |
| `foobar: Optional[Dict[str, int]]` | `foobar = None` | `foobar:` |

_Schema enforcement is not available with the `Dict` annotation._
_⚠ Schema enforcement is not available with the `Dict` annotation._

## Dataclasses

Expand Down
1 change: 1 addition & 0 deletions notebooks/profile_default/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.sqlite
Loading

0 comments on commit 02da937

Please sign in to comment.