Skip to content

Instantly share code, notes, and snippets.

@dbreunig
dbreunig / wikidata_json_prep.sh
Created August 5, 2024 15:52
Preparing Wikidata JSON extracts for processing. Wikidata ships its JSON extracts as a single file. They helpfully put each item on a new line, but unhelpfully wrap all items as a JSON array – with brackets and commas. This one-liner cleans and splits the file into manageable chunks, gzipped together, in a memory friendly manner.
zcat ../latest-all.json.gz | sed 's/,$//' | split -l 100000 - wd_items_cw --filter='gzip > $FILE.gz'
@dbreunig
dbreunig / random_emoji.rb
Last active July 3, 2024 22:55
A Ruby function that returns a random emoji. Useful for seeding and more.
EMOJI_CODEPOINTS = [
*0x1F600..0x1F64F, # Emoticons
*0x1F300..0x1F5FF, # Misc Symbols and Pictographs
*0x1F680..0x1F6FF, # Transport and Map Symbols
*0x2600..0x26FF, # Misc symbols
*0x2700..0x27BF, # Dingbats
*0x1F900..0x1F9FF, # Supplemental Symbols and Pictographs
*0x1FA70..0x1FAFF # Symbols and Pictographs Extended-A
]
def random_emoji = EMOJI_CODEPOINTS.sample.chr(Encoding::UTF_8)
@dbreunig
dbreunig / file_processing_with_ractor_pool.rb
Created June 7, 2024 15:48
A simple demo for processing files using a Ractor pool
def process_file(filename)
puts "Processing file: #{filename}"
File.open(filename) do |filename|
# Do stuff
end
end
puts "Done processing file: #{filename}"
end
# Define the number of Ractors in the pool
@dbreunig
dbreunig / places_for_description.sh
Created April 18, 2024 16:25
Describe the city, state, country, whatever you want to get Overture Maps place data for, returned as newline delimited geojson output to stdout.
#!/bin/bash
# Usage
# ./places_for_desciption.sh 'DESCRIBE THE LOCATION YOU WANT PLACES FOR'
# Check if an argument is provided
if [ "$#" -ne 1 ]; then
echo "Usage: $0 'Location Name'"
exit 1
fi
@dbreunig
dbreunig / fix_slow_network_wsl2.md
Created April 18, 2024 15:59
Fixing Slow Network on WSL2

In Powershell:

$ ipconfig

Note the name of your network connector. Then:

$ netsh int ipv4 set interface "Wi-Fi" forwarding=enable

Replacing "Wi-Fi" with the name of your connector.

@dbreunig
dbreunig / button_progress_controller.js
Last active February 22, 2024 03:20
Poor man's form button progress indicator, in Stimulus, for those long processing requests.
import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
connect() {
addEventListener("turbo:submit-end", ({ target }) => {
clearInterval(this.interval);
this.element.innerHTML = this.originalLabel;
})
this.originalLabel = this.element.innerHTML;
}
@dbreunig
dbreunig / overture_places_to_sqlite.sh
Last active January 23, 2024 14:40
A script for finding the latest Overture release, downloading the parquet files, extracting a subset of the columns into a csv, creating a SQLite database, and loading the csv into a table.
#!/bin/bash
# Create a folder called data
mkdir -p data
# Find the most recent parquet directory with the theme 'places'
recent_dir=$(aws s3 ls s3://overturemaps-us-west-2/release/ --recursive | grep "theme=places" | sort | tail -n 1 | awk -F '/' '{print $1"/"$2"/"$3"/"$4}')
# Extract the release date from the directory path
release_date=$(echo $recent_dir | awk -F '/' '{print $2}')
@dbreunig
dbreunig / updating_an_association_to_be_polymorphic.md
Created January 3, 2024 19:17
Updating an Existing ActiveRecord Association to Polymorphic

Updating an Existing ActiveRecord Association to Polymorphic

There's plenty of documentation out there describing how to create a polymorphic association in Rails and Active Record. But not much about updating an existing association.

Say you have the following model:

class Post < ApplicationRecord
  belongs_to :user
end
@dbreunig
dbreunig / form_timezone_setting_with_stimulus.md
Created December 15, 2023 19:46
Automatically Set Form Time Zone in Rails with Stimulus Controller

Setting a Form's Time Zone Using StimulusJS

First, create a new Stimulus controller. Here, we've title it time_zone_setter_controller.js. This controller does one thing: when the controller connects, it updates the value of the field target to the browser's reported time zone.

It reads:

import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
@dbreunig
dbreunig / overture-places-to-sqlite.py
Last active December 11, 2023 13:06
A (very) simple python CLI to download Overture Places data to a sqlite3 db, given a bounding box.
import click
import duckdb
import sqlite_utils
# Set up click
@click.command()
@click.option("--minx", default=-122.347183)
@click.option("--maxx", default=-122.218437)
@click.option("--miny", default=37.748729)
@click.option("--maxy", default=37.800290)