Skip to content

Instantly share code, notes, and snippets.


Alexis Gallagher algal

  • San Francisco
View GitHub Profile
algal / Zipping.swift
Created February 17, 2019 01:18
Zip files on iOS, without using external libraries and without interoperating with the low-level Compression framework
View Zipping.swift
// Zipping.swift
// known-good: Swift 4.2
// Alexis Gallagher
import Foundation
public extension URL {
/// Creates a zip archive of the file or folder represented by this URL and returns a references to the zipped file
algal / colorize-emacs.bashsource
Last active May 5, 2023 06:05
Setting up truecolor (24 bit color) in emacs in the terminal, under iTerm2,, and others.
View colorize-emacs.bashsource
# sourcing this file will define a bash functions that
# tries to run subsequent calls to emacs with 24 bit color.
# It sets TERM=xterm-emacs-leg if
# - we've created a user-local terminfo record for xterm-emacs-leg, and
# - we're using iTerm2 or something has set COLORTERM=truecolor
# This will cause emacs to use 24 bit color only when it will work,
# inside or outside of tmux. I haven't found a way to auto-detect yet.
algal / loadPoints.swift
Created December 28, 2018 19:41
Read vertex positions with Model I/O
View loadPoints.swift
// known-good: Swift 4.2, macOS
import Foundation
import ModelIO
private func LogDebug(_ s:Any) -> Void { print(s) }
private func LogInfo(_ s:Any) -> Void { print(s) }
private func LogError(_ s:Any) -> Void { print(s) }
struct POINT3D {
var x:Float
algal / tsv_to_json.swift
Last active April 22, 2023 10:01
Quick CSV to JSON in Swift
View tsv_to_json.swift
import Foundation
// Swift 2.0
// poor man's parsers for (TSV) tab-separated value files
// for something more full-featured, the best avenue is CHCSVParser
Reads a multiline, tab-separated String and returns an Array<NSictionary>, taking column names from the first line or an explicit parameter
algal /
Created September 9, 2020 20:08
Plot a confusion matrix in scikitlearn from data not from an estimator
# This uses scikit learn internals, since the sk public API requires you to pass
# in an estimator and sometimes you just want to pass in the some data you'd
# use to calculate a raw CM
def plot_cm(y_true,y_pred,labels):
from sklearn.metrics._plot.confusion_matrix import ConfusionMatrixDisplay
sample_weight = None
normalize = None
include_values = True
algal / SwiftLineReader.swift
Last active February 8, 2023 16:14
Read a file one line at a time in Swift
View SwiftLineReader.swift
import Darwin
Returns a lazy sequence that returns a String for every line of `file`.
Example 1:
// print all lines from a file
let file = fopen(NSBundle.mainBundle().pathForResource("Cartfile", ofType: nil)!,"r")
for line in lineGenerator(file) { print(line,separator: "", terminator: "") }
View MP4Writer.swift
import Foundation
import AVFoundation
import ImageIO
import MobileCoreServices
import BespokeCore
struct FrameInfo {
var frame:CGImage
var frameDuration:TimeInterval
algal / FileOutputStream.swift
Created November 30, 2015 07:25
Buffered writing to a file with Swift OutputStreamType and GCD i/o channel
View FileOutputStream.swift
/// An `OutputStreamType` which uses GCD to write to a file
class FileOutputStream : OutputStreamType
private let filepath:NSURL
private let channel:dispatch_io_t!
Initializes output stream to write to `filename` in the user's Documents directory.
algal / ScaleAspectFitImageView.swift
Last active January 14, 2023 12:36
UIImageView subclass that works with Auto Layout to express its desired aspect ratio
View ScaleAspectFitImageView.swift
import UIKit
// known-good: Xcode 8.2.1
UIImageView subclass which works with Auto Layout to try
to maintain the same aspect ratio as the image it displays.
This is unlike the usual behavior of UIImageView, where the
algal / run-with-emacs
Last active September 6, 2022 17:25
Boilerplate to write a command line script in emacs lisp
View run-with-emacs
:;exec emacs --no-init-file --no-site-lisp -script "$0" -- "$@" # -*- mode:emacs-lisp -*-
(defun main ()
(require 'cl-lib)
(let ((script-name (nth 2 command-line-args))
(args (cdr command-line-args-left)))
;; assert: ARGS now is a possibly empty list of command line args to the script
;; check for valid arguments here
(when (not args)
(princ (format "usage: %s PATH_TO_FILE" script-name))