Skip to content

Instantly share code, notes, and snippets.

@jiaaro
jiaaro / _INSTRUCTIONS.md
Last active Oct 3, 2022
Using Swift libraries in Python
View _INSTRUCTIONS.md

Using Swift libraries in Python

So... this is obviously totally, 100%, like for. real. not. supported. by. Apple. …yet?

But still... I thought it was pretty badass. And, seeing how there's already a Swift buildpack for Heroku you could move some slow code into Swift can call it as a library function. But, you know, not in production or anything. That would be silly, right?

Now, having said that, the actual Python/Swift interop may have bugs. I'll leave that as an exercise to the reader.

How to get Python code calling Swift functions:

View asyncio_example.py
import asyncio
async def sum_queue_vals(q):
result = 0
while True:
val = await q.get()
if val is None:
break
result += val
return result
@jiaaro
jiaaro / hotkey_helpers.js
Last active Jul 31, 2022
Mac Automation – Javascript (JSX) Hotkey helpers
View hotkey_helpers.js
// How to use:
// 1. Open "Script Editor" (requires OS X 10.10 Yosemite)
// 2. Change the language from "AppleScript" to "JavaScript"
// 3. Paste the code below and replace the safari example.
//
// More info:
// https://developer.apple.com/library/mac/releasenotes/InterapplicationCommunication/RN-JavaScriptForAutomation/index.html
var sys_events = Application("System Events");
@jiaaro
jiaaro / Generate WAV from MIDI.md
Last active May 3, 2022
Generate a wave file from a MIDI file with Pydub
View Generate WAV from MIDI.md

Simple example of rendering a midi file with Pydub

Basically, this takes a MIDI input file (I just googled and grabbed one of Maroon 5's "Animal" – I know, I know) and generates a WAV file.

NOTE: This is the slowest midi rendering program I have ever seen!

Dependencies:

@jiaaro
jiaaro / installing_pyaudio.md
Last active Mar 9, 2022
How to install PyAudio into a VirtualEnv on Mac OS X 10.10
View installing_pyaudio.md

Install portaudio using homebrew (or method of your choice)

brew install portaudio

create $HOME/.pydistutils.cfg using the include and lib directories of your portaudio install:

[build_ext]
View pydub_mixer.py
from pydub import AudioSegment
from pydub.utils import db_to_float
# note: see usage example at the bottom of the gist :)
class Mixer(object):
def __init__(self):
self.parts = []
def overlay(self, sound, position=0):
@jiaaro
jiaaro / controller.lua
Last active Jun 11, 2021
gamepad emulation of playdate crank
View controller.lua
-- uses:
-- https://github.com/rxi/classic
-- https://github.com/rxi/lume
local Object = require 'classic'
local lume = require 'lume'
---@class Controller
local Controller = Object:extend()
local function getStickAngle(x, y)
@jiaaro
jiaaro / async_children_with_cleanup.sh
Last active May 7, 2021
Run some commands asynchronously in bash and then make sure to kill the child processes if the user kills the process with CTRL-C (or similar)
View async_children_with_cleanup.sh
#!/bin/bash
trap "exit" INT TERM # Convert INT and TERM to EXIT
trap "kill 0" EXIT # Kill all children if we receive EXIT
# Run stuff in the background
sleep 3 &
sleep 4 &
# Find child processes and wait for them to finish so this script doesn't
@jiaaro
jiaaro / process_coinbase_export.py
Last active Apr 27, 2021
Coinbase Export: Parse and Process "Convert" Transactions
View process_coinbase_export.py
import csv
csv_path = "/path/to/Coinbase-xxxxx-TransactionsHistoryReport-2021-03-13-22-28-28.csv"
outcsv_path = csv_path + ".processed.csv"
with open(csv_path, "r") as fi, open(outcsv_path, "w") as fo:
r = csv.reader(fi)
w = csv.writer(fo)
for i, row in enumerate(r):
if row and row[-1].startswith("Converted"):
@jiaaro
jiaaro / retirement calc.md
Last active Feb 25, 2021
A Simple, Easy-to-edit Retirement Saving Calculator (in python)
View retirement calc.md

Retirement Calculator

  • ages are in years
  • contribution, and savings are in dollars
  • avg_annual_return is a ratio, so 1.07 is a 7% annual return

let's say I'm 25 years old, I am going to contribute $2000/yr in bonds (~5% return), and I've already invested $5700 in bonds