1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
# -*- python -*-
# -*- coding: utf-8 -*-
#
# This file is part of the easydev software
#
# Copyright (c) 2011-2014
#
# File author(s): Thomas Cokelaer <cokelaer@gmail.com>
#
# Distributed under the GPLv3 License.
# See accompanying file LICENSE.txt or copy at
# http://www.gnu.org/licenses/gpl-3.0.html
#
# Website: https://github.com/cokelaer/easydev
# Documentation: http://packages.python.org/easydev
#
##############################################################################
# $:Id $
import tempfile
__all__ = ["assert_list_almost_equal", "trysetattr", "TempFile"]
# from easydev.decorators import ifpandas
def assert_list_almost_equal(first, second, places=None, deltas=None):
"""Combined version nose.tools.assert_almost_equal and assert_list_equal
This function checks that 2 lists contain identical items.
The equality between pair of items is checked with assert_almost_equal
function, which means you can check for the places argument
.. note:: there may be already some tools to
check that either in nosetests or unittest
but could not find.
.. doctest::
>>> from easydev.easytest import assert_list_almost_equal
>>> assert_list_almost_equal([0,0,1], [0,0,0.9999], places=3)
>>> assert_list_almost_equal([0,0,1], [0,0,0.9999], deltas=1e-4)
"""
if places:
deltas = 10 ** -(places - 1)
if deltas:
for x, y in zip(first, second):
if abs(x - y) > deltas:
raise ValueError
def trysetattr(this, attrname, value, possible):
"""A common test pattern: try to set a non-writable attribute
::
class A(object):
def __init__(self):
self._a = 1
self._b = 2
def _get_a(self):
return self._a
def _set_a(self, value):
self._a = value
a = property(_get_a, _get_b)
def _get_b(self):
return self._b
b = property(_get_b)
>>> o = A()
>>> trysetattr(A, "a", 1, possible=True)
>>> trysetattr(A, "b", 1, False)
AssertionError
"""
if possible == True:
a1 = True
a2 = False
else:
a1 = False
a2 = True
try:
setattr(this, attrname, value)
assert a1 # if the setattr is possible, this should be True
except Exception:
assert a2
class TempFile(object):
"""A small wrapper around tempfile.NamedTemporaryFile function
::
f = TempFile(suffix="csv")
f.name
f.delete() # alias to delete=False and close() calls
"""
def __init__(self, suffix="", dir=None):
self.temp = tempfile.NamedTemporaryFile(suffix=suffix, delete=False, dir=dir)
def delete(self):
try:
self.temp._closer.delete = True
except: # pragma: no cover
self.temp.delete = True
self.temp.close()
def _get_name(self):
return self.temp.name
name = property(_get_name)
def __exit__(self, type, value, traceback):
try:
self.delete()
except AttributeError: # pragma: no cover
pass
finally:
self.delete()
def __enter__(self):
return self
|