Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion python/lsst/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#
#
# LSST Data Management System
#
# Copyright 2008-2017 AURA/LSST.
Expand Down
7 changes: 4 additions & 3 deletions python/lsst/utils/get_caller_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@
def get_caller_name(skip=2):
"""Get the name of the caller as a string in the form module.class.method

Any item that cannot be determined (or is not relevant, e.g. a free function
function has no class) is silently omitted, along with an associated separator.
An empty string is returned if `skip` exceeds the stack height.
Any item that cannot be determined (or is not relevant, e.g. a free
function function has no class) is silently omitted, along with an
associated separator. An empty string is returned if `skip` exceeds the
stack height.

Parameters
----------
Expand Down
186 changes: 117 additions & 69 deletions python/lsst/utils/tests.py

Large diffs are not rendered by default.

13 changes: 8 additions & 5 deletions python/lsst/utils/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ def isAttributeSafeToTransfer(name, value):


def continueClass(cls):
"""Re-open the decorated class, adding any new definitions into the original.
"""Re-open the decorated class, adding any new definitions into the
original.

For example,
::
Expand Down Expand Up @@ -119,7 +120,8 @@ def decorate(func):
name1 = func.__name__
else:
if hasattr(func, "__func__"):
# classmethod and staticmethod have __func__ but no __name__
# classmethod and staticmethod have __func__ but no
# __name__
name1 = func.__func__.__name__
elif hasattr(func, "fget"):
# property has fget but no __name__
Expand Down Expand Up @@ -262,8 +264,8 @@ def __new__(cls, name, bases, attrs):
def __call__(self, *args, **kwds):
# __call__ is invoked when someone tries to construct an instance of
# the abstract base class.
# If the ABC defines a "TEMPLATE_PARAMS" attribute, we use those strings
# as the kwargs we should intercept to find the right type.
# If the ABC defines a "TEMPLATE_PARAMS" attribute, we use those
# strings as the kwargs we should intercept to find the right type.
key = tuple(kwds.pop(p, d) for p, d in zip(self.TEMPLATE_PARAMS,
self.TEMPLATE_DEFAULTS))
# indices are only tuples if there are multiple elements
Expand Down Expand Up @@ -364,7 +366,8 @@ def alias(self, key, subclass):
# indices are only tuples if there are multiple elements
primaryKey = primaryKey[0]
if self._registry.get(primaryKey, None) != subclass:
raise ValueError("Subclass is not registered with this base class.")
raise ValueError(
"Subclass is not registered with this base class.")
self._registry[key] = subclass

# Immutable mapping interface defined below. We don't use collections
Expand Down
6 changes: 4 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
[flake8]
max-line-length = 110
max-line-length = 79
ignore = E133, E226, E228, N802, N803
exclude = __init__.py
exclude =
__init__.py,
.tests/
6 changes: 4 additions & 2 deletions tests/testGetPackageDir.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
class GetPackageDirTestCase(unittest.TestCase):
def testBasics(self):
utilsPath = getPackageDir("utils")
self.assertTrue(os.path.isfile(os.path.join(utilsPath, "tests", "testGetPackageDir.py")))
self.assertTrue(os.path.isfile(os.path.join(utilsPath, "tests",
"testGetPackageDir.py")))

# NOTE: one goal of this test is to confirm that the correct exception
# is raised even if we haven't explicitly imported pex.exceptions.
Expand All @@ -41,7 +42,8 @@ def testBasics(self):

def testUnicodeBasics(self):
utilsPath = getPackageDir(u"utils")
self.assertTrue(os.path.isfile(os.path.join(utilsPath, "tests", "testGetPackageDir.py")))
self.assertTrue(os.path.isfile(os.path.join(utilsPath, "tests",
"testGetPackageDir.py")))


class TestMemory(lsst.utils.tests.MemoryTestCase):
Expand Down
3 changes: 2 additions & 1 deletion tests/testGetTempFilePath.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ def testMultipleCallDepth(self):
def runGetTempFile(self, funcName):
with lsst.utils.tests.getTempFilePath(".fits") as tmpFile:
baseName = os.path.basename(tmpFile)
self.assertEqual(baseName, "testGetTempFilePath_%s.fits" % (funcName,))
self.assertEqual(baseName,
"testGetTempFilePath_%s.fits" % (funcName,))
f = open(tmpFile, "w")
f.write("foo\n")
f.close()
Expand Down
9 changes: 6 additions & 3 deletions tests/testLockProtection.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ def testNoLock(self):
with mc:
mc.danger()
except multithreading.UnsafeAccessError:
self.fail("Raised multithreading.UnsafeAccessError inside of context manager!")
self.fail("Raised multithreading.UnsafeAccessError"
" inside of context manager!")

def testSharedLock(self):
mc = MyClass(multithreading.SharedLock())
Expand All @@ -58,7 +59,8 @@ def testSharedLock(self):
with mc:
mc.danger()
except multithreading.UnsafeAccessError:
self.fail("Raised multithreading.UnsafeAccessError inside of context manager!")
self.fail("Raised multithreading.UnsafeAccessError"
" inside of context manager!")

def testSharedData(self):
mc = MyClass(multithreading.SharedData())
Expand All @@ -67,7 +69,8 @@ def testSharedData(self):
with mc:
mc.danger()
except multithreading.UnsafeAccessError:
self.fail("Raised multithreading.UnsafeAccessError inside of context manager!")
self.fail("Raised multithreading.UnsafeAccessError"
" inside of context manager!")

def testExitViaException(self):
mc = MyClass(multithreading.SharedLock())
Expand Down
10 changes: 6 additions & 4 deletions tests/testOrdering.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ def noOp(self):
class DummyMemoryTest(lsst.utils.tests.MemoryTestCase):
pass

suite = unittest.defaultTestLoader.suiteClass([DummyMemoryTest("testLeaks"),
DummyTest("noOp")])
suite = unittest.defaultTestLoader.suiteClass(
[DummyMemoryTest("testLeaks"), DummyTest("noOp")])

self.assertNotIsInstance(suite._tests[0], lsst.utils.tests.MemoryTestCase)
self.assertIsInstance(suite._tests[-1], lsst.utils.tests.MemoryTestCase)
self.assertNotIsInstance(suite._tests[0],
lsst.utils.tests.MemoryTestCase)
self.assertIsInstance(suite._tests[-1],
lsst.utils.tests.MemoryTestCase)


if __name__ == "__main__":
Expand Down
64 changes: 43 additions & 21 deletions tests/testPybind11.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,35 +76,46 @@ def testListEqualityComparison(self):

def assertAccepts(self, function, value, msg):
try:
self.assertEqual(function(value), value, msg="%s: %r != %r" % (msg, function(value), value))
self.assertEqual(function(value), value,
msg="%s: %r != %r" % (msg, function(value),
value))
except TypeError:
self.fail(msg)

def checkNumeric(self, function):
self.assertAccepts(function, int(1), msg="Failure passing int to %s" % function.__name__)
self.assertAccepts(function, long(1), msg="Failure passing long to %s" % function.__name__)
self.assertAccepts(function, int(1),
msg="Failure passing int to %s" % function.__name__)
self.assertAccepts(function, long(1),
msg="Failure passing long to %s" %
function.__name__)
# should fail to convert even numeric strings
self.assertRaises((TypeError, NotImplementedError),
function, "5") # should fail to convert even numeric strings
# We should be able to coerce integers with different signedness and size to any numeric
# type argument (as long as we don't trigger overflow)
function, "5")
# We should be able to coerce integers with different signedness and
# size to any numeric type argument (as long as we don't trigger
# overflow)
for size in (8, 16, 32, 64):
for name in ("int%d" % size, "uint%d" % size):
array = numpy.ones(1, dtype=getattr(numpy, name))
self.assertAccepts(function, array[0],
msg="Failure passing numpy.%s to %s" % (name, function.__name__))
msg="Failure passing numpy.%s to %s" %
(name, function.__name__))

def checkFloating(self, function):
self.checkNumeric(function)
self.assertAccepts(function, float(3.5), "Failure passing float to %s" % function.__name__)
self.assertAccepts(function, float(3.5),
"Failure passing float to %s" % function.__name__)

def checkInteger(self, function, size):
"""If we pass an integer that doesn't fit in the C++ argument type, we should raise OverflowError"""
"""If we pass an integer that doesn't fit in the C++ argument type,
we should raise OverflowError"""
self.checkNumeric(function)
tooBig = 2**(size + 1)
self.assertRaises(OverflowError, function, tooBig)

