Skip to content

Instantly share code, notes, and snippets.

@rohitn
rohitn / formatJSON.m
Created October 25, 2021 01:19 — forked from szhorvat/formatJSON.m
Format imported JSON for readability in Mathematica
formatJSON[json_] := formatRawJSON@rulesToAssociations[json]
rulesToAssociations[rules_] :=
Replace[rules, r : {__Rule} :> Association[r], {0, Infinity}]
basicTypeQ[_String | True | False | Null | (_?NumericQ)] = True;
basicTypeQ[_] = False;
formatRawJSON[json_] :=
@rohitn
rohitn / gridCaptive
Created September 16, 2021 16:57 — forked from rebcabin/gridCaptive
Mathematica functions for displaying captive expressions in Grid form.
dpyNullary[ex_] :=
Grid[{{ex, ""}},
Frame -> {All,False},
Alignment -> Left,
Background -> {{LightOrange,{LightYellow}}}];
dpyMultiary[key_, vals_] :=
With[{c = Length @ vals},
Module[{
spans = Table["", {c}],
@rohitn
rohitn / keychain2certfile.rb
Last active August 29, 2015 14:27 — forked from docwhat/keychain2certfile.rb
Creates an SSL_CERT_FILE on OSX (when using Homebrew) that won't break JRuby. Make sure you run this with normal ruby, not JRuby! See https://github.com/jruby/jruby-openssl/issues/56
#!/usr/bin/env ruby
# Parts stolen with no regret from Homebrew's OpenSSL formula.
require 'fileutils'
require 'openssl'
require 'digest/md5'
require 'digest/sha1'
CERT_FILE = ENV.fetch('SSL_CERT_FILE', '/usr/local/etc/openssl/cert.pem')
@rohitn
rohitn / fk.rb
Created June 14, 2014 00:23
Sequel foreign keys
# https://github.com/jeremyevans/sequel/issues/830
require 'sequel'
require 'logger'
DB = Sequel.sqlite
logger = Logger.new($stdout)
DB.loggers << logger
@rohitn
rohitn / fun.rb
Created October 1, 2013 23:41 — forked from judofyr/fun.rb
class Symbol
def | other
-> arg { other.to_proc[arg.send(self)] }
end
def call(*args)
-> arg { arg.send(self, *args) }
end
end
@rohitn
rohitn / gist:4486629
Created January 8, 2013 18:40
Sequel multi_insert with fallback to single insert
module Vulcan
module DataInterface
module RawLoader
def max_id
max(:id) || 0
end
# Rows are selected in chunks specified by page_size
# and inserted in slices by slice_size
@rohitn
rohitn / whitespace-a-like.rb
Created November 27, 2012 13:54 — forked from Dan-Q/whitespace-a-like.rb
Encode a Ruby program into a version composed almost entirely of unicode whitespace characters. Decodes itself on the fly.
#!/usr/bin/env ruby
# encoding: utf-8
CHARS = %w{                       ​ ‌ ‍    }
def encode(string)
string.chars.map{|c|"#{CHARS[c[0]/16]}#{CHARS[c[0]%16]}"}.join
end
program = <<-EOF
@rohitn
rohitn / gist:2731175
Created May 19, 2012 15:11
find_or_create from Struct
#!/usr/bin/env ruby
require 'rubygems'
require 'sequel'
DB = Sequel.sqlite
DB.create_table :users do
Integer :id
String :name
@rohitn
rohitn / gist:2727033
Created May 18, 2012 18:55
Use FasterCSV for Dataset#to_csv
module Sequel
class Dataset
def to_csv(file_name, mode, opts = {})
n = naked
cols = n.columns
FasterCSV.open(file_name, mode, opts) do |csv|
csv << cols
n.each{|r| csv << cols.map{|c| r[c]}}
end
end
@rohitn
rohitn / sequel_regression_3.34.1.rb
Created April 3, 2012 13:54
Backwards incompatible change in Sequel 3.34.1
#!/usr/bin/env ruby
require 'rubygems'
if ARGV[0]
gem 'sequel', '= 3.34.1'
else
gem 'sequel', '= 3.33.0'
end