Skip to content

Instantly share code, notes, and snippets.

@elmotec
elmotec / add_ex_context.py
Last active December 18, 2015 22:08
How to change the message in an exception to add context information.
def process(val):
try:
do_something(val)
except Exception as ex:
msg = '{}: {}'.format(val, ex.args[0]) if ex.args else str(val)
ex.args = (msg,) + ex.args[1:]
raise
@elmotec
elmotec / bootstrap_cmdline.py
Last active December 1, 2021 17:14
Minimal python program with logging and argparse.
#!python
"""See main.__doc__"""
import os
import logging
import glob
import unittest
import sys
@elmotec
elmotec / wrap_python.bat
Created July 16, 2013 14:14
Wraps call to python script
@title %~n0
@python.exe -O %~d0\%~p0\%~n0.py %*
def filter_log(msg_re=''):
"""Decorator to temporarily disable logging so as to prevent noise.
:param msg_re: regular expression to be filtered out.
:type msg_re: string.
Note that the default argument '' causes all warnings to be filtered out.
"""
def decorator(func):
@elmotec
elmotec / pyenv.bat
Last active August 29, 2015 14:12
Switch between versions of Python.
@rem remove any PYTHONHOME setting.
@set PYTHONHOME=
@rem first sets python path so we can run uniquepath.
@set PYTHON=C:\Python%1
@set PYTHONSTARTUP=%HOME%\scripts\startup.py
@dir %PYTHON% > NUL
@if ERRORLEVEL 1 GOTO FAILED
@set PATH=%PATH%;%PYTHON%
@elmotec
elmotec / uniquepath.bat
Last active August 29, 2015 14:12
Basic wrapper around uniquepath.py that allow changing PATH in one command
@echo off
set PYTHON=python.exe
for %%a in (%*) do (
if "%%a" == "-h" goto nocall
if "%%a" == "--help" goto nocall
if "%%a" == "-d" goto nocall
if "%%a" == "--debug" goto nocall
)
@elmotec
elmotec / uniquepath.py
Created December 30, 2014 14:02
Remove duplicates in the PATH or PATH-like environment variable
#!/usr/bin/env python
"""Removes duplicates in a PATH-like environment variable."""
# Copyright (c) 2012-14 Jerome Lecomte
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
@elmotec
elmotec / __main__.py
Last active July 19, 2019 10:47
A convenience test runners for unittest.
#!/usr/bin/env python
# vim: set encoding=utf-8
"""
My convenience test runners for unittest.
Place it in your tests folder and use it with python -m <test folder>.
(Requires click, and colorama (if you want colors)).
Allows one to run your tests in your tests folder in a spartain way:
sns.set(style='whitegrid', context='notebook', font_scale=1.5)
cm = np.corrcoef(df[cols].values.T)
hm = sns.heatmap(cm, cbar=True, annot=True, square=True, fmt='.2f',
annot_kws={'size': 15}, yticklabels=cols, xticklabels=cols)
plt.show()
sns.set(style='whitegrid', context='notebook', font_scale=1.5)
sns.pairplot(df[cols])
plt.show()