当前位置: 首页>>代码示例>>Python>>正文


Python func.sum方法代码示例

本文整理汇总了Python中sqlalchemy.sql.func.sum方法的典型用法代码示例。如果您正苦于以下问题:Python func.sum方法的具体用法?Python func.sum怎么用?Python func.sum使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sqlalchemy.sql.func的用法示例。


在下文中一共展示了func.sum方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: service_get_all_share_sorted

# 需要导入模块: from sqlalchemy.sql import func [as 别名]
# 或者: from sqlalchemy.sql.func import sum [as 别名]
def service_get_all_share_sorted(context):
    session = get_session()
    with session.begin():
        topic = CONF.share_topic
        label = 'share_gigabytes'
        subq = (model_query(context, models.Share,
                            func.sum(models.Share.size).label(label),
                            session=session, read_deleted="no").
                join(models.ShareInstance,
                     models.ShareInstance.share_id == models.Share.id).
                group_by(models.ShareInstance.host).
                subquery())
        return _service_get_all_topic_subquery(context,
                                               session,
                                               topic,
                                               subq,
                                               label) 
开发者ID:openstack,项目名称:manila,代码行数:19,代码来源:api.py

示例2: snapshot_data_get_for_project

# 需要导入模块: from sqlalchemy.sql import func [as 别名]
# 或者: from sqlalchemy.sql.func import sum [as 别名]
def snapshot_data_get_for_project(context, project_id, user_id,
                                  share_type_id=None, session=None):
    query = (model_query(context, models.ShareSnapshot,
                         func.count(models.ShareSnapshot.id),
                         func.sum(models.ShareSnapshot.size),
                         read_deleted="no",
                         session=session).
             filter_by(project_id=project_id))

    if share_type_id:
        query = query.join(
            models.ShareInstance,
            models.ShareInstance.share_id == models.ShareSnapshot.share_id,
        ).filter_by(share_type_id=share_type_id)
    elif user_id:
        query = query.filter_by(user_id=user_id)
    result = query.first()

    return result[0] or 0, result[1] or 0 
开发者ID:openstack,项目名称:manila,代码行数:21,代码来源:api.py

示例3: share_replica_data_get_for_project

# 需要导入模块: from sqlalchemy.sql import func [as 别名]
# 或者: from sqlalchemy.sql.func import sum [as 别名]
def share_replica_data_get_for_project(context, project_id, user_id=None,
                                       session=None, share_type_id=None):
    session = session or get_session()
    query = model_query(
        context, models.ShareInstance,
        func.count(models.ShareInstance.id),
        func.sum(models.Share.size),
        read_deleted="no",
        session=session).join(
        models.Share,
        models.ShareInstance.share_id == models.Share.id).filter(
        models.Share.project_id == project_id).filter(
        models.ShareInstance.replica_state.isnot(None))

    if share_type_id:
        query = query.filter(
            models.ShareInstance.share_type_id == share_type_id)
    elif user_id:
        query = query.filter(models.Share.user_id == user_id)

    result = query.first()
    return result[0] or 0, result[1] or 0 
开发者ID:openstack,项目名称:manila,代码行数:24,代码来源:api.py

示例4: test_recursive_union_alias_two

# 需要导入模块: from sqlalchemy.sql import func [as 别名]
# 或者: from sqlalchemy.sql.func import sum [as 别名]
def test_recursive_union_alias_two(self):
        """

        """

        # I know, this is the PG VALUES keyword,
        # we're cheating here.  also yes we need the SELECT,
        # sorry PG.
        t = select([func.values(1).label("n")]).cte("t", recursive=True)
        t = t.union_all(select([t.c.n + 1]).where(t.c.n < 100)).alias("ta")
        s = select([func.sum(t.c.n)])
        self.assert_compile(
            s,
            "WITH RECURSIVE t(n) AS "
            "(SELECT values(:values_1) AS n "
            "UNION ALL SELECT t.n + :n_1 AS anon_1 "
            "FROM t "
            "WHERE t.n < :n_2) "
            "SELECT sum(ta.n) AS sum_1 FROM t AS ta",
        ) 
开发者ID:sqlalchemy,项目名称:sqlalchemy,代码行数:22,代码来源:test_cte.py

示例5: count_usage

