Skip to content

Instantly share code, notes, and snippets.

@roganjoshp
roganjoshp / validation_demo.py
Last active October 18, 2025 07:05
Data Validation
# SETUP IN utils.py
class NumParse(Enum):
VAL = 1 # Valid number
INV = 2 # Invalid number format
NEG = 3 # Negative value
ZER = 4 # Zero value
EXC = 5 # Exceeds maximum value defined in config
def __eq__(self, other: str):
import logging
import timeit
logger = logging.getLogger(__name__)
logging.basicConfig(filename='example.log', encoding='utf-8', level=logging.INFO)
def as_args():
some_str = "This is some kind of error message reasonably long"
@roganjoshp
roganjoshp / gist:59bbabf8790aa217c878327f0dd07518
Created December 6, 2024 19:13
Pandas string operations
from string import ascii_letters
import timeit
import numpy as np
import pandas as pd
letters = [
"".join(np.random.choice(list(ascii_letters), 10, replace=True))
for x in range(1000000)
# File deliberately empty, but gist won't allow it
@roganjoshp
roganjoshp / database.py
Last active November 17, 2023 14:59
Alembic migrations
# src/ex_machina/database.py
import click
import os
from alembic import command
from alembic.config import Config as AlembicConfig
from alembic.util import AutogenerateDiffsDetected
from sqlalchemy import create_engine, MetaData
from sqlalchemy.orm import scoped_session, sessionmaker
import sqlite3
import pandas as pd
conn = sqlite3.connect(':memory:')
c = conn.cursor()
c.execute("""
CREATE TABLE food_labour (
name TEXT,
import random
import datetime as dt
import pandas as pd
df = pd.DataFrame({'date': ['2021-01-02', '2021-01-03', '2021-01-04',
'2021-01-01', '2021-01-03', '2021-01-04',
'2021-01-01', '2021-01-02', '2021-01-04',
'2021-01-01', '2021-01-02', '2021-01-03'],
'plant': [1, 1, 1,
@roganjoshp
roganjoshp / config.py
Created June 24, 2020 09:35
relative dump of config files
# The contents of beatroute/src/beatroute/config.py
import __main__
import json
import os
import sqlite3
import uuid
from pathlib import Path
@roganjoshp
roganjoshp / .env
Last active June 23, 2020 12:54
Flask with envvars
SECRET_KEY = '2a057dd2f0844f5a928578032c5b9955'
DB_URL = 'postgresql+psycopg2://postgres:######@127.0.0.1/so_test'
@roganjoshp
roganjoshp / test_postgres
Created June 5, 2020 13:17
Confirming that tuple can be used in LIKE query for postgres
from flask import Flask, render_template_string
from flask_sqlalchemy import SQLAlchemy
app = Flask('__name__')
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql+psycopg2://postgres:######@127.0.0.1/so_test'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SESSION_TYPE'] = 'sqlalchemy'