def with_session(*args, **kwargs):
 def decorator(func):
  def wrapper(*args, **kwargs):
   if kwargs.get(u'session'):
    return func(*args, **kwargs)
   with _Session() as session:
    kwargs[u'session'] = session
    return func(*args, **kwargs)
  return wrapper
 if ((len(args) == 1) and (not kwargs) and callable(args[0])):
  _Session = functools.partial(Session, expire_on_commit=False)
  return decorator(args[0])
 else:
  _Session = functools.partial(Session, *args, **kwargs)
  return decorator

def with_session ( * args , ** kwargs ) : def decorator ( func ) : def wrapper ( * args , ** kwargs ) : if kwargs . get ( u'session' ) : return func ( * args , ** kwargs ) with _Session ( ) as session : kwargs [ u'session' ] = session return func ( * args , ** kwargs ) return wrapper if ( ( len ( args ) == 1 ) and ( not kwargs ) and callable ( args [ 0 ] ) ) : _Session = functools . partial ( Session , expire_on_commit = False ) return decorator ( args [ 0 ] ) else : _Session = functools . partial ( Session , * args , ** kwargs ) return decorator
a decorator which creates a new session if one was not passed via keyword argument to the function .