csvoss / onelinerizer Goto Github PK
View Code? Open in Web Editor NEWShamelessly convert any Python 2 script into a terrible single line of code
Home Page: http://www.onelinerizer.com/
License: MIT License
Shamelessly convert any Python 2 script into a terrible single line of code
Home Page: http://www.onelinerizer.com/
License: MIT License
As a start, so that we can oneline main.py without too much trouble.
At least in python2 you can replace exec with execfile and some tempfile trickery. In python3 (which I notice isn't planned / supported yet) you can just use exec directly.
website is down?
$ python main.py
def noop():
return
noop()
Traceback (most recent call last):
File "main.py", line 810, in <module>
onelined = to_one_line(original)
File "main.py", line 777, in to_one_line
return get_init_code(t, table)
File "main.py", line 71, in get_init_code
output = Namespace(table).many_to_one(tree.body)
File "main.py", line 215, in many_to_one
T('{after}')).format(after=after)
File "main.py", line 213, in <lambda>
lambda ctx, tree: ctx.format(after=self.visit(tree)),
File "/usr/lib/python2.7/ast.py", line 241, in visit
return visitor(node)
File "main.py", line 488, in visit_FunctionDef
body = ns.many_to_one(tree.body).format(pre_return='', post_return='')
File "main.py", line 215, in many_to_one
T('{after}')).format(after=after)
File "main.py", line 213, in <lambda>
lambda ctx, tree: ctx.format(after=self.visit(tree)),
File "/usr/lib/python2.7/ast.py", line 241, in visit
return visitor(node)
File "main.py", line 637, in visit_Return
return T('{pre_return}{}{post_return}').format(self.visit(tree.value))
File "/usr/lib/python2.7/ast.py", line 241, in visit
return visitor(node)
File "main.py", line 757, in generic_visit
raise NotImplementedError('Case not caught: %s' % str(type(tree)))
NotImplementedError: Case not caught: <type 'NoneType'>
Extra information in case it matters:
$ git log | head
commit 0f9661bbdf624f16ed5c3eb6fae0f8a18e135524
Author: Chelsea Voss <[email protected]>
Date: Sun May 29 23:14:14 2016 -0400
Clarify runtime comments in README
commit 588b839b162c3fb6c70f31f0d394fbdab34d920c
Author: Chelsea Voss <[email protected]>
Date: Sun May 29 23:05:28 2016 -0400
$ python --version
Python 2.7.12
Possible fix (is this a hack or a fix?): According to the python 2 docs, None
is substituted when the expression is not present. So would it be good if Onelinerizer detects missing return expression and change the expression to None
automagically?
If this is based on On Writing Python One-liners, then I think you should do the proper credits in the README.
If it isn't, then I still think it would be worth mentioning.
Hi,
Great oneliner generator.
Anyways, when running what is generated under python3, am getting
ModuleNotFoundError: No module named 'builtin'
I think this needs to be modded to 'import builtins' instead of 'import __builtin' in the code generation.
Regards,
Chai
Didn't expect this to work, but might want to add it to the list of features not supported:
locals()['y'] = 1
print y
Cool project!
root@375204ecfbb3:/oneliner# ./unittest.sh
E.E..EE.E.......EE.EEEEEEE...EE....E
======================================================================
ERROR: test_11lines (runtests.TestOneLine)
----------------------------------------------------------------------
Traceback (most recent call last):
File "runtests.py", line 20, in new_test
capture_exec(onelined),
File "runtests.py", line 43, in capture_exec
raise type(e)(code_string + '\n\n' + exc)
NameError: (lambda __builtin__: (lambda __y, __print: (lambda REFRAIN: (lambda bottles_of_beer: (__y(lambda __this: (lambda (USED_VARS): (lambda __after: (lambda ___: (lambda bottles_of_beer: __this((USED_VARS)))(bottles_of_beer-1))(__print((REFRAIN%(bottles_of_beer,bottles_of_beer,(bottles_of_beer-1))))) if bottles_of_beer>1 else __after((MODIFIED_VARS)))(lambda (MODIFIED_VARS): None))))((USED_VARS)))(99))('\n%d bottles of beer on the wall,\n%d bottles of beer,\ntake one down, pass it around,\n%d bottles of beer on the wall!\n'))((lambda f: (lambda x: x(x))(lambda y: f(lambda *args: y(y)(*args)))), __builtin__.__dict__['print']))(__import__('__builtin__'))
Traceback (most recent call last):
File "runtests.py", line 39, in capture_exec
exec code_string in namespace
File "<string>", line 1, in <module>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
NameError: global name 'USED_VARS' is not defined
======================================================================
ERROR: test_16lines (runtests.TestOneLine)
----------------------------------------------------------------------
Traceback (most recent call last):
File "runtests.py", line 20, in new_test
capture_exec(onelined),
File "runtests.py", line 43, in capture_exec
raise type(e)(code_string + '\n\n' + exc)
NameError: (lambda __builtin__: (lambda __y, __print: (lambda csv: (lambda writer: (lambda ___: (lambda stocks: (lambda status_labels: (lambda (MODIFIED_VARS): None)(reduce((lambda (MOD_VARS_USED), (ticker,name,price,change,pct): (lambda status: (lambda ___: (MODIFIED_VARS))(__print(('%s is %s (%s%%)'%(name,status,pct)))))(status_labels[cmp(float(change),0.0)])), stocks, (MOD_VARS_USED))))({-1:'down',0:'unchanged',1:'up'}))(csv.reader(open('stocks.csv','rb'))))(writer.writerows([('GOOG','Google, Inc.',505.24,0.47,0.09),('YHOO','Yahoo! Inc.',27.38,0.33,1.22),('CNET','CNET Networks, Inc.',8.62,-0.13,-1.49)])))(csv.writer(open('stocks.csv','wb',buffering=0))))(__import__('csv')))((lambda f: (lambda x: x(x))(lambda y: f(lambda *args: y(y)(*args)))), __builtin__.__dict__['print']))(__import__('__builtin__'))
Traceback (most recent call last):
File "runtests.py", line 39, in capture_exec
exec code_string in namespace
File "<string>", line 1, in <module>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
NameError: global name 'MOD_VARS_USED' is not defined
======================================================================
ERROR: test_3lines (runtests.TestOneLine)
----------------------------------------------------------------------
Traceback (most recent call last):
File "runtests.py", line 20, in new_test
capture_exec(onelined),
File "runtests.py", line 43, in capture_exec
raise type(e)(code_string + '\n\n' + exc)
NameError: (lambda __builtin__: (lambda __y, __print: (lambda friends: (lambda (MODIFIED_VARS): None)(reduce((lambda (MOD_VARS_USED), (i,name): (lambda ___: (MODIFIED_VARS))(__print('iteration {iteration} is {name}'.format(iteration=i,name=name)))), enumerate(friends), (MOD_VARS_USED))))(['john','pat','gary','michael']))((lambda f: (lambda x: x(x))(lambda y: f(lambda *args: y(y)(*args)))), __builtin__.__dict__['print']))(__import__('__builtin__'))
Traceback (most recent call last):
File "runtests.py", line 39, in capture_exec
exec code_string in namespace
File "<string>", line 1, in <module>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
NameError: global name 'MOD_VARS_USED' is not defined
======================================================================
ERROR: test_4lines (runtests.TestOneLine)
----------------------------------------------------------------------
Traceback (most recent call last):
File "runtests.py", line 20, in new_test
capture_exec(onelined),
File "runtests.py", line 43, in capture_exec
raise type(e)(code_string + '\n\n' + exc)
NameError: (lambda __builtin__: (lambda __y, __print: (lambda (parents,babies): (__y(lambda __this: (lambda (USED_VARS): (lambda __after: (lambda ___: (lambda (parents,babies): __this((USED_VARS)))((babies,(parents+babies))))(__print('This generation has {0} babies'.format(babies))) if babies<100 else __after((MODIFIED_VARS)))(lambda (MODIFIED_VARS): None))))((USED_VARS)))((1,1)))((lambda f: (lambda x: x(x))(lambda y: f(lambda *args: y(y)(*args)))), __builtin__.__dict__['print']))(__import__('__builtin__'))
Traceback (most recent call last):
File "runtests.py", line 39, in capture_exec
exec code_string in namespace
File "<string>", line 1, in <module>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
NameError: global name 'USED_VARS' is not defined
======================================================================
ERROR: test_6lines (runtests.TestOneLine)
----------------------------------------------------------------------
Traceback (most recent call last):
File "runtests.py", line 20, in new_test
capture_exec(onelined),
File "runtests.py", line 43, in capture_exec
raise type(e)(code_string + '\n\n' + exc)
NameError: (lambda __builtin__: (lambda __y, __print: (lambda re: (lambda (MODIFIED_VARS): None)(reduce((lambda (MOD_VARS_USED), test_string: (lambda __after: (lambda ___: __after((MODIFIED_VARS)))(__print(test_string,'is a valid US local phone number')) if re.match('^\\d{3}-\\d{4}$',test_string) else (lambda ___: __after((MODIFIED_VARS)))(__print(test_string,'rejected')))(lambda (MODIFIED_VARS): (MODIFIED_VARS))), ['555-1212','ILL-EGAL'], (MOD_VARS_USED))))(__import__('re')))((lambda f: (lambda x: x(x))(lambda y: f(lambda *args: y(y)(*args)))), __builtin__.__dict__['print']))(__import__('__builtin__'))
Traceback (most recent call last):
File "runtests.py", line 39, in capture_exec
exec code_string in namespace
File "<string>", line 1, in <module>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
NameError: global name 'MOD_VARS_USED' is not defined
======================================================================
ERROR: test_fibonacci (runtests.TestOneLine)
----------------------------------------------------------------------
Traceback (most recent call last):
File "runtests.py", line 20, in new_test
capture_exec(onelined),
File "runtests.py", line 43, in capture_exec
raise type(e)(code_string + '\n\n' + exc)
NameError: (lambda __builtin__: (lambda __y, __print: (lambda x: (lambda y: (lambda counter: (__y(lambda __this: (lambda (USED_VARS): (lambda __after: (lambda counter: (lambda (x,y): (lambda ___: __this((USED_VARS)))(__print(x)))((y,(x+y))))(counter+1) if counter<15 else __after((MODIFIED_VARS)))(lambda (MODIFIED_VARS): None))))((USED_VARS)))(0))(1))(0))((lambda f: (lambda x: x(x))(lambda y: f(lambda *args: y(y)(*args)))), __builtin__.__dict__['print']))(__import__('__builtin__'))
Traceback (most recent call last):
File "runtests.py", line 39, in capture_exec
exec code_string in namespace
File "<string>", line 1, in <module>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
NameError: global name 'USED_VARS' is not defined
======================================================================
ERROR: test_for (runtests.TestOneLine)
----------------------------------------------------------------------
Traceback (most recent call last):
File "runtests.py", line 20, in new_test
capture_exec(onelined),
File "runtests.py", line 43, in capture_exec
raise type(e)(code_string + '\n\n' + exc)
NameError: (lambda __builtin__: (lambda __y, __print: (lambda x: (lambda (MODIFIED_VARS): __print(x))(reduce((lambda (MOD_VARS_USED), i: (lambda x: (MODIFIED_VARS))((x+i))), range(5), (MOD_VARS_USED))))(5))((lambda f: (lambda x: x(x))(lambda y: f(lambda *args: y(y)(*args)))), __builtin__.__dict__['print']))(__import__('__builtin__'))
Traceback (most recent call last):
File "runtests.py", line 39, in capture_exec
exec code_string in namespace
File "<string>", line 1, in <module>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
NameError: global name 'MOD_VARS_USED' is not defined
======================================================================
ERROR: test_guess_my_number (runtests.TestOneLine)
----------------------------------------------------------------------
Traceback (most recent call last):
File "runtests.py", line 20, in new_test
capture_exec(onelined),
File "runtests.py", line 43, in capture_exec
raise type(e)(code_string + '\n\n' + exc)
NameError: (lambda __builtin__: (lambda __y, __print: (lambda guess_my_number: (lambda ___: None)(guess_my_number(42)))(lambda n:(__y(lambda __this: (lambda (USED_VARS): (lambda __after: (lambda user_input: (lambda __after: (lambda ___: __after((MODIFIED_VARS)))(__print('Not a positive integer!')) if (len(user_input)==0 or (not user_input.isdigit())) else (lambda user_input: (lambda __after: (lambda ___: __after((MODIFIED_VARS)))(__print('Too big! Try again!')) if user_input>n else (lambda __after: (lambda ___: __after((MODIFIED_VARS)))(__print('Too small! Try again!')) if user_input<n else (lambda ___: True)(__print('You win!')))(lambda (MODIFIED_VARS): __after((MODIFIED_VARS))))(lambda (MODIFIED_VARS): __after((MODIFIED_VARS))))(int(user_input)))(lambda (MODIFIED_VARS): __this((USED_VARS))))(raw_input('Enter a positive integer to guess: ')) if True else __after((MODIFIED_VARS)))(lambda (MODIFIED_VARS): None))))((USED_VARS))))((lambda f: (lambda x: x(x))(lambda y: f(lambda *args: y(y)(*args)))), __builtin__.__dict__['print']))(__import__('__builtin__'))
Traceback (most recent call last):
File "runtests.py", line 39, in capture_exec
exec code_string in namespace
File "<string>", line 1, in <module>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
NameError: global name 'USED_VARS' is not defined
======================================================================
ERROR: test_if (runtests.TestOneLine)
----------------------------------------------------------------------
Traceback (most recent call last):
File "runtests.py", line 20, in new_test
capture_exec(onelined),
File "runtests.py", line 43, in capture_exec
raise type(e)(code_string + '\n\n' + exc)
NameError: (lambda __builtin__: (lambda __y, __print: (lambda x: (lambda __after: (lambda x: __after((MODIFIED_VARS)))(5) if True else __after((MODIFIED_VARS)))(lambda (MODIFIED_VARS): __print(x)))(7))((lambda f: (lambda x: x(x))(lambda y: f(lambda *args: y(y)(*args)))), __builtin__.__dict__['print']))(__import__('__builtin__'))
Traceback (most recent call last):
File "runtests.py", line 39, in capture_exec
exec code_string in namespace
File "<string>", line 1, in <module>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
NameError: global name 'MODIFIED_VARS' is not defined
======================================================================
ERROR: test_if_comprehensive (runtests.TestOneLine)
----------------------------------------------------------------------
Traceback (most recent call last):
File "runtests.py", line 20, in new_test
capture_exec(onelined),
File "runtests.py", line 43, in capture_exec
raise type(e)(code_string + '\n\n' + exc)
NameError: (lambda __builtin__: (lambda __y, __print: (lambda x: (lambda __after: (lambda x: __after((MODIFIED_VARS)))(2) if True else (lambda x: __after((MODIFIED_VARS)))(3))(lambda (MODIFIED_VARS): (lambda ___: (lambda __after: (lambda x: __after((MODIFIED_VARS)))(4) if False else (lambda x: __after((MODIFIED_VARS)))(5))(lambda (MODIFIED_VARS): __print(x)))(__print(x))))(1))((lambda f: (lambda x: x(x))(lambda y: f(lambda *args: y(y)(*args)))), __builtin__.__dict__['print']))(__import__('__builtin__'))
Traceback (most recent call last):
File "runtests.py", line 39, in capture_exec
exec code_string in namespace
File "<string>", line 1, in <module>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
NameError: global name 'MODIFIED_VARS' is not defined
======================================================================
ERROR: test_ifelifelse (runtests.TestOneLine)
----------------------------------------------------------------------
Traceback (most recent call last):
File "runtests.py", line 20, in new_test
capture_exec(onelined),
File "runtests.py", line 43, in capture_exec
raise type(e)(code_string + '\n\n' + exc)
NameError: (lambda __builtin__: (lambda __y, __print: (lambda x: (lambda __after: (lambda x: __after((MODIFIED_VARS)))(2) if False else (lambda __after: (lambda x: __after((MODIFIED_VARS)))(6) if True else (lambda x: __after((MODIFIED_VARS)))(7))(lambda (MODIFIED_VARS): __after((MODIFIED_VARS))))(lambda (MODIFIED_VARS): __print(x)))(1))((lambda f: (lambda x: x(x))(lambda y: f(lambda *args: y(y)(*args)))), __builtin__.__dict__['print']))(__import__('__builtin__'))
Traceback (most recent call last):
File "runtests.py", line 39, in capture_exec
exec code_string in namespace
File "<string>", line 1, in <module>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
NameError: global name 'MODIFIED_VARS' is not defined
======================================================================
ERROR: test_ifelifelse_comprehensive (runtests.TestOneLine)
----------------------------------------------------------------------
Traceback (most recent call last):
File "runtests.py", line 20, in new_test
capture_exec(onelined),
File "runtests.py", line 43, in capture_exec
raise type(e)(code_string + '\n\n' + exc)
NameError: (lambda __builtin__: (lambda __y, __print: (lambda x: (lambda __after: (lambda x: __after((MODIFIED_VARS)))(2) if True else (lambda __after: (lambda x: __after((MODIFIED_VARS)))(3) if False else (lambda x: __after((MODIFIED_VARS)))(4))(lambda (MODIFIED_VARS): __after((MODIFIED_VARS))))(lambda (MODIFIED_VARS): (lambda ___: (lambda __after: (lambda x: __after((MODIFIED_VARS)))(5) if False else (lambda __after: (lambda x: __after((MODIFIED_VARS)))(6) if True else (lambda x: __after((MODIFIED_VARS)))(7))(lambda (MODIFIED_VARS): __after((MODIFIED_VARS))))(lambda (MODIFIED_VARS): (lambda ___: (lambda __after: (lambda x: __after((MODIFIED_VARS)))(8) if False else (lambda __after: (lambda x: __after((MODIFIED_VARS)))(9) if False else (lambda x: __after((MODIFIED_VARS)))(10))(lambda (MODIFIED_VARS): __after((MODIFIED_VARS))))(lambda (MODIFIED_VARS): __print(x)))(__print(x))))(__print(x))))(1))((lambda f: (lambda x: x(x))(lambda y: f(lambda *args: y(y)(*args)))), __builtin__.__dict__['print']))(__import__('__builtin__'))
Traceback (most recent call last):
File "runtests.py", line 39, in capture_exec
exec code_string in namespace
File "<string>", line 1, in <module>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
NameError: global name 'MODIFIED_VARS' is not defined
======================================================================
ERROR: test_ifelse (runtests.TestOneLine)
----------------------------------------------------------------------
Traceback (most recent call last):
File "runtests.py", line 20, in new_test
capture_exec(onelined),
File "runtests.py", line 43, in capture_exec
raise type(e)(code_string + '\n\n' + exc)
NameError: (lambda __builtin__: (lambda __y, __print: (lambda x: (lambda __after: (lambda x: __after((MODIFIED_VARS)))(5) if x>8 else (lambda x: __after((MODIFIED_VARS)))(10))(lambda (MODIFIED_VARS): __print(x)))(7))((lambda f: (lambda x: x(x))(lambda y: f(lambda *args: y(y)(*args)))), __builtin__.__dict__['print']))(__import__('__builtin__'))
Traceback (most recent call last):
File "runtests.py", line 39, in capture_exec
exec code_string in namespace
File "<string>", line 1, in <module>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
NameError: global name 'MODIFIED_VARS' is not defined
======================================================================
ERROR: test_ifelse_comprehensive (runtests.TestOneLine)
----------------------------------------------------------------------
Traceback (most recent call last):
File "runtests.py", line 20, in new_test
capture_exec(onelined),
File "runtests.py", line 43, in capture_exec
raise type(e)(code_string + '\n\n' + exc)
NameError: (lambda __builtin__: (lambda __y, __print: (lambda x: (lambda __after: (lambda x: __after((MODIFIED_VARS)))(2) if True else (lambda x: __after((MODIFIED_VARS)))(3))(lambda (MODIFIED_VARS): (lambda ___: (lambda __after: (lambda x: __after((MODIFIED_VARS)))(4) if False else (lambda x: __after((MODIFIED_VARS)))(5))(lambda (MODIFIED_VARS): __print(x)))(__print(x))))(1))((lambda f: (lambda x: x(x))(lambda y: f(lambda *args: y(y)(*args)))), __builtin__.__dict__['print']))(__import__('__builtin__'))
Traceback (most recent call last):
File "runtests.py", line 39, in capture_exec
exec code_string in namespace
File "<string>", line 1, in <module>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
NameError: global name 'MODIFIED_VARS' is not defined
======================================================================
ERROR: test_name_main (runtests.TestOneLine)
----------------------------------------------------------------------
Traceback (most recent call last):
File "runtests.py", line 20, in new_test
capture_exec(onelined),
File "runtests.py", line 43, in capture_exec
raise type(e)(code_string + '\n\n' + exc)
NameError: (lambda __builtin__: (lambda __y, __print: (lambda __after: (lambda ___: __after((MODIFIED_VARS)))(__print('Pass!')) if __name__=='__main__' else __after((MODIFIED_VARS)))(lambda (MODIFIED_VARS): None))((lambda f: (lambda x: x(x))(lambda y: f(lambda *args: y(y)(*args)))), __builtin__.__dict__['print']))(__import__('__builtin__'))
Traceback (most recent call last):
File "runtests.py", line 39, in capture_exec
exec code_string in namespace
File "<string>", line 1, in <module>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
NameError: global name 'MODIFIED_VARS' is not defined
======================================================================
ERROR: test_prime_tester (runtests.TestOneLine)
----------------------------------------------------------------------
Traceback (most recent call last):
File "runtests.py", line 20, in new_test
capture_exec(onelined),
File "runtests.py", line 43, in capture_exec
raise type(e)(code_string + '\n\n' + exc)
NameError: (lambda __builtin__: (lambda __y, __print: (lambda math: (lambda is_prime: (lambda smallest_prime_larger_than: (lambda ___: (lambda ___: (lambda ___: (lambda ___: (lambda ___: (lambda ___: (lambda ___: (lambda ___: (lambda ___: (lambda ___: __print(smallest_prime_larger_than(1000)))(__print(smallest_prime_larger_than(91))))(__print(smallest_prime_larger_than(1))))(__print(is_prime(89))))(__print(is_prime(91))))(__print(is_prime(45))))(__print(is_prime(5))))(__print(is_prime(4))))(__print(is_prime(3))))(__print(is_prime(2))))(__print(is_prime(1))))(lambda x:(__y(lambda __this: (lambda (USED_VARS): (lambda __after: (lambda x: (lambda __after: x if is_prime(x) else __after((MODIFIED_VARS)))(lambda (MODIFIED_VARS): __this((USED_VARS))))((x+1)) if True else __after((MODIFIED_VARS)))(lambda (MODIFIED_VARS): None))))((USED_VARS))))(lambda n:(lambda __after: False if n<2 else __after((MODIFIED_VARS)))(lambda (MODIFIED_VARS): (lambda __after: True if n==2 else __after((MODIFIED_VARS)))(lambda (MODIFIED_VARS): (lambda n: (lambda factors: (lambda i: (__y(lambda __this: (lambda (USED_VARS): (lambda __after: (lambda factor: (lambda __after: False if (n%factor)==0 else __after((MODIFIED_VARS)))(lambda (MODIFIED_VARS): (lambda i: __this((USED_VARS)))((i+1))))(factors[i]) if i<len(factors) else __after((MODIFIED_VARS)))(lambda (MODIFIED_VARS): True))))((USED_VARS)))(0))(range(2,int((math.ceil(math.sqrt(n))+1)))))(int(n))))))(__import__('math')))((lambda f: (lambda x: x(x))(lambda y: f(lambda *args: y(y)(*args)))), __builtin__.__dict__['print']))(__import__('__builtin__'))
Traceback (most recent call last):
File "runtests.py", line 39, in capture_exec
exec code_string in namespace
File "<string>", line 1, in <module>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
NameError: global name 'MODIFIED_VARS' is not defined
======================================================================
ERROR: test_while (runtests.TestOneLine)
----------------------------------------------------------------------
Traceback (most recent call last):
File "runtests.py", line 20, in new_test
capture_exec(onelined),
File "runtests.py", line 43, in capture_exec
raise type(e)(code_string + '\n\n' + exc)
NameError: (lambda __builtin__: (lambda __y, __print: (lambda x: (__y(lambda __this: (lambda (USED_VARS): (lambda __after: (lambda x: (lambda ___: __this((USED_VARS)))(__print(x)))((x-1)) if x>0 else __after((MODIFIED_VARS)))(lambda (MODIFIED_VARS): __print('Done!')))))((USED_VARS)))(5))((lambda f: (lambda x: x(x))(lambda y: f(lambda *args: y(y)(*args)))), __builtin__.__dict__['print']))(__import__('__builtin__'))
Traceback (most recent call last):
File "runtests.py", line 39, in capture_exec
exec code_string in namespace
File "<string>", line 1, in <module>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
File "<string>", line 1, in <lambda>
NameError: global name 'USED_VARS' is not defined
----------------------------------------------------------------------
Ran 36 tests in 0.030s
FAILED (errors=17)
Easy to reproduce with
docker run -ti ubuntu:vivid bash
(or travis-ci's installed version of python2.7)
Hi, this repository looks really interesting.
I was wondering would you consider to support python3?
NotImplementedError: Open problem: yield
It seems that certain variable names can end up in name collisions in the generated code. For example, the code
def f(__print):
print __print
return __print * 4
y = f(5)
print y
is turned into the one-line code
(lambda __g, __print: [[(__print(y), None)[1] for __g['y'] in [(f(5))]][0] for __g['f'], f.__name__ in [(lambda __print: (lambda __l: [(__print(__l['__print']), (__l['__print'] * 4))[1] for __l['__print'] in [(__print)]][0])({}), 'f')]][0])(globals(), __import__('__builtin__').__dict__['print'])
which gives a TypeError when run.
When I have in print some unicode characters they can't pass to one-lined converter and throw error.
onelinerizer fails the following test cases:
def f():
exec('a = 5')
return a
print f()
The one-lined code throws an error, whereas the original code prints 5.
Also, this one:
def f():
exec ""
locals()['a'] = 6
return a
def g():
locals()['a'] = 6
return a
a = 5
print f()
a = 5
print g()
The one-lined code prints 5\n5, whereas the original code prints 6\n5.
Discovery of this issue is credited to @tjhance.
We don’t support from __future__ import division, absolute_import, print_function
. It looks like ast
already does the right thing with from __future__ import unicode_literals
.
Minimal unimplemented tests added in 5da842b.
This test case incorrectly outputs 2 when onelined.
x = 1
def foo():
x = 2
foo()
print x
Perhaps the symtable
module can help compute the correct scopes?
Is it allowed to use anything from the standard library in generated code?
If so you can handle exceptions using the unit testing module like so:
from __future__ import print_function
import sys, unittest
# input parameters
#tryme = lambda: (_ for _ in ()).throw(ValueError('ohno'))
#tryme = lambda: 'blah'
tryme = lambda: (_ for _ in ()).throw(AttributeError('ohno'))
exc = AttributeError
catch = lambda e: (print('failed (%r)' % e), 'caught')[1]
else_expr = lambda: print('else-expr')
finally_expr = lambda: print('finally')
# implementation
_catch = lambda e: state.__setitem__('result', catch(e[1]))
_throw = lambda e: (_ for _ in ()).throw(e[1])
result = type('tmp', (unittest.TestResult, ), {
'addError': lambda s,t,e: _catch(e) if isinstance((e and e[1]), exc) else _throw(e)
})()
state = dict(r=result)
case = type('tmp', (unittest.TestCase, ), {
'testMe': lambda s: (state.__setitem__('result', tryme()), else_expr())
})('testMe')
out = (case.run(state['r']), finally_expr, state['result'])[2]
does it work with flask
First, remove try...except
blocks and if __name__ == '__main__'
in main.py
in order to create something that can be compiled to one line. Once that's taken care of, I can compile a new main.ol.py
, but it has some bugs. For example, the resulting main.ol.py
does not correctly compile these programs:
print "Hello world!"
print "Hi."
— compiles into something which omits the second print statement.
print "Hello, world!"
x = 4
y = 5
print x + y
— cannot be compiled by main.ol.py
, because main.ol.py
errors with AttributeError: 'Name' object has no attribute 'op'
.
Does not below two ways equal?
class Foo(object):
def __enter__(self):
print "hello, world"
def __exit__(self, type, value, tb):
print "bye"
with Foo() as foo:
pass
mgr = Foo()
exit = type(mgr).__exit__
value = type(mgr).__enter__(mgr)
exc = True
try:
try:
foo = value
pass
except:
exc = False
if not exit(mgr, *sys.exc_info()):
raise
finally:
if exc:
exit(mgr, None, None, None)
Test case: 'print 42' * 10000
This doesn't seem to run in linear time. It has been hypothesized that this is due to string formatting being slow, as the string-to-insert grows as O(n) and it is inserted O(n) times.
Apparently this is supposed to work:
False = True
while False:
del False
Wat.
You can do it like exec("\xXX\xXX\xXX\uXXXX"):
def onelinerizer(data):
s = ""
for c in data:
c = hex(ord(c))[2:]
if len(c) > 2:
s += "\u" + c.zfill(4)
else:
s += "\x" + c.zfill(2)
return "exec('" + s + "')"
At least in python3, you can use eval
/exec
. This makes one-liners trivial.
def decorator(f):
print f.__name__
def bar():
return f()
return bar
@decorator
def foo():
pass
print foo.__name__
should output 'foo\nbar\n'
, not '<lambda>\nfoo\n'
.
This is probably problem with using older python version, but one-lined code converter can't convert when you pass async def somefunc()
/ await somefunc()
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.