PPSATO
PPSATO

Reputation: 115

Python - indenting wrapped text

I have the following function in Python-3

def test_late_logs(tabs_data):

    current_date = datetime.now(timezone.utc).replace(minute=0, second=0, microsecond=0)

    text = "*** Manual logging ***\n"
    text += f"TAB LATE REPORT. Current date is {current_date}\n"
    text += "LATE BY".ljust(9) + "LAST SUCCESSFUL RUN".ljust(27) + "ACCEPTABLE LAG".ljust(16) + "TAB".ljust(50) + "IMPACT".ljust(20) + "DESCRIPTION" + "\n"

    for tab_name in tabs_data:
        tab_data = tabs_data[tab_name]
        text += f"{str(tab_data['is_late_by']):<9}{str(tab_data['last_successful_run']):<27}{str(dtab_data['acceptable_lag']):<16}{str(tab_name):<50}{str(tab_data['impact']):<20}{textwrap.TextWrapper(width=175,subsequent_indent=' '*123).fill(text=str(tab_data['description']))} \n"
        
    return text

When I print text from the above code, I get the following

LATE BY  LAST SUCCESSFUL RUN        ACCEPTABLE LAG  TAB                                               IMPACT              DESCRIPTION
0:00:00  2022-03-07 10:00:00+00:00  5:00:00         xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx          impact_high                                                                                                                          xxxxxxxxxxxxxxxxxxxxxxx 
0:00:00  2022-03-07 10:00:00+00:00  5:00:00         xxxxxxxxxxxxxxxxxxxxxxxxxxxxx                     impact_high                                                                                                                          xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 
0:00:00  2022-03-07 10:00:00+00:00  5:00:00         xxxxxxxxxxxxxxxxxxxxxxxxxxxxx                     impact_high                                                                                                                          xxxxxxxxxxxxxxxxxxxx 
0:00:00  2022-03-07 10:00:00+00:00  5:00:00         xxxxxxxxxxxxxxxxx                                 impact_medium                                                                                                                          xxxxxxxxxxxxxxxxxxxxxxxxxxxxx 
2:00:00  2022-03-07 12:00:00+00:00  0:00:00         xxxxxxxxxxxxxxxxxxxxxxxxxxxxx                     impact_medium                                                                                                                          xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                                                                                                                           xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                                                                                                                           xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                                                                                                                           xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                                                                                                                           xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 

I also tried the following code with different values of initial_indent:

text += f"{str(tab_data['is_late_by']):<9}{str(tab_data['last_successful_run']):<27}{str(tab_data['acceptable_lag']):<16}{str(tab_name):<50}{str(tab_data['impact'])}{textwrap.TextWrapper(initial_indent=' '*1,width=175,subsequent_indent=' '*123).fill(text=str(tab_data['description']))} \n"

But, I am unable to format the wrapped text for 'description' to be placed below the title 'DESCRIPTION'. Is this because of some inherent indentation that already exists? Or am I missing something? Any help will be appreciated.

tabs_data is a dictionary as follows:

tabs_data = {
        'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx': {
            'acceptable_lag' : timedelta(hours=5),
            'description' : 'xxxxxxxxxxxxxxxxxxxxxxx',
            'emails': ['[email protected]'],
            'slack_channels': ['#xxxxxxxxxxxxxxx']
        },
        'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx': {
            'acceptable_lag': timedelta(hours=5),
            'description': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
            'emails': ['[email protected]'],
            'slack_channels': ['#xxxxxxxxxxxxxxx']
        },
        'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx': {
            'acceptable_lag' : timedelta(hours=5),
            'description': 'xxxxxxxxxxxxxxxxxxxx',
            'emails': ['[email protected]'],
            'slack_channels': ['#xxxxxxxxxxxxxxx']
        },
        'xxxxxxxxxxxxxxxxx': {
            'acceptable_lag': timedelta(hours=5),
            'description': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
            'emails': ['[email protected]'],
            'slack_channels': ['#xxxxxxxxxxxxxxx']
        },
        'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx': {
            'acceptable_lag': timedelta(hours=0),
            'description': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
            'emails': ['[email protected]'],
            'slack_channels': ['#xxxxxxxxxxxxxxx']
        }
    }

Upvotes: 3

Views: 213

Answers (1)

Liam
Liam

Reputation: 563

You can use textwrap.indent(string, ' '*154, predicate=should_indent) to specify a function that determines whether or not the text should be wrapped. Docs

I'm not sure if you're able to add a character at the start of each description but if you can then this should work.

For my example I added a 1 at the start of each description element and then in my function I checked whether the line started with 1 to determine whether it should indent the line or not.