# 需要导入模块: from sqlalchemy.sql import func [as 别名]
# 或者: from sqlalchemy.sql.func import sum [as 别名]
def count_usage(self, context, container_type, project_id, flag):
        session = get_session()
        if flag == 'containers':
            project_query = session.query(
                func.count(models.Container.id)). \
                filter_by(project_id=project_id). \
                filter_by(container_type=container_type)
        elif flag in ['disk', 'cpu', 'memory']:
            project_query = session.query(
                func.sum(getattr(models.Container, flag))). \
                filter_by(project_id=project_id). \
                filter_by(container_type=container_type)

        return project_query.first() 
开发者ID:openstack,项目名称:zun,代码行数:16,代码来源:api.py

示例6: quantity

# 需要导入模块: from sqlalchemy.sql import func [as 别名]
# 或者: from sqlalchemy.sql.func import sum [as 别名]
def quantity(self):
        """Returns quantity of items that have not been 'claimed' by a request"""
        requests = RequestItem.query \
                    .filter_by(entry_id=self.id) \
                    .join(hardwarecheckout.models.request.Request) \
                    .filter_by(status=hardwarecheckout.models.request.RequestStatus.APPROVED) \
                    .with_entities(func.sum(RequestItem.quantity)).scalar()
        if not requests: requests = 0
        return Item.query.filter_by(entry_id = self.id, user = None).count() - requests 
开发者ID:techx,项目名称:cog,代码行数:11,代码来源:inventory_entry.py

示例7: re_evaluate_did

# 需要导入模块: from sqlalchemy.sql import func [as 别名]
# 或者: from sqlalchemy.sql.func import sum [as 别名]
def re_evaluate_did(scope, name, rule_evaluation_action, session=None):
    """
    Re-Evaluates a did.

    :param scope:                   The scope of the did to be re-evaluated.
    :param name:                    The name of the did to be re-evaluated.
    :param rule_evaluation_action:  The Rule evaluation action.
    :param session:                 The database session in use.
    :raises:                        DataIdentifierNotFound
    """

    try:
        did = session.query(models.DataIdentifier).filter(models.DataIdentifier.scope == scope,
                                                          models.DataIdentifier.name == name).one()
    except NoResultFound:
        raise DataIdentifierNotFound()

    if rule_evaluation_action == DIDReEvaluation.ATTACH:
        __evaluate_did_attach(did, session=session)
    else:
        __evaluate_did_detach(did, session=session)

    # Update size and length of did
    if session.bind.dialect.name == 'oracle':
        stmt = session.query(func.sum(models.DataIdentifierAssociation.bytes),
                             func.count(1)).\
            with_hint(models.DataIdentifierAssociation,
                      "index(CONTENTS CONTENTS_PK)", 'oracle').\
            filter(models.DataIdentifierAssociation.scope == scope,
                   models.DataIdentifierAssociation.name == name)
        for bytes, length in stmt:
            did.bytes = bytes
            did.length = length

    # Add an updated_col_rep
    if did.did_type == DIDType.DATASET:
        models.UpdatedCollectionReplica(scope=scope,
                                        name=name,
                                        did_type=did.did_type).save(session=session) 
开发者ID:rucio,项目名称:rucio,代码行数:41,代码来源:rule.py

示例8: get_global_account_usage

# 需要导入模块: from sqlalchemy.sql import func [as 别名]
# 或者: from sqlalchemy.sql.func import sum [as 别名]
def get_global_account_usage(account, rse_expression=None, session=None):
    """
    Read the account usage and connect it with the global account limits of the account.

    :param account:          The account to read.
    :param rse_expression:   The RSE expression (If none, get all).
    :param session:          Database session in use.

    :returns:                List of dicts {'rse_id', 'bytes_used', 'files_used', 'bytes_limit'}
    """
    result_list = []
    if not rse_expression:
        # All RSE Expressions
        limits = get_global_account_limits(account=account, session=session)
        all_rse_usages = {usage['rse_id']: (usage['bytes'], usage['files']) for usage in get_all_rse_usages_per_account(account=account, session=session)}
        for rse_expression, limit in limits.items():
            usage = 0
            files = 0
            for rse in limit['resolved_rse_ids']:
                usage += all_rse_usages.get(rse, [0])[0]
                files += all_rse_usages.get(rse, [0, 0])[1]
            result_list.append({'rse_expression': rse_expression,
                                'bytes': usage, 'files': files,
                                'bytes_limit': limit['limit'],
                                'bytes_remaining': limit['limit'] - usage})
    else:
        # One RSE Expression
        limit = get_global_account_limit(account=account, rse_expression=rse_expression, session=session)
        resolved_rses = [resolved_rse['id'] for resolved_rse in parse_expression(rse_expression, session=session)]
        usage = session.query(func.sum(models.AccountUsage.bytes), func.sum(models.AccountUsage.files))\
                       .filter(models.AccountUsage.account == account, models.AccountUsage.rse_id.in_(resolved_rses))\
                       .group_by(models.AccountUsage.account).first()
        result_list.append({'rse_expression': rse_expression,
                            'bytes': usage[0], 'files': usage[1],
                            'bytes_limit': limit,
                            'bytes_remaining': limit - usage[0]})
    return result_list 
