A readable attempt other than processing with computation

I have a program where I have quite a lot of calculations that I need to do, but where the input might be incomplete (so we can't always calculate all the results), which is fine in itself, but gives the code readability problems:

def try_calc():
    a = {'1': 100, '2': 200, '3': 0, '4': -1, '5': None, '6': 'a'}
    try:
        a['10'] = float(a['1'] * a['2'])
    except (ZeroDivisionError, KeyError, TypeError, ValueError) as e:
        a['10'] = None
    try:
        a['11'] = float(a['1'] * a['5'])
    except (ZeroDivisionError, KeyError, TypeError, ValueError) as e:
        a['11'] = None
    try:
        a['12'] = float(a['1'] * a['6'])
    except (ZeroDivisionError, KeyError, TypeError, ValueError) as e:
        a['12'] = None
    try:
        a['13'] = float(a['1'] / a['2'])
    except (ZeroDivisionError, KeyError, TypeError, ValueError) as e:
        a['13'] = None
    try:
        a['14'] = float(a['1'] / a['3'])
    except (ZeroDivisionError, KeyError, TypeError, ValueError) as e:
        a['14'] = None
    try:
        a['15'] = float((a['1'] * a['2']) / (a['3'] * a['4']))
    except (ZeroDivisionError, KeyError, TypeError, ValueError) as e:
        a['15'] = None
    return a

In [39]: %timeit try_calc()
100000 loops, best of 3: 11 µs per loop

      

So this works well, is high performance, but is actually unreadable. We came up with two more methods for this. 1: use specialized functions that handle internal problems

import operator
def div(list_of_arguments):
    try:
        result = float(reduce(operator.div, list_of_arguments, 1))
    except (ZeroDivisionError, KeyError, TypeError, ValueError) as e:
        result = None
    return result

def mul(list_of_arguments):
    try:
        result = float(reduce(operator.mul, list_of_arguments, 1))
    except (ZeroDivisionError, KeyError, TypeError, ValueError) as e:
        result = None
    return result

def add(list_of_arguments):
    try:
        result = float(reduce(operator.add, list_of_arguments, 1))
    except (ZeroDivisionError, KeyError, TypeError, ValueError) as e:
        result = None
    return result

def try_calc2():
    a = {'1': 100, '2': 200, '3': 0, '4': -1, '5': None, '6': 'a'}
    a['10'] = mul([a['1'], a['2']])
    a['11'] = mul([a['1'], a['5']])
    a['12'] = mul([a['1'], a['6']])
    a['13'] = div([a['1'], a['2']])
    a['14'] = div([a['1'], a['3']])
    a['15'] = div([
        mul([a['1'], a['2']]), 
        mul([a['3'], a['4']])
        ])
    return a

In [40]: %timeit try_calc2()
10000 loops, best of 3: 20.3 µs per loop

      

Twice as slow and yet not as readable to be honest. Option 2: encapsulate inside eval statements

def eval_catcher(term):
    try:
        result = float(eval(term))
    except (ZeroDivisionError, KeyError, TypeError, ValueError) as e:
        result = None
    return result

def try_calc3():
    a = {'1': 100, '2': 200, '3': 0, '4': -1, '5': None, '6': 'a'}
    a['10'] = eval_catcher("a['1'] * a['2']")
    a['11'] = eval_catcher("a['1'] * a['5']")
    a['12'] = eval_catcher("a['1'] * a['6']")
    a['13'] = eval_catcher("a['1'] / a['2']")
    a['14'] = eval_catcher("a['1'] / a['3']")
    a['15'] = eval_catcher("(a['1'] * a['2']) / (a['3'] * a['4'])")
    return a

In [41]: %timeit try_calc3()
10000 loops, best of 3: 130 µs per loop

      

So very slow (compared to other alternatives) but at the same time the most readable. I know some of the problems (KeyError, ValueError) could have been handled by pre-processing the dictionary to ensure the keys are available, but would still leave None (TypeError) and ZeroDivisionErrors anyway, so I don't see any advantage there

My question (s): - Am I missing other options? - Am I completely insane trying to solve it this way? - Is there a more pythonic approach? - Do you think this is the best solution for this and why?

+3


source to share


3 answers


How about storing your calculations as a lambda? Then you can skip all of them using just one try-except block.

def try_calc():
    a = {'1': 100, '2': 200, '3': 0, '4': -1, '5': None, '6': 'a'}
    calculations = {
        '10': lambda: float(a['1'] * a['2']),
        '11': lambda: float(a['1'] * a['5']),
        '12': lambda: float(a['1'] * a['6']),
        '13': lambda: float(a['1'] / a['2']),
        '14': lambda: float(a['1'] / a['3']),
        '15': lambda: float((a['1'] * a['2']) / (a['3'] * a['4']))
    }
    for key, calculation in calculations.iteritems():
        try:
            a[key] = calculation()
        except (ZeroDivisionError, KeyError, TypeError, ValueError) as e:
            a[key] = None

      




By the way, I do not recommend doing this if the order of calculations matters, for example, if you had it in your source code:

a['3'] = float(a['1'] * a['2'])
a['5'] = float(a['3'] * a['4'])

      

Since the dicts are unordered, you have no guarantee that the first equation will run before the second. So it a['5']

can be calculated using the new value a['3']

, or it can use the old value. (This is not a computational issue in the question, since keys one through six are never assigned, and keys 10 through 15 are never used in the calculation.)

+6


source


A slight deviation from Kevin is that you don't need to pre-store the calculations, but instead use a decorator lambda

for error handling too, like this:

from functools import wraps

def catcher(f):
    @wraps
    def wrapper(*args):
        try:
            return f(*args)
        except (ZeroDivisionError, KeyError, TypeError, ValueError):
            return None
    return wrapper

a = {'1': 100, '2': 200, '3': 0, '4': -1, '5': None, '6': 'a'}

print catcher(lambda: a['1'] * a['5'])()

      



And as I mentioned in the comments, you can also do a generic second example of yours:

import operator

def reduce_op(list_of_arguments, op):
    try:
        result = float(reduce(op, list_of_arguments, 1))
    except (ZeroDivisionError, KeyError, TypeError, ValueError) as e:
        result = None
    return result

a['10'] = do_op([a['1'], a['2']], operator.mul) 
# etc...

      

+3


source


I have two solutions, one is slightly faster than the other.

More readable:

def try_calc():
    a = {'1': 100, '2': 200, '3': 0, '4': -1, '5': None, '6': 'a'}

    fn_map = {
        '*': operator.mul,
        '/': operator.div,
    }

    def calc(x, fn, y):
        try:
            return float(fn_map[fn](a[x], a[y]))
        except (ZeroDivisionError, KeyError, TypeError, ValueError):
            return None

    a['10'] = calc('1', '*', '2')
    a['11'] = calc('1', '*', '5')
    a['12'] = calc('1', '*', '6')
    a['13'] = calc('1', '/', '2')
    a['14'] = calc('1', '/', '3')
    a['15'] = calc(calc('1', '*', '2', '/', calc('3', '*', '4'))
    return a

      

Slightly faster:

from operator import mul, div

def try_calc():
    a = {'1': 100, '2': 200, '3': 0, '4': -1, '5': None, '6': 'a'}

    def calc(x, fn, y):
        try:
            return float(fn(a[x], a[y]))
        except (ZeroDivisionError, KeyError, TypeError, ValueError):
            return None

    a['10'] = calc('1', mul, '2')
    a['11'] = calc('1', mul, '5')
    a['12'] = calc('1', mul, '6')
    a['13'] = calc('1', div, '2')
    a['14'] = calc('1', div, '3')
    a['15'] = calc(calc('1', mul, '2', div, calc('3', mul, '4'))
    return a

      

+1


source







All Articles