Skip to content

Instantly share code, notes, and snippets.

View kelvingakuo's full-sized avatar

Kelvin Gakuo kelvingakuo

View GitHub Profile
@kelvingakuo
kelvingakuo / db_conn_via_tunnel.py
Created March 1, 2022 15:04
How to connect to a Postgres DB using SSH tunnels
# Run this in your instance that doesn't have a direction connection to the DB
import sqlalchemy
from sshtunnel import SSHTunnelForwarder
import pandas as pd
bastion_url = "ec2-xxxxx.us-west-1.compute.amazonaws.com" # The EC2 instance from which you can connect to RDS
host_prod = "my-db-instance.us-west-1.rds.amazonaws.com" # RDS instance URL
username = "postgres" # DB username
from b_tree_index import BTreeIndex
import pprint
if __name__ == "__main__":
tbl = [
{"id": 1, "col_a": 7, "col_b": "A", "TID": 101},
{"id": 2, "col_a": 7, "col_b": "B", "TID": 102},
{"id": 3, "col_a": 25, "col_b": "C", "TID": 103},
{"id": 4, "col_a": 30, "col_b": "D", "TID": 104},
{"id": 5, "col_a": 4, "col_b": "E", "TID": 105},
{"id": 6, "col_a": 55, "col_b": "F", "TID": 106},
class DoublyLinkedNode(object):
def __init__(self, value: BPlusTreeNode) -> None:
""""Init a doubly-linked list node
Params:
value (BPlusTreeNode) - The value of the node as a B+ tree node, specifically leaf node
"""
if(type(value) != BPlusTreeNode):
print("The value of the node needs to be a BPlusTreeNode")
else:
def lookup_using_hash_index(self, value: str) -> list:
""" Return all the rows where the indexed column contains a value
Params:
value (str) - The condition
Returns:
rows (list) - The matching rows from our table
"""
def create_hash_index(self) -> None:
""" Creates a hash index of our data based on the column
"""
self.hash_table = dict()
for row in self.table:
hsh, buck = self.hash_function(row[self.on])
if(self.hash_table.get(buck, None) is None):
# Empty bucket, insert TID
self.hash_table[buck] = []
class HashIndex(object):
def __init__(self, data: List, col: str) -> None:
""" Init the hash index class
Params:
data (list) - Our data as a list of dicts
col (str) - The column (dict key) to create an index on
"""
self.table = data
self.on = col
def query():
if(accept("SELECT")):
if(columns()):
if(accept("FROM")):
if(name()):
if(terminal(False)):
show_tree(tree)
elif(accept("WHERE")):
if(condition_list()):
if(terminal()):
def columns():
if(accept("all_cols", False)):
return True
else:
if(accept("name")):
if(accept("punctuation", False)):
columns() # Recursively call self till next token is no longer a punctuation or name
else:
return True
else:
def term():
if(accept("integer", False) or accept("float", False) or accept("name", False)):
return True
def terminal():
if(accept("terminal")):
return True
def query():
""" <query> ::= "SELECT " <columns> " FROM " <name> <terminal> | "SELECT " <columns> " FROM " <name> " WHERE " <conditionList> <terminal>
"""
pass
def columns():
""" <columns> ::= (<name> ", ")+ | "*"
Accepts:
- *