Skip to content

Instantly share code, notes, and snippets.

@tpjfern03
tpjfern03 / sql.py
Last active November 29, 2018 18:38 — forked from jorisvandenbossche/sql.py
Patched version of pandas.io.sql to support PostgreSQL. Collection of query wrappers / abstractions to both facilitate data retrieval and to reduce dependency on DB-specific API. #pgsql
"""
Patched version to support PostgreSQL
(original version: https://github.com/pydata/pandas/blob/v0.13.1/pandas/io/sql.py)
Adapted functions are:
- added _write_postgresql
- updated table_exist
- updated get_sqltype
- updated get_schema
@tpjfern03
tpjfern03 / SQLAlchemy Cheat Sheet
Last active May 31, 2021 10:26 — forked from akkefa/SQLAlchemy Cheat Sheet
SQLAlchemy is a deep and powerful thing made up of many layers. This cheat sheet sticks to parts of the ORM (Object Relational Mapper) layer,and aims to be a reference not a tutorial. That said, if you are familiar with SQL then this cheat sheet should get you well on your way to understanding SQLAlchemy. #sqlalchemy
#A SQLAlchemy Cheat Sheet
###Introduction
##Basic Models
One model is used to describe one database table. For example:
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import scoped_session,sessionmaker
from zope.sqlalchemy import ZopeTransactionExtension
from sqlalchemy import (
@tpjfern03
tpjfern03 / cx_oracle_to_pandas.py
Created November 29, 2018 18:33 — forked from mvaz/cx_oracle_to_pandas.py
Example of executing and reading a query into a pandas dataframe #cx_oracle
import cx_Oracle
import pandas
connection = cx_Oracle.connect('username/pwd@host:port/dbname')
def read_query(connection, query):
cursor = connection.cursor()
try:
cursor.execute( query )
names = [ x[0] for x in cursor.description]
@tpjfern03
tpjfern03 / Working_with_cx_Oracle.md
Last active November 29, 2018 18:30 — forked from jtrive84/Working_with_cx_Oracle.md
Introduction to accessing Oracle data from Python using the cx_Oracle library. #cx_oracle

Introduction to cx_Oracle

cx_Oracle is a third-party Python library that facilitates Oracle-to-Python database communication. A cursor is a control structure that enables traversal over the records in a database. This is Python's primary means of accessing database table data. The setup is:

  • Create a connection object
  • Define a cursor and call the cursor() method on the connection object
  • Construct a query string for the data of interest
  • Pass the query string to the cursor's execute method
@tpjfern03
tpjfern03 / sqlalchemy_upsert.py
Created November 29, 2018 18:25 — forked from malexer/sqlalchemy_upsert.py
Modelling UPSERT in SQLAlchemy (well actually it is not upsert but speed improvement is significant in comparison with simple session.merge) #sqlalchemy
# Note: it is a copy of great answer by "mgoldwasser" from Stackoverflow
# Check the original answer here: http://stackoverflow.com/a/26018934/1032439
# Imagine that post1, post5, and post1000 are posts objects with ids 1, 5 and 1000 respectively
# The goal is to "upsert" these posts.
# we initialize a dict which maps id to the post object
my_new_posts = {1: post1, 5: post5, 1000: post1000}
for each in posts.query.filter(posts.id.in_(my_new_posts.keys())).all():
@tpjfern03
tpjfern03 / load_data.py
Last active November 29, 2018 18:17 — forked from evz/load_data.py
Faster loader for loading data to pgsql #pgsql #sqlalchemy
import csv
import sqlalchemy as sa
import os
import pytz
from datetime import datetime
import itertools
tz = pytz.timezone('America/Chicago')
# DB_CONN = os.environ['DATABASE_URL']
@tpjfern03
tpjfern03 / sqlalchemy.py
Created November 29, 2018 18:13
sqlalchemy Objects #sqlalchemy
"""Aliases for SQL/SQLAlchemy objects that are assured to be correctly type-checked.
https://gist.github.com/Stiivi/8618942769b0bc6a8ba3371c4431ad75
"""
import sqlalchemy
# Engine
# ======
@tpjfern03
tpjfern03 / rtdbReadWrite.py
Created November 28, 2018 18:33
RTDB Read/Write
rtdb_init()
# Open the file for a status station
# status_file = vde.open_status_telem(10, 'R', 'S', read_only=False)
for station, station_name in rtdb_station_list(names=True):
if station in stationList:
if station_name != '':
for cat in categoryList:
for pt in pointTypeList:
# print('Listing points for ' + str(station) + '/' + cat + '/' + pt)
# Open the vde file...
@tpjfern03
tpjfern03 / vdeReadWrite.py
Last active November 28, 2018 18:40
VDE Read / Write
from acsprism import vde
from acsprism import rtdb_init, rtdb_station_list, rtdb_point_list, RtdbPoint
# Open the file for a status station
# status_file = vde.open_status_telem(10, 'R', 'S', read_only=False)
#records = status_record.record_count()
#logger.info("Reading %d records: station=%d, category=%s, type=%s from rtdb." % (records, stn, cat, pointType))
for station, station_name in rtdb_station_list(names=True):
for cat in categoryList:
@tpjfern03
tpjfern03 / checklig.sh
Created November 28, 2018 18:16
Check Logs after Oracle Load
echo "Loading data....."
sqlldr schema/pw@sid control=hdc_stage_points.ctl rows=10000 bad=hdc_stage_points.bad log=hdc_stage_points.log errors=100000
# Wait until the data is loaded & log file is available
cat hdc_stage_points.log | grep "Rows successfully loaded." > temp.log
IFS=" "
while read -r records b c d; do
export RC=$records
#echo "${RC}"
done < temp.log