Ejemplo n.º 1
0
    def list_food_groups(self, max, offset=0, sort='n'):
        """
        Get a list of available food groups in the database.

        :param int max: Maximum number of groups to return, up to 1500.
        :param int offset: Index to start listing at.
        :param str sort: The sorting method.
           ``'r'`` for relevance, ``'n'`` for group ID.
        :returns: Generator yielding food groups as
           :class:`usda.domain.ListItem` instances.
        :rtype: usda.pagination.ModelPaginator
        """
        return ModelPaginator(
            ListItem,
            self.list_food_groups_raw(max=max, offset=offset, sort=sort),
        )
Ejemplo n.º 2
0
    def list_foods(self, max, offset=0, sort='n'):
        """
        Get a list of available food items in the database.
        Useful to generate Food Reports.

        :param int max: Maximum number of items to return, up to 1500.
        :param int offset: Index to start listing at.
        :param str sort: The sorting method.
           ``'r'`` for relevance, ``'n'`` for item ID.
        :returns: Generator yielding food items as
           :class:`usda.domain.Food` instances.
        :rtype: usda.pagination.ModelPaginator
        """
        return ModelPaginator(
            Food,
            self.list_foods_raw(max=max, offset=offset, sort=sort),
        )
Ejemplo n.º 3
0
    def search_foods(self, query, max, offset=0, sort='r'):
        """
        Get a list of food items matching a specified query.

        :param str query: A search query.
        :param int max: Maximum number of items to return, up to 1500.
        :param int offset: Index to start listing at.
        :param str sort: The sorting method.
           ``'r'`` for relevance, ``'n'`` for item ID.
        :returns: Generator yielding food items as
           :class:`usda.domain.Food` instances.
        :rtype: usda.pagination.ModelPaginator
        """
        return ModelPaginator(
            Food,
            self.search_foods_raw(q=query, max=max, offset=offset, sort=sort),
        )
Ejemplo n.º 4
0
    def get_nutrient_report(self, *nutrients):
        r"""
        Get a Nutrient Report for each of the given nutrient IDs.

        :param int \*nutrients: Up to 20 nutrient IDs.
        :returns: A generator yielding food items with the specified nutrients
           as :class:`usda.domain.NutrientReportFood` instances.
        :rtype: usda.pagination.ModelPaginator
        :raises ValueError: If there are more than 20 nutrient IDs.
        :raises requests.exceptions.HTTPError: If a response has a HTTP 4xx or
           5xx status code.
        :raises DataGovApiError: If a Data.gov API returns an error.
        """
        if len(nutrients) > 20:
            raise ValueError("A nutrient report request cannot contain "
                             "more than 20 nutrients")
        return ModelPaginator(
            NutrientReportFood,
            self.get_nutrient_report_raw(nutrients=nutrients),
        )
Ejemplo n.º 5
0
    def search_foods(self, query, max, offset=0, sort='r', ds=None):
        """
        Get a list of food items matching a specified query.

        :param str query: A search query.
        :param int max: Maximum number of items to return, up to 1500.
        :param int offset: Index to start listing at.
        :param str sort: The sorting method.
           ``'r'`` for relevance, ``'n'`` for item ID.
        :param ds: Optionally filter by data source: Standard Reference or
           Branded Food Products.
        :type ds: usda.enums.UsdaNdbDataSource
        :returns: Generator yielding food items as
           :class:`usda.domain.Food` instances.
        :rtype: usda.pagination.ModelPaginator
        """
        params = {'q': query, 'max': max, 'offset': offset, 'sort': sort}
        if ds:
            if isinstance(ds, UsdaNdbDataSource):
                params['ds'] = ds.value
            else:
                params['ds'] = ds
        return ModelPaginator(Food, self.search_foods_raw(**params))