Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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%
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 / wrap_python.bat
Created July 16, 2013 14:14
Wraps call to python script
@title %~n0
@python.exe -O %~d0\%~p0\%~n0.py %*
@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 / 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