Dan_Patterson

Basic fancy formats ....

Blog Post created by Dan_Patterson Champion on Mar 1, 2016

Basic fancy formats...

 

Created:   2016-02-28

Modified: 2016-08-13

Attachments:

    Script attached in zip file below.

References:

 

This is a work in progress.

I am tired of reinventing the wheel every time I want to format output so it looks appealing and conveys the information I want.  To this end, I am going to present what I have... but I will do this in increments.

 

I will not provide commentary... I don't want to cloud the issue with rules and specifics.  I use a monospaced font to ensure that the output is what you would expect in any python IDE, and to control vertical alignment within a pure text environment.  Asample script is attached so you can experiment with your own ideas.

 

Consult the script header for further information.  The script also contains some format tips that arise from formatting script... which I have posted on previously.

 

Sample output (courier new 10pt)

This is the only font that will maintain monospacing from the available font family.  I formatted using the "Plain" syntax highlighter so that line numbers were created for reference purposes.

 

Basic formatting....

    :  |_{ [name/num] [!s, !r, none] [: format_spec]_|}
    :  |_{ vertical bar to denote start, _ optional spaces
    :  }_| vertical bar to denote end, _ optional spaces
    : ------------------------------------

    :   [name/num]  = empty, name, number
    :   [!s or !r]  = str or repr format or nothing
    :   [format]    = see examples in output

1   |{0:_>15.0f}|   - lead _, right, integer           |_________123456|

2   |{0:_>15.3f}|   - lead _, right, float             |_____123456.012|

3   |{0:_>+15.3f}|  - lead _, right, req. sign, float  |____+123456.012|

4   |{0:_> 15.3f}|  - lead _, right, opt. sign, float  |____ 123456.012|

5   |{0:> 15.3f}|   - no lead, right, opt. sign, float |     123456.012|

6   |{0:>15.3f}|    - whatever, float                  |     123456.012|

7   |{0: <15.3f}|   - left, float, trailing space(s)   |123456.012     |

8   |{0: < 15.3f}|  - left, space(s), float, spaces    | 123456.012    |

9   |{0:_< 15.3f}|  - left, space(s), float, __        | 123456.012____|

10  |{0!s:<15}|     - left, string                     |123456.01234   |

11  |{0!s:*<15}|    - left, string space(s)            |123456.01234***|

12  |{0!s:*^15}|    - center, lead/trail               |*123456.01234**|

13  |_{0!s: ^13}_|  - lead, center, trail              |_123456.01234 _|

14  |_{0!s:*<}_|    - no size, lead ignored, left      |_123456.01234_|

15  |_{0: >5f}_|    - float, undersized                |_123456.012340_|


Fancy formatting
....

Format elements are generated from field widths and content.
Some examples for ... 1234.5 ...

widths.... [6, 8, 10, 12]
building..

    f = [ "|_{{0!s:^{}}}_|".format(width) for width in widths ]
    txt = "".join(i for i in f)
    print(txt.format(1234.5))

formats... ['|_{0!s:^6}_|', '|_{0!s:^8}_|', '|_{0!s:^10}_|', '|_{0!s:^12}_|']

result.... |_1234.5_||_ 1234.5 _||1234.5  _||_   1234.5   _|


 

Comments welcome.

 

More to come....

Outcomes