开发者ID:rucio,项目名称:rucio,代码行数:39,代码来源:account_limit.py

示例9: total_sent

# 需要导入模块: from sqlalchemy.sql import func [as 别名]
# 或者: from sqlalchemy.sql.func import sum [as 别名]
def total_sent(self):
        return int(
            db.session.query(func.sum(server.models.credit_transfer.CreditTransfer.transfer_amount).label('total')).execution_options(show_all=True)
            .filter(server.models.credit_transfer.CreditTransfer.transfer_status == TransferStatusEnum.COMPLETE)
            .filter(server.models.credit_transfer.CreditTransfer.sender_transfer_account_id == self.id).first().total or 0
        ) 
开发者ID:teamsempo,项目名称:SempoBlockchain,代码行数:8,代码来源:transfer_account.py

示例10: total_received

# 需要导入模块: from sqlalchemy.sql import func [as 别名]
# 或者: from sqlalchemy.sql.func import sum [as 别名]
def total_received(self):
        return int(
            db.session.query(func.sum(server.models.credit_transfer.CreditTransfer.transfer_amount).label('total')).execution_options(show_all=True)
            .filter(server.models.credit_transfer.CreditTransfer.transfer_status == TransferStatusEnum.COMPLETE)
            .filter(server.models.credit_transfer.CreditTransfer.recipient_transfer_account_id == self.id).first().total or 0
        ) 
开发者ID:teamsempo,项目名称:SempoBlockchain,代码行数:8,代码来源:transfer_account.py

示例11: _add_clusters_filters

# 需要导入模块: from sqlalchemy.sql import func [as 别名]
# 或者: from sqlalchemy.sql.func import sum [as 别名]
def _add_clusters_filters(self, query, filters):
        if filters is None:
            filters = {}

        possible_filters = ["cluster_template_id", "name", "stack_id",
                            "api_address", "node_addresses", "project_id",
                            "user_id"]

        filter_names = set(filters).intersection(possible_filters)
        filter_dict = {filter_name: filters[filter_name]
                       for filter_name in filter_names}

        query = query.filter_by(**filter_dict)

        if 'status' in filters:
            query = query.filter(models.Cluster.status.in_(filters['status']))

        # Helper to filter based on node_count field from nodegroups
        def filter_node_count(query, node_count, is_master=False):
            nfunc = func.sum(models.NodeGroup.node_count)
            nquery = model_query(models.NodeGroup)
            if is_master:
                nquery = nquery.filter(models.NodeGroup.role == 'master')
            else:
                nquery = nquery.filter(models.NodeGroup.role != 'master')
            nquery = nquery.group_by(models.NodeGroup.cluster_id)
            nquery = nquery.having(nfunc == node_count)
            uuids = [ng.cluster_id for ng in nquery.all()]
            return query.filter(models.Cluster.uuid.in_(uuids))

        if 'node_count' in filters:
            query = filter_node_count(
                query, filters['node_count'], is_master=False)
        if 'master_count' in filters:
            query = filter_node_count(
                query, filters['master_count'], is_master=True)

        return query 
开发者ID:openstack,项目名称:magnum,代码行数:40,代码来源:api.py

示例12: get_cluster_stats

# 需要导入模块: from sqlalchemy.sql import func [as 别名]
# 或者: from sqlalchemy.sql.func import sum [as 别名]
def get_cluster_stats(self, context, project_id=None):
        query = model_query(models.Cluster)
        node_count_col = models.NodeGroup.node_count
        ncfunc = func.sum(node_count_col)

        if project_id:
            query = query.filter_by(project_id=project_id)
            nquery = query.session.query(ncfunc.label("nodes")).filter_by(
                project_id=project_id)
        else:
            nquery = query.session.query(ncfunc.label("nodes"))

        clusters = query.count()
        nodes = int(nquery.one()[0]) if nquery.one()[0] else 0
        return clusters, nodes 
