Parse Python3 code string to Julia akin to Numba/JIT compilers

Forgive me if I’m catching up here, but I just started with Julia about 4 months ago.
I hope this question falls into the tooling category.

I have a special interest parsing Python code and turning it into Julia, such that I can use pythons call to Julia to execute the code.

Question:

  1. Is anyone else working on this?
  2. Is there any technical obstacle that makes this pursuit long term infeasible?

Example for context:

import julia
import py2julia

# the python function I want to translate:
def sieve(n):
    """ the prime number sieve of Eratosthenes """
    if n % 2 == 0:
        return False
    return not any(n % i == 0 for i in range(3, int(n**(1/2))+1, 2))

sieve_jl = py2julia(sieve)  # write the file to sieve.jl respecting namespace hierarchy.

j = julia.Julia()
new_sieve = j.include(sieve_jl)
assert new_sieve(n=1299827) is True # sends the call to Julia for execution, returns the result to Python.

Dear @Bjorn_Madsen, welcome to the forum!
Do not hesitate in ask any question to the forum.

In my opinion, it is easy to call Python code from Julia (with PyCall) and to call Julia code from Python (using julia package).

I think there is not anone working in automatic translating code because although these syntax are similar, there are not the same, and a good Julia code (taking in account the performance) could implies changes from Python that are away from automatic translator. The effort in automatic translation of the code does not worth it, because later one should fix it, or change it to improve the performance (also, the API of the libraries are clearly different, making almost impossible to change it automatically). Julia is a new language, easy to learn, that in my opinion it deserves to be learnt.

Anyway, if you do not want to translate the Python code, you could see jit options (like numba).

Hi @dmolina
Thank you for the quick response and the welcome :slight_smile:
I like your suggestion - learn Julia instead - and will think about it.

You’re welcome.

As a example, the Julia version of your code should be:

function sieve(n)
           """ the prime number sieve of Eratosthenes """
           if n % 2 == 0
               return false 
           else
               return ! any(n % i == 0 for i in range(3, convert(Int, ceil(n^0.5)), step=2))
           end
       end

as you can see it is very similar, the changes were simple:

  • False => false.
  • the conversion: convert(Int, XX) instead of int(XX).
  • x**y => x^y
  • range use or step size, or length, so I add the keyword step.

It is a simple example that the code can be very similar, and the improvement in time is clear, in my computer:


%time sieve(1299827)                                                                                                                                                           
CPU times: user 166 µs, sys: 12 µs, total: 178 µs
Wall time: 185 µs

while in Julia it takes around 3 µs:

julia> @time sieve(1299827)
  0.000003 seconds
1 Like

See the FAQ.

3 Likes

I add my voice to those saying there is no actually big demand for such a tool, as Julia for a Python developer has a similar enough look to be directly accessible.
If you have a large app and can’t rewrite it all in Julia you can easily translate in Julia just the critical parts and then call it from the rest of your Python app:

In Julia:

ENV["PYTHON"] = "/path/to/python/you/want/to/use";
using Pkg;
Pkg.add("PyCall")

In shell/Python:

python3 -m pip install --user julia
import julia
julia.install() # only once
ij = julia.Julia(compiled_modules=False)
jl.include("myJuliafunctions.jl")
myoutput = jl.myJuliafunction(myArgs)
2 Likes

Thank you. I’m not sure why this didn’t come up while I was searching. I think all my keywords are in this article. Ha :slight_smile: Thanks.

It has been on my mind too. Why not simply re-write bottleneck functions and keep it as such? Good point. Thank you.