Changeset 9372


Ignore:
Timestamp:
21 Oct 2012, 10:43:54 (12 years ago)
Author:
uli
Message:

Add a helper to provide a (Python 3 compatible) product calculator for sequences of numbers.

Location:
main/waeup.kofa/trunk/src/waeup/kofa/utils
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/trunk/src/waeup/kofa/utils/helpers.py

    r9043 r9372  
    685685        writer.writerow(row)
    686686    return tmp_path
     687
     688def product(sequence, start=1):
     689    """Returns the product of a sequence of numbers (_not_ strings)
     690    multiplied by the parameter `start` (defaults to 1). If the
     691    sequence is empty, returns 0.
     692    """
     693    if not len(sequence):
     694        return 0
     695    result = start
     696    for item in sequence:
     697        result *= item
     698    return result
  • main/waeup.kofa/trunk/src/waeup/kofa/utils/tests/test_helpers.py

    r8632 r9372  
    3939    implements(IFakeObject)
    4040
     41class SimpleHelpersTestCase(unittest.TestCase):
     42    # Tests for simple functions in `helpers`.
     43    def test_product(self):
     44        # the product will return zero without input
     45        result1 = helpers.product([])
     46        result2 = helpers.product([1,2,3])
     47        result3 = helpers.product([], start=5)
     48        result4 = helpers.product([1,2,3], start=5)
     49        self.assertEqual(result1, 0)
     50        self.assertEqual(result2, 6)
     51        self.assertEqual(result3, 0)
     52        self.assertEqual(result4, 30)
     53        return
     54
    4155class RemoveFileOrDirectoryTestCase(unittest.TestCase):
    4256
     
    447461            contents, '\r\n')
    448462        return
     463
    449464
    450465def test_suite():
     
    463478        GetFileFormatTestCase,
    464479        MergeCSVFileTestCase,
     480        SimpleHelpersTestCase,
    465481        ]:
    466482        suite.addTests(
Note: See TracChangeset for help on using the changeset viewer.