Skip to content

Instantly share code, notes, and snippets.

View samredai's full-sized avatar

Samuel Redai samredai

View GitHub Profile
@samredai
samredai / trino-and-sqlalchemy.py
Created July 9, 2024 15:13
Python: An example of querying trino using sqlalchemy
# pip install trino
from sqlalchemy.engine import create_engine
import pandas as pd
uri = "trino://localhost:8080/tpch/sf1"
engine = create_engine(uri, connect_args={'http_scheme': 'http', 'user': 'admin'})
connection = engine.connect()
df = pd.read_sql("select * from customer", connection)
@samredai
samredai / hugo_build_all_versions.sh
Created September 23, 2022 15:22
Hugo: Build the homepage and all versioned sites
hugo -s landing-page -d ../public --cleanDestinationDir --configDir ../config --environment landing-page
for VERSION in "$@"
do
hugo -s docs -d ../public/docs/${VERSION} --contentDir content/${VERSION}/ --configDir ../config --environment ${VERSION}
done
hugo -s docs -d ../public/docs/latest --contentDir content/$1/ --configDir ../config --environment $1
@samredai
samredai / write.java
Created September 7, 2022 23:40
Iceberg: Write records to an Iceberg table
import org.apache.iceberg.CatalogProperties;
import org.apache.iceberg.DataFile;
import org.apache.iceberg.Files;
import org.apache.iceberg.PartitionSpec;
import org.apache.iceberg.Schema;
import org.apache.iceberg.Table;
import org.apache.iceberg.catalog.Catalog;
import org.apache.iceberg.catalog.TableIdentifier;
import org.apache.iceberg.catalog.Namespace;
import org.apache.iceberg.data.GenericRecord;
@samredai
samredai / trino.go
Created June 30, 2022 01:00
Go: Query Trino Using trino-go-client
package main
import "fmt"
import "database/sql"
import _ "github.com/trinodb/trino-go-client/trino"
func main() {
dsn := "http://admin@localhost:8080?catalog=iceberg&schema=examples"
db, err := sql.Open("trino", dsn)
if err != nil {
@samredai
samredai / rest_api_json_to_struct.go
Created September 26, 2021 19:48
Go: Query a REST API and convert the JSON payload to a struct
package main
import (
"encoding/json"
"net/http"
"fmt"
)
// 20210926113330
// https://api.coindesk.com/v1/bpi/currentprice.json
@samredai
samredai / add_manager.py
Created August 13, 2020 04:57
Python: Add a manager to a matplotlib FigureCanvasBase object
def add_manager(fig):
"""
Adds a manager to a FigureCanvasBase instance to enable show method
Example:
>>> from matplotlib.figure import Figure
>>> fig = Figure(figsize=(10, 10))
>>> ax = fig.add_subplot(1, 1, 1)
>>> ax.set_title("Example axis")
>>> add_manager(fig)
@samredai
samredai / terminalColors.sh
Created August 13, 2020 01:53
Shell: Daniel Crisman's script to output terminal colors
#!/bin/bash
# Found here: http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html
# I add a bashrc alias to colors that calls this script
#
# This file echoes a bunch of color codes to the
# terminal to demonstrate what's available. Each
# line is the color code of one forground color,
# out of 17 (default + 16 escapes), followed by a
# test use of that color on all nine background
@samredai
samredai / create_relationships_between_random_nodes.cypher
Created June 14, 2020 17:52
Cypher: Create relationships between random nodes
MATCH (p:Person), (l:Location)
WITH p, l, rand() as r
ORDER BY r
LIMIT 100000
WHERE rand() < 0.1
MERGE (p)-[:LIKES]->(l)
@samredai
samredai / random_float_2_dec.cypher
Created June 14, 2020 17:26
Cypher: Return a random float rounded to 2 decimal places (and less than 1000)
return round((rand()*1000) * 100)/100
@samredai
samredai / get_git_diffs.sh
Created March 12, 2020 15:36
Git: Get all commit diffs for a single file
# Retrieve all commit hashes that include the particular file
git log -p path/to/target/file/filename.py | grep -n "^commit" | grep -Eo "[^ ]+$" > ~/commit_hashes.txt
# Loop through and perform a git diff on each hash
while read line; do git diff $line; done < ~/commit_hashes.txt > commit_diffs.txt