Opine is an OCaml library that unparses the python AST produced by pyre-ast library back to python source code.
I find this to be useful to do large scale python code transformations in OCaml.
Yes. But if you are looking for an OCaml library to do this, you probably already know why you are looking. Maybe this will help. I needed something like this to maintain python source code transformations.
Fairly simple. The pyre-ast module provides a concrete AST representation of the python AST. The unparse module walks through the ast recursively while maintaining some state information and converts the AST back to python code. It doesn’t use the visitor pattern. Everything is pure, simple functions.
If you wanted to add getters and setters to all the members in the class
dune exec examples -- module test.py
class Sample():
def __init__(self, embed_dim, backend=None):
self.embed_dim = embed_dim
self.backend = backend
def forward(self, weight, value):
return torch.bmm(weight, value)
class Sample:
def __init__(self, embed_dim, backend=None):
self.embed_dim = embed_dim
self.backend = backend
def forward(self, weight, value):
return torch.bmm(weight, value)
@property
def embed_dim(self):
return self._embed_dim
@property
def backend(self):
return self._backend
@embed_dim.setter
def embed_dim(self, embed_dim):
self._embed_dim = embed_dim
@backend.setter
def backend(self, backend):
self._backend = backend
Pass a python file or module as input and have it generate the python ast and back.
dune exec bin/main.exe -- module expr.py
This library exists because of pyre-ast