init_categories.py 文件源码

python
阅读 18 收藏 0 点赞 0 评论 0

项目:OilLibrary 作者: NOAA-ORR-ERD 项目源码 文件源码
def get_categories_by_names(session, top_name, child_names):
    '''
        Get the top level category by name, and a list of child categories
        directly underneath it by their names.

        This is a utility function that serves some common functionality in
        our various categorization functions.  Probably not useful outside
        of this module.
    '''
    try:
        top_category = (session.query(Category)
                        .filter(Category.parent == None)
                        .filter(Category.name == top_name)
                        .one())
    except MultipleResultsFound as ex:
        ex.message = ('Multiple top categories named "{}" found.'
                      .format(top_name))
        ex.args = (ex.message, )

        raise ex
    except NoResultFound:
        ex.message = ('Top category "{}" not found.'.format(top_name))
        ex.args = (ex.message, )

        raise ex

    child_categories = [c for c in top_category.children
                        if c.name in child_names]

    return top_category, child_categories
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号