Skip to content

Instantly share code, notes, and snippets.

@MegaIng
MegaIng / ast_transformer.py
Created December 27, 2023 22:59
Proof-of-concept `@ast_transformer` decorator that allows somewhat easy AST based transformation of functions
"""
Proof-of-concept `@ast_transformer` decorator that allows somewhat easy
AST based transformation of functions, even if those are deeply nested within
classes or other functions, while correctly dealing with closures.
Also tries to handle composition of transformers, however currently, they will
be applied multiple times, an extra time for each transformer above it.
The correct handling of closures is achieved by compiling the entire file
again, with the transformed AST integrated. Then the correct code object
is extracted and inserted into the function.
from __future__ import annotations
from functools import cache
# Solution for https://stackoverflow.com/questions/75807633
def countGoodArrays(arr):
MOD = 10 ** 9 + 7
zero_count = 0
last = None
@MegaIng
MegaIng / vanon1.py
Last active September 1, 2021 19:02 — forked from Drvanon/vanon1.py
LALR try for vanon (1)
from lark import Lark, UnexpectedToken, Token, UnexpectedCharacters
from lark.lexer import LexerThread, LexerState, LineCounter
grammar = r"""
start: _NL* _section+
_section: _HEADER _content* _HEADER
_HEADER: "--- SECTION ---" _NL
_content: number_list
| assign
| _other
from __future__ import annotations
from dataclasses import dataclass
from typing import List, NewType, Dict, Tuple
Player = NewType('Player', str)
@dataclass(frozen=True)
class Move: