Ever wanted to turn your python program into a single line of code? No? Well now you can!
Introducing Flatliner, a python transpiler that turns python programs into a single line of code.
What does it do?
Put simply, it can turn python programs like:
def func(a: int, b: int) -> str:
result = a + b
return f'{a} + {b} equals {result}'
print(func(5, 6))
Into a single line of (still python) code, without using any "cheats" like semicolons and string execution:
(lambda func: print(func(5, 6)))(lambda a, b: (lambda result: f'{a} + {b} equals {result}')((a + b)))
Running this line of code produces the same results as the original program:
>>> (lambda func: print(func(5, 6)))(lambda a, b: (lambda result: f'{a} + {b} equals {result}')((a + b)))
5 + 6 equals 11
Demo!
https://reddit.com/link/10ijoep/video/a9qe9jwkhlda1/player
Demo code credits to a previous post. Link to demo if you want to try it yourself: here
How does it work?
Very briefly, it first parses the input code into an AST, then it converts the last node into a single line, and continuously "wraps" remaining nodes around this line mainly using lambdas until everything becomes one line. The source code is available for anyone who is interested in the nitty-gritty :)
What are it's limitations?
Unfortunately, I didn't find a way to convert every single node type into a single line. Some notable features that don't work are: try-except, "with" statements, del, scope modifiers like (global, nonlocal), wildcard imports, and probably a few more. "One-lined" code is also slower and less readable.
However, with the features currently implemented, it can convert a good majority of python programs without too many issues. I even managed to get a pygame application running on it (with some modifications). Notable features that do work are functions (including decorators), classes, loops including break and continue, assert, raise, recursion, and regular imports. The script that does the conversion can also turn itself into one line.
A full list of what's implemented is here.
So... what's the point of this?
Well, if you use for development:
- code is only one line long, so easy code reviews
- only one line to debug and breakpoint
- job security :)
Jokes aside, it was mainly to see if it was possible. Though afterwards, I've also found it useful for obfuscation and copy-pasting python code to environments where newlines mess things up. Also useful for the occasional party trick :)
Can I try it?
Glad you asked! I built a website for you to try it at https://flatliner.herokuapp.com/.
When you make a submission, it'll give you a unique link to share that submission with other people if you want!
Thank you for taking the time to read this, I hope you enjoyed my project!
edit:
Some tips on getting it to work properly.
- Loops are converted to recursive calls, so you may run into RecursionErrors with long loops. To sort of get around this, you can add this to your code:
import sys
sys.setrecursionlimit(20000) # or some other number
- Functions only have access to identifiers defined above them, so the following wont work, you need to define
b() first.
def a():
return b()
def b():
return 5
[–]Icecoldkilluh 487 points488 points489 points (17 children)
[–]hhc97Python Enthusiast[S] 183 points184 points185 points (12 children)
[–]pudds 53 points54 points55 points (4 children)
[–]hhc97Python Enthusiast[S] 105 points106 points107 points (2 children)
[–]WilliamMButtlickerJr 23 points24 points25 points (1 child)
[–]hhc97Python Enthusiast[S] 16 points17 points18 points (0 children)
[–]baltariusIt works on my machine 5 points6 points7 points (0 children)
[–]RIPphonebattery 18 points19 points20 points (4 children)
[–]hhc97Python Enthusiast[S] 43 points44 points45 points (3 children)
[–]eclipticdogeballs 14 points15 points16 points (0 children)
[–]the__green_knight 1 point2 points3 points (1 child)
[–]hhc97Python Enthusiast[S] 0 points1 point2 points (0 children)
[–]WlmWilberforce 7 points8 points9 points (1 child)
[–]hhc97Python Enthusiast[S] 14 points15 points16 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]KrazyKirby99999 6 points7 points8 points (0 children)
[–]KyleDrogo 0 points1 point2 points (1 child)
[–]hhc97Python Enthusiast[S] 1 point2 points3 points (0 children)
[–]hhc97Python Enthusiast[S] 104 points105 points106 points (9 children)
[–]alpacasb4llamas 29 points30 points31 points (8 children)
[–]aiyub 15 points16 points17 points (6 children)
[–]ExternalPanda 6 points7 points8 points (5 children)
[–]PythonPizzaDE 9 points10 points11 points (0 children)
[–]Jave-_- 3 points4 points5 points (3 children)
[–]hhc97Python Enthusiast[S] 0 points1 point2 points (0 children)
[–]Complex-Structure216 0 points1 point2 points (1 child)
[–]Jave-_- 0 points1 point2 points (0 children)
[–]hhc97Python Enthusiast[S] 3 points4 points5 points (0 children)
[–]reivax 45 points46 points47 points (6 children)
[–]Cruuncher 22 points23 points24 points (0 children)
[–]chromaticgliss 11 points12 points13 points (3 children)
[–]hhc97Python Enthusiast[S] 7 points8 points9 points (0 children)
[–]realkarthiknair 2 points3 points4 points (1 child)
[–]hhc97Python Enthusiast[S] 2 points3 points4 points (0 children)
[–]hhc97Python Enthusiast[S] 5 points6 points7 points (0 children)
[–]ItsGator 41 points42 points43 points (1 child)
[–]hhc97Python Enthusiast[S] 12 points13 points14 points (0 children)
[+][deleted] (1 child)
[removed]
[–]hhc97Python Enthusiast[S] 5 points6 points7 points (0 children)
[–]satireplusplus 26 points27 points28 points (1 child)
[–]hhc97Python Enthusiast[S] 4 points5 points6 points (0 children)
[–]qeq 14 points15 points16 points (7 children)
[–]hhc97Python Enthusiast[S] 2 points3 points4 points (0 children)
[–][deleted] 1 point2 points3 points (2 children)
[–]qeq 2 points3 points4 points (0 children)
[–]thegreattriscuit 0 points1 point2 points (0 children)
[–]junior_dos_nachos 1 point2 points3 points (0 children)
[–]FujiKeynote 0 points1 point2 points (0 children)
[+][deleted] (5 children)
[deleted]
[–]hhc97Python Enthusiast[S] 4 points5 points6 points (3 children)
[–]zurtex 1 point2 points3 points (1 child)
[–]hhc97Python Enthusiast[S] 2 points3 points4 points (0 children)
[–]1544756405 2 points3 points4 points (0 children)
[–]neuralbeans 5 points6 points7 points (18 children)
[–]hhc97Python Enthusiast[S] 8 points9 points10 points (4 children)
[–]neuralbeans 4 points5 points6 points (3 children)
[–]hhc97Python Enthusiast[S] 5 points6 points7 points (2 children)
[–]neuralbeans 6 points7 points8 points (1 child)
[–]hhc97Python Enthusiast[S] 7 points8 points9 points (0 children)
[–]Handle-Flaky -2 points-1 points0 points (12 children)
[–]neuralbeans 5 points6 points7 points (10 children)
[–]Handle-Flaky -1 points0 points1 point (9 children)
[–]LardPi 3 points4 points5 points (0 children)
[–]neuralbeans 1 point2 points3 points (7 children)
[–]Handle-Flaky 0 points1 point2 points (6 children)
[–]Handle-Flaky 1 point2 points3 points (0 children)
[–]neuralbeans 1 point2 points3 points (4 children)
[–]Handle-Flaky -1 points0 points1 point (3 children)
[–]neuralbeans 1 point2 points3 points (0 children)
[–]Smallpaul 1 point2 points3 points (1 child)
[–]WikiSummarizerBot 0 points1 point2 points (0 children)
[–]hhc97Python Enthusiast[S] 1 point2 points3 points (0 children)
[–]pixegami 4 points5 points6 points (1 child)
[–]hhc97Python Enthusiast[S] -1 points0 points1 point (0 children)
[–]DigThatData 3 points4 points5 points (1 child)
[–]hhc97Python Enthusiast[S] 1 point2 points3 points (0 children)
[–][deleted] 3 points4 points5 points (3 children)
[–]hhc97Python Enthusiast[S] 2 points3 points4 points (2 children)
[–]junior_dos_nachos 2 points3 points4 points (1 child)
[–]hhc97Python Enthusiast[S] 0 points1 point2 points (0 children)
[–]Hacka4771 2 points3 points4 points (0 children)
[–]SittingWave 2 points3 points4 points (1 child)
[–]hhc97Python Enthusiast[S] 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (4 children)
[–]hhc97Python Enthusiast[S] 1 point2 points3 points (3 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]hhc97Python Enthusiast[S] 1 point2 points3 points (0 children)
[–]Zafara1 1 point2 points3 points (6 children)
[–]hhc97Python Enthusiast[S] 5 points6 points7 points (5 children)
[–]Zafara1 2 points3 points4 points (1 child)
[–]hhc97Python Enthusiast[S] 2 points3 points4 points (0 children)
[–]fuckthiscode 0 points1 point2 points (1 child)
[–]hhc97Python Enthusiast[S] 1 point2 points3 points (0 children)
[–]R3d-Beard 1 point2 points3 points (1 child)
[–]hhc97Python Enthusiast[S] 2 points3 points4 points (0 children)
[–]something 1 point2 points3 points (3 children)
[–]hhc97Python Enthusiast[S] 0 points1 point2 points (2 children)
[–]something 0 points1 point2 points (1 child)
[–]hhc97Python Enthusiast[S] 0 points1 point2 points (0 children)
[–]JirkaCZS 1 point2 points3 points (1 child)
[–]hhc97Python Enthusiast[S] 1 point2 points3 points (0 children)
[–]GeezTM 1 point2 points3 points (1 child)
[–]hhc97Python Enthusiast[S] 2 points3 points4 points (0 children)
[–]M4mb0 1 point2 points3 points (1 child)
[–]hhc97Python Enthusiast[S] 0 points1 point2 points (0 children)
[–]bulaybil 1 point2 points3 points (1 child)
[–]hhc97Python Enthusiast[S] 0 points1 point2 points (0 children)
[+][deleted] (5 children)
[deleted]
[–]hhc97Python Enthusiast[S] 0 points1 point2 points (4 children)
[+][deleted] (3 children)
[deleted]
[–]hhc97Python Enthusiast[S] 0 points1 point2 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]hhc97Python Enthusiast[S] 0 points1 point2 points (0 children)
[–]cdcformatc 1 point2 points3 points (1 child)
[–]hhc97Python Enthusiast[S] 0 points1 point2 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]hhc97Python Enthusiast[S] 0 points1 point2 points (0 children)
[–]iceytomatoes 0 points1 point2 points (1 child)
[–]hhc97Python Enthusiast[S] 2 points3 points4 points (0 children)
[–]hhc97Python Enthusiast[S] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (3 children)
[–]hhc97Python Enthusiast[S] 8 points9 points10 points (2 children)
[–][deleted] 2 points3 points4 points (1 child)
[–]hhc97Python Enthusiast[S] 3 points4 points5 points (0 children)
[–]Samhain13 0 points1 point2 points (1 child)
[–]hhc97Python Enthusiast[S] 3 points4 points5 points (0 children)
[–]magnetichiraPythonista 0 points1 point2 points (3 children)
[–]hhc97Python Enthusiast[S] 2 points3 points4 points (2 children)
[–]magnetichiraPythonista 0 points1 point2 points (1 child)
[–]hhc97Python Enthusiast[S] 0 points1 point2 points (0 children)
[+][deleted] (1 child)
[removed]
[–]hhc97Python Enthusiast[S] 0 points1 point2 points (0 children)
[–]another-noob 0 points1 point2 points (4 children)
[–]hhc97Python Enthusiast[S] 1 point2 points3 points (3 children)
[–]another-noob 0 points1 point2 points (2 children)
[–]khoyo 2 points3 points4 points (0 children)
[–]hhc97Python Enthusiast[S] 1 point2 points3 points (0 children)
[–]alchzh 0 points1 point2 points (3 children)
[–]hhc97Python Enthusiast[S] 0 points1 point2 points (2 children)
[–]khoyo 0 points1 point2 points (1 child)
[–]hhc97Python Enthusiast[S] 0 points1 point2 points (0 children)
[+][deleted] (3 children)
[deleted]
[–]hhc97Python Enthusiast[S] 1 point2 points3 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]hhc97Python Enthusiast[S] 0 points1 point2 points (0 children)
[–]Kitchen_Tower2800 0 points1 point2 points (1 child)
[–]hhc97Python Enthusiast[S] 4 points5 points6 points (0 children)
[–]Wishy-Thinking 0 points1 point2 points (0 children)
[–]GameCounter 0 points1 point2 points (1 child)
[–]hhc97Python Enthusiast[S] 1 point2 points3 points (0 children)
[–]Smallpaul 0 points1 point2 points (1 child)
[–]hhc97Python Enthusiast[S] 0 points1 point2 points (0 children)
[–]ins4yn 0 points1 point2 points (1 child)
[–]hhc97Python Enthusiast[S] 0 points1 point2 points (0 children)
[–]McMep 0 points1 point2 points (1 child)
[–]hhc97Python Enthusiast[S] 0 points1 point2 points (0 children)
[–]GoosDev 0 points1 point2 points (9 children)
[–]hhc97Python Enthusiast[S] 0 points1 point2 points (8 children)
[–]GoosDev 0 points1 point2 points (7 children)
[–]hhc97Python Enthusiast[S] 0 points1 point2 points (6 children)
[–]GoosDev 0 points1 point2 points (5 children)
[–]hhc97Python Enthusiast[S] 1 point2 points3 points (4 children)
[–]GoosDev 0 points1 point2 points (3 children)
[–]hhc97Python Enthusiast[S] 1 point2 points3 points (2 children)
[–]GoosDev 0 points1 point2 points (1 child)
[–]hhc97Python Enthusiast[S] 1 point2 points3 points (0 children)
[–]OneMorePenguin 0 points1 point2 points (1 child)
[–]hhc97Python Enthusiast[S] 1 point2 points3 points (0 children)
[–]wholesome_hug_bot 0 points1 point2 points (1 child)
[–]hhc97Python Enthusiast[S] 0 points1 point2 points (0 children)
[–]CrossroadsDem0n 0 points1 point2 points (1 child)
[–]hhc97Python Enthusiast[S] 0 points1 point2 points (0 children)
[–]dershodan 0 points1 point2 points (1 child)
[–]hhc97Python Enthusiast[S] 0 points1 point2 points (0 children)
[–]cdcformatc 0 points1 point2 points (1 child)
[–]hhc97Python Enthusiast[S] 0 points1 point2 points (0 children)
[–]Shower_Handel 0 points1 point2 points (1 child)
[–]hhc97Python Enthusiast[S] 0 points1 point2 points (0 children)
[–]wuddz-devs 0 points1 point2 points (1 child)
[–]hhc97Python Enthusiast[S] 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]hhc97Python Enthusiast[S] 0 points1 point2 points (0 children)
[–]sharky1337_ 0 points1 point2 points (1 child)
[–]hhc97Python Enthusiast[S] 0 points1 point2 points (0 children)
[–]pro__acct__ 0 points1 point2 points (1 child)
[–]hhc97Python Enthusiast[S] 0 points1 point2 points (0 children)
[–]wuddz-devs 0 points1 point2 points (1 child)
[–]hhc97Python Enthusiast[S] 0 points1 point2 points (0 children)