You can replace the lambda by a proper function and get some speedups. There is nothing that prevents your lambda from being a function.
Additionally, you cannot add dynamic expressions to a function. The closest approach would be to pass the keys and thresholds as parameters to your function:
def comparevalues(values, keys, thresholds, cmpfuncs):
return [f(values[k], t) for f, k, t in zip(cmpfuncs, keys, thresholds)]
Here values is a dictionary, keys is a list of keys used in each "rule", threshold is the threshold applied to each "rule" and cmpfuncs are comparison functions used in each rule (e.g. <, <=, >...), you can create your own operators if they are complex, for simple thresholds you can use python's operators:
>>> import operator as op
>>> (5 > 6) == op.gt(5, 6) # Same operation, explicit vs function call
All 3 lists keys, thresholds and cmpfuncs should have the same length, as each of the elements forms a rule triplet.
As an example:
>>> import operator as op
>>> dictionary = {'a': 5, 'b': 10}
>>> keys = ['a', 'b', 'a']
>>> thresholds = [1, 4, 5]
>>> cmpfuncs = [op.gt, op.gt, op.lt]
>>> comparevalues(dictionary, keys, thresholds, cmpfuncs)
[True, True, False]
The above is creating 3 rules: (1) 'a' > 1, (2) 'b' > 4 and (3) 'a' < 5. Note that op.gt = > and op.lt = <.
If you want to dynamically add more operators, you just have to maintain a global lists of keys, threshodlsandcmpfuncs`, update them with new rules, and pass them with every function call.
Now that the approach is explained, find bellow a way of making the same problem more portable:
def comparerules(values, rules):
return [f(values[k], t) for f, k, t in rules]
The approach is the same as above, but in this case rules is a list of triplets, where each triplet is a tuple of the form (cmpfunc, key, threshold).
To use this function with equivalent rules to the above example:
>>> values = {'a': 5, 'b': 10}
>>> rules = [(op.gt, 'a', 1), (op.gt, 'b', 4), (op.lt, 'a', 5)]
>>> comparerules(values, rules)
[True, True, False]
This way, you only have to maintain a global rules array, edit it to append/remove new rules, and send it in every function call:
>>> rules += [(op.gt, 'b', 10)] # values['b'] > 10
>>> comparerules(values, rules)
[True, True, False, False]
And last, to check whether values satisfies all the conditions, just use python's all:
>>> all(comparerules(values, rules))
False
lambdais the way to go here? If the logic is not plain simple, I would go for a full-pledged functionall(values[el] < 5 for el in brands)depending on scope...