Skip to content

Instantly share code, notes, and snippets.

@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)
@dbreunig
dbreunig / podcast-to-transcript-to-sqlite.py
Created February 15, 2023 19:05
Download podcasts from an XML feed, transcribe them with whisper, and insert the data into a sqlite db.
import feedparser
import whisper
import sqlite3
import requests
podcast_feed_url = "https://feeds.libsyn.com/92106/rss"
db_name = "podcast.db"
# Create the database and its tables.
con = sqlite3.connect(db_name)
module Battleship
class Game
attr_accessor :player_1, :player_2
def initialize(player_1, player_2)
@player_1 = player_1
@player_2 = player_2
@player_1.game = self
@player_2.game = self
end