# \[ANN\] Opine 0.1.0 - Python source code transformations in OCaml

**URL:** <https://discuss.ocaml.org/t/ann-opine-0-1-0-python-source-code-transformations-in-ocaml/11365>\
**Category:** Community\
**Tags:** announce\
**Created:** [February 8, 2023, 6:35am UTC](https://discuss.ocaml.org/t/ann-opine-0-1-0-python-source-code-transformations-in-ocaml/11365 "2023-02-08T06:35:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arul](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/arul/32/1604_2.png) [@Arul](https://discuss.ocaml.org/u/Arul)\
**Post date:** [February 8, 2023, 6:35am UTC](https://discuss.ocaml.org/t/ann-opine-0-1-0-python-source-code-transformations-in-ocaml/11365/1 "2023-02-08T06:35:27Z")

</div>

[pyre-ast](https://github.com/grievejia/pyre-ast) is an OCaml library that provides python AST representation in OCaml.

I had a need to do python source code transformations in OCaml. I made [Opine](https://github.com/ArulselvanMadhavan/opine) to do transformations on the AST and convert it back to python. So, in OCaml, you can write python code now, if you ever need to.

P.S. It still doesn’t support the entire python syntax 100%. I haven’t added support for things like for-comprehension but it should be fairly easy to add it. I plan to add support for the missing ones as I need it for my projects.

---

<div class="post-metadata">

**Author:** ![gasche](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/gasche/32/4_2.png) [@gasche](https://discuss.ocaml.org/u/gasche)\
**Post date:** [February 8, 2023, 8:13pm UTC](https://discuss.ocaml.org/t/ann-opine-0-1-0-python-source-code-transformations-in-ocaml/11365/2 "2023-02-08T20:13:29Z")

</div>

Out of curiosity I looked at the code randomly and I see

> <https://github.com/ArulselvanMadhavan/opine/blob/ddf60ebb2b162586171023089399c263a2f70f41/lib/unparse.ml#L98>

It looks like you are building a large string by concatenating many small strings using the `( ^ )` operator, which has quadratic complexity. Unless I am missing something, this is wrong and you should be using buffers instead.

---

<div class="post-metadata">

**Author:** ![yawaramin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yawaramin/32/3384_2.png) [@yawaramin](https://discuss.ocaml.org/u/yawaramin)\
**Post date:** [February 8, 2023, 8:16pm UTC](https://discuss.ocaml.org/t/ann-opine-0-1-0-python-source-code-transformations-in-ocaml/11365/3 "2023-02-08T20:16:30Z")

</div>

Looks like it’s not on opam?

---

<div class="post-metadata">

**Author:** ![Arul](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/arul/32/1604_2.png) [@Arul](https://discuss.ocaml.org/u/Arul)\
**Post date:** [February 8, 2023, 9:56pm UTC](https://discuss.ocaml.org/t/ann-opine-0-1-0-python-source-code-transformations-in-ocaml/11365/4 "2023-02-08T21:56:19Z")

</div>

You are right. I didn’t have a performance requirement when I was putting this together as proof of concept for people at work. But now, I think it’s better to change the implementation to use buffers

---

<div class="post-metadata">

**Author:** ![Arul](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/arul/32/1604_2.png) [@Arul](https://discuss.ocaml.org/u/Arul)\
**Post date:** [February 8, 2023, 9:57pm UTC](https://discuss.ocaml.org/t/ann-opine-0-1-0-python-source-code-transformations-in-ocaml/11365/5 "2023-02-08T21:57:27Z")

</div>

> <https://github.com/ocaml/opam-repository/pull/23258>
>
> \### \`opine.0.1.3\`
> Python AST unparse implementation in OCaml
> Opine is an OCaml… library that unparses the python AST back to python code.
> 
> 
> 
> \---
> \* Homepage: https://github.com/ArulselvanMadhavan/opine
> \* Source repo: git+https://github.com/ArulselvanMadhavan/opine.git
> \* Bug tracker: https://github.com/ArulselvanMadhavan/opine/issues
> 
> \---
> :camel: Pull-request generated by opam-publish v2.2.0

I have a PR request to opam-repository. It’s been failing tests. I’m working my way through issues there. But I hope to have it on opam soon!

---

<div class="post-metadata">

**Author:** ![Arul](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/arul/32/1604_2.png) [@Arul](https://discuss.ocaml.org/u/Arul)\
**Post date:** [February 8, 2023, 11:49pm UTC](https://discuss.ocaml.org/t/ann-opine-0-1-0-python-source-code-transformations-in-ocaml/11365/6 "2023-02-08T23:49:05Z")

</div>

> <https://github.com/ArulselvanMadhavan/opine/pull/2/files>
>
> Change internal state representation to use Buffers as opposed to vanilla string…s

Thanks for the feedback! I modified the logic to use buffers as opposed to using string concatenation. I will run some tests and cut a new version
