Skip to content

Commit 9ba65da

Browse files
authored
Merge pull request #99 from realpython/python-sqlite-sqlalchemy
Python sqlite sqlalchemy
2 parents 6c80e2e + 8201d46 commit 9ba65da

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1542
-1195
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,6 @@ ENV/
108108

109109
# Mac
110110
*.DS_Store
111+
112+
# VS Code workspace
113+
*.code-workspace

python-sqlite-sqlalchemy/README.md

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
1-
# Python / Sqlite /SqlAlchemy article
1+
# Python / SQLite /SQLAlchemy article
22

3-
This repository contains the content and example code
4-
for the python/sqlite/sqlaclchemy article I'm writing
3+
This repository contains the content and example code
4+
for the Python / SQLite / SQLAlchemy article I'm writing
55
for Real Python.
66

77
This project was built using Python 3.8.0
88

9+
## Python Virtualenv
10+
11+
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:
12+
13+
```shell
14+
$ pyenv install 3.8.0
15+
```
16+
917
## Installing The Project
1018

11-
From the main folder take the following steps:
19+
From the main folder take the following steps (on a Mac):
1220

1321
* Install a Python virtual environment for this project
22+
* `pyenv local 3.8.0`
23+
* `python -m venv .venv`
1424
* Activate the virtual environment
25+
* `source .venv/bin/activate`
1526
* Install the project:
16-
17-
```shell script
18-
python -m pip install -e .
19-
```
20-
27+
* `python -m pip install -e .`

python-sqlite-sqlalchemy/project/build_data/README.md

Lines changed: 21 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,31 @@
11
# Build Files Used By The Examples
22

33
The code in this directory builds the various data files used
4-
by the example programs. There are three build programs:
4+
by the example programs. There is one build program:
5+
6+
* build_author_book_publisher_sqlite.py
57

6-
* build_temp_data_csv.py
7-
* build_temp_data_sqlite.py
8-
* build_author_book_publisher_sqlite.py
9-
10-
## Build the Temperature CSV File
11-
12-
The build_temp_data_csv.py file builds a CSV data file
13-
(temp_data.csv) in the data directory
14-
containing temperature samples taken by students in a class.
15-
The top row contains the labels, the students name followed
16-
by a date value for each Wednesday of the week for a year.
17-
18-
It then creates data for each sample based on a table of
19-
temperature data, +/- 10 to make the data look variable
20-
and reasonable.
21-
22-
## Build the Temperature Database File
23-
24-
The build_temp_data_sqlite.py file builds a Sqlite database
25-
from the previously created temp_data.csv file called
26-
temp_data.db in the data directory.
27-
288
## Build the Author / Book / Publisher Database File
29-
30-
The build_author_book_publisher_sqlite.py file builds
31-
a database from the data/author_book_publisher.csv file.
32-
This database contains the tables necessary to describe
33-
the data and the relationships between the data necessary
34-
for the examples.
35-
9+
10+
The build_author_book_publisher_sqlite.py file builds
11+
a database from the `data/author_book_publisher.csv` file.
12+
This database contains the rows of comma delimited text to describe
13+
the data and the relationships between the data necessary
14+
for the examples.
15+
3616
## Directory Structure
37-
38-
The directory structure is set up in such a way the
39-
build programs (as well as the examples) can find the
40-
data files needed for each one.
41-
17+
18+
The directory structure is set up in such a way the
19+
build programs (as well as the examples) can find the
20+
data files needed for each one.
21+
4222
## Executing the Programs
43-
44-
* Activate your Python virtualenv
45-
* cd into the build/code directory
46-
* python build_temp_data_csv.py - builds the csv data file
47-
* python build_temp_data_sqlite.py - builds the temperature data from the csv file
48-
* python build_author_book_publisher_sqlite.py - builds the author_book_publisher.db database file
23+
24+
Follow these steps to build the `data/author_book_publisher.db` database file.
25+
26+
* Activate your Python virtualenv
27+
* cd into the build/code directory
28+
* python build_author_book_publisher_sqlite.py - builds the author_book_publisher.db database file
4929

5030

5131

python-sqlite-sqlalchemy/project/build_data/build_author_book_publisher_sqlite.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import os
77
import csv
8-
from pkg_resources import resource_filename
8+
from importlib import resources
99
from sqlalchemy import create_engine
1010
from sqlalchemy.orm import sessionmaker
1111
from project.modules.models import Base
@@ -30,11 +30,13 @@ def populate_database(session, author_book_publisher_data):
3030

3131
author = (
3232
session.query(Author)
33-
.filter(Author.lname == row["lname"])
33+
.filter(Author.last_name == row["last_name"])
3434
.one_or_none()
3535
)
3636
if author is None:
37-
author = Author(fname=row["fname"], lname=row["lname"])
37+
author = Author(
38+
first_name=row["first_name"], last_name=row["last_name"]
39+
)
3840
session.add(author)
3941

4042
book = (
@@ -69,19 +71,19 @@ def main():
6971
print("starting")
7072

7173
# get the author/book/publisher data into a dictionary structure
72-
csv_filepath = resource_filename(
74+
with resources.path(
7375
"project.data", "author_book_publisher.csv"
74-
)
75-
author_book_publisher_data = get_author_book_publisher_data(csv_filepath)
76+
) as csv_filepath:
77+
data = get_author_book_publisher_data(csv_filepath)
78+
author_book_publisher_data = data
7679

7780
# get the filepath to the database file
78-
sqlite_filepath = resource_filename(
81+
with resources.path(
7982
"project.data", "author_book_publisher.db"
80-
)
81-
82-
# does the database exist?
83-
if os.path.exists(sqlite_filepath):
84-
os.remove(sqlite_filepath)
83+
) as sqlite_filepath:
84+
# does the database exist?
85+
if os.path.exists(sqlite_filepath):
86+
os.remove(sqlite_filepath)
8587

8688
# create the database
8789
engine = create_engine(f"sqlite:///{sqlite_filepath}")

python-sqlite-sqlalchemy/project/build_data/build_temp_data_csv.py

Lines changed: 0 additions & 114 deletions
This file was deleted.

python-sqlite-sqlalchemy/project/build_data/build_temp_data_sqlite.py

Lines changed: 0 additions & 78 deletions
This file was deleted.
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
fname,lname,title,publisher
2-
Issac,Asimov,Foundation,Random House
1+
first_name,last_name,title,publisher
2+
Isaac,Asimov,Foundation,Random House
33
Pearl,Buck,The Good Earth,Random House
44
Pearl,Buck,The Good Earth,Simon & Schuster
55
Tom,Clancy,The Hunt For Red October,Berkley
66
Tom,Clancy,Patriot Games,Simon & Schuster
77
Stephen,King,It,Random House
88
Stephen,King,It,Penguin Random House
99
Stephen,King,Dead Zone,Random House
10-
Stephen,King,The Shinning,Penguin Random House
10+
Stephen,King,The Shining,Penguin Random House
1111
John,Le Carre,"Tinker, Tailor, Solider, Spy: A George Smiley Novel",Berkley
1212
Alex,Michaelides,The Silent Patient,Simon & Schuster
1313
Carol,Shaben,Into The Abyss,Simon & Schuster
0 Bytes
Binary file not shown.
864 KB
Binary file not shown.

python-sqlite-sqlalchemy/project/data/temp_data.csv

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)