Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions #Count_Positives.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#Count Positives - Given a list of numbers, create a function to replace last value with number of positive values. Example, count_positives([-1,1,1,1]) changes list #to [-1,1,1,3] and returns it. (Note that zero is not considered to be a positive number).
def count_positives(array):
count = 0
for val in array:
if val > 0:
count += 1
array[len(array)-1] = count
return array
print(count_positives([-1,1,1,1]))




108 changes: 108 additions & 0 deletions APIconnectionMySQL.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import mysql.connector as mariadb"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Open database connection using connect method"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"connecting to database...\n"
]
}
],
"source": [
"con = mariadb.connect(\n",
" host = \"localhost\",\n",
" user = \"root\",\n",
" password = \"password\",\n",
" database = \"classicmodels\"\n",
")\n",
"print(\"connecting to database...\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[('Motorcycles', 13)]\n"
]
}
],
"source": [
"cur = con.cursor()\n",
"st = \"Select productLine, count(*) from products where productLine = '{}'\"\n",
"cur.execute(st.format('Motorcycles'))\n",
"result = cur.fetchall()\n",
"print(result)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"con.close()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.0 (main, Oct 24 2022, 18:26:48) [MSC v.1933 64 bit (AMD64)]"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "8b70a4ea831fd5b9b770fab33cac3f0a285469b7491720cba6f6d634f19e8405"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
244 changes: 244 additions & 0 deletions APIfunctionsMySQL.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import mysql.connector as mdb1"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"connected to database...\n",
"User_table created\n"
]
}
],
"source": [
"def generate_user_table():\n",
" con = mdb1.connect(\n",
" host = \"localhost\",\n",
" user = \"root\",\n",
" password = \"password\",\n",
" database = \"test_db\"\n",
" \n",
" )\n",
" print(\"connected to database...\")\n",
" cur = con.cursor()\n",
" st1 = \"Create table user_table2(email varchar(100), name varchar(50), password varchar(30))\"\n",
" st2 = \"Insert into user_table2 \\\n",
" (email, name, password) VALUES \\\n",
" ('ywbaek@perscholas.org', 'young', 'letsgomets'), \\\n",
" ('mcordon@perscholas.org', 'marcial', 'perscholas'), \\\n",
" ('mhaseeb@perscholas.org', 'haseeb', 'platform') \"\n",
" \n",
" cur.execute(st1)\n",
" cur.execute(st2)\n",
" print('User_table created')\n",
" con.commit()\n",
" con.close()\n",
"\n",
" \n",
"\n",
"\n",
"generate_user_table()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"connected to database...\n",
"[('ywbaek@perscholas.org', 'young', 'letsgomets'), ('mcordon@perscholas.org', 'marcial', 'perscholas'), ('mhaseeb@perscholas.org', 'haseeb', 'platform')]\n"
]
}
],
"source": [
"def get_all_users():\n",
" con = mdb1.connect(\n",
" host = \"localhost\",\n",
" user = \"root\",\n",
" password = \"password\",\n",
" database = \"test_db\"\n",
" \n",
" )\n",
" print(\"connected to database...\")\n",
" cur = con.cursor()\n",
" st = \"Select * from user_table2\"\n",
" cur.execute(st)\n",
" result = cur.fetchall()\n",
" print(result)\n",
" con.close()\n",
"\n",
"get_all_users()"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"connected to database...\n",
"[('ywbaek@perscholas.org', 'letsgomets')]\n"
]
}
],
"source": [
"def get_user_by_name(name):\n",
" con = mdb1.connect(\n",
" host = \"localhost\",\n",
" user = \"root\",\n",
" password = \"password\",\n",
" database = \"test_db\"\n",
" \n",
" )\n",
" print(\"connected to database...\")\n",
" cur = con.cursor()\n",
" st = \"SELECT email, password FROM user_table2 WHERE name = '{}'\"\n",
" cur.execute(st.format(name))\n",
"\n",
" result = cur.fetchall()\n",
" print(result)\n",
" con.close()\n",
"\n",
"get_user_by_name('young')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"connected to database...\n",
"True\n"
]
}
],
"source": [
"def validate_user(email, password):\n",
" con = mdb1.connect(\n",
" host = \"localhost\",\n",
" user = \"root\",\n",
" password = \"password\",\n",
" database = \"test_db\"\n",
" \n",
" )\n",
" print(\"connected to database...\")\n",
" cur = con.cursor()\n",
" st = \"SELECT email, password FROM user_table2 WHERE email = '{}' and password = '{}'\"\n",
" cur.execute(st.format(email, password))\n",
" result = cur.fetchall()\n",
" if result != []:\n",
" return True\n",
" if result == []:\n",
" return False\n",
" \n",
" con.close()\n",
" #return result \n",
" \n",
" \n",
"\n",
"a = print(validate_user('ywbaek@perscholas.org', 'letsgomets'))"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"connected to database...\n"
]
},
{
"ename": "KeyError",
"evalue": "'name'",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mKeyError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[10], line 19\u001b[0m\n\u001b[0;32m 16\u001b[0m con\u001b[39m.\u001b[39mclose()\n\u001b[0;32m 17\u001b[0m \u001b[39mreturn\u001b[39;00m result\n\u001b[1;32m---> 19\u001b[0m \u001b[39mprint\u001b[39m(update_user(\u001b[39m'\u001b[39;49m\u001b[39myoung\u001b[39;49m\u001b[39m'\u001b[39;49m, \u001b[39m'\u001b[39;49m\u001b[39mletsgomets\u001b[39;49m\u001b[39m'\u001b[39;49m, \u001b[39m'\u001b[39;49m\u001b[39mywbaek@perscholas.org\u001b[39;49m\u001b[39m'\u001b[39;49m))\n",
"Cell \u001b[1;32mIn[10], line 12\u001b[0m, in \u001b[0;36mupdate_user\u001b[1;34m(name, password, email)\u001b[0m\n\u001b[0;32m 10\u001b[0m cur \u001b[39m=\u001b[39m con\u001b[39m.\u001b[39mcursor()\n\u001b[0;32m 11\u001b[0m st \u001b[39m=\u001b[39m \u001b[39m\"\u001b[39m\u001b[39mUpdate user_table2 SET name = \u001b[39m\u001b[39m'\u001b[39m\u001b[39m{name}\u001b[39;00m\u001b[39m'\u001b[39m\u001b[39m and password = \u001b[39m\u001b[39m'\u001b[39m\u001b[39m{password}\u001b[39;00m\u001b[39m'\u001b[39m\u001b[39m where email = \u001b[39m\u001b[39m'\u001b[39m\u001b[39m{email}\u001b[39;00m\u001b[39m'\u001b[39m\u001b[39m\"\u001b[39m\n\u001b[1;32m---> 12\u001b[0m result \u001b[39m=\u001b[39m cur\u001b[39m.\u001b[39mexecute(st\u001b[39m.\u001b[39;49mformat(name, password, email))\n\u001b[0;32m 13\u001b[0m \u001b[39m#result = cur.fetchall()\u001b[39;00m\n\u001b[0;32m 14\u001b[0m \u001b[39mif\u001b[39;00m result:\n",
"\u001b[1;31mKeyError\u001b[0m: 'name'"
]
}
],
"source": [
"def update_user(name, password, email):\n",
" con = mdb1.connect(\n",
" host = \"localhost\",\n",
" user = \"root\",\n",
" password = \"password\",\n",
" database = \"test_db\"\n",
" \n",
" )\n",
" print(\"connected to database...\")\n",
" cur = con.cursor()\n",
" st = \"Update user_table2 SET name = '{name}' and password = '{password}' where email = '{email}'\"\n",
" cur.execute(st.format(name, password, email))\n",
" result = cur.fetchall()\n",
" \n",
" if result != []:\n",
" return True\n",
" \n",
" else:\n",
" return False\n",
" \n",
"\n",
"print(update_user('young', 'letsgomets', 'ywbaek@perscholas.org'))\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.0 (main, Oct 24 2022, 18:26:48) [MSC v.1933 64 bit (AMD64)]"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "8b70a4ea831fd5b9b770fab33cac3f0a285469b7491720cba6f6d634f19e8405"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading