Skip to content

Instantly share code, notes, and snippets.

View gbarreiro's full-sized avatar

Guillermo Barreiro gbarreiro

View GitHub Profile
@gbarreiro
gbarreiro / triggers.sql
Created August 20, 2020 15:17
MySQL cheatsheet: triggers
-- Triggered after a INSERT operation on "Students" table
CREATE TRIGGER my_trigger AFTER INSERT ON Students FOR EACH ROW
BEGIN
-- ... instructions to be performed ... --
END;
-- Triggered before a DELETE operation on "Teachers" table
CREATE TRIGGER my_trigger BEFORE DELETE ON Teachers FOR EACH ROW
BEGIN
@gbarreiro
gbarreiro / scheduled_event.sql
Created August 20, 2020 15:29
MySQL cheatsheet: scheduled event
SET GLOBAL event_scheduler = ON; -- ensures that the MySQL event scheduler is on
-- One-time events
CREATE EVENT IF NOT EXISTS my_event
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 12 HOUR -- the event will be run in 12h and then will be deleted
DO
DELETE * FROM Logs; -- something random
CREATE EVENT IF NOT EXISTS my_event
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 24 MINUTE -- the event will be run in 24 minutes...
@gbarreiro
gbarreiro / Dockerfile
Created September 17, 2020 14:09
Sample Dockerfile for creating a Docker image
# Use an Ubuntu image as base image
FROM ubuntu:latest
# Metadata for the container
LABEL description="This is a dummy container" author="Guillermo Barreiro"
# Set the working directory inside the container for the following Dockerfile instructions
WORKDIR /root
# Use an argument sent to the "docker build" command inside the Dockerfile
@gbarreiro
gbarreiro / docker-compose.yml
Last active February 17, 2021 14:56
Docker Compose sample file
version: "3.8"
services:
database:
image: mysql:latest
volumes:
- db-data:/var/lib/mysql/data
command: --default-authentication-plugin=mysql_native_password # run this command in the container
networks:
- database-api
restart: always # the container must be always on
@gbarreiro
gbarreiro / bestcuisines_setup.py
Last active September 20, 2020 14:46
Best cuisines: setup code
# Define the dependencies
import pandas as pd
import requests
import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
mpl.style.use(['seaborn'])
# Foursquare API credentials and endpoint
CLIENT_ID = # your client ID
@gbarreiro
gbarreiro / bestcuisines_get_restaurants.py
Last active September 22, 2020 09:31
Best cuisines: get the number of restaurants for each cuisine on each city
restaurants = pd.DataFrame(columns=['City', 'Cuisine', 'Number of restaurants']) # define the DataFrame where the results will be stored
for city in cities:
for cuisine_name, category_id in cuisines.items():
# For each city, retrieve the top restaurants from Foursquare for each cuisine...
query = requests.get("{url}&near={city}&categoryId={category_id}".format(url=URL, city=city, category_id=category_id))
if query.status_code == 200:
number = query.json()
restaurants = restaurants.append({
'City': city,
# One-hot encoding
restaurants_onehot = pd.get_dummies(restaurants['Cuisine'], prefix="", prefix_sep="")
restaurants_onehot = restaurants_onehot.multiply(restaurants['Number of restaurants'], axis=0)
restaurants_onehot = pd.concat([restaurants['City'], restaurants_onehot], axis=1)
# Group by city
cuisines_city = restaurants_onehot.groupby('City').sum().reset_index()
cuisines_city = cuisines_city.set_index(keys='City')
# Normalize the numbers
@gbarreiro
gbarreiro / bestcuisines_insight_1.py
Created September 20, 2020 14:47
Best cuisines: insight 1
fig = plt.figure()
for index, city in enumerate(cities):
# For each city, get the top 10 cuisines and plot them in a horizontal bar graph
ax = fig.add_subplot(3, 3, index+1)
# Get the top 10 cuisines for the city
cuisines = cuisines_city_normalized.loc[city]
cuisines = cuisines.sort_values(ascending=False).head(10).sort_values(ascending=True)
# Plot in red the bar with the local cuisine, plot in blue the rest
@gbarreiro
gbarreiro / bestcuisines_insight_2.py
Created September 20, 2020 14:51
Best cuisines: insight 2
fig = plt.figure()
for index, cuisine in enumerate(city_local_cuisines.values()):
# For the selected cuisine, plot a bar graph with the cities where there are more restaurants of that cuisine
ax = fig.add_subplot(3, 3, index+1)
cuisine_cities = cuisines_city_normalized[cuisine]
cuisine_cities = cuisine_cities.sort_values(ascending=True).head(9)
# Plot in red the bar with the city where this cuisine is local, plot in blue the rest
color = ''
for i in cuisine_cities.keys():
@gbarreiro
gbarreiro / bestcuisines_insight_3.py
Created September 20, 2020 14:54
Best cuisines: insight 3
percentages = {}
for city, cuisine in city_local_cuisines.items():
# For each city, get the percentage of local cuisine restaurants
percentage = cuisines_city_normalized.at[city, cuisine]
percentages[city] = percentage
# Plot the data in a bar graph
df_percentages = pd.Series(percentages).sort_values(ascending=True)
df_percentages.plot(kind='barh')
plt.xlabel('% of local cuisine restaurants')