kajiki icon indicating copy to clipboard operation
kajiki copied to clipboard

Structural Pattern Matching

Open CastixGitHub opened this issue 1 year ago • 2 comments

Remake of #74 and #73 (just with squashed commits)


It would be cool if kajiki could generate code using PEP622 pattern matching when running on python3.10+

Benefits Speed

Motivation

I wanted to do something easy Directive Lookup

py:match was used by genshi, but it's free within kajiki py:case is used together with py:switch (and py:else) Directive Proposal

py:match (with on attribute) and py:case (with match attribute instead of value)

so kajiki have always been ahead of time :D

running the switch example I see the code generated is

In [5]: print(Template.py_text) class template: @kajiki.expose def main(): yield '

\n' yield self.kj.escape(i) yield local.kj.gettext(' is') yield ' ' local.kj.push_switch(i % 2) if False: pass elif local.kj.case(0): yield local.kj.gettext('even') else: yield local.kj.gettext('odd') local.kj.pop_switch() yield '
' template = kajiki.Template(template)

I see switches are pushed to a list to allow them to be nested. and case does the equality check

But of course due to the concepts behind it being different, the two approaches are incompatible.

I'm doing it.

I noticed after the dom is parsed with sax, it is reconstructed due to some optimization, i was loosing the attribute tough, so i added support to define tuples of attributes in the directives.

I also noticied the flattener does not flatten my deep stuff. But since the backtrace was just crazy, instead of fixing the flattener I made a bad patch for generate_python

ok, I got the loose of the attribute was caused because py:case can be used even within other tags. patched.

This is a squash of 14 commits:

tests and doc added too structural pattern matching added some tests for SPM patched py:case directive used as attribute, added doc for pep622 test fixes skip pep622 tests on python<3.10 one more test black reformatting, flake8 decected F401 i don't like isort follow @jackrosenthal review comment py:match on genshi migration guide rephrasing docs, adding newline to gitignore support for python 3.11.0b3 fixed? docs: Add table with summary of the how XML directives can be used docs: add py:match to directives summary added in PR #71

CastixGitHub avatar Aug 24 '22 10:08 CastixGitHub

Can you resolve the merge conflicts? Also, please split unrelated changes to a separate commit (e.g., .gitignore changes).

jackrosenthal avatar Aug 29 '22 22:08 jackrosenthal

You do not need to create a new PR for each iteration. Just push to this PR.

jackrosenthal avatar Aug 29 '22 22:08 jackrosenthal