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

1from typing import Tuple, Union 

2 

3from .. import BaseProvider 

4 

5localized = True 

6 

7PrefixType = Tuple[Union[int, str, Tuple[Union[int, str], ...]], ...] 

8 

9 

10class Provider(BaseProvider): 

11 """Implement default barcode provider for Faker. 

12 

13 Sources: 

14 

15 - https://gs1.org/standards/id-keys/company-prefix 

16 """ 

17 

18 local_prefixes: PrefixType = () 

19 

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") 

23 

24 code = [self.random_digit() for _ in range(length - 1)] 

25 

26 if prefixes: 

27 prefix: str = self.random_element(prefixes) # type: ignore[assignment] 

28 code[: len(prefix)] = map(int, prefix) 

29 

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] 

34 

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) 

38 

39 return "".join(str(x) for x in code) 

40 

41 def ean(self, length: int = 13, prefixes: PrefixType = ()) -> str: 

42 """Generate an EAN barcode of the specified ``length``. 

43 

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. 

46 

47 If a value for ``prefixes`` is specified, the result will begin with one 

48 of the sequences in ``prefixes``. 

49 

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) 

56 

57 def ean8(self, prefixes: Tuple[()] = ()) -> str: 

58 """Generate an EAN-8 barcode. 

59 

60 This method uses |ean| under the hood with the ``length`` argument 

61 explicitly set to ``8``. 

62 

63 If a value for ``prefixes`` is specified, the result will begin with one 

64 of the sequences in ``prefixes``. 

65 

66 :sample: 

67 :sample: prefixes=('00',) 

68 :sample: prefixes=('45', '49') 

69 """ 

70 return self._ean(8, prefixes=prefixes) 

71 

72 def ean13(self, prefixes: PrefixType = ()) -> str: 

73 """Generate an EAN-13 barcode. 

74 

75 This method uses |ean| under the hood with the ``length`` argument 

76 explicitly set to ``13``. 

77 

78 If a value for ``prefixes`` is specified, the result will begin with one 

79 of the sequences in ``prefixes``. 

80 

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|. 

85 

86 :sample: 

87 :sample: prefixes=('00',) 

88 :sample: prefixes=('45', '49') 

89 """ 

90 return self._ean(13, prefixes=prefixes) 

91 

92 def localized_ean(self, length: int = 13) -> str: 

93 """Generate a localized EAN barcode of the specified ``length``. 

94 

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. 

97 

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. 

101 

102 :sample: 

103 :sample: length=13 

104 :sample: length=8 

105 """ 

106 return self._ean(length, prefixes=self.local_prefixes) 

107 

108 def localized_ean8(self) -> str: 

109 """Generate a localized EAN-8 barcode. 

110 

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) 

115 

116 def localized_ean13(self) -> str: 

117 """Generate a localized EAN-13 barcode. 

118 

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)