Пример #1
0
 def add_fun(string):
     string = BOM_UTF8 + string.encode('utf-8')
     globals_ = {}
     try:
         util.pyexec(string, {'log': _log}, globals_)
     except Exception as e:
         return {
             'error': {
                 'id': 'map_compilation_error',
                 'reason': e.args[0]
             }
         }
     err = {
         'error': {
             'id':
             'map_compilation_error',
             'reason':
             'string must eval to a function '
             '(ex: "def(doc): return 1")'
         }
     }
     if len(globals_) != 1:
         return err
     function = list(globals_.values())[0]
     if type(function) is not FunctionType:
         return err
     functions.append(function)
     return True
Пример #2
0
    def reduce(*cmd, **kwargs):
        code = BOM_UTF8 + cmd[0][0].encode('utf-8')
        args = cmd[1]
        globals_ = {}
        try:
            util.pyexec(code, {'log': _log}, globals_)
        except Exception as e:
            log.error('runtime error in reduce function: %s', e, exc_info=True)
            return ['error', 'reduce_compilation_error', e.args[0]]
        err = [
            'error', 'reduce_compilation_error',
            'string must eval to a function '
            '(ex: "def(keys, values): return 1")'
        ]
        if len(globals_) != 1:
            return err
        function = list(globals_.values())[0]
        if type(function) is not FunctionType:
            return err

        rereduce = kwargs.get('rereduce', False)
        results = []
        if rereduce:
            keys = None
            vals = args
        else:
            if args:
                keys, vals = zip(*args)
            else:
                keys, vals = [], []
        if util.funcode(function).co_argcount == 3:
            results = function(keys, vals, rereduce)
        else:
            results = function(keys, vals)
        return [True, [results]]
Пример #3
0
    def reduce(*cmd, **kwargs):
        code = BOM_UTF8 + cmd[0][0].encode('utf-8')
        args = cmd[1]
        globals_ = {}
        try:
            util.pyexec(code, {'log': _log}, globals_)
        except Exception as e:
            log.error('runtime error in reduce function: %s', e,
                      exc_info=True)
            return {'error': {
                'id': 'reduce_compilation_error',
                'reason': e.args[0]
            }}
        err = {'error': {
            'id': 'reduce_compilation_error',
            'reason': 'string must eval to a function '
                      '(ex: "def(keys, values): return 1")'
        }}
        if len(globals_) != 1:
            return err
        function = list(globals_.values())[0]
        if type(function) is not FunctionType:
            return err

        rereduce = kwargs.get('rereduce', False)
        results = []
        if rereduce:
            keys = None
            vals = args
        else:
            if args:
                keys, vals = zip(*args)
            else:
                keys, vals = [], []
        if util.funcode(function).co_argcount == 3:
            results = function(keys, vals, rereduce)
        else:
            results = function(keys, vals)
        return [True, [results]]
Пример #4
0
 def add_fun(string):
     string = BOM_UTF8 + string.encode('utf-8')
     globals_ = {}
     try:
         util.pyexec(string, {'log': _log}, globals_)
     except Exception as e:
         return {'error': {
             'id': 'map_compilation_error',
             'reason': e.args[0]
         }}
     err = {'error': {
         'id': 'map_compilation_error',
         'reason': 'string must eval to a function '
                   '(ex: "def(doc): return 1")'
     }}
     if len(globals_) != 1:
         return err
     function = list(globals_.values())[0]
     if type(function) is not FunctionType:
         return err
     functions.append(function)
     return True