This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import contextlib | |
import mysql.connector | |
@contextlib.contextmanager | |
def get_mysql_conn(db): | |
""" | |
Context manager to automatically close DB connection. | |
We retrieve credentials from Environment variables |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
from mysql_conn import get_mysql_conn | |
with get_mysql_conn(db='mytestdb') as conn: | |
df = pd.read_sql('SELECT * FROM mytable', conn) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import contextlib | |
import boto3 | |
s3 = boto3.client('s3', aws_access_key_id='my_aws_access_key', | |
aws_secret_access_key='my_aws_secret_key', | |
region_name='eu-central-1') | |
@contextlib.contextmanager |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with open('data.txt', 'w') as myfile: | |
myfile.write('Hello from context manager!') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with open('data.txt', 'r') as myfile: | |
data = myfile.read() | |
print(data) | |
# 'Hello from context manager!' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
myfile = open('data.txt', 'w') | |
myfile.write('Hello from context manager!') | |
myfile.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
myfile = open('data.txt', 'w') | |
try: | |
myfile.write('Hello from context manager!') | |
finally: | |
myfile.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Get the 100 most recently added data points | |
SELECT time, truck_id, model, | |
measure_name, measure_value::double | |
FROM "sampleDB"."IoT" | |
ORDER BY time DESC LIMIT 100; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Find the average load and max speed for each truck for the past week. | |
SELECT | |
bin(time, 1d) as binned_time, | |
fleet, | |
truck_id, | |
make, | |
model, | |
AVG(CASE WHEN measure_name = 'load' | |
THEN measure_value::double ELSE NULL END) AS avg_load_tons, | |
MAX(CASE WHEN measure_name = 'speed' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pyodbc | |
import pandas as pd | |
df = pd.read_csv('myfile.csv') | |
MY_TABLE = 'some_tbl' | |
conn = pyodbc.connect(driver='{ODBC Driver 17 for SQL Server}', | |
server='MYSERVER', | |
database='MYDB', | |
uid='MYUSER', pwd='MYPASSWORD') |