Beta

Parse python terminal output

Description:

Write a method that inputs a string which is the output of a python command line session, and returns the list of pairs (executed command, command printout).

In such an output, an issued command starts with >>>_ (where _ stands for space). If the command takes more lines, those lines start with ..._, and the block ends with ..., i.e., without space in the end of line.
A simple example of the input string is:

>>> print 42
42
>>> for x in 1,2,3:
...   print x,
...
1 2 3

On this input, the method should return the following list.

[('print 42', '42'), 
 ('for x in 1,2,3:\n  print x,', '1 2 3')]

Also, take care of broken lines. First identify the length of the longest line, and if it is above the default 80 characters, take this as max_width. Then, concatenate each line of length max_width to the next line.

For example, the method on input

>>> 'x'*80
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
x'

should return the command result without line break:

[("'x'*80", 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')]

However, on the input:

>>> print '01234567890123456789012345678901234567890123456789012345678901234567890123456789\nkeep linebreak'
01234567890123456789012345678901234567890123456789012345678901234567890123456789
keep linebreak

it should keep the linebreak after the 80 chars long line in the answer, because the longest line in the whole input string is longer.

Note: The empty commands should be ignored.

Fundamentals

More By Author:

Check out these other kata created by zellerede

Stats:

CreatedFeb 25, 2016
PublishedFeb 28, 2016
Warriors Trained72
Total Skips0
Total Code Submissions277
Total Times Completed10
Python Completions10
Total Stars6
% of votes with a positive feedback rating80% of 5
Total "Very Satisfied" Votes4
Total "Somewhat Satisfied" Votes0
Total "Not Satisfied" Votes1
Total Rank Assessments5
Average Assessed Rank
4 kyu
Highest Assessed Rank
4 kyu
Lowest Assessed Rank
5 kyu
Ad
Contributors
  • zellerede Avatar
  • user8436785 Avatar
Ad