def testFloatingPoints(self):
"""Test our customized numeric scalar typemaps, including support for NumPy scalars."""
"""Test our customized numeric scalar typemaps, including support
for NumPy scalars."""
self.checkFloating(_example.accept_float32)
self.checkFloating(_example.accept_cref_float32)
self.checkFloating(_example.accept_cref_float64)
Expand All @@ -114,12 +125,15 @@ def testExtendedIntegers(self):
for size in (8, 16, 32, 64):
self.checkInteger(getattr(_example, "accept_int%d" % size), size)
self.checkInteger(getattr(_example, "accept_uint%d" % size), size)
self.checkInteger(getattr(_example, "accept_cref_int%d" % size), size)
self.checkInteger(getattr(_example, "accept_cref_uint%d" % size), size)
self.checkInteger(
getattr(_example, "accept_cref_int%d" % size), size)
self.checkInteger(
getattr(_example, "accept_cref_uint%d" % size), size)
# Test that we choose the floating point overload when we pass a float,
# and we get the integer overload when we pass an int.
# We can't ever distinguish between different kinds of ints or different
# kinds of floats in an overloading context, but that's a Pybind11 limitation.
# We can't ever distinguish between different kinds of ints or
# different kinds of floats in an overloading context, but that's a
# Pybind11 limitation.

