Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dot-notation for __getitem__ is not supported #35

Closed
vthriller opened this issue Mar 30, 2018 · 4 comments
Closed

Dot-notation for __getitem__ is not supported #35

vthriller opened this issue Mar 30, 2018 · 4 comments

Comments

@vthriller
Copy link

In [1]: foo = dict(bar = 'baz')

In [2]: from genshi.template import NewTextTemplate

In [3]: NewTextTemplate('${foo.bar}').generate(foo=foo).render()
Out[3]: 'baz'
In [4]: from kajiki import TextTemplate

In [5]: TextTemplate('${foo.bar}')(dict(foo=foo)).render()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-5-b774b6cc6e8e> in <module>()
----> 1 TextTemplate('${foo.bar}')(dict(foo=foo)).render()

…/venv/lib/python3.6/site-packages/kajiki/template.py in render(self)
    103     def render(self):
    104         """Render the template to a string."""
--> 105         return ''.join(self)
    106 
    107     def _gettext(self, s):

…/venv/lib/python3.6/site-packages/kajiki/template.py in __iter__(self)
     98         Here, ``chunk`` can be the computed expression result.
     99         """
--> 100         for chunk in self.__main__():
    101             yield str(chunk)
    102 

…/venv/lib/python3.6/site-packages/kajiki/util.py in __iter__(self)
     75 
     76     def __iter__(self):
---> 77         for x in self.iterator:
     78             if type(x) == flattener:
     79                 for xx in x:

<string> in __main__()

AttributeError: 'dict' object has no attribute 'bar'
@nandoflorestan
Copy link
Collaborator

Kajiki isn't supposed to implement everything that Genshi had. Kajiki is glad to drop a few of the misfeatures, especially those that made Genshi slow.

Now this feature here can be very convenient for the programmer, but the implementation would certainly cost something. I am not sure what is more valuable here.

I don't have the answer, I am just commenting like, don't expect Kajiki to be a clone of Genshi.

@vthriller
Copy link
Author

don't expect Kajiki to be a clone of Genshi.

I certainly don't, but re: cost, well, even Jinja (which is universally considered a fast templating engine) supports this, so I thought this is the feature that's worth porting.

In [5]: from jinja2 import Template

In [8]: Template('{{ foo.bar }}').render(foo=foo)
Out[8]: 'baz'

@amol-
Copy link
Collaborator

amol- commented Apr 6, 2018

Uhm, I'm vaguely against this proposal for the reason that Kajiki maps your code to pure python code, which I like as it guarantees me that the code I wrote will be the resulting code. Providing this would mean injecting a call in every attribute access (which apart slowing down things) would break the guarantee that we don't inject additional behaviours in user code.

@CastixGitHub
Copy link
Contributor

I don't like this feature neither, getitem and getattr are two separate things in python

In [1]: spam = dict(egg='Spam')

In [2]: spam['egg']
Out[2]: 'Spam'

In [3]: spam.egg
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-3-1f641a831589> in <module>()
----> 1 spam.egg

AttributeError: 'dict' object has no attribute 'egg'

In [4]: pippo = type('Pippo', (object, ), dict(pluto='topolino'))()

In [5]: pippo.pluto
Out[5]: 'topolino'

In [6]: pippo['pluto']
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-dc79750a7340> in <module>()
----> 1 pippo['pluto']

TypeError: 'Pippo' object is not subscriptable

You may be intrested in https://stackoverflow.com/a/23689767 anyway. It's not about kajiki

@amol- amol- closed this as completed May 31, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants