Пример #1
0
    def _save_to_disk(seft, books):
        tmp_table_name = seft.book_table + ".tmp"

        with open(tmp_table_name, mode="w") as file:
            writer = csv.DictWriter(file, fieldnames=Books.schema())
            writer.writerows(books)

        os.remove(seft.book_table)
        os.rename(tmp_table_name, seft.book_table)
Пример #2
0
def _print_list(book_list):

    table = BeautifulTable()
    headers = [name.upper() for name in Books.schema()]
    table.column_headers = headers

    for book_item in book_list:
        table.append_row([
            book_item["name"], book_item["author"], book_item["year"],
            book_item["publisher"], book_item["topic"], book_item["uid"]
        ])

    # This was an effort for padding the columns
    # max_len = 0
    # for i in table["NAME"]:
    #     if len(str(i)) >= max_len:
    #         max_len = len(str(i))

    # table.set_padding_widths(5)
    # # table.left_padding_widths["NAME"] = mayor - len("NAME")
    # # table.right_padding_widths["NAME"] = mayor - len("NAME")

    click.echo(table)
Пример #3
0
    def list_books(seft):
        with open(seft.book_table, mode="r") as file:
            reader = csv.DictReader(file, fieldnames=Books.schema())

            return list(reader)
Пример #4
0
 def create_book(seft, book):
     with open(seft.book_table, mode="a") as file:
         write = csv.DictWriter(file, fieldnames=Books.schema())
         write.writerow(book.to_dict())