|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +""" |
| 4 | +A set of classes to facilitate report writing |
| 5 | +""" |
| 6 | + |
| 7 | +import math |
| 8 | +import operator |
| 9 | +from uuid import uuid4 |
| 10 | + |
| 11 | + |
| 12 | +class Row: |
| 13 | + """ |
| 14 | + This class represents a single row |
| 15 | + with ID, first name, last name and state attributes |
| 16 | + """ |
| 17 | + def __init__(self, fname, lname, state): |
| 18 | + self.row_id = str(uuid4()) # randomly generated unique ID |
| 19 | + self.fname = fname |
| 20 | + self.lname = lname |
| 21 | + self.state = state |
| 22 | + |
| 23 | + def __str__(self): |
| 24 | + return f"| {self.row_id} | {self.fname + ' ' + self.lname:<15} | {self.state} |" |
| 25 | + |
| 26 | + |
| 27 | +class Report: |
| 28 | + def __init__(self, limit: int): |
| 29 | + self.limit = limit |
| 30 | + self.rows = [] |
| 31 | + |
| 32 | + def add_row(self, row): |
| 33 | + """Add a row object to the list""" |
| 34 | + pass |
| 35 | + |
| 36 | + def remove_row(self, row_id): |
| 37 | + """Remove a row object by the row ID""" |
| 38 | + pass |
| 39 | + |
| 40 | + def size(self): |
| 41 | + """Return how many total rows the report has""" |
| 42 | + pass |
| 43 | + |
| 44 | + def get_number_of_pages(self): |
| 45 | + """ |
| 46 | + Get how many pages the report has; this will be based on limit variable. |
| 47 | + If your limit=4 and rows list has 6 records then there are two pages: page1 has 4 records, page2 has 2 records |
| 48 | + hint: you'll want to round up |
| 49 | + """ |
| 50 | + pass |
| 51 | + |
| 52 | + def get_paged_rows(self, sort_field, page): |
| 53 | + """Return a list of rows for a specific page number |
| 54 | + :param sort_field: field to sort on, "name" or "-name" (descending) |
| 55 | + :param page: specific page for returning data |
| 56 | + :return: list of row objects for specific page |
| 57 | +
|
| 58 | + Hints: |
| 59 | + 1. You'll want to determine if sort is reversed or not (remember that |
| 60 | + sorted() takes in param for that) this is based on if the fields |
| 61 | + start with a minus sign for DESCENDING sort |
| 62 | + 2. When sorting on passed in field you can use handy `operator` library |
| 63 | + with `attrgetter` method (look up official docs) |
| 64 | + 3. To actually determine what rows belong on the specific page you'll be |
| 65 | + using list slicing (remember the slicing lab?) |
| 66 | +
|
| 67 | + Here is an illustration to help with the code logic: |
| 68 | +
|
| 69 | + The list has 6 rows => [<row1>, <row2>, <row3>, <row4>, <row5>, <row6>] |
| 70 | + for page=2 we expect to get => [<row5>, <row6>] |
| 71 | + with slicing you'll want to offset your list by 4 in this case |
| 72 | + (extra hint: we can define offset as `offset = (page - 1) * self.limit`) |
| 73 | +
|
| 74 | + """ |
| 75 | + pass |
| 76 | + |
| 77 | + |
| 78 | +if __name__ == "__main__": |
| 79 | + |
| 80 | + report = Report(4) |
| 81 | + |
| 82 | + report.add_row(Row("natasha", "smith", "WA")) |
| 83 | + report.add_row(Row("devin", "lei", "WA")) |
| 84 | + report.add_row(Row("bob", "li", "CA")) |
| 85 | + report.add_row(Row("tracy", "jones", "OR")) |
| 86 | + report.add_row(Row("johny", "jakes", "WA")) |
| 87 | + report.add_row(Row("derek", "wright", "WA")) |
| 88 | + |
| 89 | + |
| 90 | + def run_report(sort_field): |
| 91 | + print(f"... PAGED REPORT SORTED BY: '{sort_field}'...") |
| 92 | + page = 1 |
| 93 | + while True: |
| 94 | + rows = report.get_paged_rows(sort_field, page=page) |
| 95 | + |
| 96 | + if not rows: |
| 97 | + break |
| 98 | + |
| 99 | + input(f"Press ENTER to see page {page}") |
| 100 | + |
| 101 | + print(f"PAGE: {page} of {report.get_number_of_pages()}") |
| 102 | + print("---------------------------------------------------------------") |
| 103 | + |
| 104 | + for row in rows: |
| 105 | + print(row) |
| 106 | + |
| 107 | + print("---------------------------------------------------------------") |
| 108 | + |
| 109 | + page += 1 |
| 110 | + |
| 111 | + |
| 112 | + run_report("fname") |
| 113 | + |
| 114 | + print(f"\n\nRemoving student: {report.rows[1].fname} [{report.rows[1].row_id}]... \n\n") |
| 115 | + |
| 116 | + report.remove_row(report.rows[1].row_id) |
| 117 | + |
| 118 | + run_report("-fname") |
0 commit comments