Skip to content

Instantly share code, notes, and snippets.

import logging
import os
SCRIPT_ID = 'name_of_your_script'
logger = logging.getLogger(SCRIPT_ID)
def setup_logging(filename, loglevel='INFO'):
"""
Set up logging to a file on disk and with a proper log format.
@genba
genba / run_cmd.py
Last active August 29, 2015 14:05
run_cmd
import shlex
import subprocess
def run_cmd(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, block=True, *args, **kwargs):
"""
Run a command using the subprocess module.
:param cmd: command to be executed
:type cmd: str or list
:return: process object
@genba
genba / iter_parent_dirs.py
Last active August 29, 2015 14:03
Get all parent directories of a given path
import os
def iter_parent_dirs(endpath):
previous = ''
basepath = endpath
while previous != '/':
yield basepath
previous = basepath
basepath, _ = os.path.split(basepath)
@genba
genba / format_duration_v1.py
Last active August 29, 2015 14:03
format_duration
def format_duration(seconds):
"""
Format a duration given in seconds as a string expressing the number of hours, minutes and seconds
:param seconds: duration in seconds
:type seconds: int or float
:returns: str
"""
minutes = int(seconds / 60)
if minutes > 0:
@genba
genba / printfunctions.py
Last active August 29, 2015 14:02
Python print functions to stdout and stderr using termcolor
import sys
try:
from termcolor import colored
except ImportError:
def colored(msg, *args, **kwargs):
return msg
def print_info(msg, color='', *args, **kwargs):
if color == '':
@genba
genba / Preferences.sublime-settings
Last active December 26, 2015 12:49
Custom Sublime Text user settings (~/.config/sublime-text-3/Packages/User/Preferences.sublime-settings)
// Settings in here override those in "Default/Preferences.sublime-settings",
// and are overridden in turn by file type specific settings.
{
// Columns in which to display vertical rulers
"rulers": [80],
// Set to true to turn spell checking on by default
"spell_check": true,
// The number of spaces a tab is considered equal to
@genba
genba / forms.py
Created August 4, 2013 12:35
LoginForm and SignupForm for Django apps
#-*- coding: utf-8 -*-
from django import forms
from models import User
class PasswordField(forms.CharField):
def __init__(self):
super(PasswordField, self).__init__(widget=forms.PasswordInput)
class LoginForm(forms.Form):
@genba
genba / test_program.py
Created July 24, 2013 09:53
Python script to run a program a given number of times and check its return code for successful execution.
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import sys
import os
import subprocess
import argparse
def decode_retval(retval):
retval_meanings = {
@genba
genba / Makefile
Last active December 14, 2015 14:49
Utilities for compiling LaTeX documents: - Makefile - open.sh: Script to open a PDF file on either Linux or OS X after compiling with pdflatex
all: pdf2 bib pdf open
@#@echo -e "\aSe ha generado el PDF."
pdf:
pdflatex mi_documento
pdf2:
pdflatex mi_documento
pdflatex mi_documento