Skip to content

Instantly share code, notes, and snippets.

View nicoddemus's full-sized avatar

Bruno Oliveira nicoddemus

View GitHub Profile
@nicoddemus
nicoddemus / get-reqs.py
Created January 4, 2016 11:20
get requirements from current conda env
from __future__ import unicode_literals
from __future__ import print_function
import json
import subprocess
libs = json.loads(subprocess.check_output('conda list -e --json', shell=True))
lines = []
for name in libs:
@nicoddemus
nicoddemus / 2.9.0.rst
Last active December 22, 2015 22:19
pytest changelog sample

2.9.0.dev

  • New pytest.mark.skip mark, which unconditional skips marked tests. Thanks @MichaelAquilina for the complete PR (#1040).
  • Fix #680: the -s and -c options should now work under xdist; Config.fromdictargs now represents its input much more faithfully. Thanks to @bukzor for the complete PR.
@nicoddemus
nicoddemus / warnings.py
Created November 26, 2015 17:42
warnings snippets
# Code typically replaced by _warnings
def warn(message, category=None, stacklevel=1):
"""Issue a warning, or maybe ignore it or raise an exception."""
# Check if message is already a Warning object
if isinstance(message, Warning):
category = message.__class__
# Check category argument
if category is None:
category = UserWarning
if not (isinstance(category, type) and issubclass(category, Warning)):
@nicoddemus
nicoddemus / kw.py
Last active November 7, 2015 21:31
Handling keyword arguments default values
# python 2
def foo(**kwargs):
x = kwargs.pop('x', 10)
y = kwargs.pop('y', 'no value')
if kwargs:
raise TypeError('unknown keyword arguments: %s' % kwargs.keys())
print x, y
@nicoddemus
nicoddemus / conftest.py
Created September 29, 2015 22:50
load_tests example fixed
import types
import unittest
from _pytest.unittest import TestCaseFunction
import pytest
class LoadTestsSuiteCollector(pytest.Collector):
def __init__(self, name, parent, suite):
@nicoddemus
nicoddemus / close_issues.py
Created June 16, 2015 21:00
Close bitbucket issues for pytest repository, after migrating to GitHub
import requests
auth = ('nicoddemus', 'XXXX')
for issue_id in xrange(1, 768+1):
url = 'https://bitbucket.org/api/1.0/repositories/{accountname}/{repo_slug}/issues/{issue_id}/comments'
url = url.format(accountname='pytest-dev', repo_slug='pytest', issue_id=issue_id)
print '-' *10, issue_id
data = {
"content": "This issue has been moved to GitHub: https://github.com/pytest-dev/pytest/issues/{issue_id}".format(issue_id=issue_id),
}
@nicoddemus
nicoddemus / ren_pytest_prefix.py
Last active August 29, 2015 14:22
Rename all tests from "pytest_" to "test_", including data directories
import os
import fnmatch
import sys
directory = sys.argv[1]
for root, dirs, names in os.walk(sys.argv[1]):
for name in names:
if fnmatch.fnmatch(name, 'pytest_*.py') and os.path.basename(root) == '_tests':
source = os.path.join(root, name)
@nicoddemus
nicoddemus / cx_freeze_and_distutils.md
Created May 19, 2015 17:45
Problem trying to freeze an executable which imports distutils modules (directly or indirectly)

cxFreeze and distutils

Problem

When trying to freeze a script which imports any distutils module (for example, distutils.dist), the script fails at runtime with an exception like this:

>   from distutils import dist, sysconfig
E   ImportError: cannot import name dist
@nicoddemus
nicoddemus / gist:d30c616122e30c938b6b
Created April 27, 2015 17:08
Test instance deletion (pytest runner, both pytest style class and xUnit)
from __future__ import unicode_literals
import weakref
from coilib50 import unittest
class Test(unittest.TestCase):
_TESTS = []
def test_01(self):
Test._TESTS.append(weakref.ref(self))
@nicoddemus
nicoddemus / image_diff.py
Created March 25, 2015 23:18
Image diff fixture (draft)
from PIL import ImageChops, ImageDraw, Image
import pytest
import os
import py.path
import math
import operator
def rms_diff(im1, im2):
"""Calculate the root-mean-square difference between two images
Taken from: http://snipplr.com/view/757/compare-two-pil-images-in-python/