def evaluate_i(self, static, dynamic, local): left = utils.get_single(self.left.evaluate(static, dynamic, local)) right = utils.get_single(self.right.evaluate(static, dynamic, local)) if left.jpath_type != right.jpath_type: raise e.TypeException( "Left and right values to the ++ operator " "have to be of the same type, but the left value was of " "type " + str(type(left)) + " and the right value was of " "type " + str(type(right)) ) if isinstance(left, d.String): # String concatenation return utils.singleton(d.StandardString(left.get_value() + right.get_value())) if isinstance(left, d.List): # List concatenation return utils.singleton(d.StandardList(left.to_python_list() + right.to_python_list())) if isinstance(left, d.Object): # Object union; take the object on the # left side, create a new copy of it, then update it with the # pairs of the object on the right side. The right side should # override the left side in the case of a key conflict. dictionary = {} for k, v in left: dictionary[k] = v for k, v in right: dictionary[k] = v return utils.singleton(d.StandardObject([d.StandardPair(k, v) for k, v in dictionary.iteritems()])) raise e.TypeException( "Values of type " + str(type(left)) + " can't " "be concatenated using the ++ operator. Only values of type " "string, list, or object can be given to ++. To convert " "values to strings, you can use the string() function." )
def evaluate_i(self, static, dynamic, local): value = utils.get_single(self.value.evaluate(static, dynamic, local)) reference = utils.get_single(self.reference.evaluate(static, dynamic, local)) if isinstance(self.position, Production): position = self.position.evaluate(static, dynamic, local) position = utils.get_single_instance(position, d.Number).get_as_int() else: position = self.position return utils.singleton(d.StandardInsert(value, reference, position))
def query(jpath_query, json={}, vars={}, interpreter=None, as_tuple=False): if interpreter is None: interpreter = Interpreter() module = interpreter.get_binder("jpath").create_query(jpath_query) wrapped_vars = utils.singleton(translate.json_to_jpath(vars)) result = module.call_with_values( context.DynamicContext(translate.json_to_jpath(json), 1, 1), [wrapped_vars]) if len(result) == 0 and not as_tuple: return None elif len(result) == 1 and not as_tuple: return translate.jpath_to_json(utils.get_single(result)) else: return tuple(translate.jpath_to_json(v) for v in result)
def process_result(self, result, update): if update is None: update = True for value in result: # result is a Sequence if not isinstance(result, d.Update): update = False break if update: self.storage.apply_updates(result) result = None else: if len(result) == 0: # Result is the empty sequence result = None else: # Try to get the single resulting item result = translate.jpath_to_json(utils.get_single(result)) self.storage.commit() return result
def get_for_pair_indexer(self, value): result = self.get_pair(utils.get_single(value)) if result is None: return EmptySequence() return utils.singleton(result)
def evaluate_i(self, static, dynamic, local): left = self.left.evaluate(static, dynamic, local) right = self.right.evaluate(static, dynamic, local) return d.StandardSequence([d.StandardPair(utils.get_single(left), utils.get_single(right))])
def evaluate_i(self, static, dynamic, local): left = utils.get_single(self.left.evaluate(static, dynamic, local)) right = utils.get_single(self.right.evaluate(static, dynamic, local)) return d.StandardSequence([d.StandardBoolean(left == right)])
def string(dynamic, value): return utils.singleton(d.StandardString(utils.get_single(value).to_string()))