Skip to content

Instantly share code, notes, and snippets.

View ramn's full-sized avatar

ramn ramn

View GitHub Profile
@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 / 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 / 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 / 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
/**
* <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 / 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:
#
@ramn
ramn / selfdestructing_tempdir.py
Created November 11, 2013 14:30
Selfdestructing tempdir (Python)
class selfdestructing_tempdir(object):
def __enter__(self):
import tempfile
self._tempdir = tempfile.mkdtemp()
return self._tempdir
def __exit__(self, type, value, traceback):
import shutil
shutil.rmtree(self._tempdir)
@ramn
ramn / sockets_as_devices_in_bash.sh
Created October 29, 2013 16:18
Sockets as devices in Bash
exec 4<>/dev/tcp/gopher.meulie.net/70
echo "" >&4
cat <&4
@ramn
ramn / zeromq_demo_publisher.py
Last active August 24, 2022 21:52
Python ZeroMQ pub/sub example
import time.sleep
import zmq
context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind('tcp://127.0.0.1:2000')
# Allow clients to connect before sending data
sleep(10)
socket.send_pyobj({1:[1,2,3]})
@ramn
ramn / ScalaReplJail.scala
Created October 11, 2013 17:23
Run scala app continuously with hidden break out
SECRET_BREAKOUT_RETURN_CODE=119
while scala; [ $? != $SECRET_BREAKOUT_RETURN_CODE ] ; do continue; done