polydojo/dotsi

Feature request: Object wrapper

TobiSan5 opened this issue · 1 comments

I read the following in the Jinja2 documentation, and thought about this project. Could it be useful to include an access-agnostic object wrapper?

"
For the sake of convenience, foo.bar in Jinja2 does the following things on the Python layer:

check for an attribute called bar on foo (getattr(foo, 'bar'))

if there is not, check for an item 'bar' in foo (foo.getitem('bar'))

if there is not, return an undefined object.

foo['bar'] works mostly the same with a small difference in sequence:

check for an item 'bar' in foo. (foo.getitem('bar'))

if there is not, check for an attribute called bar on foo. (getattr(foo, 'bar'))

if there is not, return an undefined object.

"

Hi @TobiSan5,

Thank you for thinking of Dotsi! And thank you for creating this issue.

To make dot-access work, Dotsi internally implements __getattr__ (and __setattr__). As a result, Dotsi should already be fully compatible with Jinja2. Consider:

>>> import dotsi
>>> d = dotsi.Dict({"foo": {"bar": "baz"}})
>>> getattr(d, 'foo') # Same as d.foo
{'bar': 'baz'}
getattr(getattr(d, 'foo'), 'bar') # Same a d.foo.bar
'baz'
>>> 

I think you should be able to use Dotsi with Jinja2 out of the box, without any issues at all. If you run into a specific problem, please submit an issue here, and I'll be happy to help. As no specific shortcoming/problem is mentioned here, I'll close this issue for now.