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)
@arnauldvm
arnauldvm / dest_if.sh
Last active March 13, 2019 15:38
Determine network interface used for a given destination
$ ip route get 10.32.213.84
10.32.213.84 via 10.0.239.1 dev bond1.509 src 10.0.239.163
cache mtu 1500 advmss 1460 hoplimit 64
@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
@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 / 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 / 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 / git-commit-style.md
Last active April 10, 2019 06:53
git style guide

Remember:

| {type}({scope}): {subject} | | {body} (optional) |

{type}:

  • feat (new feature for the user, not a new feature for build script)
  • fix (bug fix for the user, not a fix to a build script)
  • docs (changes to the documentation only)
@arnauldvm
arnauldvm / .gitconfig
Last active January 27, 2024 10:00
Git sync all local tracking branches with remotes
[alias]
tracking = "!f() { git for-each-ref --format '%(refname:short):%(upstream:short)' 'refs/heads' | egrep -v ':$'; }; f"
is-clean-workdir = "!f() { git diff --stat --exit-code || { echo \"Workdir dirty\"; exit 1; }; }; f"
is-clean-index = "!f() { git diff --stat --cached --exit-code || { echo \"Index dirty\"; exit 2; }; }; f"
is-clean = "!f() { git is-clean-workdir && git is-clean-index; }; f"
co-merge = "!f() { local=\"$1\"; remote=\"$2\"; git checkout \"$local\"; git merge --ff-only \"$remote\"; }; f"
current-branch = rev-parse --abbrev-ref HEAD
sync = "!f() { git is-clean || { echo Aborting sync.; exit 1; }; current=$(git current-branch); git fetch --all; git tracking | while IFS=: read local remote; do echo \"Merging $local with $remote\"; git co-merge \"$local\" \"$remote\"; done 3>&1 1>&2 2>&3 | egrep -i --color 'fatal|$' 3>&1 1>&2 2>&3; git checkout \"$current\"; }; f"