From bd30fc37376a829551b5b69c40d6aa953f39c684 Mon Sep 17 00:00:00 2001 From: Steven Westmoreland <61995746+StevenWestmoreland@users.noreply.github.com> Date: Sat, 25 Jul 2020 15:40:45 -0400 Subject: [PATCH 01/15] Create insert_titanic.py --- module2-sql-for-analysis/insert_titanic.py | 51 ++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 module2-sql-for-analysis/insert_titanic.py diff --git a/module2-sql-for-analysis/insert_titanic.py b/module2-sql-for-analysis/insert_titanic.py new file mode 100644 index 00000000..dd7ab397 --- /dev/null +++ b/module2-sql-for-analysis/insert_titanic.py @@ -0,0 +1,51 @@ +import pandas as pd +import os +import psycopg2 +from dotenv import load_dotenv + +load_dotenv() + +DB_NAME = os.getenv('DB_NAME') +DB_USER = os.getenv('DB_USER') +DB_PASS = os.getenv('DB_PASS') +DB_HOST = os.getenv('DB_HOST') + +conn = psycopg2.connect(dbname=DB_NAME, + user=DB_USER, + password=DB_PASS, + host=DB_HOST) + +cursor = conn.cursor() + +create_titanic_table = ''' + CREATE TABLE IF NOT EXISTS titanic ( + pass_id SERIAL PRIMARY KEY, + survived INT, + pclass INT, + name VARCHAR(200), + sex VARCHAR(20), + age INT, + siblings_spouses INT, + parents_children INT, + fare NUMERIC(30) + ) +''' + +cursor.execute(create_titanic_table) +conn.commit() + +titanic_db = pd.read_csv('titanic.csv') +titanic_db['Name'] = titanic_db['Name'].apply(lambda x: x.replace("'","")) + +data = list(titanic_db.to_records()) + +for item in data: + + insert_query = f''' INSERT INTO titanic + (pass_id, survived, pclass, name, sex, age, siblings_spouses, + parents_children, fare) VALUES {item} + ''' + + cursor.execute(insert_query) + +conn.commit() From 6a46d21e2266f83eab04fa3ad7a2039c31696fd4 Mon Sep 17 00:00:00 2001 From: Steven Westmoreland <61995746+StevenWestmoreland@users.noreply.github.com> Date: Sat, 25 Jul 2020 15:41:38 -0400 Subject: [PATCH 02/15] Create elephant_queries.py --- module2-sql-for-analysis/elephant_queries.py | 67 ++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 module2-sql-for-analysis/elephant_queries.py diff --git a/module2-sql-for-analysis/elephant_queries.py b/module2-sql-for-analysis/elephant_queries.py new file mode 100644 index 00000000..ba3d21cc --- /dev/null +++ b/module2-sql-for-analysis/elephant_queries.py @@ -0,0 +1,67 @@ +import os +import psycopg2 +from dotenv import load_dotenv + +load_dotenv() + +DB_NAME = os.getenv('DB_NAME') +DB_USER = os.getenv('DB_USER') +DB_PASS = os.getenv('DB_PASS') +DB_HOST = os.getenv('DB_HOST') + +### Connect to ElephantSQL-hosted PostgreSQL +conn = psycopg2.connect(dbname=DB_NAME, + user=DB_USER, + password=DB_PASS, + host=DB_HOST) + +### A "cursor", a structure to iterate over db records to perform queries +cursor = conn.cursor() + +### An example query +cursor.execute('SELECT * from test_table;') + +### Note - nothing happened yet! We need to actually *fetch* from the cursor +results = cursor.fetchone() +# print(results) + + +################ Connect to SQLite3 DB for RPG data ##################### + +import sqlite3 + +sl_conn = sqlite3.connect('rpg_db.sqlite3') +sl_cursor = sl_conn.cursor() +characters = sl_cursor.execute('SELECT * FROM charactercreator_character LIMIT 10').fetchall() +print(characters) + +################# Create Character Table in PostGRES #################### + +create_character_table_query = ''' +CREATE TABLE IF NOT EXISTS rpg_characters ( + character_id SERIAL PRIMARY KEY, + name VARCHAR(30), + level INT, + exp INT, + hp INT, + strength INT, + intelligence INT, + dexterity INT, + wisdom INT +) +''' + +cursor.execute(create_character_table_query) +conn.commit() + +################# Insert Character Data in PostGRES #################### + +for character in characters: + + insert_query = f''' INSERT INTO rpg_characters + (character_id, name, level, exp, hp, strength, intelligence, dexterity, wisdom) VALUES + {character} + ''' + cursor.execute(insert_query) + +conn.commit() From bea8648ce1a2a076de4bef1e20773d712d5bd3a7 Mon Sep 17 00:00:00 2001 From: Steven Westmoreland <61995746+StevenWestmoreland@users.noreply.github.com> Date: Sat, 25 Jul 2020 15:43:35 -0400 Subject: [PATCH 03/15] Update insert_titanic.py --- module2-sql-for-analysis/insert_titanic.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/module2-sql-for-analysis/insert_titanic.py b/module2-sql-for-analysis/insert_titanic.py index dd7ab397..67fbc3c1 100644 --- a/module2-sql-for-analysis/insert_titanic.py +++ b/module2-sql-for-analysis/insert_titanic.py @@ -5,6 +5,8 @@ load_dotenv() +########## Connect to PostGRES ########## + DB_NAME = os.getenv('DB_NAME') DB_USER = os.getenv('DB_USER') DB_PASS = os.getenv('DB_PASS') @@ -17,6 +19,8 @@ cursor = conn.cursor() +########## Create Titanic SQL Table ########## + create_titanic_table = ''' CREATE TABLE IF NOT EXISTS titanic ( pass_id SERIAL PRIMARY KEY, @@ -34,6 +38,8 @@ cursor.execute(create_titanic_table) conn.commit() +########## Read in CSV and populate Titanic Table ########## + titanic_db = pd.read_csv('titanic.csv') titanic_db['Name'] = titanic_db['Name'].apply(lambda x: x.replace("'","")) From f425bfbe293c0d27936c607e8528df194999d70e Mon Sep 17 00:00:00 2001 From: Steven Westmoreland <61995746+StevenWestmoreland@users.noreply.github.com> Date: Sat, 25 Jul 2020 15:48:39 -0400 Subject: [PATCH 04/15] Create Pipfile.lock --- module2-sql-for-analysis/Pipfile.lock | 65 +++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 module2-sql-for-analysis/Pipfile.lock diff --git a/module2-sql-for-analysis/Pipfile.lock b/module2-sql-for-analysis/Pipfile.lock new file mode 100644 index 00000000..aed2e55d --- /dev/null +++ b/module2-sql-for-analysis/Pipfile.lock @@ -0,0 +1,65 @@ +{ + "_meta": { + "hash": { + "sha256": "d2f7f8cb967ddbd2921f1a455faef3341e4ca3985397f61c4320aa4cf8e395fe" + }, + "pipfile-spec": 6, + "requires": { + "python_version": "3.8" + }, + "sources": [ + { + "name": "pypi", + "url": "https://pypi.org/simple", + "verify_ssl": true + } + ] + }, + "default": { + "psycopg2-binary": { + "hashes": [ + "sha256:008da3ab51adc70a5f1cfbbe5db3a22607ab030eb44bcecf517ad11a0c2b3cac", + "sha256:07cf82c870ec2d2ce94d18e70c13323c89f2f2a2628cbf1feee700630be2519a", + "sha256:08507efbe532029adee21b8d4c999170a83760d38249936038bd0602327029b5", + "sha256:107d9be3b614e52a192719c6bf32e8813030020ea1d1215daa86ded9a24d8b04", + "sha256:17a0ea0b0eabf07035e5e0d520dabc7950aeb15a17c6d36128ba99b2721b25b1", + "sha256:3286541b9d85a340ee4ed42732d15fc1bb441dc500c97243a768154ab8505bb5", + "sha256:3939cf75fc89c5e9ed836e228c4a63604dff95ad19aed2bbf71d5d04c15ed5ce", + "sha256:40abc319f7f26c042a11658bf3dd3b0b3bceccf883ec1c565d5c909a90204434", + "sha256:51f7823f1b087d2020d8e8c9e6687473d3d239ba9afc162d9b2ab6e80b53f9f9", + "sha256:6bb2dd006a46a4a4ce95201f836194eb6a1e863f69ee5bab506673e0ca767057", + "sha256:702f09d8f77dc4794651f650828791af82f7c2efd8c91ae79e3d9fe4bb7d4c98", + "sha256:7036ccf715925251fac969f4da9ad37e4b7e211b1e920860148a10c0de963522", + "sha256:7b832d76cc65c092abd9505cc670c4e3421fd136fb6ea5b94efbe4c146572505", + "sha256:8f74e631b67482d504d7e9cf364071fc5d54c28e79a093ff402d5f8f81e23bfa", + "sha256:930315ac53dc65cbf52ab6b6d27422611f5fb461d763c531db229c7e1af6c0b3", + "sha256:96d3038f5bd061401996614f65d27a4ecb62d843eb4f48e212e6d129171a721f", + "sha256:a20299ee0ea2f9cca494396ac472d6e636745652a64a418b39522c120fd0a0a4", + "sha256:a34826d6465c2e2bbe9d0605f944f19d2480589f89863ed5f091943be27c9de4", + "sha256:a69970ee896e21db4c57e398646af9edc71c003bc52a3cc77fb150240fefd266", + "sha256:b9a8b391c2b0321e0cd7ec6b4cfcc3dd6349347bd1207d48bcb752aa6c553a66", + "sha256:ba13346ff6d3eb2dca0b6fa0d8a9d999eff3dcd9b55f3a890f12b0b6362b2b38", + "sha256:bb0608694a91db1e230b4a314e8ed00ad07ed0c518f9a69b83af2717e31291a3", + "sha256:c8830b7d5f16fd79d39b21e3d94f247219036b29b30c8270314c46bf8b732389", + "sha256:cac918cd7c4c498a60f5d2a61d4f0a6091c2c9490d81bc805c963444032d0dab", + "sha256:cc30cb900f42c8a246e2cb76539d9726f407330bc244ca7729c41a44e8d807fb", + "sha256:ccdc6a87f32b491129ada4b87a43b1895cf2c20fdb7f98ad979647506ffc41b6", + "sha256:d1a8b01f6a964fec702d6b6dac1f91f2b9f9fe41b310cbb16c7ef1fac82df06d", + "sha256:e004db88e5a75e5fdab1620fb9f90c9598c2a195a594225ac4ed2a6f1c23e162", + "sha256:eb2f43ae3037f1ef5e19339c41cf56947021ac892f668765cd65f8ab9814192e", + "sha256:fa466306fcf6b39b8a61d003123d442b23707d635a5cb05ac4e1b62cc79105cd" + ], + "index": "pypi", + "version": "==2.8.5" + }, + "python-dotenv": { + "hashes": [ + "sha256:8c10c99a1b25d9a68058a1ad6f90381a62ba68230ca93966882a4dbc3bc9c33d", + "sha256:c10863aee750ad720f4f43436565e4c1698798d763b63234fb5021b6c616e423" + ], + "index": "pypi", + "version": "==0.14.0" + } + }, + "develop": {} +} From da8d1c5972c13acb2a57db5caf8020be850b10b3 Mon Sep 17 00:00:00 2001 From: Steven Westmoreland <61995746+StevenWestmoreland@users.noreply.github.com> Date: Sat, 25 Jul 2020 15:49:04 -0400 Subject: [PATCH 05/15] Create Pipfile --- module2-sql-for-analysis/Pipfile | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 module2-sql-for-analysis/Pipfile diff --git a/module2-sql-for-analysis/Pipfile b/module2-sql-for-analysis/Pipfile new file mode 100644 index 00000000..53c32968 --- /dev/null +++ b/module2-sql-for-analysis/Pipfile @@ -0,0 +1,13 @@ +[[source]] +name = "pypi" +url = "https://pypi.org/simple" +verify_ssl = true + +[dev-packages] + +[packages] +python-dotenv = "*" +psycopg2-binary = "*" + +[requires] +python_version = "3.8" From 2ffec92d2ce6a0d6a9eb69ab517998da6acf0bd8 Mon Sep 17 00:00:00 2001 From: Steven Westmoreland <61995746+StevenWestmoreland@users.noreply.github.com> Date: Mon, 10 Aug 2020 17:10:48 -0400 Subject: [PATCH 06/15] initial assignment commit --- .../buddymove_holidayiq.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 module1-introduction-to-sql/buddymove_holidayiq.py diff --git a/module1-introduction-to-sql/buddymove_holidayiq.py b/module1-introduction-to-sql/buddymove_holidayiq.py new file mode 100644 index 00000000..adff4c47 --- /dev/null +++ b/module1-introduction-to-sql/buddymove_holidayiq.py @@ -0,0 +1,15 @@ +import sqlite3 +import pandas as pd + +connect = sqlite3.connect('buddymove_holidayiq.sqlite3') +curs = connect.cursor() + +df = pd.read_csv('buddymove_holidayiq.csv') + +df.to_sql('buddy', connect) + +curs.execute('SELECT COUNT(*) FROM buddy') +print(f'There are {curs.fetchall()[0][0]} rows in the database.') + +curs.execute('SELECT COUNT(*) FROM buddy WHERE Nature > 99 AND Shopping > 99') +print(f'{curs.fetchall()[0][0]} users reviewed at least 100 in both Nature and Shopping.') \ No newline at end of file From 557650b157ff6438fe47381ea60564dec954200c Mon Sep 17 00:00:00 2001 From: Steven Westmoreland <61995746+StevenWestmoreland@users.noreply.github.com> Date: Mon, 10 Aug 2020 17:11:16 -0400 Subject: [PATCH 07/15] initial assignment commit --- .../buddymove_holidayiq.sqlite3 | Bin 0 -> 20480 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 module1-introduction-to-sql/buddymove_holidayiq.sqlite3 diff --git a/module1-introduction-to-sql/buddymove_holidayiq.sqlite3 b/module1-introduction-to-sql/buddymove_holidayiq.sqlite3 new file mode 100644 index 0000000000000000000000000000000000000000..b4e7a00e46aca5ad888da58b8b63c53c3d6f7f21 GIT binary patch literal 20480 zcmeI3d3aNGy2szF&2knj&?HS-YZV1tXx1hmQkDpWLTDSHDYV#z(iU1uLur8)?LiCL z;uN*$fLfL6h)Z4Sf+8;Hhzg@3%BZM=`+~cUI^&)D%zb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6 z6C`;NO^z3#7M|u5FVZW>OItha$Wf-HI)93De0|tcg_p}A*p{nY#N%;rPjXK0a=r96 zildv#x+fhRU=4yH935T?3E1IOE@YTY{RxhPIQ1h`Z9edu174K3e z)6VI2{y4`0tg0$f??X-{l*;`W$A0XZyzFAh1yjNMIQ5ZNKJprYT^BM;ZcmSLj3w9C ziF7L#bb#e1C*={2NyXt$19)D$T#BD_oY%vgyEDtN9?8X&qxeR$YvCb|@k+(pi-IfD z>r%X_@L|RaLs=C#t-&Vf$yGcwoZi76fv_m9`BHoQu*)W+)pa{K6-D*7sm@y`GZ<$NBGMtl#)p3DY(;p)Tu-A?6P9*&E5H|K6FibHrk;@~C{O6IPGyErW% zKd$xCKm9Ic0&PIcVlQhD!28@V-Ubhm+u(Q>PfG4ij&bo6IM7rNhjNv3QtAJ|F5&6up5{57NERzYupR+fwv;PTffH^+(+-0T4&^kK7|$Q<;M6Zvc64;E_%C{`I~@Q3 literal 0 HcmV?d00001 From 8b6c94eac564f76f0f864a6d6a8f793861e3e80b Mon Sep 17 00:00:00 2001 From: Steven Westmoreland <61995746+StevenWestmoreland@users.noreply.github.com> Date: Mon, 10 Aug 2020 17:11:51 -0400 Subject: [PATCH 08/15] initial assignment commit --- module1-introduction-to-sql/rpg_queries.py | 124 +++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 module1-introduction-to-sql/rpg_queries.py diff --git a/module1-introduction-to-sql/rpg_queries.py b/module1-introduction-to-sql/rpg_queries.py new file mode 100644 index 00000000..5cff51c8 --- /dev/null +++ b/module1-introduction-to-sql/rpg_queries.py @@ -0,0 +1,124 @@ +import sqlite3 + + +def connect_to_db(db_name='rpg_db.sqlite3'): + return sqlite3.connect(db_name) + +def execute_query(cursor, query): + cursor.execute(query) + return cursor.fetchall() + + +CHARACTER_COUNT = """ +SELECT COUNT(character_id) +FROM charactercreator_character; +""" + +CLERIC_COUNT = """ +SELECT COUNT(character_ptr_id) +FROM charactercreator_cleric; +""" + +FIGHTER_COUNT = """ +SELECT COUNT(character_ptr_id) +FROM charactercreator_fighter; +""" + +MAGE_COUNT = """ +SELECT COUNT(character_ptr_id) +FROM charactercreator_mage; +""" + +NECRO_COUNT = """ +SELECT COUNT(mage_ptr_id) +FROM charactercreator_necromancer; +""" + +THIEF_COUNT = """ +SELECT COUNT(character_ptr_id) +FROM charactercreator_thief; +""" + +ITEM_COUNT = """ +SELECT COUNT(item_id) +FROM armory_item; +""" + +WEAPON_COUNT = """ +SELECT COUNT(DISTINCT item_id) +FROM armory_item, armory_weapon +WHERE item_id = item_ptr_id; +""" + +CHAR_INV_COUNT = """ +SELECT COUNT(item_id) +FROM charactercreator_character_inventory +GROUP BY character_id +LIMIT 20; +""" + +CHAR_WEAP_COUNT = """ +SELECT COUNT(item_id) +FROM charactercreator_character_inventory +WHERE item_id > 137 +GROUP BY character_id +LIMIT 20; +""" + +CHAR_INV_AVG = """ +SELECT AVG(items.avg_count) FROM +(SELECT COUNT(item_id) AS avg_count +FROM charactercreator_character_inventory +GROUP BY character_id) AS items; +""" + +CHAR_WEAP_AVG = """ +SELECT AVG(weapons.avg_count) FROM +(SELECT COUNT(item_id) AS avg_count +FROM charactercreator_character_inventory +WHERE item_id > 137 +GROUP BY character_id) AS weapons; +""" + + +if __name__ == '__main__': + conn = connect_to_db() + curs = conn.cursor() + + results1 = execute_query(curs, CHARACTER_COUNT)[0][0] + print(f'There are {results1} characters.') + print('***************') + + total_cleric = execute_query(curs, CLERIC_COUNT)[0][0] + total_fight = execute_query(curs, FIGHTER_COUNT)[0][0] + total_mage = execute_query(curs, MAGE_COUNT)[0][0] + total_necro = execute_query(curs, NECRO_COUNT)[0][0] + total_thief = execute_query(curs, THIEF_COUNT)[0][0] + print(f'Breakdown of classes: {total_cleric} clerics, {total_fight} fighters, {total_thief} thieves, and {total_mage} mages, {total_necro} of which are necromancers.') + print('***************') + + total_items = execute_query(curs, ITEM_COUNT)[0][0] + print(f'There are {total_items} items in the game.') + print('***************') + + total_weap = execute_query(curs, WEAPON_COUNT)[0][0] + print(f'Of these, {total_weap} are weapons and {total_items - total_weap} are not.') + print('***************') + + char_items = execute_query(curs, CHAR_INV_COUNT)[0:20] + print('Number of items in the top 20 inventories:') + print(char_items) + print('***************') + + char_weapons = execute_query(curs, CHAR_WEAP_COUNT)[0:20] + print('And these are the number of weapons within them:') + print(char_weapons) + print('***************') + + char_avg_items = execute_query(curs, CHAR_INV_AVG)[0][0] + print(f'The average number of items carried by players is {char_avg_items}') + print('***************') + + char_avg_weaps = execute_query(curs, CHAR_WEAP_AVG)[0][0] + print(f'The average number of items carried by players is {char_avg_weaps}') + print('***************') \ No newline at end of file From d81d35c88d708b3f6ccaf70dda914dfed0e9d5d4 Mon Sep 17 00:00:00 2001 From: Steven Westmoreland <61995746+StevenWestmoreland@users.noreply.github.com> Date: Mon, 10 Aug 2020 18:42:59 -0400 Subject: [PATCH 09/15] Print statements now include question blocks. --- module1-introduction-to-sql/rpg_queries.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/module1-introduction-to-sql/rpg_queries.py b/module1-introduction-to-sql/rpg_queries.py index 5cff51c8..efaadfdc 100644 --- a/module1-introduction-to-sql/rpg_queries.py +++ b/module1-introduction-to-sql/rpg_queries.py @@ -86,6 +86,7 @@ def execute_query(cursor, query): curs = conn.cursor() results1 = execute_query(curs, CHARACTER_COUNT)[0][0] + print('How many total characters are there?') print(f'There are {results1} characters.') print('***************') @@ -94,31 +95,38 @@ def execute_query(cursor, query): total_mage = execute_query(curs, MAGE_COUNT)[0][0] total_necro = execute_query(curs, NECRO_COUNT)[0][0] total_thief = execute_query(curs, THIEF_COUNT)[0][0] + print('How many of each specific subclass?') print(f'Breakdown of classes: {total_cleric} clerics, {total_fight} fighters, {total_thief} thieves, and {total_mage} mages, {total_necro} of which are necromancers.') print('***************') total_items = execute_query(curs, ITEM_COUNT)[0][0] + print('How many total items?') print(f'There are {total_items} items in the game.') print('***************') total_weap = execute_query(curs, WEAPON_COUNT)[0][0] + print ('How many of the items are weapons? How many are not?') print(f'Of these, {total_weap} are weapons and {total_items - total_weap} are not.') print('***************') char_items = execute_query(curs, CHAR_INV_COUNT)[0:20] + print('How many Items does each character have? (Return first 20 rows)') print('Number of items in the top 20 inventories:') print(char_items) print('***************') char_weapons = execute_query(curs, CHAR_WEAP_COUNT)[0:20] + print('How many Weapons does each character have? (Return first 20 rows)') print('And these are the number of weapons within them:') print(char_weapons) print('***************') char_avg_items = execute_query(curs, CHAR_INV_AVG)[0][0] + print('On average, how many Items does each Character have?') print(f'The average number of items carried by players is {char_avg_items}') print('***************') char_avg_weaps = execute_query(curs, CHAR_WEAP_AVG)[0][0] - print(f'The average number of items carried by players is {char_avg_weaps}') - print('***************') \ No newline at end of file + print('On average, how many Weapons does each Character have?') + print(f'The average number of weapons carried by players is {char_avg_weaps}') + print('***************') From 69c0478d384326b24faaa8d6d70a38a00b73c57b Mon Sep 17 00:00:00 2001 From: Steven Westmoreland <61995746+StevenWestmoreland@users.noreply.github.com> Date: Tue, 11 Aug 2020 16:17:18 -0400 Subject: [PATCH 10/15] FOR DS17 --- .../elephant_queries_DS17.py | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 module2-sql-for-analysis/elephant_queries_DS17.py diff --git a/module2-sql-for-analysis/elephant_queries_DS17.py b/module2-sql-for-analysis/elephant_queries_DS17.py new file mode 100644 index 00000000..1ecac508 --- /dev/null +++ b/module2-sql-for-analysis/elephant_queries_DS17.py @@ -0,0 +1,66 @@ +import os +import psycopg2 +from dotenv import load_dotenv +import sqlite3 + +load_dotenv() + +# Variables saved in .env file +DB_NAME = os.getenv('DB_NAME') +DB_USER = os.getenv('DB_USER') +DB_PASS = os.getenv('DB_PASS') +DB_HOST = os.getenv('DB_HOST') + +# Connect to ElephantSQL-hosted PostgreSQL and instantiate cursor +conn = psycopg2.connect(dbname=DB_NAME, + user=DB_USER, + password=DB_PASS, + host=DB_HOST) +cursor = conn.cursor() + +# An example query +# Note - nothing happened yet! We need to actually *fetch* from the cursor +cursor.execute('SELECT * from test_table;') +results = cursor.fetchone() + + +# Connect to SQLite3 DB for RPG data +sl_conn = sqlite3.connect('rpg_db.sqlite3') +sl_cursor = sl_conn.cursor() +characters = sl_cursor.execute('SELECT * FROM charactercreator_character LIMIT 10').fetchall() +print(characters) + +# Create Character Table in PostGRES + +create_character_table_query = ''' +CREATE TABLE IF NOT EXISTS rpg_characters ( + character_id SERIAL PRIMARY KEY, + name VARCHAR(30), + level INT, + exp INT, + hp INT, + strength INT, + intelligence INT, + dexterity INT, + wisdom INT +) +''' + +cursor.execute(create_character_table_query) +conn.commit() + +# Insert Character Data in PostGRES + +for character in characters: + insert_query = f''' INSERT INTO rpg_characters + (character_id, name, level, exp, hp, strength, intelligence, dexterity, wisdom) VALUES + {character} + ''' + cursor.execute(insert_query) + +# Commit and close out connection and cursors +conn.commit() +sl_cursor.close() +sl_conn.close() +cursor.close() +conn.close() From ce90f3d7dbfb2f352a2cbe1577a4266825396ba0 Mon Sep 17 00:00:00 2001 From: Steven Westmoreland <61995746+StevenWestmoreland@users.noreply.github.com> Date: Tue, 11 Aug 2020 16:18:26 -0400 Subject: [PATCH 11/15] FOR DS17 --- .../insert_titanic_DS17.py | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 module2-sql-for-analysis/insert_titanic_DS17.py diff --git a/module2-sql-for-analysis/insert_titanic_DS17.py b/module2-sql-for-analysis/insert_titanic_DS17.py new file mode 100644 index 00000000..07f8fef3 --- /dev/null +++ b/module2-sql-for-analysis/insert_titanic_DS17.py @@ -0,0 +1,53 @@ +import pandas as pd +import os +import psycopg2 +from dotenv import load_dotenv + +load_dotenv() + +DB_NAME = os.getenv('DB_NAME') +DB_USER = os.getenv('DB_USER') +DB_PASS = os.getenv('DB_PASS') +DB_HOST = os.getenv('DB_HOST') + +conn = psycopg2.connect(dbname=DB_NAME, + user=DB_USER, + password=DB_PASS, + host=DB_HOST) + +cursor = conn.cursor() + +create_titanic_table = ''' + CREATE TABLE IF NOT EXISTS titanic ( + pass_id SERIAL PRIMARY KEY, + survived INT, + pclass INT, + name VARCHAR(200), + sex VARCHAR(20), + age INT, + siblings_spouses INT, + parents_children INT, + fare NUMERIC(30) + ) +''' + +cursor.execute(create_titanic_table) +conn.commit() + +titanic_db = pd.read_csv('titanic.csv') +titanic_db['Name'] = titanic_db['Name'].apply(lambda x: x.replace("'","")) + +data = list(titanic_db.to_records()) + +for item in data: + + insert_query = f''' INSERT INTO titanic + (pass_id, survived, pclass, name, sex, age, siblings_spouses, + parents_children, fare) VALUES {item} + ''' + + cursor.execute(insert_query) + +conn.commit() +cursor.close() +conn.close() From 84de76a3aff732b360d7683f9d40748998e05df7 Mon Sep 17 00:00:00 2001 From: StevenWestmoreland Date: Tue, 11 Aug 2020 17:00:46 -0400 Subject: [PATCH 12/15] push debug --- module2-sql-for-analysis/insert_titanic.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/module2-sql-for-analysis/insert_titanic.py b/module2-sql-for-analysis/insert_titanic.py index 67fbc3c1..83755a5c 100644 --- a/module2-sql-for-analysis/insert_titanic.py +++ b/module2-sql-for-analysis/insert_titanic.py @@ -55,3 +55,5 @@ cursor.execute(insert_query) conn.commit() + +# test edit for push \ No newline at end of file From f9a61f9f4785a11578c91749595fb8f0a9d346d7 Mon Sep 17 00:00:00 2001 From: StevenWestmoreland Date: Wed, 12 Aug 2020 17:55:55 -0400 Subject: [PATCH 13/15] Assignment upload --- .../mongodb.py | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 module3-nosql-and-document-oriented-databases/mongodb.py diff --git a/module3-nosql-and-document-oriented-databases/mongodb.py b/module3-nosql-and-document-oriented-databases/mongodb.py new file mode 100644 index 00000000..db768496 --- /dev/null +++ b/module3-nosql-and-document-oriented-databases/mongodb.py @@ -0,0 +1,72 @@ +import pymongo +import os +import sqlite3 +from dotenv import load_dotenv + +load_dotenv() + +# Setup connection to MongoDB +DB_NAME = os.getenv('DB_NAME') +DB_PASS = os.getenv('DB_PASS') +connection = ('mongodb+srv://Me:' + DB_PASS + + '@cluster0.zrrji.mongodb.net/' + DB_NAME + + '?retryWrites=true&w=majority') + +client = pymongo.MongoClient(connection) +db = client.test + +# Set up connection to RPG_DB +sl_conn = sqlite3.connect('rpg_db.sqlite3') +sl_curs = sl_conn.cursor() + +def table_insert(table): + document = sl_curs.execute(table).fetchall() + db.test.insert_one(document) + +# Get and copy the charactercreator_character table from +# SQLite3 to MongoDB +get_characters = 'SELECT * FROM charactercreator_character;' +table_insert(get_characters) + +# Repeat for classes +get_clerics = 'SELECT * FROM charactercreator_cleric;' +table_insert(get_clericss) + +get_fighters = 'SELECT * FROM charactercreator_fighter;' +table_insert(get_fighters) + +get_mages = 'SELECT * FROM charactercreator_mage;' +table_insert(get_mages) + +get_necros = 'SELECT * FROM charactercreator_necromancer;' +table_insert(get_necros) + +get_theives = 'SELECT * FROM charactercreator_thief;' +table_insert(get_thieves) + +# And again for items, weapons, and inventories +get_invs = 'SELECT * FROM charactercreator_character_inventory;' +table_insert(get_invs) + +get_items = 'SELECT * FROM armory_item;' +table_insert(get_items) + +get_weapons = 'SELECT * FROM armory_weapon;' +table_insert(get_weapons) + +# Commit and close connections +client.close() +sl_curs.close() +sl_conn.close() + +########################################## +# How was working with MongoDB different from working with PostgreSQL? +# What was easier, and what was harder? + +# I think the biggest difference in dealing with MongoDB vs PostgreSQL +# is the fact that MongoDB can handle NOSQL documents while PostgreSQL cannot. +# While it meant that MongoDB could take in more documents, it also meant that +# we had to be much more careful with the structure and schema of our inserts. +# MongoDB was also more secure, so sometimes I would find myself fighting +# security features when I didn't have to in PostgreSQL. Dotenv helped +# with that, but only to an extent. \ No newline at end of file From c67916c4dc3c8c8e8b7cbc992cc7a5becfec8dbb Mon Sep 17 00:00:00 2001 From: StevenWestmoreland Date: Wed, 12 Aug 2020 18:17:31 -0400 Subject: [PATCH 14/15] Assignment upload --- module3-nosql-and-document-oriented-databases/mongodb.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/module3-nosql-and-document-oriented-databases/mongodb.py b/module3-nosql-and-document-oriented-databases/mongodb.py index db768496..35eeda90 100644 --- a/module3-nosql-and-document-oriented-databases/mongodb.py +++ b/module3-nosql-and-document-oriented-databases/mongodb.py @@ -21,7 +21,7 @@ def table_insert(table): document = sl_curs.execute(table).fetchall() - db.test.insert_one(document) + db.test.insert_one({'document': document) # Get and copy the charactercreator_character table from # SQLite3 to MongoDB From f6e8af8a4a97a1a6a4042afaf0e4958f4c370b8e Mon Sep 17 00:00:00 2001 From: StevenWestmoreland Date: Thu, 13 Aug 2020 16:48:32 -0400 Subject: [PATCH 15/15] assignment --- .../map_reduce_example.py | 25 ++ .../postgresql_queries.py | 224 ++++++++++++++++++ .../sqlite_demo.py | 32 +++ 3 files changed, 281 insertions(+) create mode 100644 module4-acid-and-database-scalability-tradeoffs/map_reduce_example.py create mode 100644 module4-acid-and-database-scalability-tradeoffs/postgresql_queries.py create mode 100644 module4-acid-and-database-scalability-tradeoffs/sqlite_demo.py diff --git a/module4-acid-and-database-scalability-tradeoffs/map_reduce_example.py b/module4-acid-and-database-scalability-tradeoffs/map_reduce_example.py new file mode 100644 index 00000000..7d7002cb --- /dev/null +++ b/module4-acid-and-database-scalability-tradeoffs/map_reduce_example.py @@ -0,0 +1,25 @@ +from functools import reduce + +my_list = [1, 2, 3, 4] + +# We want the sum of squared values +# (A fairly real statistical task) + +# Traditional (non-mapreduce approach) +ssv_trad = sum([i**2 for i in my_list]) + +# That works fine - but what it ew had 40 billion numbers? +# We could use a mapreduce approach + +# To be clear - this code still runs on one computer +# But mapreduce paradigm *could* be distributerd more directly + +squared_values = map(lambda i: i**2, my_list) + +def add_numbers(x1, x2): + return x1 + x2 + +ssv_mapreduce = reduce(add_numbers, squared_values) + +print('Sum of squared values (traditional): '+ str(ssv_trad)) +print('Sum of squared values (map-reduce): ' + str(ssv_mapreduce)) diff --git a/module4-acid-and-database-scalability-tradeoffs/postgresql_queries.py b/module4-acid-and-database-scalability-tradeoffs/postgresql_queries.py new file mode 100644 index 00000000..c33e3f04 --- /dev/null +++ b/module4-acid-and-database-scalability-tradeoffs/postgresql_queries.py @@ -0,0 +1,224 @@ +import os +from dotenv import load_dotenv +import psycopg2 + +load_dotenv() + +DB_NAME = os.getenv("DB_NAME") +DB_USER = os.getenv("DB_USER") +DB_PASS = os.getenv("DB_PASS") +DB_HOST = os.getenv("DB_HOST") + +conn = psycopg2.connect(dbname=DB_NAME, + user=DB_USER, + password=DB_PASS, + host=DB_HOST) +cursor = conn.cursor() + +# How many passengers survived, and how many died? +survivors = (""" + SELECT COUNT(*) + FROM titanic + WHERE survived = 1 +""") +deaths = (""" + SELECT COUNT(*) + FROM titanic + WHERE survived = 0 +""") +print(cursor.execute(survivors).fetchall() + " survived.") +print(cursor.execute(deaths).fetchall() + " died.") + +# How many passengers were in each class? +class_query = (""" + SELECT pclass, count(DISTINCT id) + FROM titanic + GROUP BY pclass +""") +class_result = cursor.execute(class_query).fetchall() + +for each in result: + if each[0] == 1: + print(f'{each[1]} passengers were in first class.') + elif each[0] == 2: + print(f'{each[1]} passengers were in second class.') + else: + print(f'{each[1]} passengers were in third class.') + +# How many passengers survived/died in each class? +class_surv_query = (""" + SELECT pclass, COUNT(DISTINCT id) + FROM titanic + WHERE survived = 1 + GROUP BY pclass +""") +class_surv_result = cursor.execute(class_surv_query).fetchall() + +class_died_query = (""" + SELECT pclass, COUNT(DISTINCT id) + FROM titanic + WHERE survived = 0 + GROUP BY pclass +""") +class_died_result = cursor.execute(class_died_query).fetchall() + +for each in class_died_result: + if each[0] == 1: + dead_first = each[1] + elif each[0] == 2: + dead_second = each[1] + else: + dead_third = each[1] + +for each in class_surv_result: + if each[0] == 1: + print(f'In first class {each[1]} passengers survived and + {dead_first} died.') + elif each[0] == 2: + print(f'In first class {each[1]} passengers survived and + {dead_second} died.') + else: + print(f'In first class {each[1]} passengers survived and + {dead_third} died.') + +# What was the average age of survivors vs. nonsurvivors? +age_query = (""" + SELECT survived, AVG(age) + FROM titanic + GROUP BY survived +""") +age_result = cursor.execute(age_query).fetchall() + +for each in age_result: + if each[0] == 1: + print(f'The average age of survivors is {each[1]}') + else: + print(f'The average age of the deceased is {each[1]}') + +# What was the average age of each passenger class? +class_age_query = (""" + SELECT pclass, AVG(age) + FROM titanic + GROUP BY pclass +""") +class_age_result = cursor.execute(class_age_query).fetchall() + +for each in class_age_result: + if each[0] == 1: + print(f'The average age of first class passengers is {each[1]}') + elif each[0] == 2: + print(f'The average age of second class passengers is {each[1]}') + else: + print(f'The average age of third class passengers is {each[1]}') + +# What was the average fare by passenger class? By survival? +class_fare_query = (""" + SELECT pclass, AVG(fare) + FROM titanic + GROUP BY pclass +""") +class_fare_result = cursor.execute(class_fare_query).fetchall() + +surv_fare_query = (""" + SELECT survived, AVG(fare) + FROM titanic + GROUP BY survived +""") +surv_fare_result = cursor.execute(surv_fare_query).fetchall() + +for each in class_fare_result: + if each[0] == 1: + print(f'The average fare for first class passengers is {each[1]}') + elif each[0] == 2: + print(f'The average fare for second class passengers is {each[1]}') + else: + print(f'The average fare for third class passengers is {each[1]}') + +for each in surv_fare_result: + if each[0] == 1: + print(f'The average fare for survivors is {each[1]}') + else: + print(f'The average fare for the deceased is {each[1]}') + +# How many siblings/spouses aboard on average, by passenger class? By survival? +class_sib_query = (""" + SELECT pclass, AVG(siblings_spouses) + FROM titanic + GROUP BY pclass +""") +class_sib_result = cursor.execute(class_sib_query).fetchall() + +surv_sib_query = (""" + SELECT survived, AVG(siblings_spouses) + FROM titanic + GROUP BY survived +""") +surv_sib_result = cursor.execute(surv_sib_query).fetchall() + +for each in class_sib_result: + if each[0] == 1: + print(f'The average number of siblings and spouses for survivors + is {each[1]}') + else: + print(f'The average number of siblings and spouses for the deceased + is {each[1]}') + +for each in surv_sib_result: + if each[0] == 1: + print(f'The average number of siblings and spouses for first class + passengers is {each[1]}') + elif each[0] == 2: + print(f'The average number of siblings and spouses for second class + passengers is {each[1]}') + else: + print(f'The average number of siblings and spouses for third class + passengers is {each[1]}') + +# How many parents/children aboard on average, by passenger class? By survival? +class_parent_query = (""" + SELECT pclass, AVG(parents_children) + FROM titanic + GROUP BY pclass +""") +class_parent_result = cursor.execute(class_parent_query).fetchall() + +surv_parent_query = (""" + SELECT survived, AVG(parents_children) + FROM titanic + GROUP BY survived +""") +surv_parent_result = cursor.execute(surv_parent_query).fetchall() + +for each in class_parent_result: + if each[0] == 1: + print(f'The average number of parents and children for survivors + is {each[1]}') + else: + print(f'The average number of parents and children for the deceased + is {each[1]}') + +for each in surv_parent_result: + if each[0] == 1: + print(f'The average number of parents and children for first class + passengers is {each[1]}') + elif each[0] == 2: + print(f'The average number of parents and children for second class + passengers is {each[1]}') + else: + print(f'The average number of parents and children for third class + passengers is {each[1]}') + +# Do any passengers have the same name? +unique_query = (""" + SELECT name, COUNT(*) + FROM titanic + GROUP BY name +""") +unique_result = cursor.execute(unique_query).fetchall() + +print("The following passengers have the same name:") +for each in unique_result: + if each[1] > 1: + print(f' {each[0]}') + else: + pass diff --git a/module4-acid-and-database-scalability-tradeoffs/sqlite_demo.py b/module4-acid-and-database-scalability-tradeoffs/sqlite_demo.py new file mode 100644 index 00000000..b72d9343 --- /dev/null +++ b/module4-acid-and-database-scalability-tradeoffs/sqlite_demo.py @@ -0,0 +1,32 @@ +import sqlite3 + +def create_table(conn): + curs = conn.cursor() + create_statement = """ + CREATE TABLE students ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name CHAR(20), + favorite_number INTEGER, + least_favorite_number INTEGER + ); + """ + curs.execute(create_statement) + curs.close() + conn.commit() + +def insert_data(conn) + my_data = [ + ('Malven', 7, 10,) + ('Steven', -3, 12,) + ('Dondre', 80, -1) + ] + for row in my_data: + pass + # TODO write an insert statement! + curs.close() + conn.commit() + +if __name__ == '__main__': + conn = sqlite3.connect('example_db.sqlite3') + create_table(conn) + insert_data(conn) \ No newline at end of file