Esempio n. 1
0
 def checkSchema(k,schema,attribute='',start=None,end=None):
   #print('In reader.Schemas.checkSchema(%s,%s,%s,%s)'%(schema,attribute,start,end))
   schema = k.getSchema(schema)
   if not schema: return False
   f = schema.get('check')
   if not f: 
     v = True
   else:
     try:
       now = time.time()
       start = fun.notNone(start,now-1)
       end = fun.notNone(end,now)
       k.LOCALS.update({'attribute':attribute.lower(),
             'match':clmatch,'clmatch':clmatch,
             'start':start,'end':end,'now':now,
             'reader':schema.get('reader',schema.get('api')),
             'schema':schema.get('schema'),
             'dbname':schema.get('dbname',schema.get('schema','')),
             })
       #print('(%s)%%(%s)'%(f,[t for t in k.LOCALS.items() if t[0] in f]))
       v =fun.evalX(f,k.LOCALS,k.MODULES)
     except:
       traceback.print_exc()
       v =False
   #print('checkSchema(%s): %s'%(schema,v))
   return v
Esempio n. 2
0
def trial(tries,excepts=None,args=None,kwargs=None,return_exception=None):
    """ This method executes a try,except clause in a single line
    :param tries: may be a callable or a list of callables
    :param excepts: it can be a callable, a list of callables, a map of {ExceptionType:[callables]} or just a default value to return	
    :param args,kwargs: arguments to be passed to the callable
    :return exception: whether to return exception or None (default)
    """
    try:
        return_exception = return_exception or (excepts is not None and not any(fun.isCallable(x) for x in fun.toSequence(excepts)))
        tries = fun.toSequence(tries)
        args = fun.toSequence(fun.notNone(args,[]))
        kwargs = fun.notNone(kwargs,{})
        excepts = fun.notNone(excepts,lambda e: log.printf(str(e)))
        result = [t(*args,**kwargs) for t in tries if fun.isCallable(t)]
        return result[0] if len(result)==1 else result
    except Exception,e:
        if fun.isCallable(excepts):
            v = excepts(e)
            return v if return_exception else None
        else:
            if fun.isDictionary(excepts):
                if type(e) in excepts: excepts = excepts.get(type(e))
                elif type(e).__name__ in excepts: excepts = excepts.get(type(e).__name__)
                else:
                    candidates = [t for t in excepts if isinstance(e,t)]
                    if candidates: excepts = excepts[candidates[0]]
                    else: excepts = excepts.get('') or excepts.get(None) or []
            vals = [x(e) for x in fun.toSequence(excepts) if fun.isCallable(x)]
            if return_exception:
               return vals or fun.notNone(excepts,None)
Esempio n. 3
0
def trial(tries,excepts=None,args=None,kwargs=None,return_exception=None):
    """ This method executes a try,except clause in a single line
    :param tries: may be a callable or a list of callables
    :param excepts: it can be a callable, a list of callables, a map of {ExceptionType:[callables]} or just a default value to return	
	:param args,kwargs: arguments to be passed to the callable
	:return exception: whether to return exception or None (default)
    """
    try:
        return_exception = return_exception or (excepts is not None and not any(fun.isCallable(x) for x in fun.toSequence(excepts)))
        tries = fun.toSequence(tries)
        args = fun.toSequence(fun.notNone(args,[]))
        kwargs = fun.notNone(kwargs,{})
        excepts = fun.notNone(excepts,lambda e: log.printf(str(e)))
        result = [t(*args,**kwargs) for t in tries if fun.isCallable(t)]
        return result[0] if len(result)==1 else result
    except Exception,e:
        if fun.isCallable(excepts):
            v = excepts(e)
            return v if return_exception else None
        else:
            if fun.isDictionary(excepts):
                if type(e) in excepts: excepts = excepts.get(type(e))
                elif type(e).__name__ in excepts: excepts = excepts.get(type(e).__name__)
                else:
                    candidates = [t for t in excepts if isinstance(e,t)]
                    if candidates: excepts = excepts[candidates[0]]
                    else: excepts = excepts.get('') or excepts.get(None) or []
            vals = [x(e) for x in fun.toSequence(excepts) if fun.isCallable(x)]
            if return_exception:
               return vals or fun.notNone(excepts,None)