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.