[ANN] Announcing Jl2Py.jl: A minimal Julia-to-Python transpiler

Jl2Py.jl is a minimal package (roughly 400 LOC at the moment) that transpiles Julia code to Python.

Example (from LeetCode.jl):

function two_sum(nums::Vector{Int}, target::Int)::Union{Nothing,Tuple{Int,Int}}
    seen = Dict{Int,Int}()
    for (i, n) in enumerate(nums)
        m = target - n
        if haskey(seen, m)
            return seen[m], i
        else
            seen[n] = i
        end
    end
end

And the converted Python version:

def two_sum(nums: List[int], target: int, /) -> Union[None, Tuple[int, int]]:
    seen = {}
    for (i, n) in enumerate(nums):
        m = target - n
        if haskey(seen, m):
            return (seen[m], i)
        else:
            seen[n] = i

Note that some of Julia’s built-in functions need to be polyfilled, e.g., haskey() in the above example.

The package is kinda opinionated since there are many cases where a perfect equivalence cannot be found. I am happy to hear from the community more advice on where to improve.

5 Likes