Skip to content

Instantly share code, notes, and snippets.

@liquidgenius
liquidgenius / setup.sh
Created April 4, 2021 17:22 — forked from michaelbrewer/setup.sh
Setting up aws-lambda-powertools
# Ensure we have the latest pip and pipenv
pip3 install --upgrade pip pipenv
# Setup new Python 3.8 project
pipenv --python 3.8
# Install aws-lambda-powertools
pipenv install boto3 aws-lambda-powertools
# Install dev dependencies
pipenv install black pytest --dev --pre
@liquidgenius
liquidgenius / dataclass_from_dict.py
Created August 23, 2020 14:33 — forked from gatopeich/dataclass_from_dict.py
Python 3.7 dataclass to/from dict/json
from dataclasses import dataclass, fields as datafields
from ujson import dumps, loads
# Note: ujson seamlessly serializes dataclasses, unlike stdlib's json
@dataclass
class Point:
x: float
y: float
# Shallow dataclass can be rebuilt from dict/json:
@liquidgenius
liquidgenius / usaddress_adapter.py
Created August 20, 2020 17:15 — forked from andersontep/usaddress_adapter.py
Normalizing addresses with the usaddress package
"""
* requires usaddress https://github.com/datamade/usaddress
* check out the website! https://parserator.datamade.us/usaddress
The usaddress package is pretty great for normalizing inconsistent address data,
especially when if you have a lot to process and can't rely on using a geocoding api.
The results are really granular, probably moreso than you'll need, and definitely
more than most CRM systems if integrating these addresses is your goal.
This is just a simple wrapper around the usaddress.tag() function that I feel will
@liquidgenius
liquidgenius / gist:956790dc251369091eccce511d61534d
Created August 8, 2020 15:37 — forked from vehrka/gist:c33c65ab366e7aec14ad
How to create hexagonal grids in PostGIS
-- Function: makegrid_epsg3857(text, text, text, numeric)
-- DROP FUNCTION makegrid_epsg3857(text, text, text, numeric);
CREATE OR REPLACE FUNCTION makegrid_epsg3857(schemaname text, boundingbox text, gridtable text, halfwidth numeric)
RETURNS text AS
$BODY$
DECLARE
tbl_cnt int;
XMIN numeric;
@liquidgenius
liquidgenius / batch_at_will_commander.sql
Created August 8, 2020 15:31 — forked from aanari/batch_at_will_commander.sql
7 PostgreSQL data migration hacks you should be using (but aren't)
CREATE FUNCTION batch_at_will() RETURNS INTEGER LANGUAGE plpgsql AS $$
DECLARE batched_count INTEGER = 1;
BEGIN
WITH selected_users AS (
SELECT id
FROM users
WHERE role = 'moderator'
AND registration_date < CURRENT_DATE - INTERVAL '4' YEAR
LIMIT 1000
FOR UPDATE NOWAIT
@liquidgenius
liquidgenius / README.md
Created August 8, 2020 15:24 — forked from jpetazzo/README.md
Manual custom geocoding using OSM database

Someone asked how to get the latlong from a specific road near a town on OpenStreetMap.

If you need to do it only once (e.g., you're about to go on a trip, and your GPS cannot find your destination city, but allows you to enter GPS coordinates), you can use Nominatim, OpenStreetMap's geocoding interface.

If you need to do it multiple times, in a programmatic manner, there are at least two ways to do that.

Note: I worked with OSM data a couple of years ago, but I don't have an OSM database on my local laptop right now, so some instructions will be a bit fuzzy. I do apologize in advance.

PostGIS queries on a local OSM DB

@liquidgenius
liquidgenius / create_triggers
Created August 8, 2020 15:22 — forked from colophonemes/create_triggers
Postgres TRIGGER to call NOTIFY with a JSON payload
CREATE TRIGGER person_notify AFTER INSERT OR UPDATE OR DELETE ON income
FOR EACH ROW EXECUTE PROCEDURE notify_trigger(
'id',
'email',
'username'
);
CREATE TRIGGER income_notify AFTER INSERT OR UPDATE OR DELETE ON income
FOR EACH ROW EXECUTE PROCEDURE notify_trigger(
'id',
@liquidgenius
liquidgenius / gist:e9dcfd3c34f8275e880351f6a7a246ac
Created August 8, 2020 15:18 — forked from rolo/gist:1481128
Install Postgres 9.1, PostGIS and create PostGIS template on Ubuntu 11.10 Oneiric Ocelot
#!/bin/bash
#
# Install Postgres 9.1, PostGIS and create PostGIS template on a clean Ubuntu 11.10 Oneiric Ocelot box
# http://wildfish.com
# add the ubuntu gis ppa
sudo apt-get -y install python-software-properties
sudo add-apt-repository ppa:ubuntugis/ubuntugis-unstable
sudo apt-get update
--
-- Create a valid GEOMETRY in 4326 from a lat/lng pair
--
-- @param lat A numeric latitude value.
--
-- @param lng A numeric longitude value.
--
--
CREATE OR REPLACE FUNCTION CDB_LatLng (lat NUMERIC, lng NUMERIC) RETURNS geometry as $$