Python Single Line Converter
Convert multi-line Python code to a single line for easy copy-pasting
Input Code
Output
Calculation: The conversion process will be shown here after conversion.
User Guide
What this tool does
This tool converts multiple-line Python code to a single line that can be easily copy-pasted into environments like Python shell or through Putty.
How it works
It pastes your multiline code together into a triple-quoted string and wraps it with exec. To keep the code legal, the string is processed as follows:
- Escapes all backslashes (\), then escapes triple quotes (""").
- Optionally removes indentation from the first line if selected.
- Wraps the exec statement or function based on the Python version option.
Limitations
- Won't remove comments and docstrings. Handling these properly requires more work.
- Can't check if the original code is syntactically correct or properly indented.
Alternative Solutions
If you find yourself needing this frequently, consider using ipython with the %paste command, or a graphics-based solution like Dreampie.
Rationale
Ease copy pasting Python code in some cases (e.g paste into Python shell or through Putty). If you find yourself need this a lot you may consider using ipython with the %paste
command, or a graphic based one like dreampie
How it works
Basically it paste your multiline code together into a triple quoted string and wraps it with exec. To keep the code legal the string is processed as follows:
- Escape all
\
, then escape"""
. - Reindent to 0 indent based on first line if option is selected. So you can paste indented code directly.
- Wraps
exec
statement or function based on version option.
Limitations
- Won't remove comments and docstring. Properly handling these needs more work.
- Of course can't check the original code is syntatically legal or properly indented.
- Only tested by randomly copying code from Python Recipes... So please report missing cases.
0 Comments