Skip to content

Instantly share code, notes, and snippets.

View tomplex's full-sized avatar

Tom Caruso tomplex

View GitHub Profile
@tomplex
tomplex / mount_local.sh
Last active September 28, 2023 15:22
Mount a docker image's contents to your local machine for inspection
IMAGE_REGISTRY=""
IMAGE_NAME="image"
IMAGE_TAG="tag"
VOLUME_NAME="${IMAGE_NAME}-${IMAGE_TAG}"
# Contents will be accessible in /tmp/IMAGE_NAME-IMAGE_TAG
docker volume create \
--driver local \
--opt type=none \
@tomplex
tomplex / flatten.py
Created May 12, 2022 20:22
flatten dict, keeping the deepest or shallowest instance of each key
import json
from typing import Any, Iterator, Tuple, Union
def _deserializable(obj: Any) -> bool:
try:
o = json.loads(obj)
assert isinstance(o, Union[list, dict])
return True
except:
@tomplex
tomplex / nouns.py
Last active April 28, 2022 21:11
mypy: protocol + bound
from typing import Protocol, TypeVar
class Record:
"""in practice, this class has many more methods/
attributes that I need access to."""
def update(self):
pass
@tomplex
tomplex / surfaces.py
Created April 26, 2022 13:35
proposed workflow for loading surface geometry per https://github.com/shapely/shapely/discussions/1369
from geomet import wkt
from io import StringIO
import psycopg2
def main():
# we'll use this to copy data into postgres
fileobj = StringIO()
# build WKT geometries
from typing import List
class House:
def __init__(self, rooms: List[Room]):
self.rooms = rooms
def __len__(self):
return len(self.rooms)
def __getitem__(self, item):
from typing import List
class House:
def __init__(self, rooms: List[Room]):
self.rooms = rooms
def __len__(self):
return len(self.rooms)
from dataclasses import dataclass
from typing import List
@dataclass
class Room:
name: str
length: int
width: int
height: int

Keybase proof

I hereby claim:

  • I am tomplex on github.
  • I am tomcaruso (https://keybase.io/tomcaruso) on keybase.
  • I have a public key ASAUYKZvWV_kuAcYihLNmDfx1CX57Qtvv1OYvGRqhUrdhQo

To claim this, I am signing this object:

@tomplex
tomplex / nearest.py
Last active August 19, 2020 08:36
Nearest implementation with index
from shapely.geometry import Point, LineString, box
def _expand(geom, amt=0.5):
bounds = geom.bounds
d = math.sqrt(2 * (amt ** 2))
while True:
yield box(*bounds)
bounds = (bounds[0] - d, bounds[1] - d, bounds[2] + d, bounds[3] + d)
@tomplex
tomplex / json_sub_array.sql
Last active December 12, 2023 13:47
Postgres JSON/B array slice
/*
Taken & expanded from https://stackoverflow.com/a/40377186/4453925
*/
drop function if exists json_sub_array(json, int, int);
create or replace function json_sub_array(json_array json, from_pos int, to_pos int)
returns json language sql as $$
select json_agg(value)
from json_array_elements(json_array) with ordinality
where ordinality-1 between from_pos and to_pos
$$;