Skip to content

Instantly share code, notes, and snippets.

@jsundram
jsundram / julia-thoughts.md
Last active February 20, 2021 18:12
summarizing some of the surprises I encountered in programming Julia for the first time over about 3 months.

Initial impressions of Julia coming from Python (and C++, JavaScript ...)

Surprises / Gotchas

  • 1-based indexing?? in 2021? I thought this battle was over. Esp with C/Python FFI, it's a weird choice.
  • installing packages is weird. open up the repl, type ], and then "add X", or import Pkg; Pkg.add("X")?
  • Manifest.toml is cool but a bit non-obvious to figure out how to update it.
  • methods on objects vs methods that take objects. I get it, but less discoverable. e.g. haskey(mydict, "a") instead of has_key?
    • methodswith(Dict) is a nice workaround (but also isn't itself discoverable!)
  • methodswith doesn't return sorted results! You need sort(methodswith(Dict), by=m->m.name) or methodswith(Dict, Base) to exclude other mod. And this actually doesn't include values or keys unless you do methodswith(Dict, Base; supertypes=true) (thanks [discourse](https://discourse.julialang.org/t/can-methodswith-return-
from datetime import datetime
from dateutil import parser
import csv
import json
import requests
def id_for_page(page):
"""Uses the wikipedia api to find the wikidata id for a page"""
api = "https://en.wikipedia.org/w/api.php"
@jsundram
jsundram / graph_merge.jl
Last active January 28, 2021 01:24
implementation of `merge_hierarchical` in Julia
import Colors
using DataStructures
using ImageSegmentation
using LightGraphs
using MetaGraphs
# idea from https://vcansimplify.wordpress.com/2014/08/17/hierarchical-merging-of-region-adjacency-graphs/
function _weight_mean_color(data::MetaGraph, n1::Int, n2::Int)::Real
# Determine weight of an edge between n1 and n2
@jsundram
jsundram / lapser.py
Created January 11, 2021 16:08
turn a timelapse into a sliced timelapse
from skimage import io
import argparse
import glob
import numpy as np
import os
def make_frames(files, outdir, n=8, prefix="composite"):
split = int(len(files) / n)
pathstart = os.path.join(outdir, prefix)
@jsundram
jsundram / history.jl
Created December 10, 2020 20:46
`IJulia.history()` prints everything on one line in jupyter-console, not sure why; this is a quick workaround
function history()
map(sort(collect(keys(IJulia.In)))) do k
println(k, ": ", IJulia.In[k])
end;
return Nothing;
end
@jsundram
jsundram / explore_types.jl
Created December 7, 2020 23:09
looking at julia's type hierarchy
function super(a)
local t
try
t = supertype(a)
catch
t = typeof(a)
end
if t == a
@jsundram
jsundram / fix_web.sh
Last active July 6, 2021 20:50
Internet connection goes down periodically on macos and needs to be restarted by turning wifi on and off again. Sigh.
#!/bin/bash
# Adapted from https://stackoverflow.com/questions/6118948/bash-loop-ping-successful.
# Network connection mysteriously dies; ping every N seconds, and if ping fails
# turn wifi off and on again, which seems to fix it.
echo "run this script with sudo!"
echo "Starting ..."
DOWN=0
while true; do
@jsundram
jsundram / setup.sh
Last active July 3, 2023 13:34 — forked from bradp/setup.sh
New Mac Setup Script
# forked from https://gist.github.com/bradp/bea75b16d3325f5c47d4
# usage:
# 1) attach ssd to old machine and start running backup.sh
# 2) grab the latest version of this file from
# https://gist.github.com/jsundram/eeca472a8929bfab27209783b16bd6d9
# 3) copy this script onto the new machine and start running it
# sh setup.sh
# you will need to add homebrew to your .zprofile path
# PATH=$PATH:/opt/homebrew/bin
# echo 'export PATH="/opt/homebrew/bin:$PATH"' >> ~/.zprofile
from datetime import datetime, timedelta
import random
import matplotlib.pyplot as plt
import matplotlib.dates as dates
def make_data():
random.seed(0) # Make it repeatable
s = datetime(2020, 1, 1) # Pick a leap year!
@jsundram
jsundram / worldladder.py
Created May 21, 2020 23:44
I got nerd sniped by @vanreece
from collections import defaultdict
import networkx as nx
import string
import matplotlib.pyplot as plt
def get_words(filename='/usr/share/dict/words'):
d = defaultdict(set)
with open(filename) as f:
for line in f: