Skip to content

Commit 68b10ec

Browse files
committed
Changed things based on Dan's feedback, and re-arranged the project a lot to make accessing the data files more consistent. I've also added a setup.py file to make this a project, which also installs the SqlAlchemy and treelib modules.
1 parent fa2498a commit 68b10ec

31 files changed

+139
-71
lines changed

python-sqlite-sqlalchemy/README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
1-
# Pythoh / Sqlite /SqlAlchemy article
1+
# Python / Sqlite /SqlAlchemy article
22

33
This repository contains the content and example code
44
for the python/sqlite/sqlaclchemy article I'm writing
5-
for Real Python.
5+
for Real Python.
6+
7+
This project was built using Python 3.8.0
8+
9+
## Installing The Project
10+
11+
From the main folder take the following steps:
12+
13+
* Install a Python virtual environment for this project
14+
* Activate the virtual environment
15+
* Install the project:
16+
17+
```shell script
18+
python -m pip install -e .
19+
```
20+

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

Lines changed: 0 additions & 10 deletions
This file was deleted.
-20 KB
Binary file not shown.
File renamed without changes.

python-sqlite-sqlalchemy/build_data/code/README.md renamed to python-sqlite-sqlalchemy/project/build_data/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ by the example programs. There are three build programs:
77
* build_temp_data_sqlite.py
88
* build_author_book_publisher_sqlite.py
99

10-
## Build the Temperature CSV File
10+
## Build the Temperature CSV File
1111

1212
The build_temp_data_csv.py file builds a CSV data file
1313
(temp_data.csv) in the data directory
@@ -19,27 +19,27 @@ by the example programs. There are three build programs:
1919
temperature data, +/- 10 to make the data look variable
2020
and reasonable.
2121

22-
## Build the Temperature Database File
22+
## Build the Temperature Database File
2323

2424
The build_temp_data_sqlite.py file builds a Sqlite database
2525
from the previously created temp_data.csv file called
2626
temp_data.db in the data directory.
2727

28-
## Build the Author / Book / Publisher Database File
28+
## Build the Author / Book / Publisher Database File
2929

3030
The build_author_book_publisher_sqlite.py file builds
3131
a database from the data/author_book_publisher.csv file.
3232
This database contains the tables necessary to describe
3333
the data and the relationships between the data necessary
3434
for the examples.
3535

36-
## Directory Structure
36+
## Directory Structure
3737

3838
The directory structure is set up in such a way the
3939
build programs (as well as the examples) can find the
4040
data files needed for each one.
4141

42-
# Executing the Programs
42+
## Executing the Programs
4343

4444
* Activate your Python virtualenv
4545
* cd into the build/code directory

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

Whitespace-only changes.

python-sqlite-sqlalchemy/build_data/code/build_author_book_publisher_sqlite.py renamed to python-sqlite-sqlalchemy/project/build_data/build_author_book_publisher_sqlite.py

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

66
import os
77
import csv
8+
from pkg_resources import resource_filename
89
from sqlalchemy import create_engine
910
from sqlalchemy.orm import sessionmaker
10-
from lib.models import Base
11-
from lib.models import Author
12-
from lib.models import Book
13-
from lib.models import Publisher
11+
from project.modules.models import Base
12+
from project.modules.models import Author
13+
from project.modules.models import Book
14+
from project.modules.models import Publisher
1415

1516

1617
def get_author_book_publisher_data(filepath):
@@ -68,12 +69,11 @@ def main():
6869
print("starting")
6970

7071
# get the author/book/publisher data into a dictionary structure
71-
path = os.path.dirname(os.path.abspath(__file__))
72-
csv_filepath = os.path.join(path, "../data/author_book_publisher.csv")
72+
csv_filepath = resource_filename("project.data", "author_book_publisher.csv")
7373
author_book_publisher_data = get_author_book_publisher_data(csv_filepath)
7474

7575
# get the filepath to the database file
76-
sqlite_filepath = os.path.join(path, "../data/author_book_publisher.db")
76+
sqlite_filepath = resource_filename("project.data", "author_book_publisher.db")
7777

7878
# does the database exist?
7979
if os.path.exists(sqlite_filepath):

python-sqlite-sqlalchemy/build_data/code/build_temp_data_csv.py renamed to python-sqlite-sqlalchemy/project/build_data/build_temp_data_csv.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import os
21
import csv
32
import datetime
43
from random import randint
4+
from pkg_resources import resource_filename
55

66

77
start_date = datetime.datetime.strptime("2019-01-02", "%Y-%m-%d")
@@ -86,8 +86,8 @@ def offset_temp(temperature):
8686

8787
def main():
8888
# create the CSV file
89-
path = os.path.dirname(os.path.abspath(__file__))
90-
csv_filepath = os.path.join(path, "../data/temp_data.csv")
89+
csv_filepath = resource_filename("project.data", "temp_data.csv")
90+
9191
with open(csv_filepath, "w") as data_fh:
9292

9393
# create the writer

python-sqlite-sqlalchemy/build_data/code/build_temp_data_sqlite.py renamed to python-sqlite-sqlalchemy/project/build_data/build_temp_data_sqlite.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import os
66
import csv
7+
from pkg_resources import resource_filename
78
from datetime import datetime
89
from sqlalchemy import create_engine
910
from sqlalchemy import Column, Integer, String, Date, Float
@@ -52,12 +53,11 @@ def main():
5253
print("starting")
5354

5455
# get the temperature data into a dictionary structure
55-
path = os.path.dirname(os.path.abspath(__file__))
56-
csv_filepath = os.path.join(path, "../data/temp_data.csv")
56+
csv_filepath = resource_filename("project.data", "temp_data.csv")
5757
temperature_data = get_temperature_data(csv_filepath)
5858

5959
# get the filepath to the database file
60-
sqlite_filepath = os.path.join(path, "../data/temp_data.db")
60+
sqlite_filepath = resource_filename("project.data", "temp_data.db")
6161

6262
# does the database exist?
6363
if os.path.exists(sqlite_filepath):

python-sqlite-sqlalchemy/project/data/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)