Skip to content

Commit a5504e3

Browse files
docs: add a new Utilities section (barcode-related). Explain how to use the API for other flavors (#470)
1 parent e5474de commit a5504e3

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

docs/usage.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,24 @@ print(results)
100100
# 'vegetarian': 'yes'}]
101101
```
102102

103+
### Working with the other projects
104+
105+
The SDK also supports the sibling **Open \[[Beauty](https://world.openbeautyfacts.org/)|[Pet Food](https://world.openpetfoodfacts.org/)|[Products](https://world.openproductsfacts.org/)\] Facts** projects, not just food:
106+
107+
```python
108+
from openfoodfacts import API, Flavor
109+
110+
# Open Beauty Facts
111+
beauty_api = API(user_agent="<app>", flavor=Flavor.obf)
112+
product = beauty_api.product.get("3600523577941")
113+
114+
# Open Pet Food Facts
115+
petfood_api = API(user_agent="<app>", flavor=Flavor.opff)
116+
117+
# Open Products Facts
118+
products_api = API(user_agent="<app>", flavor=Flavor.opf)
119+
```
120+
103121
## Using the dataset
104122

105123
If you're planning to perform data analysis on Open Food Facts, the easiest way is to download and use the Open Food Facts dataset dump. Fortunately it can be done really easily using the SDK:
@@ -131,3 +149,21 @@ for product in dataset:
131149
## Taxonomies
132150

133151
For a deep dive on how to handle taxonomies, check out the [dedicated page](./handle_taxonomies.md).
152+
153+
## Utilities
154+
155+
### Barcode
156+
157+
The SDK includes utilities for normalizing and validating barcodes (GTINs):
158+
159+
```python
160+
from openfoodfacts.barcode import normalize_barcode, has_valid_check_digit
161+
162+
# Normalize a barcode (pad to 8 or 13 digits)
163+
normalize_barcode("3017620422003") # '3017620422003'
164+
normalize_barcode("4003") # '00004003' (padded to 8)
165+
166+
# Validate a barcode check digit
167+
has_valid_check_digit("3017620422003") # True
168+
has_valid_check_digit("3017620422004") # False
169+
```

0 commit comments

Comments
 (0)