Package: onelinerizer
Oneliner-izerConvert any Python file into a single line of code which has the same functionality.No newlines allowed. No semicolons allowed, either.Live demo atonelinepy.herokuapp.com!Presentation at PyCon 2016, andslide deck.User Installation and UsageInstall viapipfrom PyPI:$pipinstallonelinerizerUse either the command line function or the Python module:$echo"def f(x):\n    print x\nf(4)">sandbox.py
$onelinerizersandbox.py--debug
$onelinerizersandbox_ol.pyfromonelinerizerimportonelinerizeonelinerize("def f(x):\nprint x\nf(4)")ExamplesBefore:x=3y=4print(x<y<5)After:(lambda__builtin__:(lambda__print,__y,d:[[__print(d.x<d.y<5)ford.yin[(4)]][0]ford.xin[(3)]][0])(__builtin__.__dict__['print'],(lambdaf:(lambdax:x(x))(lambday:f(lambda*args:y(y)(*args)))),type('StateDict',(),__builtin__.__dict__)()))(__import__('__builtin__'))That line looks complicated, because we need some tricks to import the print function and to support certain tricks which are needed for more complicated features such aswhileandif. For a program as simple as this one, though, you can think of it as working this way:(lambdax:(lambday:print(x<y<5))(4))(3)Before:deff(x):returnx+5printf(13)After:(lambda__builtin__:(lambda__print,__y,d:[__print(d.f(13))ford.fin[(lambdax:[(d.x+5)ford.xin[(x)]][0])]][0])(__builtin__.__dict__['print'],(lambdaf:(lambdax:x(x))(lambday:f(lambda*args:y(y)(*args)))),type('StateDict',(),__builtin__.__dict__)()))(__import__('__builtin__'))...or, if you want to think about something more simplified:(lambdaf:print(f(13)))(lambdax:x+5)Before:defguess_my_number(n):whileTrue:user_input=raw_input("Enter a positive integer to guess: ")iflen(user_input)==0ornotuser_input.isdigit():print"Not a positive integer!"else:user_input=int(user_input)ifuser_input>n:print"Too big! Try again!"elifuser_input<n:print"Too small! Try again!"else:print"You win!"returnTrueguess_my_number(42)After:(lambda__builtin__:(lambda__print,__y,d:[(lambda___:None)(d.guess_my_number(42))ford.guess_my_numberin[(lambdan:[(__y(lambda__this:(lambdad:(lambda__after:[(lambda__after:(lambda___:__after(d))(__print('Not a positive integer!'))if(d.len(d.user_input)==0or(notd.user_input.isdigit()))else[(lambda__after:(lambda___:__after(d))(__print('Too big! Try again!'))ifd.user_input>d.nelse(lambda__after:(lambda___:__after(d))(__print('Too small! Try again!'))ifd.user_input<d.nelse(lambda___:d.True)(__print('You win!')))(lambdad:__after(d)))(lambdad:__after(d))ford.user_inputin[(d.int(d.user_input))]][0])(lambdad:__this(d))ford.user_inputin[(d.raw_input('Enter a positive integer to guess: '))]][0]ifd.Trueelse__after(d))(lambdad:None))))(d)ford.nin[(n)]][0])]][0])(__builtin__.__dict__['print'],(lambdaf:(lambdax:x(x))(lambday:f(lambda*args:y(y)(*args)))),type('StateDict',(),__builtin__.__dict__)()))(__import__('__builtin__'))FAQOh dear God why?Yeah. I'm sorry. But on the other hand, why not?Can't you use semicolons?That would be against the spirit of this exercise. Why pass up a perfectly good excuse to abuselambda functions,ternary expressions,list comprehensions, and even the occasionalY combinator? Never pass up an opportunity to use the Y combinator.AnalysisOutput program sizeO(n). No code is ever duplicated, so the one-lined code produced is linear in the size of the input code.Runtime of one-lined codeI have no reason to believe the resulting code, however absurd, is more than a constant factor slower than the original code. Since while loops, etc. are implemented using recursive function calls, causing more overhead for setting up those function calls, the constant factor is likely to be somewhat bad.TipsThe one-lined code tends to contain many nested lambdas; if there are too many, Python will refuse to run it.$pythonmain_ol.py
s_push:parserstackoverflow
MemoryErrorThis can be fixed using pypy.$pypymain_ol.pyHowever, since while loops and for loops are implemented with recursion, you might encountermaximum recursion depth exceedederrors during runtime if your loops go on for too long.To get around this, you can putimportsyssys.setrecursionlimit(new_limit)in your original Python code. (onelinerizer will not place this command in for you.)Not Implementedfrom foo import *Open ProblemswithyieldDeveloper Installation and Testing$gitclonehttps://github.com/csvoss/onelinerizer
$cdonelinerizer
$python-monelinerizer.setup.pysetup.py
$pythonsetup.pytestTo install the local module:$pipinstall.Further ReadingLambda calculusFixed-point combinatorStackOverflowpost about how functional programming helps with Python masteryOn writing Python one-liners- a similar ideaShout-out to the author ofthis StackOverflow post, who seems to have worked on a very similar project