def testOverloads(self):
self.assertEqual(_example.getName(int(1)), "int")
Expand All @@ -129,7 +143,8 @@ def testCppIndex1Axis(self):
"""Test the 1-axis (2 argument) version of cppIndex
"""
# loop over various sizes
# note that when size == 0 no indices are valid, but the "invalid indices" tests still run
# note that when size == 0 no indices are valid, but the "invalid
# indices" tests still run
for size in range(4):
# loop over all valid indices
for ind in range(size):
Expand Down Expand Up @@ -161,15 +176,21 @@ def testCppIndex2Axis(self):
# loop over all valid indices
for ind0 in range(size0):
for ind1 in range(size1):
# negative indices that point to the same element as the positive index
# negative indices that point to the same element as
# the positive index
negind0 = ind0 - size0
negind1 = ind1 - size1

# both indeces valid
self.assertEqual(cppIndex(size0, size1, ind0, ind1), (ind0, ind1))
self.assertEqual(cppIndex(size0, size1, ind0, negind1), (ind0, ind1))
self.assertEqual(cppIndex(size0, size1, negind0, ind1), (ind0, ind1))
self.assertEqual(cppIndex(size0, size1, negind0, negind1), (ind0, ind1))
self.assertEqual(cppIndex(size0, size1, ind0, ind1),
(ind0, ind1))
self.assertEqual(cppIndex(size0, size1, ind0, negind1),
(ind0, ind1))
self.assertEqual(cppIndex(size0, size1, negind0, ind1),
(ind0, ind1))
self.assertEqual(cppIndex(size0, size1,
negind0, negind1),
(ind0, ind1))

# one index invalid
with self.assertRaises(IndexError):
Expand All @@ -181,7 +202,8 @@ def testCppIndex2Axis(self):
with self.assertRaises(IndexError):
cppIndex(size0, size1, negbad0, ind1)

# both indices invalid (just test the invalid indices closest to 0)
# both indices invalid (just test the invalid indices
# closest to 0)
with self.assertRaises(IndexError):
cppIndex(size0, size1, size0, size1)
with self.assertRaises(IndexError):
Expand Down
12 changes: 8 additions & 4 deletions tests/testRaDecToStr.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,21 @@ def testStrToRadDelim(self):
decRad = decDeg*math.pi/180.
for delim in ['_', ' ']:
self.assertAlmostEqual(
lsstutils.raStrToRad(raStr.replace(':', delim), delim), raRad, 6)
lsstutils.raStrToRad(raStr.replace(':', delim), delim),
raRad, 6)
self.assertAlmostEqual(
lsstutils.decStrToRad(decStr.replace(':', delim), delim), decRad, 6)
lsstutils.decStrToRad(decStr.replace(':', delim), delim),
decRad, 6)

def testStrToDegDelim(self):
for raDeg, decDeg, raStr, decStr in self.goodData:
for delim in ['_', ' ']:
self.assertAlmostEqual(
lsstutils.raStrToDeg(raStr.replace(':', delim), delim), raDeg, 4)
lsstutils.raStrToDeg(raStr.replace(':', delim), delim),
raDeg, 4)
self.assertAlmostEqual(
lsstutils.decStrToDeg(decStr.replace(':', delim), delim), decDeg, 3)
lsstutils.decStrToDeg(decStr.replace(':', delim), delim),
decDeg, 3)


class TestMemory(lsst.utils.tests.MemoryTestCase):
Expand Down
12 changes: 8 additions & 4 deletions tests/testSharedData.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ def testAcquire(self):
self.sd.acquire()
self.assertTrue(self.sd._is_owned(), "lock not kept")
self.sd.release()
self.assertTrue(self.sd._is_owned(), "lock not kept after partial release")
self.assertTrue(self.sd._is_owned(),
"lock not kept after partial release")
self.sd.release()
self.assertFalse(self.sd._is_owned(), "lock not released")

Expand Down Expand Up @@ -107,7 +108,8 @@ def testAdd(self):
with self.sd:
self.sd.lname = "Plante"
attrs = self.sd.dir()
self.assertEqual(len(attrs), 4, "Wrong number of items: " + str(attrs))
self.assertEqual(len(attrs), 4,
"Wrong number of items: " + str(attrs))
self.assertEqual(self.sd.lname, "Plante")


Expand All @@ -123,7 +125,8 @@ def testAcquire(self):
self.sd.acquire()
self.assertTrue(self.sd._is_owned(), "lock not kept")
self.sd.release()
self.assertTrue(self.sd._is_owned(), "lock not kept after partial release")
self.assertTrue(self.sd._is_owned(),
"lock not kept after partial release")
self.sd.release()
self.assertFalse(self.sd._is_owned(), "lock not released")

Expand Down Expand Up @@ -178,7 +181,8 @@ def testAdd(self):
with self.sd:
self.sd.lname = "Plante"
attrs = self.sd.dir()
self.assertEqual(len(attrs), 4, "Wrong number of items: " + str(attrs))
self.assertEqual(len(attrs), 4,
"Wrong number of items: " + str(attrs))
self.assertEqual(self.sd.lname, "Plante")


Expand Down
Loading