Merge lp:~ben-hutchings/ensoft-sextant/audit-table into lp:ensoft-sextant

Proposed by Ben Hutchings
Status: Superseded
Proposed branch: lp:~ben-hutchings/ensoft-sextant/audit-table
Merge into: lp:ensoft-sextant
Diff against target: 53 lines (+35/-8)
1 file modified
src/sextant/__main__.py (+35/-8)
To merge this branch: bzr merge lp:~ben-hutchings/ensoft-sextant/audit-table
Reviewer Review Type Date Requested Status
Robert Needs Fixing
Review via email: mp+236136@code.launchpad.net

This proposal has been superseded by a proposal from 2014-09-29.

Commit message

Audit command now prints output as a formatted table

To post a comment you must log in.
Revision history for this message
Robert (rjwills) :
review: Needs Fixing
Revision history for this message
Robert (rjwills) :
review: Needs Fixing
26. By Ben Hutchings

Added a maximum width restriction on table columns, removed a couple of instances of trailing whitespace

27. By Ben Hutchings

Added space around binary operator

28. By Ben Hutchings

Capitalised COLMAXLENS to make it look like a constant

Unmerged revisions

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/sextant/__main__.py'
2--- src/sextant/__main__.py 2014-09-26 11:25:09 +0000
3+++ src/sextant/__main__.py 2014-09-29 15:01:36 +0000
4@@ -81,14 +81,41 @@
5 location = _displayable_url(args)
6 logging.warning('No programs on database at {}.'.format(location))
7 else:
8- for program in audited:
9- st = ('Program {progname} with {numfuncs} functions '
10- 'uploaded by {uploader} (id {uploaderid}) on {date}.')
11- print(st.format(progname=program.program_name,
12- numfuncs=program.number_of_funcs,
13- uploader=program.uploader,
14- uploaderid=program.uploader_id,
15- date=program.date))
16+ # print data about all the programs in the database in a formatted table
17+ titles = ("Name", "#Func", "Uploader", "User-ID", "Upload Date")
18+ colminlens = (len(entry) for entry in titles)
19+ # maximum lengths to avoid one entry from throwing the whole table
20+ # date format is <YYYY:MM:DD HH:MM:SS.UUUUUU> = 26 characters
21+ COLMAXLENS = (25, 5, 25, 10, 26)
22+
23+ # make a table of the strings of each data entry we will display
24+ text = [map(str, (p.program_name, p.number_of_funcs,
25+ p.uploader, p.uploader_id, p.date))
26+ for p in audited]
27+
28+ # and a table of the corresponding lengths for every text entry
29+ textlens = ((len(entry) for entry in pentry) for pentry in text)
30+
31+ # the maxes of the column maxes of the textlens table and the
32+ # lengths of the title row words give the column widths
33+ textcollens = map(max, zip(*textlens))
34+ # the lambda is exactly equivalent to calling
35+ # min(max(colminlens, textcollens), COLMAXLENS),
36+ # eg for x < low: sorted((low, x, high))[1] = (x, low, high)[1] = low
37+ collens = map(lambda l: sorted(l)[1], zip(colminlens, textcollens, COLMAXLENS))
38+
39+ # insert the column widths into the format string
40+ st = ('{{0:{0}}} {{1:>{1}}} {{2:{2}}} {{3:{3}}} '
41+ '{{4:{4}}}'.format(*collens))
42+
43+ # write the title line so we know how long to make the separator line
44+ titleline = st.format(*titles)
45+ sepline = '-' * len(titleline)
46+
47+ # output the formatted table
48+ print(titleline)
49+ print(sepline)
50+ print('\n'.join(st.format(*pentry) for pentry in text))
51
52
53 def _add_program(args):

Subscribers

People subscribed via source and target branches