def should_indent(line):
    if(line[0] != '1'):
        return True
    else:
        return False
def test_late_logs(tabs_data):

    current_date = datetime.now(timezone.utc).replace(
        minute=0, second=0, microsecond=0)

    text = "*** Manual logging ***\n"
    text += f"TAB LATE REPORT. Current date is {current_date}\n"
    text += "LATE BY".ljust(9) + "LAST SUCCESSFUL RUN".ljust(27) + "ACCEPTABLE LAG".ljust(
        16) + "TAB".ljust(50) + "IMPACT".ljust(20) + "DESCRIPTION" + "\n"

    for tab_name in tabs_data:
        tab_data = tabs_data[tab_name]
        text += f"{str(tab_data['is_late_by']):<9}\
            {str(tab_data['last_successful_run']):<27}\
                {str(tab_name):<50}{str(tab_data['impact']):<20}\
                    {textwrap.indent(textwrap.fill(str(tab_data['description']), width=24), ' '*154, predicate=should_indent)} \n"

    return text
tabs_data = {
    'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx': {
        'is_late_by': "0:00:00",
        'last_successful_run': "2022-03-07 10:00:00+00:00",
        'impact': "impact_high",
        'acceptable_lag': timedelta(hours=5),
        'description': '1xxxxxxxxxxxxxxxxxxxxxxx',
        'emails': ['[email protected]'],
        'slack_channels': ['#xxxxxxxxxxxxxxx']
    },
    'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx': {
        'is_late_by': "0:00:00",
        'impact': "impact_high",
        'last_successful_run': "2022-03-07 10:00:00+00:00",
        'acceptable_lag': timedelta(hours=5),
        'description': '1xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
        'emails': ['[email protected]'],
        'slack_channels': ['#xxxxxxxxxxxxxxx']
    },
    'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx': {
        'is_late_by': "0:00:00",
        'impact': "impact_high",
        'last_successful_run': "2022-03-07 10:00:00+00:00",
        'acceptable_lag': timedelta(hours=5),
        'description': '1xxxxxxxxxxxxxxxxxxxx',
        'emails': ['[email protected]'],
        'slack_channels': ['#xxxxxxxxxxxxxxx']
    },
    'xxxxxxxxxxxxxxxxx': {
        'is_late_by': "0:00:00",
        'impact': "impact_high",
        'last_successful_run': "2022-03-07 10:00:00+00:00",
        'acceptable_lag': timedelta(hours=5),
        'description': '1xxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
        'emails': ['[email protected]'],
        'slack_channels': ['#xxxxxxxxxxxxxxx']
    },
    'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx': {
        'is_late_by': "0:00:00",
        'impact': "impact_high",
        'last_successful_run': "2022-03-07 10:00:00+00:00",
        'acceptable_lag': timedelta(hours=0),
        'description': '1xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
        'emails': ['[email protected]'],
        'slack_channels': ['#xxxxxxxxxxxxxxx']
    }
}

This is the output I get:

*** Manual logging ***
TAB LATE REPORT. Current date is 2022-03-08 17:00:00+00:00
LATE BY  LAST SUCCESSFUL RUN        ACCEPTABLE LAG  TAB                                               IMPACT              DESCRIPTION
0:00:00              2022-03-07 10:00:00+00:00                  xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx          impact_high                             1xxxxxxxxxxxxxxxxxxxxxxx 
0:00:00              2022-03-07 10:00:00+00:00                  xxxxxxxxxxxxxxxxxxxxxxxxxxxxx                     impact_high                             1xxxxxxxxxxxxxxxxxxxxxxx
                                                                                                                                                          xxxxxxxxxxxxxxxxxxxxxxxx
                                                                                                                                                          xxxxxxxxxxxxxxxxxxxxxxxx
                                                                                                                                                          xxxxxxxxxxxxxxxxxxxxxxxx
                                                                                                                                                          xxxxxxxxxxxxxxxxxxxxxxxx
                                                                                                                                                          xxxxxxxxxxxxxxxxxxxxxxxx
                                                                                                                                                          xxxxxxxxxxxxxxxxxxxxxxxx
                                                                                                                                                          xxxxxxxxxxxxxxxxxxxxxxxx
                                                                                                                                                          xxxxxxxxxxxxxxxxxxxxxxxx
                                                                                                                                                          xxxxxxxxxxxxxxxxxxx 
0:00:00              2022-03-07 10:00:00+00:00                  xxxxxxxxxxxxxxxxx                                 impact_high                             1xxxxxxxxxxxxxxxxxxxxxxx
                                                                                                                                                          xxxxxx 

enter image description here

Upvotes: 1

Related Questions