Skip to content

Latest commit

 

History

History
71 lines (60 loc) · 2.03 KB

README.org

File metadata and controls

71 lines (60 loc) · 2.03 KB

Opine

Opine is an OCaml library that unparses the python AST produced by pyre-ast library back to python source code.

Why would this be useful?

I find this to be useful to do large scale python code transformations in OCaml.

Can this be done in Python itself?

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.

How it works?

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.

Example transformation

If you wanted to add getters and setters to all the members in the class

dune exec examples -- module test.py

Input file

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)

Output file

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

How to run the unparser?

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

Credits

This library exists because of pyre-ast