def assert__(engine, obj, condition, message=u'Assertion failed'): """:yaql:assert Evaluates condition against object. If it evaluates to true returns the object, otherwise throws an exception with provided message. :signature: obj.assert(condition, message => "Assertion failed") :arg obj: object to evaluate condition on :argType obj: any :arg condition: lambda function to be evaluated on obj. If result of function evaluates to false then trows exception message :argType condition: lambda :arg message: message to trow if condition returns false :argType message: string :returnType: obj type or message .. code:: yaql> 12.assert($ < 2) Execution exception: Assertion failed yaql> 12.assert($ < 20) 12 yaql> [].assert($, "Failed assertion") Execution exception: Failed assertion """ if utils.is_iterator(obj): obj = utils.memorize(obj, engine) if not condition(obj): raise AssertionError(message) return obj
def rec(seq): for t in seq: if utils.is_iterator(t): for t2 in rec(t): yield t2 else: yield t
def main(context, show_tokens, parser): print("Yet Another Query Language - command-line query tool") print("Version {0}".format(version)) if context.get_data('legacy', False): print("Running in a legacy (0.2.x compatible) mode") print("Copyright (c) 2013-2015 Mirantis, Inc") print("") if not context['']: print("No data loaded into context ") print("Type '@load data-file.json' to load data") print("") readline.parse_and_bind('') comm = True while comm != 'exit': try: comm = raw_input(PROMPT).decode(sys.stdin.encoding or locale.getpreferredencoding(True)) except EOFError: return if not comm: continue if comm[0] == '@': func_name, args = parse_service_command(comm) if func_name not in SERVICE_FUNCTIONS: print('Unknown command ' + func_name) else: SERVICE_FUNCTIONS[func_name](args, context) continue try: if show_tokens: parser.lexer.input(comm) tokens = [] while True: tok = parser.lexer.token() if not tok: break tokens.append(tok) print('Tokens: ' + str(tokens)) expr = parser(comm) if show_tokens: print('Expression: ' + str(expr)) except YaqlParsingException as ex: if ex.position: pointer_string = (" " * (ex.position + len(PROMPT))) + '^' print(pointer_string) print(ex.message) continue try: res = expr.evaluate(context=context) if utils.is_iterator(res): res = list(itertools.islice(res, LIMIT)) print(json.dumps(res, indent=4, ensure_ascii=False)) except Exception as ex: print(u'Execution exception: {0}'.format(ex))
def main(context, show_tokens, parser): print("Yet Another Query Language - command-line query tool") print("Version {0}".format(version)) if context.get_data('legacy', False): print("Running in a legacy (0.2.x compatible) mode") print("Copyright (c) 2013-2015 Mirantis, Inc") print("") if not context['']: print("No data loaded into context ") print("Type '@load data-file.json' to load data") print("") readline.parse_and_bind('') comm = True while comm != 'exit': try: comm = raw_input(PROMPT).decode( sys.stdin.encoding or locale.getpreferredencoding(True)) except EOFError: return if not comm: continue if comm[0] == '@': func_name, args = parse_service_command(comm) if func_name not in SERVICE_FUNCTIONS: print('Unknown command ' + func_name) else: SERVICE_FUNCTIONS[func_name](args, context) continue try: if show_tokens: parser.lexer.input(comm) tokens = [] while True: tok = parser.lexer.token() if not tok: break tokens.append(tok) print('Tokens: ' + str(tokens)) expr = parser(comm) if show_tokens: print('Expression: ' + str(expr)) except YaqlParsingException as ex: if ex.position: pointer_string = (" " * (ex.position + len(PROMPT))) + '^' print(pointer_string) print(ex.message) continue try: res = expr.evaluate(context=context) if utils.is_iterator(res): res = list(itertools.islice(res, LIMIT)) print(json.dumps(res, indent=4, ensure_ascii=False)) except Exception as ex: print(u'Execution exception: {0}'.format(ex))
def _map_list(self, data, spec, context, path): if utils.is_iterator(data): data = list(data) elif not utils.is_sequence(data): if data is None or data is dsl.NO_VALUE: data = [] else: data = [data] if len(spec) < 1: return data shift = 0 max_length = -1 min_length = 0 if isinstance(spec[-1], int): min_length = spec[-1] shift += 1 if len(spec) >= 2 and isinstance(spec[-2], int): max_length = min_length min_length = spec[-2] shift += 1 if max_length >= 0 and not min_length <= len(data) <= max_length: raise exceptions.ContractViolationException( 'Array length {0} is not within [{1}..{2}] range'.format( len(data), min_length, max_length)) elif not min_length <= len(data): raise exceptions.ContractViolationException( 'Array length {0} must not be less than {1}'.format( len(data), min_length)) def map_func(): for index, item in enumerate(data): spec_item = (spec[-1 - shift] if index >= len(spec) - shift else spec[index]) yield self._map(item, spec_item, context, '{0}[{1}]'.format(path, index)) return tuple(map_func())
def assert__(engine, obj, condition, message=u'Assertion failed'): if utils.is_iterator(obj): obj = utils.memorize(obj, engine) if not condition(obj): raise AssertionError(message) return obj