Skip to content

Instantly share code, notes, and snippets.

View tomplex's full-sized avatar

Tom Caruso tomplex

View GitHub Profile
@tomplex
tomplex / pydantic_redirecting_type.py
Created April 26, 2020 06:28
Use Pydantic to dynamically create specific implementations of an ABC based on input data
import abc
import typing
import pydantic
class File(pydantic.BaseModel, abc.ABC):
@classmethod
def __get_validators__(cls):
# one or more validators may be yielded which will be called in the
@tomplex
tomplex / leaflet_zoom.html
Last active March 11, 2020 20:59
Unexpected behavior with Leaflet map.setZoom
<!DOCTYPE html>
<html lang="en" xmlns="">
<head>
<meta charset="UTF-8">
<title>Zoom Weirdness</title>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.6.0/dist/leaflet.css" />
<script src="https://unpkg.com/leaflet@1.6.0/dist/leaflet.js"></script>
<style>
#map {
height: 500px;
@tomplex
tomplex / calculate_area.py
Last active July 20, 2023 13:02
Calculate area of geometry in meters with Python/Shapely
import pyproj
from shapely import wkt, geometry, ops
from functools import partial
def get_utm_zone(point: geometry.Point) -> int:
"""
Helper function to get the UTM Zone EPSG of the input point.
@tomplex
tomplex / list-dir-size.sh
Last active January 13, 2018 01:17
List top 10 largest files / directories in given directory
#!/bin/bash
# Usage:
# ./list-dir-size.sh my_directory
#
du -hsx $1 | sort -rh | head -10
@tomplex
tomplex / create_fishnet.sql
Last active January 30, 2019 19:20
Create a grid of equal-sized cells from an array of geometries in PostGIS
/*
Author: Tom Caruso
Divide up a box into multiple equal-size cells.
Helpful to split up a coverage of geometries into a gridded
Usage:
SELECT create_grid(array_agg(geom), 10, 10, 4326) FROM my_geometry_table;
@tomplex
tomplex / publish_date.py
Created December 18, 2017 04:35
Argparse & Pika example for /r/learnpython
"""
Help for /r/learnpython: https://www.reddit.com/r/learnpython/comments/7kilzt/cant_get_my_head_around_using_argparse_in_my/
"""
import argparse
import json
import pika
import sys
CONFIG_FILE = 'some_filename.json'
@tomplex
tomplex / container_stop_rm
Created December 2, 2017 01:42
Stop & remove Docker container with completions
#! /usr/bin/env bash
container_stop_rm () {
docker rm $(docker stop $1)
}
_container_stop_rm () {
containers=$(docker ps --format '{{ .Names }}')
COMPREPLY=( $(compgen -W "$containers" -- "${COMP_WORDS[COMP_CWORD]}" ) )
@tomplex
tomplex / clean-up-boot-partition-ubuntu.md
Created November 22, 2017 03:57 — forked from ipbastola/clean-up-boot-partition-ubuntu.md
Safest way to clean up boot partition - Ubuntu 14.04LTS-x64

Safest way to clean up boot partition - Ubuntu 14.04LTS-x64

Reference

Case I: if /boot is not 100% full and apt is working

1. Check the current kernel version

$ uname -r 
@tomplex
tomplex / find_open_port.sh
Created June 15, 2017 15:12
Find an open port on the host machine
#!/bin/bash
read LOWERPORT UPPERPORT < /proc/sys/net/ipv4/ip_local_port_range
while :
do
PORT="`shuf -i $LOWERPORT-$UPPERPORT -n 1`"
ss -lpn | grep -q ":$PORT " || break
done
echo $PORT
@tomplex
tomplex / dict_attr.py
Last active May 15, 2017 13:26
A class for which attributes can be accessed like a `dict`.
class DictAttr:
def __init__(self):
self.attr1 = "Let's go"
self.attr2 = "Pens!"
def __getitem__(self, item):
if getattr(self, item, None):
return getattr(self, item)
else: