Skip to content

Instantly share code, notes, and snippets.

Avatar
😷

Dario Berzano dberzano

😷
View GitHub Profile
View beads2tex.py
#!/usr/bin/env python
"""Generate a LaTeX longtable automatically spanning on multiple pages with images proportionally
scaled appropriately. Adds a scale ruler too. Image names must have a certain format.
"""
from __future__ import print_function
import sys
import os.path
import re
View default_disaster.py
#!/usr/bin/python
from __future__ import print_function
def disaster_function(a_default=[]):
# BEWARE: the next line, if a_default is not provided, modifies the default
# object, and **not a copy** of the default object! This is because you are
# modifying in place a "name" pointing to the default, by effectively changing
# the default value for all future function calls
a_default.append("a string")
print("disaster_function(): %s" % a_default)
@dberzano
dberzano / memdec.py
Last active Mar 7, 2017
Python decorators for class members
View memdec.py
#!/usr/bin/env python
# Will print:
# init method
# decorator here
# called with a message
# 12345
# modern decorator here with decorator with a message
# yabba with another message
# 54321
@dberzano
dberzano / cpueff.py
Created Nov 11, 2015
CPU efficiency on Linux with Python
View cpueff.py
#!/usr/bin/env python
from time import sleep
ncpus = sum([ 1 for x in open("/proc/cpuinfo").read().split("\n") if "bogomips" in x ])
def getup():
tot,idle = open("/proc/uptime").read().split(" ")
return float(tot)*ncpus,float(idle)
@dberzano
dberzano / cvm-context-api.py
Created Jan 30, 2015
Get CernVM Online context from API
View cvm-context-api.py
#!/usr/bin/env python
# Usage:
# cvm-context-api.py [--user-data] <context_id> [<context_key>]
import sys
import requests
import base64
import hashlib
from Crypto.Cipher import AES
@dberzano
dberzano / CMakeLists.txt
Created Jan 28, 2015
CMake: how to use optional arguments in macros and how to escape/unescape args to a shell
View CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project(testoptargsandescape)
macro(testmacro testarg1 testarg2)
message(STATUS "testarg1 = \"${testarg1}\"")
message(STATUS "testarg2 = \"${testarg2}\"")
message(STATUS "Optional arg = \"${ARGV2}\"")
@dberzano
dberzano / ShowRootMaps.C
Created Jan 27, 2015
Show current rootmaps in ROOT
View ShowRootMaps.C
void ShowRootMaps() {
TObjArray *oa = gInterpreter->GetRootMapFiles();
TIter i(oa);
TObject *o;
TNamed *n;
TString s;
while ((o = i.Next()) != NULL) {
n = dynamic_cast<TNamed *>(o);
Printf("%-40s --> %s", n->GetName(), n->GetTitle());
}
@dberzano
dberzano / condor_job_memory
Created Oct 23, 2014
HTCondor: define memory policies
View condor_job_memory
## Pretend that all jobs have very low memory requirements: useful
## when running pilot jobs (e.g. AliEn)
JOB_DEFAULT_REQUESTMEMORY = 42
@dberzano
dberzano / patch-ucvm-mtu.sh
Created Jul 21, 2014
Patch MTU on KVM uCernVM image
View patch-ucvm-mtu.sh
#!/bin/bash
cd $(dirname "$0")
ucvm=$PWD/ucernvm-prod.1.18-2.cernvm.x86_64.hdd
[ ! -e ${ucvm}.orig ] && cp $ucvm ${ucvm}.orig
t=$(mktemp -d /tmp/cvm-initrd-XXXXX)
cd $t
echo starting guestfish
eval "`guestfish --listen`" || exit 1
echo mounting fs
guestfish --remote add $ucvm : run : mount /dev/sda1 / || exit 1
@dberzano
dberzano / condor_user_quotas
Last active Aug 29, 2015
HTCondor: per-user "quotas" using match making
View condor_user_quotas
#
# "Quotas" per user
#
# Our requirements contain an expression that changes at every accepted job. We
# cannot therefore optimize matchmaking by caching the results for a specific
# "requirements" string, but we will need to evaluate it per job.
NEGOTIATOR_MATCHLIST_CACHING = False
# The following two variables are set to enforce these "quotas" on setups with
You can’t perform that action at this time.