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

**URL:** https://discourse.julialang.org/t/parse-python3-code-string-to-julia-akin-to-numba-jit-compilers/38231
**Category:** Tooling
**Tags:** question
**Created:** [April 26, 2020, 10:44am UTC](https://discourse.julialang.org/t/parse-python3-code-string-to-julia-akin-to-numba-jit-compilers/38231 "2020-04-26T10:44:14Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Bjorn\_Madsen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bjorn_madsen/32/11752_2.png) [@Bjorn\_Madsen](https://discourse.julialang.org/u/Bjorn_Madsen)
#### Post date: [April 26, 2020, 10:44am UTC](https://discourse.julialang.org/t/parse-python3-code-string-to-julia-akin-to-numba-jit-compilers/38231/1 "2020-04-26T10:44:14Z")

</div>

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:

```python
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.

```

---

<div class="post-metadata">

### Author: ![dmolina](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmolina/32/5246_2.png) [@dmolina](https://discourse.julialang.org/u/dmolina)
#### Post date: [April 26, 2020, 12:36pm UTC](https://discourse.julialang.org/t/parse-python3-code-string-to-julia-akin-to-numba-jit-compilers/38231/2 "2020-04-26T12:36:45Z")

</div>

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).

---

<div class="post-metadata">

### Author: ![Bjorn\_Madsen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bjorn_madsen/32/11752_2.png) [@Bjorn\_Madsen](https://discourse.julialang.org/u/Bjorn_Madsen)
#### Post date: [April 26, 2020, 12:46pm UTC](https://discourse.julialang.org/t/parse-python3-code-string-to-julia-akin-to-numba-jit-compilers/38231/3 "2020-04-26T12:46:19Z")

</div>

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

---

<div class="post-metadata">

### Author: ![dmolina](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmolina/32/5246_2.png) [@dmolina](https://discourse.julialang.org/u/dmolina)
#### Post date: [April 26, 2020, 1:01pm UTC](https://discourse.julialang.org/t/parse-python3-code-string-to-julia-akin-to-numba-jit-compilers/38231/4 "2020-04-26T13:01:14Z")

</div>

You’re welcome.

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

```julia
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:

```python

%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
julia> @time sieve(1299827)
  0.000003 seconds

```

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [April 26, 2020, 2:08pm UTC](https://discourse.julialang.org/t/parse-python3-code-string-to-julia-akin-to-numba-jit-compilers/38231/5 "2020-04-26T14:08:09Z")

</div>

> [@Bjorn\_Madsen](#):
>
> 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.

See [the FAQ](https://docs.julialang.org/en/v1/manual/faq/#Why-don't-you-compile-Matlab/Python/R/%E2%80%A6-code-to-Julia?-1).

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [April 27, 2020, 5:03am UTC](https://discourse.julialang.org/t/parse-python3-code-string-to-julia-akin-to-numba-jit-compilers/38231/6 "2020-04-27T05:03:07Z")

</div>

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:

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

```

In shell/Python:

```julia
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)

```

---

<div class="post-metadata">

### Author: ![Bjorn\_Madsen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bjorn_madsen/32/11752_2.png) [@Bjorn\_Madsen](https://discourse.julialang.org/u/Bjorn_Madsen)
#### Post date: [April 27, 2020, 6:43am UTC](https://discourse.julialang.org/t/parse-python3-code-string-to-julia-akin-to-numba-jit-compilers/38231/8 "2020-04-27T06:43:26Z")

</div>

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 🙂 Thanks.

---

<div class="post-metadata">

### Author: ![Bjorn\_Madsen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bjorn_madsen/32/11752_2.png) [@Bjorn\_Madsen](https://discourse.julialang.org/u/Bjorn_Madsen)
#### Post date: [April 27, 2020, 6:45am UTC](https://discourse.julialang.org/t/parse-python3-code-string-to-julia-akin-to-numba-jit-compilers/38231/9 "2020-04-27T06:45:41Z")

</div>

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