From 408274152baf75485f17eee9cc0550fd7bb82960 Mon Sep 17 00:00:00 2001 From: Erdenezul Batmunkh Date: Tue, 4 Sep 2018 20:24:34 +0800 Subject: [PATCH] reduce cycle complexity using logic map --- mongoengine/queryset/transform.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/mongoengine/queryset/transform.py b/mongoengine/queryset/transform.py index 5f777f41..555be6f9 100644 --- a/mongoengine/queryset/transform.py +++ b/mongoengine/queryset/transform.py @@ -214,17 +214,20 @@ def update(_doc_cls=None, **update): if parts[0] in UPDATE_OPERATORS: op = parts.pop(0) # Convert Pythonic names to Mongo equivalents - if op in ('push_all', 'pull_all'): - op = op.replace('_all', 'All') - elif op == 'dec': + operator_map = { + 'push_all': 'pushAll', + 'pull_all': 'pullAll', + 'dec': 'inc', + 'add_to_set': 'addToSet', + 'set_on_insert': 'setOnInsert' + } + # If operator doesn't found from operator map, op value will stay + # unchanged + op = operator_map.get(op, op) + if op == 'dec': # Support decrement by flipping a positive value's sign # and using 'inc' - op = 'inc' value = -value - elif op == 'add_to_set': - op = 'addToSet' - elif op == 'set_on_insert': - op = 'setOnInsert' match = None if parts[-1] in COMPARISON_OPERATORS: