Skip to content

Instantly share code, notes, and snippets.

View arnauldvm's full-sized avatar

Arnauld Van Muysewinkel arnauldvm

View GitHub Profile
@arnauldvm
arnauldvm / parseLines.R
Created December 13, 2016 10:09
parse a vector of lines with a named regex and produce a data.frame
parseLines = function(perlNamedPattern, linesVector) {
match = regexpr(perlNamedPattern, linesVector, perl=TRUE)
found_idx = as.vector(match)>0
names = attr(match, "capture.names")
start = attr(match, "capture.start")
length = attr(match, "capture.length")
end = start + length -1
results_matrix = c()
for (col in seq(1, length(names))) {
results_matrix = rbind(results_matrix, substr(linesVector, start[,col], end[,col]))
@arnauldvm
arnauldvm / parse_duration_to_ms.R
Created December 13, 2016 10:11
parse a string representing a duration
parse_duration_to_ms = function(string) { # e.g. "1w 2d5h15m 08.24s", "0.000045s"
opt_sep_re = "\\h*"
re = paste0("^",
"(?:(?<weeks>\\d+)w)?", opt_sep_re,
"(?:(?<days>\\d+)d)?", opt_sep_re,
"(?:(?<hours>\\d+)h)?", opt_sep_re,
"(?:(?<minutes>\\d+)m)?", opt_sep_re,
"(?:(?<seconds>\\d+)(?:\\.(?<milliseconds>\\d*))?s)?",
"$")
data = parseLines(re, string)
from os import path
import inspect
scriptdir = path.dirname(inspect.getfile(inspect.currentframe()))
@arnauldvm
arnauldvm / pre-commit
Created September 13, 2018 09:04
Git pre-commit hook for python code
#!/bin/sh
# requires py_compile and pycodestyle Python module
if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
@arnauldvm
arnauldvm / pre-commit
Last active October 7, 2018 20:56
Git pre-commit hook for python code
#!/bin/sh
# requires py_compile and pycodestyle Python module
if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
@arnauldvm
arnauldvm / logging.r
Last active March 13, 2019 15:36
R logging framework
require(logging)
# LOG_LEVEL = "FINEST"
# LOG_LEVEL = "DEBUG"
LOG_LEVEL = "INFO"
# LOG_LEVEL = "ERROR"
logReset()
basicConfig(LOG_LEVEL) # bootstrapping the logging package
removeHandler("basic.stdout")
addHandler("mybasic.stdout", action=writeToConsole, level=LOG_LEVEL,
@arnauldvm
arnauldvm / engineering.r
Last active March 13, 2019 15:36
R engineering notation
si_prefixes_positive = c("k", "M", "G", "T", "P", "E", "Z", "Y")
si_prefixes_negative = c("m", "µ", "n", "p", "f", "a", "z", "y")
engineering_notation = function(x, digits=6, digitsafter=NA, si_prefix=TRUE, scale=1, unit="") {
if (is.na(x)) return(NA)
negative = (x<0)
x = scale*abs(x)
if (x==0) {
# ! log10(0) = -Inf
pow = 0
digitsbefore = 1
@arnauldvm
arnauldvm / auto_lib.r
Last active March 13, 2019 15:37
R library auto install
load_lib = function(lib_name) {
if (!require(package=lib_name, character.only=TRUE)) {
install.packages(lib_name)
if (!require(package=lib_name, character.only=TRUE)) stop(paste0("Package '", lib_name, "' not found"))
}
library(package=lib_name, character.only=TRUE)
}
# Usage:
load_lib("data.table") # for example
@arnauldvm
arnauldvm / cleanup.r
Last active March 13, 2019 15:37
R session cleanup
while (sink.number()>0) sink(file=NULL) # Close all remaining files opened by a previous run
cat("All file descriptors closed.\n")
rm(list=ls()); gc() # Clean up as much memory as possible
# Replace by the following line if you want to keep getargs array to retrieve arguments:
# rm(list=grep("getargs", ls(), fixed=T, value=T, invert=T)); gc()
cat("Whole memory cleaned.\n")
@arnauldvm
arnauldvm / args.r
Last active March 13, 2019 15:37
R retrieve args (~ $*) and script dir location (~ $0)
if (sys.nframe()>0) {
script.path = sys.frame(1)[[
switch(as.character(sys.call(1)[[1]]),
source='ofile', # Started with source(...)
debugSource='fileName', # Started with debugSource(...)
NA
)
]]
if (!exists("getargs")) {
# getargs array should be set before invoking source() from RStudio