本文整理汇总了Python中sqlalchemy.func.extract方法的典型用法代码示例。如果您正苦于以下问题:Python func.extract方法的具体用法?Python func.extract怎么用?Python func.extract使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy.func
的用法示例。
在下文中一共展示了func.extract方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_extract_bind
# 需要导入模块: from sqlalchemy import func [as 别名]
# 或者: from sqlalchemy.func import extract [as 别名]
def test_extract_bind(self, connection):
"""Basic common denominator execution tests for extract()"""
date = datetime.date(2010, 5, 1)
def execute(field):
return connection.execute(select([extract(field, date)])).scalar()
assert execute("year") == 2010
assert execute("month") == 5
assert execute("day") == 1
date = datetime.datetime(2010, 5, 1, 12, 11, 10)
assert execute("year") == 2010
assert execute("month") == 5
assert execute("day") == 1
示例2: test_extract_expression
# 需要导入模块: from sqlalchemy import func [as 别名]
# 或者: from sqlalchemy.func import extract [as 别名]
def test_extract_expression(self, connection):
meta = self.metadata
table = Table("test", meta, Column("dt", DateTime), Column("d", Date))
meta.create_all(connection)
connection.execute(
table.insert(),
{
"dt": datetime.datetime(2010, 5, 1, 12, 11, 10),
"d": datetime.date(2010, 5, 1),
},
)
rs = connection.execute(
select([extract("year", table.c.dt), extract("month", table.c.d)])
)
row = rs.first()
assert row[0] == 2010
assert row[1] == 5
rs.close()
示例3: __init__
# 需要导入模块: from sqlalchemy import func [as 别名]
# 或者: from sqlalchemy.func import extract [as 别名]
def __init__(self, field, expr, **kwargs):
"""Return a :class:`.Extract` construct.
This is typically available as :func:`.extract`
as well as ``func.extract`` from the
:data:`.func` namespace.
"""
self.type = type_api.INTEGERTYPE
self.field = field
self.expr = _literal_as_binds(expr, None)
示例4: __init__
# 需要导入模块: from sqlalchemy import func [as 别名]
# 或者: from sqlalchemy.func import extract [as 别名]
def __init__(self, field, expr, **kwargs):
"""Return a :class:`.Extract` construct.
This is typically available as :func:`.extract`
as well as ``func.extract`` from the
:data:`.func` namespace.
"""
self.type = type_api.INTEGERTYPE
self.field = field
self.expr = coercions.expect(roles.ExpressionElementRole, expr)
示例5: test_non_functions
# 需要导入模块: from sqlalchemy import func [as 别名]
# 或者: from sqlalchemy.func import extract [as 别名]
def test_non_functions(self):
expr = func.cast("foo", Integer)
self.assert_compile(expr, "CAST(:param_1 AS INTEGER)")
expr = func.extract("year", datetime.date(2010, 12, 5))
self.assert_compile(expr, "EXTRACT(year FROM :param_1)")
示例6: get_user_activity
# 需要导入模块: from sqlalchemy import func [as 别名]
# 或者: from sqlalchemy.func import extract [as 别名]
def get_user_activity(session):
"""Create a plot showing the user statistics."""
# Create a subquery to ensure that the user fired a inline query
# Group the new users by date
creation_date = cast(User.created_at, Date).label("creation_date")
all_users_subquery = (
session.query(creation_date, func.count(User.id).label("count"))
.filter(User.inline_queries.any())
.group_by(creation_date)
.subquery()
)
# Create a running window which sums all users up to this point for the current millennium ;P
all_users = (
session.query(
all_users_subquery.c.creation_date,
cast(
func.sum(all_users_subquery.c.count).over(
partition_by=func.extract(
"millennium", all_users_subquery.c.creation_date
),
order_by=all_users_subquery.c.creation_date.asc(),
),
Integer,
).label("running_total"),
)
.order_by(all_users_subquery.c.creation_date)
.all()
)
all_users = [("all", q[0], q[1]) for q in all_users]
# Combine the results in a single dataframe and name the columns
user_statistics = all_users
dataframe = pandas.DataFrame(user_statistics, columns=["type", "date", "users"])
# Plot each result set
fig, ax = plt.subplots(figsize=(30, 15), dpi=120)
for key, group in dataframe.groupby(["type"]):
ax = group.plot(ax=ax, kind="line", x="date", y="users", label=key)
image = image_from_figure(fig)
image.name = "user_statistics.png"
return image