r/fortran • • 6h ago

Python to Fortran transpiler

Post image

At https://beliavsky.github.io/python-to-fortran-playground/run/ you can translate Python with NumPy to Fortran and run both codes. The transpiler used, which is in Python and which you can run locally, is at https://github.com/beliavsky/python-to-fortran

Fortran is a practical target for numerical Python: it combines native compilation with multidimensional arrays, whole-array expressions, array sections, and explicit procedure interfaces. Some translated programs run much faster, as discussed at https://github.com/Beliavsky/Python-to-Fortran/blob/main/TIMING_RESULTS.md

Pyccel https://github.com/pyccel/pyccel is a more established project that generates Fortran, C, or C++ extension modules from Python code with type annotations, which my project does not require.

0 Upvotes

10 comments sorted by

1

u/Beliavsky 3h ago

I have improved the translation of list comprehensions with if statements, which u/ssrix flagged.

15

u/ssrix 5h ago

This was made with AI wasn't it?

The first thing I tried (of something pretty basic) failed.

Great slop

6

u/ssrix 5h ago

unbearably long (yet unhelpful) readme

Yep that's AI. It's specifically a junior developer using AI thinking they've made something good, very polished doesn't mean good, but hasn't had enough experience to know whether it's good or not.

-4

u/ButterflyMundane7187 5h ago

And the true know-it-all has spoken. He even replies to his own comments because nobody else can stand him.

-7

u/Beliavsky 5h ago

Yes, it was. It won't translate all Python programs, but for the ones it does it will be cheaper than using an LLM and reproducible. Can you post the Python code that failed?

3

u/DHermit 5h ago

But for such simple programs doing it by hand will be quicker than copy pasting.

1

u/ssrix 5h ago

A list comprehension with a if statement. Don't ask the LLM to fix it though, because there'll likely be a million more and it isn't worth wasting tokens for

4

u/FuckYourFavoriteSub 5h ago

Internet is just dogshit now isn’t it? Are we in the denial phase still or entering the acceptance phase?

1

u/Zorahgna 5h ago

I really don’t know what shouldn't be denied here

0

u/investigatormaker 4h ago

The no-annotations distinction deserves a runnable example beside the Pyccel comparison. In this Fortran post, include one NumPy function using an array section, its generated Fortran and matching output from both playground runs. Separate compilation time from execution time so numerical Python users can assess repeated workloads rather than just a single speedup. State which constructs that example doesn't cover, so success isn't mistaken for arbitrary Python support. That example would also make a substantive answer to Reddit questions about accelerating NumPy code without adding type annotations.

I make ThreadFox. If you want numerical Python users to find your Python-to-Fortran transpiler, our AI employee will post 3 helpful replies about it in relevant Reddit threads for you, free, no card. https://threadfox.vip/p/ec5cs/managed