开发者ID:openstack,项目名称:magnum,代码行数:17,代码来源:api.py

示例13: share_data_get_for_project

# 需要导入模块: from sqlalchemy.sql import func [as 别名]
# 或者: from sqlalchemy.sql.func import sum [as 别名]
def share_data_get_for_project(context, project_id, user_id,
                               share_type_id=None, session=None):
    query = (model_query(context, models.Share,
                         func.count(models.Share.id),
                         func.sum(models.Share.size),
                         read_deleted="no",
                         session=session).
             filter_by(project_id=project_id))
    if share_type_id:
        query = query.join("instances").filter_by(share_type_id=share_type_id)
    elif user_id:
        query = query.filter_by(user_id=user_id)
    result = query.first()
    return (result[0] or 0, result[1] or 0) 
开发者ID:openstack,项目名称:manila,代码行数:16,代码来源:api.py

示例14: child_victimisation_worksheet

# 需要导入模块: from sqlalchemy.sql import func [as 别名]
# 或者: from sqlalchemy.sql.func import sum [as 别名]
def child_victimisation_worksheet(self, wb):
        from dexter.models.views import DocumentChildrenView

        ws = wb.add_worksheet('child_secondary_victimisation')

        rows = self.filter(
            db.session.query(
                func.sum(DocumentChildrenView.c.secondary_victim_source == 'secondary-victim-source', type_=Integer).label('secondary_victim_source'),
                func.sum(DocumentChildrenView.c.secondary_victim_identified == 'secondary-victim-identified', type_=Integer).label('secondary_victim_identified'),
                func.sum(DocumentChildrenView.c.secondary_victim_victim_of_abuse == 'secondary-victim-abused', type_=Integer).label('secondary_victim_victim_of_abuse'),
                func.sum(DocumentChildrenView.c.secondary_victim_source_identified_abused == 'secondary-victim-source-identified-abused', type_=Integer).label('secondary_victim_source_identified_abused'),
            )
            .join(Document)).all()
        if not rows:
            return

        d = rows[0]._asdict()
        data = [[k, d[k]] for k in sorted(d.keys(), key=len)]
        ws.add_table(0, 0, len(data), 1, {
            'name': 'ChildSecondaryVictimisation',
            'data': data,
            'columns': [
                {'header': ''},
                {'header': 'count'},
            ]
        }) 
开发者ID:Code4SA,项目名称:mma-dexter,代码行数:28,代码来源:xlsx_export.py

示例15: child_context_worksheet

# 需要导入模块: from sqlalchemy.sql import func [as 别名]
# 或者: from sqlalchemy.sql.func import sum [as 别名]
def child_context_worksheet(self, wb):
        from dexter.models.views import DocumentChildrenView

        rows = self.filter(
            db.session.query(
                func.sum(DocumentChildrenView.c.basic_context == 'basic-context', type_=Integer).label('basic_context'),
                func.sum(DocumentChildrenView.c.causes_mentioned == 'causes-mentioned', type_=Integer).label('causes_mentioned'),
                func.sum(DocumentChildrenView.c.consequences_mentioned == 'consequences-mentioned', type_=Integer).label('consequences_mentioned'),
                func.sum(DocumentChildrenView.c.solutions_offered == 'solutions-offered', type_=Integer).label('solutions_offered'),
                func.sum(DocumentChildrenView.c.relevant_policies == 'relevant-policies', type_=Integer).label('relevant_policies'),
                func.sum(DocumentChildrenView.c.self_help_offered == 'self-help-offered', type_=Integer).label('self_help_offered'),
            )
            .join(Document)).all()
        if not rows:
            return

        ws = wb.add_worksheet('child_context')

        d = rows[0]._asdict()
        data = [[k, d[k]] for k in d.keys()]
        ws.add_table(0, 0, len(data), 1, {
            'name': 'ChildContext',
            'data': data,
            'columns': [
                {'header': ''},
                {'header': 'count'},
            ]
        }) 
开发者ID:Code4SA,项目名称:mma-dexter,代码行数:30,代码来源:xlsx_export.py


注:本文中的sqlalchemy.sql.func.sum方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。