Skip to content

Instantly share code, notes, and snippets.

View ramn's full-sized avatar

ramn ramn

View GitHub Profile
@ramn
ramn / django_bash_completion
Created November 29, 2013 16:19
Django manage tab completion in bash
# #########################################################################
# This bash script adds tab-completion feature to django-admin.py and
# manage.py.
#
# Testing it out without installing
# =================================
#
# To test out the completion without "installing" this, just run this file
# directly, like so:
#
/**
* <b>Fixed Point Combinator is:</b>
* Y = λf.(λx.f (x x)) (λx.f (x x))
*
* <b>Proof of correctness:</b>
* Y g = (λf . (λx . f (x x)) (λx . f (x x))) g (by definition of Y)
* = (λx . g (x x)) (λx . g (x x)) (β-reduction of λf: applied main function to g)
* = (λy . g (y y)) (λx . g (x x)) (α-conversion: renamed bound variable)
* = g ((λx . g (x x)) (λx . g (x x))) (β-reduction of λy: applied left function to right function)
* = g (Y g) (by second equality) [1]
@ramn
ramn / PrimeGenerator.scala
Last active September 19, 2019 08:14
Prime generator in Scala
val primes: Stream[Int] = 2 #:: Stream.from(3).filter { n => !primes.takeWhile(_ <= math.sqrt(n)).exists(n % _ == 0) }
def isPrime(n: Int) = primes.dropWhile(_ < n).head == n
@ramn
ramn / gist:8499987
Last active January 3, 2016 18:09
Close FD of another process
gdb -batch -ex 'call close(3)' -p <PID> # closes file descriptor 3 for process with <PID>
@ramn
ramn / build_command_completions.sh
Last active August 14, 2021 14:56
Bash command completion builder
#!/bin/bash
#
# Build shell command completions
#
function _build_completions {
local current_word
COMPREPLY=()
current_word=${COMP_WORDS[COMP_CWORD]}
@ramn
ramn / calc_and_plot_durations_from_log_file.sh
Created July 21, 2014 08:15
Calc and plot durations from log file
#!/bin/bash
# Given a logfile with pairwise loglines when an event starts, and when it finishes,
# this script plots the duration for the events.
# Feed me a log file with rows like these:
# 2014-07-21 07:50:03,440 INFO Begin job
# 2014-07-21 07:52:50,530 INFO End job
# 2014-07-21 08:00:03,044 INFO Begin job
# 2014-07-21 08:02:02,548 INFO End job
@ramn
ramn / slugify.sh
Last active August 29, 2015 14:04
Slugify string
#!/bin/bash
rlwrap tr -d '\n\r' | tr -sc [:alnum:] '_' | tr [:upper:] [:lower:]
@ramn
ramn / png_from_dot_file.scala
Created November 3, 2014 21:52
Scala sys.process example, reading bytes back (create png from Dot)
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import scala.sys.process._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import scala.concurrent.blocking
import scala.concurrent.Await
import scala.concurrent.duration._
@ramn
ramn / serve_file_over_http.sh
Last active May 19, 2023 14:25
Serve file over HTTP with Socat
#!/bin/bash
FILE="$1"
PORT=${PORT:-9999}
MIME_TYPE=$(mimetype "$FILE")
SIZE_BYTES=$(du -b "$FILE" | cut -f1)
FILE_NAME=$(basename "$FILE")
HEADER="\
HTTP/1.1 200 OK
@ramn
ramn / GetUnsafeClass.scala
Created November 20, 2014 22:02
sun.misc.Unsafe use in Scala
def getUnsafeInstance: sun.misc.Unsafe = {
val f = classOf[sun.misc.Unsafe].getDeclaredField("theUnsafe")
f.setAccessible(true)
val unsafe = f.get(null).asInstanceOf[sun.misc.Unsafe]
unsafe
}