yunline / olg4p Goto Github PK
View Code? Open in Web Editor NEWOne-Liner Generator for Python
License: MIT License
One-Liner Generator for Python
License: MIT License
Original
for i in range(10):
pass
print(i)
Converted
[[None for i in range(10)], print(i)]
Error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'i' is not defined. Did you mean: 'id'?
As well as raise
, finally
and with
.
Unfortunately, I haven't had any idea to implement this yet.
Test Code
# file: ./__devtmp__/test.py
def func():
return
Test Command
py .\oneliner.py .\__devtmp__\test.py
Error
Traceback (most recent call last):
File "X:\python\一行代码转换器\oneliner.py", line 592, in <module>
result = convert_code_string(script, filename=input_file_name)
File "X:\python\一行代码转换器\oneliner.py", line 536, in convert_code_string
return ast.unparse(
File "C:\Users\yunline\AppData\Local\Programs\Python\Python310\lib\ast.py", line 1673, in unparse
return unparser.visit(ast_obj)
File "C:\Users\yunline\AppData\Local\Programs\Python\Python310\lib\ast.py", line 807, in visit
self.traverse(node)
File "C:\Users\yunline\AppData\Local\Programs\Python\Python310\lib\ast.py", line 798, in traverse
super().visit(node)
File "C:\Users\yunline\AppData\Local\Programs\Python\Python310\lib\ast.py", line 410, in visit
return visitor(node)
File "C:\Users\yunline\AppData\Local\Programs\Python\Python310\lib\ast.py", line 844, in visit_NamedExpr
self.traverse(node.value)
File "C:\Users\yunline\AppData\Local\Programs\Python\Python310\lib\ast.py", line 798, in traverse
super().visit(node)
File "C:\Users\yunline\AppData\Local\Programs\Python\Python310\lib\ast.py", line 410, in visit
return visitor(node)
File "C:\Users\yunline\AppData\Local\Programs\Python\Python310\lib\ast.py", line 1570, in visit_Lambda
self.traverse(node.body)
File "C:\Users\yunline\AppData\Local\Programs\Python\Python310\lib\ast.py", line 798, in traverse
super().visit(node)
File "C:\Users\yunline\AppData\Local\Programs\Python\Python310\lib\ast.py", line 410, in visit
return visitor(node)
File "C:\Users\yunline\AppData\Local\Programs\Python\Python310\lib\ast.py", line 1467, in visit_Subscript
self.traverse(node.value)
File "C:\Users\yunline\AppData\Local\Programs\Python\Python310\lib\ast.py", line 798, in traverse
super().visit(node)
File "C:\Users\yunline\AppData\Local\Programs\Python\Python310\lib\ast.py", line 410, in visit
return visitor(node)
File "C:\Users\yunline\AppData\Local\Programs\Python\Python310\lib\ast.py", line 1231, in visit_List
self.interleave(lambda: self.write(", "), self.traverse, node.elts)
File "C:\Users\yunline\AppData\Local\Programs\Python\Python310\lib\ast.py", line 696, in interleave
f(x)
File "C:\Users\yunline\AppData\Local\Programs\Python\Python310\lib\ast.py", line 798, in traverse
super().visit(node)
File "C:\Users\yunline\AppData\Local\Programs\Python\Python310\lib\ast.py", line 410, in visit
return visitor(node)
File "C:\Users\yunline\AppData\Local\Programs\Python\Python310\lib\ast.py", line 844, in visit_NamedExpr
self.traverse(node.value)
File "C:\Users\yunline\AppData\Local\Programs\Python\Python310\lib\ast.py", line 798, in traverse
super().visit(node)
File "C:\Users\yunline\AppData\Local\Programs\Python\Python310\lib\ast.py", line 410, in visit
return visitor(node)
File "C:\Users\yunline\AppData\Local\Programs\Python\Python310\lib\ast.py", line 414, in generic_visit
for field, value in iter_fields(node):
File "C:\Users\yunline\AppData\Local\Programs\Python\Python310\lib\ast.py", line 252, in iter_fields
for field in node._fields:
AttributeError: 'NoneType' object has no attribute '_fields'
TestCode
[a,b,c]=range(3)
print(b)
Error
Traceback (most recent call last):
File "X:\python\一行代码转换器\oneliner.py", line 661, in <module>
result = convert_code_string(script, filename=input_file_name)
File "X:\python\一行代码转换器\oneliner.py", line 606, in convert_code_string
c.convert(ast.parse(code).body, top_level=True),
File "X:\python\一行代码转换器\oneliner.py", line 514, in convert
converted_list = self.node_handler_map[type(node)](node)
File "X:\python\一行代码转换器\oneliner.py", line 348, in handle_assign
out.append(template_auto_assign(_target, assign.value))
File "X:\python\一行代码转换器\oneliner.py", line 89, in template_auto_assign
raise ConvertError(f"Unknown assign type at line {target.lineno}")
__main__.ConvertError: Unknown assign type at line 1
Test Code
def func():
for i in range(5):
if i==3:
return 123
elif i==2:
continue
print(i)
print(func())
Converted Code
[(itertools := __import__('itertools')), (func := (lambda : [(__ol_return_value_nxazjlgisn := None), (__ol_not_return_nxazjlgisn := True), [[(__ol_not_cont_fldniruftr := True), [(__ol_not_return_nxazjlgisn := False), (__ol_return_value_nxazjlgisn := 123)] if i == 3 else (__ol_not_cont_fldniruftr := False) if i == 2 else None, print(i) if __ol_not_cont_fldniruftr else None, print(i) if __ol_not_return_nxazjlgisn else None] for i in itertools.takewhile(lambda _: __ol_not_return_nxazjlgisn, range(5))], __ol_return_value_nxazjlgisn][-1])), print(func())]
Expected Output
0
1
123
Real Output
0
0
1
1
2
3
123
Use globals()["var"]
to simulate the behavior of global
TestCode
b=[]
a,*b=1,2,3
print(b)
Error
Traceback (most recent call last):
result = convert_code_string(script, filename=input_file_name)
c.convert(ast.parse(code).body, top_level=True),
File "X:\python\一行代码转换器\oneliner.py", line 457, in convert
out.extend(self.node_handler_map[type(node)](node))
File "X:\python\一行代码转换器\oneliner.py", line 299, in handle_assign
File "X:\python\一行代码转换器\oneliner.py", line 96, in template_auto_assign
raise ConvertError(f"Unknown assign type at line {target.lineno}")
__main__.ConvertError: Unknown assign type at line 2
维护屎山失败
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.