Print Columns From Python Dictionary
I have a dictionary of commits over a week. I want to print them out in a weekly calendar style of columns. { 'Fri': ['Commit: 04:15PM Move flex to mixin and do mobile-first q
Solution 1:
I think you can solve this without numpy
and using only stdlib modules!
from itertools import zip_longest
d = {'Fri': ['Commit: 04:15PM Move flex to mixin and do mobile-first queries\n',
'Commit: 03:52PM use padding to get the margins\n',
'Commit: 10:09AM Remove field_prepared_logo height\n',
'Commit: 03:15PM Add the final div to footer\n',
'Commit: 03:05PM Merge from redesign\n'],
'Thu': ['Commit: 10:25AM Design qa fixes on /clients page\n'],
'Tue': ['Commit: 09:40AM remove transform and tweak span placement in '
'hamburger\n'],
'Wed': ['Commit: 02:19PM Change blockquote font and width\n']}
forrowinzip_longest(d['Tue'], d['Wed'], d['Thu'], d['Fri']):
print(row)
# ('Commit: 09:40AM remove transform and tweak span placement in hamburger\n', 'Commit: 02:19PM Change blockquote font and width\n', 'Commit: 10:25AM Design qa fixes on /clients page\n', 'Commit: 04:15PM Move flex to mixin and do mobile-first queries\n')
# (None, None, None, 'Commit: 03:52PM use padding to get the margins\n')
# (None, None, None, 'Commit: 10:09AM Remove field_prepared_logo height\n')
# (None, None, None, 'Commit: 03:15PM Add the final div to footer\n')
# (None, None, None, 'Commit: 03:05PM Merge from redesign\n')
zip_longest
obviates the need to "even out" your arrays...it just returns None
where there was nothing to put. You could also pass fillvalue=''
or similar to set the default value.
You could also use an ordered dict to avoid manually specifying the order of the days as I did.
Now that you have the individual rows, all that's left is an exercise in pretty-printing. The textwrap
module is probably your friend here.
EDIT: This took a bit of doing, but here's the pretty printing taken care of as well
maxwidth = (80//len(d)) - 1 # set this to whatever value you want
wrapper = textwrap.TextWrapper(width=maxwidth, subsequent_indent=' ')
wrapped_commits = {k: [wrapper.wrap(commit) forcommitin v] fork, v in d.items()}
justified_commits = {k: [line.ljust(maxwidth) forcommitin v forlinein commit] fork, v in wrapped_commits.items()}
forlinzip_longest(justified_commits['Tue'], justified_commits['Wed'], justified_commits['Thu'], justified_commits['Fri'], fillvalue=' '*maxwidth):
print(' '.join(l))
Here's the output from that:
Commit: 09:40AM Commit: 02:19PM Commit: 10:25AM Commit: 04:15PM
remove transform Change blockquote Design qa fixes on Move flex to mixin
and tweak span font and width /clients page and do mobile-
placement infirst queries
hamburger Commit: 03:52PM use
padding toget the
margins
Commit: 10:09AM
Remove field_prepa
red_logo height
Commit: 03:15PM Add
the final div to
footer
Commit: 03:05PM
Mergefrom
redesign
Post a Comment for "Print Columns From Python Dictionary"