noahmorrison / chevron

A Python implementation of mustache
MIT License
480 stars 52 forks source link

Use getattr instead of _asdict/__getitem__ #87

Closed tekknolagi closed 3 years ago

tekknolagi commented 3 years ago

_asdict is not public API for namedtuple.

You may want to consider using the built-in string module, which implements this kind of .0/.foo formatting.

coveralls commented 3 years ago

Coverage Status

Coverage decreased (-0.01%) to 99.184% when pulling c450567d2626fe707725e8d57ebc64e75833b31d on tekknolagi:main into 4bc5648a4f7b9f07e7d7246d2131ba72379552e8 on noahmorrison:main.

tekknolagi commented 3 years ago

Oops, I forgot a test for changing behavior in this commit and you merged faster than I could amend my commit D: I'll file a subsequent PR.

noahmorrison commented 3 years ago

Ahh, thanks for catching that. That's obviously a much better way to handle it.

I'm not sure I understand your point about the string module?

tekknolagi commented 3 years ago

Kind of like this:

import string

class C:
    foo = "bar"

f = string.Formatter()
instance = C()
result = f.format("{0.foo}", instance)
assert result == "bar"

There's get_field and get_value, etc: https://docs.python.org/3.7/library/string.html#string.Formatter