Skip to content

Instantly share code, notes, and snippets.

View anna-anisienia's full-sized avatar

Anna Geller anna-anisienia

View GitHub Profile
@anna-anisienia
anna-anisienia / mysql_conn.py
Created September 29, 2020 20:34
context manager
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
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)
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
with open('data.txt', 'w') as myfile:
myfile.write('Hello from context manager!')
with open('data.txt', 'r') as myfile:
data = myfile.read()
print(data)
# 'Hello from context manager!'
myfile = open('data.txt', 'w')
myfile.write('Hello from context manager!')
myfile.close()
myfile = open('data.txt', 'w')
try:
myfile.write('Hello from context manager!')
finally:
myfile.close()
-- 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;
-- 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'
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')