Here are the examples of the python api sqlalchemy.sql.func taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
6 Examples
3
Example 1
def resolve(self, variable):
"""Resolve `variable` – return either a column, variable from a
dictionary or a SQL constant (in that order)."""
if variable in self._columns:
return self._columns[variable]
elif variable in self.parameters:
result = self.parameters[variable]
elif variable in SQL_VARIABLES:
result = getattr(sql.func, variable)()
else:
label = " in {}".format(self.label) if self.label else ""
raise ExpressionError("Unknown attribute, variable or parameter "
"'{}'{}" .format(variable, label))
return result
3
Example 2
def function(self, name):
"""Return a SQL function"""
if name not in SQL_ALL_FUNCTIONS:
raise ExpressionError("Unknown function '{}'"
.format(name))
return getattr(sql.func, name)
3
Example 3
Project: zaqar Source File: utils.py
def raises_conn_error(func):
"""Handles sqlalchemy DisconnectionError
When sqlalchemy detects a disconnect from the database server, it
retries a number of times. After failing that number of times, it
will convert the internal DisconnectionError into an
InvalidRequestError. This decorator handles that error.
"""
@functools.wraps(func)
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except exc.InvalidRequestError as ex:
LOG.exception(ex)
raise errors.ConnectionError()
return wrapper
0
Example 4
@dispatch(UnaryStringFunction, ColumnElement)
def compute_up(expr, data, **kwargs):
func_name = type(expr).__name__
return getattr(sa.sql.func, func_name)(data).label(expr._name)
0
Example 5
def ratelimited(max_per_second=1):
"""
Decorate a function, only allowing it to be called every so often.
"""
# we could make this available to other modules if it would be useful.
min_interval = 1.0 / float(max_per_second)
warn_every = 10 # seconds
def decorate(func):
last_called = [0.0]
last_warned = [0.0]
num_skips = [0]
def rate_limited_function(*args, **kargs):
curtime = time.clock()
elapsed = curtime - last_called[0]
left_to_wait = min_interval - elapsed
if left_to_wait <= 0:
# return the actual function so it can be called
ret = func(*args, **kargs)
last_called[0] = curtime
# we want to note in the logs that some calls were skipped,
# while not printing the log too often -
# that would slow down the system and defeat the purpose of rate limiting.
if ((curtime - last_warned[0] >= warn_every) and (num_skips[0] > 0)):
logging.warning("Skipped %s calls to '%s()' in %s seconds." % (num_skips[0], func.__name__, warn_every))
last_warned[0] = curtime
num_skips[0] = 0
else:
# ignore the function call and continue
ret = lambda: True
num_skips[0] += 1
return ret
return rate_limited_function
return decorate
0
Example 6
def __call__(self, func):
def wrapped(*args, **kwargs):
with self:
return func(self.session, *args, **kwargs)
return wrapped