I'm sorry, I feel like there is a better title for my question, but I can't think of it.
The situation, essentially, is this: I am creating a fixed-width information table. I have a list of (k,v) for k in a list of keys.
When done, the " : " should be centered in the line, and there should be a "|" on the far left and right sides.
My problem is, I have a few lists that are too long to fix into a single line. I either need to be able to have it so that once the list is x characters in, it will start a new line, and have the text indented to the same level, or I need to be able to have a number of values encoded such that they align with the same left padding (or what I'll have be the "tabbed" version of the content.
An example of what I have so far:
def make_information_file(t):
pairs=[("SERiES","Game of Thrones"),("SiZE","47,196,930,048 bytes"),(AUDiO TRACKS,['English: DTS-HD Master Audio 5.1', 'French: DTS 5.1', 'Spanish: DTS 5.1', 'Polish: DTS 2.0', 'Spanish: DTS 2.0'])]
general_keys=["COLLECTiON NAME","UPC","RETAiL RELEASE DATE","REGiON","STUDiO"]
video_keys=["ViDEO","SOURCE","RESOLUTiON","ASPECT RATiO"]
audio_keys=["AUDiO FORMATS"]
subtitle_keys=["SUBTiTLES"]
all_keys=general_keys+video_keys+audio_keys+subtitle_keys
longest_key=(sorted(all_keys,key=len) or [""])[-1]
longest_key_length=len(longest_key)
left_padding=longest_key_length+5
right_padding=106-left_padding
empty_left_padding=left_padding+3
empty_right_padding=106-left_padding-3
line_formatter=lambda p: "|{field:>{left_padding}} : {value:<{right_padding}}|".format(field=p[0],value=p[-1],left_padding=left_padding,right_padding=right_padding)
now, notice that depending on how long the longest key is, everything with be aligned such that the ":" is at a fixed point, with one space to either side of it and right text to the left right-aligned, and the stuff to the left left-aligned.
however, the "AUDiO TRACKS" list is too long to fit into one line. I can either have it automatically split if the word is going to push it over it's limit (my preference, I believe, at which point the second line (and any lines thereafter) will have to indent the text to keep in inline with the first line's text. The other option is to have it so that I have it so that every value is centered with empty_left_padding to the left, followed by the string value, followed by enough blank spaces such that the line's final length is the standard 111 characters long, with a "|" as the first and last characters
desired_output=""""
| SERiES : Game of Thrones |
| SiZE : 47,196,930,048 bytes |
| AUDiO FORMATS : English: DTS-HD Master Audio 5.1, French: DTS 5.1, Spanish: DTS 5.1, |
| Polish: DTS 2.0, Spanish: DTS 2.0 |
| UPC : 883929191505 |
| RETAiL RELEASE DATE : 03-06-2012 |
| REGiON : A, B, C |
| STUDiO : HBO Home Video |
| ViDEO : 1080p 1.78:1 |
| SOURCE : BD50 |
| RESOLUTiON : 1080p |
| ASPECT RATiO : 16:9 |"""
So, I can't figure out how to deal with the "AUDiO FORMATS" case above (I have the same issue with the list of subtitles available).
pprint, this has numerous function which can be used to print in a desired fashion.