Skip to content

Commit f206017

Browse files
committed
Refactor.
1 parent b33d58d commit f206017

File tree

7 files changed

+377
-11
lines changed

7 files changed

+377
-11
lines changed

main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""App entry point."""
2-
from tutorial import init_script
2+
from pandas_sqlalchemy_tutorial import init_script
33

44
script = init_script()
55

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
"""Fetch and insert data into SQL database using Pandas."""
2-
from .read import load_csv_data
3-
from .database import Database
42
from config import (
53
SQLALCHEMY_DATABASE_URI,
64
SQLALCHEMY_ENGINE_OPTIONS,
75
CSV_FILE
86
)
7+
from .read import load_csv_data
8+
from .database import Database
99

1010

1111
def init_script():
1212
"""Demonstrate Pandas' SQLAlchemy integration."""
1313
jobs_df = load_csv_data(CSV_FILE)
1414
db = Database(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_ENGINE_OPTIONS)
15-
db.upload_dataframe_to_sql(jobs_df, 'nyc_jobs')
16-
db.get_dataframe_from_sql('nyc_jobs')
15+
upload_result = db.upload_dataframe_to_sql(jobs_df, 'nyc_jobs')
16+
download_result = db.get_dataframe_from_sql('nyc_jobs')
17+
return upload_result, download_result

tutorial/database.py renamed to pandas_sqlalchemy_tutorial/database.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,16 @@ def upload_dataframe_to_sql(self, csv_df, table_name):
3737
"posting_updated": DateTime
3838
}
3939
)
40-
print(f'Loaded {len(csv_df)} rows into {table_name} table.')
40+
result = f'Loaded {len(csv_df)} rows INTO {table_name} table.'
41+
print(result)
42+
return result
4143

4244
def get_dataframe_from_sql(self, table_name):
4345
"""Create DataFrame form SQL table."""
4446
table_df = pd.read_sql_table(
4547
table_name,
4648
con=self.engine
4749
)
48-
print(f'Loaded {len(table_df)} rows from {table_name}.')
50+
result = f'Loaded {len(table_df)} rows FROM {table_name}.'
4951
print(table_df.info())
50-
return table_df
51-
52+
return result
File renamed without changes.

poetry.lock

Lines changed: 355 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[tool.poetry]
2-
name = "Pandas SQLAlchemy Tutorial"
2+
name = "pandas_sqlalchemy_tutorial"
33
version = "0.1.0"
44
description = "Load or insert data into a SQL database using Pandas DataFrames."
55
authors = ["Todd Birchard <toddbirchard@gmail.com>"]
@@ -18,7 +18,7 @@ keywords = ["Python",
1818
[tool.poetry.dependencies]
1919
python = "^3.8"
2020
pandas = "*"
21-
sqllchemy = "*"
21+
sqlalchemy = "*"
2222
pymysql = "*"
2323
python-dotenv = "*"
2424

requirements.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1+
attrs==19.3.0
2+
mock==4.0.2
3+
more-itertools==8.3.0
14
numpy==1.18.5
5+
packaging==20.4
26
pandas==1.0.4
7+
pluggy==0.13.1
8+
py==1.8.1
39
PyMySQL==0.9.3
10+
pyparsing==2.4.7
11+
pytest==5.4.3
412
python-dateutil==2.8.1
513
python-dotenv==0.13.0
614
pytz==2020.1
715
six==1.15.0
816
SQLAlchemy==1.3.17
17+
wcwidth==0.2.4

0 commit comments

Comments
 (0)