Skip to content

Instantly share code, notes, and snippets.

View Tantas's full-sized avatar

Joseph Preiss Tantas

  • Toronto Area, Ontario, Canada
View GitHub Profile
@Tantas
Tantas / sqlalchemy_pydantic_json.py
Last active February 18, 2024 18:35
SqlAlchemy Pydantic 2+ Json Column
from pydantic import BaseModel, TypeAdapter
from sqlalchemy import JSON, TypeDecorator
from sqlalchemy.orm import DeclarativeBase
from sqlalchemy.sql import elements
class PydanticJson(TypeDecorator):
impl = JSON
cache_ok = True
@Tantas
Tantas / sqlalchemy_pydantic_yaml.py
Last active February 18, 2024 18:44
SqlAlchemy Pydantic 2+ Yaml Column
import json
from io import BytesIO, IOBase, StringIO
from typing import Type, TypeVar
from pydantic import BaseModel, TypeAdapter
from ruamel.yaml import YAML
from sqlalchemy import Text, TypeDecorator
T = TypeVar("T", bound=BaseModel)
@Tantas
Tantas / vpn_setup.sh
Created April 6, 2019 20:18
IKEv2 VPN server on Amazon linux
#!/bin/bash
# Installs an IKEv2 VPN server on Amazon linux.
# Reference:
# https://hub.zhovner.com/geek/universal-ikev2-server-configuration/
# https://www.zeitgeist.se/2013/11/22/strongswan-howto-create-your-own-vpn/
# Operates well on a t2.nano instance for administrative use. t2 allows full CPU
# usage as long as < 5% of daily operation time which is perfect for an
# administrative VPN. The server fits well inside the ram requirements and uses
@Tantas
Tantas / clean_characters.py
Last active June 8, 2018 18:18
This replaces or removes special characters. Useful when a font cannot reproduce.
# Some fonts may not be able to represent all characters.
# This replaces or removes the offending characters.
# Get the contents.
f = open('translate.txt', 'r')
file_contents = f.read()
f.close()
# Escape all special characters.
escaped_string = file_contents.encode('ascii', 'xmlcharrefreplace').decode('utf-8')
@Tantas
Tantas / mysql_backup.sh
Created February 25, 2016 22:18
mysql_backup.sh
#!/bin/bash
DUMP_DIR="<dump_dir>"
MYSQLDUMP="/usr/bin/mysqldump"
DUMP_FILE_PREFIX="<database>_dump"
dump_date=`date +"%Y%m%d-%H"`
dump_file="${DUMP_FILE_PREFIX}_${dump_date}.sql"
if [ ! -d "$DUMP_DIR" ]; then
@Tantas
Tantas / objc_codebase_stats.sh
Created August 21, 2015 16:20
Gives some metrics of the size of an objective-c code base.
#!/bin/bash
# Lines of code
cloc ./Classes
# Number of classes
grep -r "^\@interface [a-zA-Z0-9]* \: " ./Classes | wc -l
# Number of instance selectors
grep -r "^\- ([a-zA-Z0-9 ]*)[a-zA-Z0-8:() ]* {" ./Classes | wc -l
@Tantas
Tantas / md5.m
Created April 29, 2015 16:42
Objective-C MD5 C Function
#import <CommonCrypto/CommonHMAC.h>
NSString* md5(NSString *string) {
const char *cStr = [string UTF8String];
unsigned char digest[CC_MD5_DIGEST_LENGTH];
CC_MD5( cStr, (unsigned int)strlen(cStr), digest );
NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
[output appendFormat:@"%02x", digest[i]];
return output;
@Tantas
Tantas / basic_auth.go
Last active August 29, 2015 14:18
Golang Basic Auth Handler
var alphaNumeric = regexp.MustCompile("^[a-zA-Z0-9]*$")
var db *sql.DB
// Checks that the user is authorized by validating the username and password
// provided through basic authentication against values in the database.
// Presents a login page if not present or invalid.
func authHandler(fn func(http.ResponseWriter, *http.Request)) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Send a 401 Not Authorized and set a header value to trigger login.
presentLogin := func(w http.ResponseWriter, r *http.Request) {
@Tantas
Tantas / log.go
Last active September 8, 2021 18:22 — forked from cespare/log.go
Apache access logs for golang.
import (
"fmt"
"io"
"net/http"
"strings"
"time"
)
// https://httpd.apache.org/docs/2.2/logs.html#combined + execution time.
const apacheFormatPattern = "%s - - [%s] \"%s %s %s\" %d %d \"%s\" \"%s\" %.4f\n"
@Tantas
Tantas / gitsetup.sh
Created November 30, 2014 04:30
Ubuntu 14.04 LTS GOGS Installation using Packager, MySQL and Nginx
#!/bin/bash
# Install gogs as a package
wget -qO - https://deb.packager.io/key | sudo apt-key add -
echo "deb https://deb.packager.io/gh/pkgr/gogs trusty pkgr" | sudo tee /etc/apt/sources.list.d/gogs.list
sudo apt-get update
sudo apt-get install gogs
# Put down a webserver and mysql
APP_NAME="gogs"