Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/faker/providers/barcode/__init__.py: 31%
33 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
1from typing import Tuple, Union
3from .. import BaseProvider
5localized = True
7PrefixType = Tuple[Union[int, str, Tuple[Union[int, str], ...]], ...]
10class Provider(BaseProvider):
11 """Implement default barcode provider for Faker.
13 Sources:
15 - https://gs1.org/standards/id-keys/company-prefix
16 """
18 local_prefixes: PrefixType = ()
20 def _ean(self, length: int = 13, prefixes: PrefixType = ()) -> str:
21 if length not in (8, 13):
22 raise AssertionError("length can only be 8 or 13")
24 code = [self.random_digit() for _ in range(length - 1)]
26 if prefixes:
27 prefix: str = self.random_element(prefixes) # type: ignore[assignment]
28 code[: len(prefix)] = map(int, prefix)
30 if length == 8:
31 weights = [3, 1, 3, 1, 3, 1, 3]
32 elif length == 13:
33 weights = [1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3]
35 weighted_sum = sum(x * y for x, y in zip(code, weights))
36 check_digit = (10 - weighted_sum % 10) % 10
37 code.append(check_digit)
39 return "".join(str(x) for x in code)
41 def ean(self, length: int = 13, prefixes: PrefixType = ()) -> str:
42 """Generate an EAN barcode of the specified ``length``.
44 The value of ``length`` can only be ``8`` or ``13`` (default) which will
45 create an EAN-8 or an EAN-13 barcode respectively.
47 If a value for ``prefixes`` is specified, the result will begin with one
48 of the sequences in ``prefixes``.
50 :sample: length=13
51 :sample: length=8
52 :sample: prefixes=('00',)
53 :sample: prefixes=('45', '49')
54 """
55 return self._ean(length, prefixes=prefixes)
57 def ean8(self, prefixes: Tuple[()] = ()) -> str:
58 """Generate an EAN-8 barcode.
60 This method uses |ean| under the hood with the ``length`` argument
61 explicitly set to ``8``.
63 If a value for ``prefixes`` is specified, the result will begin with one
64 of the sequences in ``prefixes``.
66 :sample:
67 :sample: prefixes=('00',)
68 :sample: prefixes=('45', '49')
69 """
70 return self._ean(8, prefixes=prefixes)
72 def ean13(self, prefixes: PrefixType = ()) -> str:
73 """Generate an EAN-13 barcode.
75 This method uses |ean| under the hood with the ``length`` argument
76 explicitly set to ``13``.
78 If a value for ``prefixes`` is specified, the result will begin with one
79 of the sequences in ``prefixes``.
81 .. note::
82 Codes starting with a leading zero are treated specially in some
83 barcode readers. For more information on compatibility with UPC-A
84 codes, see |EnUsBarcodeProvider.ean13|.
86 :sample:
87 :sample: prefixes=('00',)
88 :sample: prefixes=('45', '49')
89 """
90 return self._ean(13, prefixes=prefixes)
92 def localized_ean(self, length: int = 13) -> str:
93 """Generate a localized EAN barcode of the specified ``length``.
95 The value of ``length`` can only be ``8`` or ``13`` (default) which will
96 create an EAN-8 or an EAN-13 barcode respectively.
98 This method uses the standard barcode provider's |ean| under the hood
99 with the ``prefixes`` argument explicitly set to ``local_prefixes`` of
100 a localized barcode provider implementation.
102 :sample:
103 :sample: length=13
104 :sample: length=8
105 """
106 return self._ean(length, prefixes=self.local_prefixes)
108 def localized_ean8(self) -> str:
109 """Generate a localized EAN-8 barcode.
111 This method uses |localized_ean| under the hood with the ``length``
112 argument explicitly set to ``8``.
113 """
114 return self.localized_ean(8)
116 def localized_ean13(self) -> str:
117 """Generate a localized EAN-13 barcode.
119 This method uses |localized_ean| under the hood with the ``length``
120 argument explicitly set to ``13``.
121 """
122 return self.localized_ean(13)