biip.gtin
Support for Global Trade Item Number (GTIN).
The biip.gtin module contains Biip's support for parsing GTIN formats.
A GTIN is a number that uniquely identifies a trade item.
This class can interpet the following GTIN formats:
- GTIN-8, found in EAN-8 barcodes.
- GTIN-12, found in UPC-A and UPC-E barcodes.
- GTIN-13, found in EAN-13 barcodes.
- GTIN-14, found in ITF-14 barcodes, as well as a data field in GS1 barcodes.
If you only want to parse GTINs, you can import the GTIN parser directly
instead of using biip.parse().
>>> from biip.gtin import Gtin
If parsing succeeds, it returns a Gtin object.
>>> gtin = Gtin.parse("7032069804988")
>>> pprint(gtin)
Gtin(
value='7032069804988',
format=GtinFormat.GTIN_13,
prefix=GS1Prefix(
value='703',
usage='GS1 Norway'
),
company_prefix=GS1CompanyPrefix(
value='703206'
),
item_reference='980498',
payload='703206980498',
check_digit=8
)
A GTIN can be converted to any other GTIN format, as long as the target format is longer.
>>> gtin.as_gtin_14()
'07032069804988'
As all GTINs can be converted to GTIN-14 using
as_gtin_14(), it is the recommended format to use
when storing or comparing GTINs. For example, when looking up a product
associated with a GTIN, the GTIN should first be expanded to a GTIN-14 before
using it to query the product catalog.
Gtin
dataclass
Data class containing a GTIN.
Source code in src/biip/gtin.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 | |
check_digit
instance-attribute
Check digit used to check if the GTIN as a whole is valid.
company_prefix
instance-attribute
The GS1 Company Prefix.
Identifying the company that issued the GTIN.
format
instance-attribute
GTIN format, either GTIN-8, GTIN-12, GTIN-13, or GTIN-14.
Classification is done after stripping leading zeros.
item_reference
instance-attribute
The item reference part of the GTIN.
For GTIN-12/13/14, this is the part of the payload that is assigned by the company that owns the company prefix. It is only set if the company prefix is known.
For GTIN-8, this is the part of the payload after the GS1-8 Prefix.
For company RCNs, this is the part of the payload after the GS1 Prefix.
For geographical RCNs, this is a part of the payload, depending on the geographical region's RCN parsing rules.
packaging_level
class-attribute
instance-attribute
Packaging level is the first digit in GTIN-14 codes.
This digit is used for wholesale shipments, e.g. the GTIN-14 product identifier in GS1-128 barcodes, but not in the GTIN-13 barcodes used for retail products.
payload
instance-attribute
The actual payload.
Including packaging level if any, company prefix, and item reference. Excludes the check digit.
prefix
instance-attribute
The GS1 Prefix.
When parsing GTIN-8, this will be a GS1-8 Prefix instead.
Indicating what GS1 country organization that assigned code range.
as_gtin_12
as_gtin_13
as_gtin_14
as_gtin_8
parse
classmethod
Parse the given value into a Gtin object.
Both GTIN-8, GTIN-12, GTIN-13, and GTIN-14 are supported.
The checksum is guaranteed to be valid if a GTIN object is returned.
Parameters:
-
value(str) –The value to parse.
-
config(ParseConfig | None, default:None) –Configuration options for parsing.
Returns:
-
Gtin–GTIN data structure with the successfully extracted data.
Raises:
-
ParseError–If the parsing fails.
Source code in src/biip/gtin.py
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 | |
without_variable_measure
Create a new GTIN where the variable measure is zeroed out.
This method is a no-op for proper GTINs. For RCNs, see the method on the
Rcn subclass.
Returns:
-
Gtin–A GTIN instance with zeros in the variable measure places.
Raises:
-
EncodeError–If the rules for variable measures in the region are unknown.
Source code in src/biip/gtin.py
GtinFormat
Bases: IntEnum
Enum of GTIN formats.