JSON and related Flashcards
(47 cards)
Import json
import json
How do you convert from json to python?
import json
x = ‘{“name”:”John”, “age”:30, “city”:”New York”}’
y = json.loads(x)
print(y[‘age’])
How do you convert from python to json?
import json
x = {
“name”: “John”,
“age”: 30,
“city”: “New York”
}
y = json.dumps(x)
print(y)
What is the below? How would you convert this?
x = {
“this”: “that”,
“the”: “other”
}
That is python, so you would convert to json
Jason would eat the python and take a dump. The byproduct is json
What is the below? How would you convert it?
x = { “this”: “that”, “the”: “other” }
JSON so you would convert it to python
The magician loads Jason into a magic box, he shuts the door and twirls his wand. Upon opening the door we see that Jason has changed into a python
which conversion type can we change things around on?
Remember, JSON works with json data so it manipulates JSON, not python.
Jason eats the python and takes a messy dump. We have to pick out the corn now!
Havery DENT comes in and claims he can fix this.
He pulls out a short knubby pair of keys, and then SiPs coffee from A florida gATORS mug before getting to work
json.dumps(x, indent=4, separators=(“.”,”=”), sort_keys=True)
What would you use to ask for the html info from a webpage
import requests
You go on a QUEST to find the page
pip install requests
How would you get the html page from:
https://w3schools.com/python/demopage.html?
import requests
x = requests.get(‘https://w3schools.com/python/demopage.html’)
print(x.text)
What HTTP requests can you do?
delete - delete a resource
get - data retrieval
head - header info only
patch - Like put but for partial mods
post - submit data
put - replace resource
request - sends a request of a specific method to the specified url
DELsin runs at super speeds to GET a banana from a farmer’s market.
He then takes off going 90 miles an hour.
Along the way he runs past a POSTed speed limit of 50 mph and laughs at it.
He arrives at a farmers market in Japan and PUTs the banana in one of the carts before taking off running again.
Unfortunately he wasn’t paying attenting and ran right into a samurai’s sword who was training cutting his HEAD off.
Guts finds his head and vows to go on QUEST to get it place back on.
He finds a wizard to help. The wizard casts a long mystical spell and poof! Delsin’s head is PATHed back on with a bandaid.
What sites can we use to test requests and posts?
postb.in
requestbin.com
How would you verify that you’re receiving a success code?
url = “you_url”
data = {“json”:”data”}
response = requests.post(url, json=data)
if response.status_code == 200:
print(“success”)
else:
print(“failure”)
or
response = requests.get(url)
if response.status_code == 200:
print(response.text)
How would you get info from an api with requests?
import requests
url = “https://url”
response = requests.get(url)
if response.status_code == 200:
data = response.json()
print(data)
else:
print(“error”)
Install mysql as well as pandas
pip install mysql-connector-python
pip install pandas
import everything you need to run mysql
import mysql.connector
from mysql.connector import Error
import pandas as pd
Download MYSQL community on your computer
https://www.mysql.com/downloads/
Create a connection to a database
import mysql.connector
import pymysql <- do this instead
mydb = mysql.connector.connect(
host=”localhost”,
user=”yourusername”,
password=”yourpassword”
)
print(mydb)
Remember to install cryptography
Createa database named: mydatabase
import mysql.connector
mydb = mysql.connector.connect(
host=”localhost”,
user=”yourusername”,
password=”yourpassword”
)
mycursor = mydb.cursor()
mycursor.execute(“CREATE DATABASE mydatabase”)
Check if your database exists
import mysql.connector
mydb = mysql.connector.connect(
host=”localhost”,
user=”yourusername”,
password=”yourpassword”
)
mycursor = mydb.cursor()
mycursor.execute(“SHOW DATABASES”)
for x in mycursor:
print(x)
If your database exists you can just connect, do this
import mysql.connector
mydb = mysql.connector.connect(
host=”localhost”,
user=”yourusername”,
password=”yourpassword”,
database=”mydatabase”
)
Create a table called customers
import mysql.connector
mydb = mysql.connector.connect(
host=”localhost”,
user=”yourusername”,
password=”yourpassword”,
database=”mydatabase”
)
mycursor = mydb.cursor()
mycursor.execute(“CREATE TABLE customers (name VARCHAR(255), address VARCHAR(255))”)
confirm the table exists
import mysql.connector
mydb = mysql.connector.connect(
host=”localhost”,
user=”yourusername”,
password=”yourpassword”,
database=”mydatabase”
)
mycursor = mydb.cursor()
mycursor.execute(“SHOW TABLES”)
for x in mycursor:
print(x)
What is a primary key?
Create one
import mysql.connector
mydb = mysql.connector.connect(
host=”localhost”,
user=”yourusername”,
password=”yourpassword”,
database=”mydatabase”
)
mycursor = mydb.cursor()
mycursor.execute(“CREATE TABLE customers (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), address VARCHAR(255))”)
The business dolphin throws his ID INTO the automobile and the automobile start to duplicate (auto_increment)
The business dolphin calls it a day and goes to O’Charlies to get the PRIME RIB for dinner, he cuts it up with his keys
When creating a table, you should also create a column with a unique key for each record.
import mysql.connector
mydb = mysql.connector.connect(
host=”localhost”,
user=”yourusername”,
password=”yourpassword”,
database=”mydatabase”
)
mycursor = mydb.cursor()
mycursor.execute(“CREATE TABLE customers (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), address VARCHAR(255))”)
Create a primary key for an existing table
import mysql.connector
mydb = mysql.connector.connect(
host=”localhost”,
user=”yourusername”,
password=”yourpassword”,
database=”mydatabase”
)
mycursor = mydb.cursor()
mycursor.execute(“ALTER TABLE customers ADD COLUMN id INT AUTO_INCREMENT PRIMARY KEY”)