A form of defensive design intended to ensure the continuing function of a piece of software in spite of unforeseeable usage of said software.
Defensive programming techniques are used especially when a piece of software could be misused mischievously or inadvertently to catastrophic effect.
1 def normalize_ranges(colname):
2 # Range of 1-D numpy array we loaded application with
3 orig_range = get_base_range(colname)
4 colspan = orig_range['datamax'] - orig_range['datamin']
5
6 # User filtered data from GUI
7 live_data = get_column_data(colname)
8 live_min = numpy.min(live_data)
9 live_max = numpy.max(live_data)
10
11 ratio = {}
12 ratio['min'] = (live_min - orig_range['datamin']) / colspan
13 ratio['max'] = (live_max - orig_range['datamin']) / colspan
14 return ratio
1 # Base/starting data
2 >>> age = numpy.array([10.0, 20.0, 30.0, 40.0, 50.0])
3 >>> height = numpy.array([60.0, 66.0, 72.0, 63.0, 66.0])
4 >>> normalize_ranges('age')
5 {'max': 1.0, 'min': 0.0}
6
7 # Change current range, simulate user filtering data
8 >>> age = numpy.array([20.0, 30.0, 40.0, 50.0])
9 >>> normalize_ranges('age')
10 {'max': 1.0, 'min': 0.25}
11
12 >>> age = numpy.array([-10.0, 20.0, 30.0, 40.0, 50.0])
13 >>> normalize_ranges('age')
14 {'max': 1.0, 'min': -0.5}
1 def normalize_ranges(colname):
2 orig_range = get_base_range(colname)
3
4 # Max will always be greater than the minimum
5 colspan = orig_range['datamax'] - orig_range['datamin']
6
7 # User filtered data from GUI
8 live_data = get_column_data(colname)
9 live_min = numpy.min(live_data)
10 live_max = numpy.max(live_data)
11
12 ratio = {}
13
14 # Numbers should always be positive
15 ratio['min'] = (live_min - orig_range['datamin']) / colspan
16 ratio['max'] = (live_max - orig_range['datamin']) / colspan
17
18 # Should be between [0 - 1]
19 return ratio
1 assert 0.0 <= ratio['min'] <= 1.0, (
2 '"%s" min (%f) not in [0-1] given (%f) colspan (%f)' % (
3 colname, ratio['min'], orig_range['datamin'], colspan))
4
5 assert 0.0 <= ratio['max'] <= 1.0, (
6 '"%s" max (%f) not in [0-1] given (%f) colspan (%f)' % (
7 colname, ratio['max'], orig_range['datamax'], colspan))
1 def normalize_ranges(colname):
2 assert isinstance(colname, str)
3 orig_range = get_base_range(colname)
4 assert orig_range['datamin'] >= 0
5 assert orig_range['datamax'] >= 0
6 assert orig_range['datamin'] <= orig_range['datamax']
7 colspan = orig_range['datamax'] - orig_range['datamin']
8 assert colspan >= 0, 'Colspan (%f) is negative' % (colspan)
9
10 live_data = get_column_data(colname)
11 assert len(live_data), 'Empty live data'
12 live_min = numpy.min(live_data)
13 live_max = numpy.max(live_data)
14
15 ratio = {}
16 ratio['min'] = (live_min - orig_range['datamin']) / colspan
17 ratio['max'] = (live_max - orig_range['datamin']) / colspan
18
19 assert 0.0 <= ratio['min'] <= 1.0
20 assert 0.0 <= ratio['max'] <= 1.0
21 return ratio
1 def main():
2 logging.basicConfig(level=logging.WARNING)
3 log = logging.getLogger('example')
4 try:
5 throws()
6 return 0
7 except AssertionError:
8 log.debug('Error: %s' % (err))
9 except Exception as err:
10 log.exception('Error from throws():')
11 return 1