提交 f55d93f6 编写于 作者: D Doug Farrell

Updating code to reflect comments in PR

上级 b8bc3e03
# Python / Sqlite /SqlAlchemy article
# Python / SQLite /SqlAlchemy article
This repository contains the content and example code
for the python / sqlite / sqlaclchemy article I'm writing
for the Python / SQLite / SqlAclchemy article I'm writing
for Real Python.
This project was built using Python 3.8.0
......@@ -11,7 +11,7 @@ This project was built using Python 3.8.0
I use the `pyenv` tool to install Python versions on my Mac. I find it a very useful tool, and the instructions that follow use it, and are based on having Python version 3.8.0 installed using the following command:
```shell
pyenv install 3.8.0
$ pyenv install 3.8.0
```
## Installing The Project
......
......@@ -57,21 +57,16 @@ def add_new_book(data, author_name, book_title, publisher_name):
"""Adds a new book to the system"""
# Does the book exist?
if book_title in data["title"].values:
raise ValueError("Book exists", book_title)
# Does the author exist?
first_name, _, last_name = author_name.partition(" ")
if not any(
data["first_name"].str.contains(first_name)
& data["last_name"].str.contains(last_name)
if any(
(data.first_name == first_name)
& (data.last_name == last_name)
& (data.title == book_title)
& (data.publisher == publisher_name)
):
raise ValueError("No author found", author_name)
# Does the publisher exist?
if publisher_name not in data["publisher"].values:
raise ValueError("No publisher found", publisher_name)
raise ValueError(
"New item exists", author_name, book_title, publisher_name
)
# Add the new book
return data.append(
{
......
"""
This program gathers information from the temp_data.csv file about temperature
This program gathers information from the author_book_publisher.db
SQLite database file
"""
from uuid import uuid4
from pkg_resources import resource_filename
from importlib import resources
from sqlalchemy import and_, create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.sql import asc, desc, func
......@@ -93,9 +92,10 @@ def add_new_book(session, author_name, book_title, publisher_name):
.filter(Publisher.name == publisher_name)
.one_or_none()
)
# Does book not exist, and the author or publisher also not existexist?
if book is not None and not (author is None or publisher is None):
raise Exception(
# Does book, author and publisher already exist?
print(book)
if book is not None and author is not None and publisher is not None:
raise ValueError(
"New item exists", author_name, book_title, publisher_name
)
# Create the book
......@@ -129,14 +129,12 @@ def output_author_hierarchy(authors):
)
for book in author.books:
authors_tree.create_node(
f"{book.title}",
f"{book.title}",
book.title,
book.title,
parent=f"{author.first_name} {author.last_name}",
)
for publisher in book.publishers:
authors_tree.create_node(
f"{publisher.name}", uuid4(), parent=f"{book.title}"
)
authors_tree.create_node(publisher.name, parent=book.title)
# Output the hierarchical authors data
authors_tree.show()
......@@ -145,16 +143,16 @@ def main():
"""Main entry point of program"""
# Connect to the database using SqlAlchemy
sqlite_filepath = resource_filename(
with resources.path(
"project.data", "author_book_publisher.db"
)
engine = create_engine(f"sqlite:///{sqlite_filepath}")
) as sqlite_filepath:
engine = create_engine(f"sqlite:///{sqlite_filepath}")
Session = sessionmaker()
Session.configure(bind=engine)
session = Session()
# Get the total number of books printed by each publisher
books_by_publisher = get_books_by_publishers(session)
books_by_publisher = get_books_by_publishers(session, ascending=False)
for row in books_by_publisher:
print(f"Publisher: {row.name}, total books: {row.total_books}")
print()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册