[ANN] CrudePythonTranslator

Introducing CrudePythonTranslator, a tool to help with porting Python code to Julia.

It’s crude in the sense that the output most of the time will be a mix of Python and Julia which can’t be run. It also doesn’t save you from needing to know both languages, nor does it help with redesigning the code for Julia. Additionally it’s much too crude to employ any large language model magic.

What it does do:

  • It reduces the amount of typing you have to do for a literal port. In particular it eliminates the most repetitive parts of the typing.

  • It automates the error prone step of converting indentation-marked blocks into end-delimited blocks.

  • It retains comments, docstrings, and formatting.

  • It supports adding custom translation rules which are appropriate for your code base.

A minimal example from the tutorial to give an idea of what it does. The Python function

import numpy as np
# Sample function
def tutorial(x, mode):
    '''
    Sample tutorial function.
    '''
    if mode == 'max':
        # Maximum!
        return np.max(x)
    elif mode == 'min':
        return np.min(x)

is out of the box translated to

import numpy as np
# Sample function
"""
Sample tutorial function.
"""
function tutorial(x, mode)
    if mode == "max"
        # Maximum!
        return np.max(x)
    elseif mode == "min"
        return np.min(x)
    end
end

which can then be fixed up with manual editing, or if your code base contains many similar occurrences, by adding translation rules.

See the tutorial for more details on that example. You can read about how the package works and how to write custom translation rules. If you want to help making it more useful for more people, you can contribute to the library of translation rules.

27 Likes

Thanks! Wish I would have had this package a few weeks ago that would have saved me quite some time :slight_smile:
I was wondering, does it do anything about converting to one-based arrays?
I assume for many use cases it would probably enough to replace every np.array with an OffsetArray though.

Not out of the box, no. Since it’s working at the syntactical level of tokens it’s difficult to reliably reason about indexing.

That said, you can often create custom rules that do things you need for your particular code base. E.g. if you happen to have lots of occurrences of foo[...] and you want all of them to become foo[1 + ...], you can add simple_rule("foo[", "foo[1 + ") to your translate call. If you need a more flexible rule you can use the Rule constructor or even write your own function which analyzes and modifies the tokens directly. It’s all a trade-off between how much work you spend on writing rules and how much you save on manual editing.

2 Likes