This repository was archived by the owner on Apr 23, 2025. It is now read-only.
forked from WhyNotHugo/python-barcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupc.py
More file actions
executable file
·114 lines (86 loc) · 2.85 KB
/
upc.py
File metadata and controls
executable file
·114 lines (86 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from barcode.base import Barcode
from barcode.charsets import upc as _upc
from barcode.errors import *
try:
reduce
except NameError:
from functools import reduce
"""Module: barcode.upc
:Provided barcodes: UPC-A
"""
__docformat__ = 'restructuredtext en'
class UniversalProductCodeA(Barcode):
"""Initializes new UPC-A barcode.
:parameters:
upc : String
The upc number as string.
writer : barcode.writer Instance
The writer to render the barcode (default: SVGWriter).
make_ean: boolean
"""
name = 'UPC-A'
digits = 11
def __init__(self, upc, writer=None, make_ean=False):
self.ean = make_ean
upc = upc[:self.digits]
if not upc.isdigit():
raise IllegalCharacterError('UPC code can only contain numbers.')
if len(upc) != self.digits:
raise NumberOfDigitsError('UPC must have {0} digits, not '
'{1}.'.format(self.digits, len(upc)))
self.upc = upc
self.upc = '{}{}'.format(upc, self.calculate_checksum())
self.writer = writer or Barcode.default_writer()
def __unicode__(self):
if self.ean:
return '0' + self.upc
else:
return self.upc
__str__ = __unicode__
def get_fullcode(self):
if self.ean:
return '0' + self.upc
else:
return self.upc
def calculate_checksum(self):
"""Calculates the checksum for UPCA/UPC codes
:return: The checksum for 'self.upc'
:rtype: Integer
"""
def sum_(x, y): return int(x) + int(y)
upc = self.upc[0:self.digits]
oddsum = reduce(sum_, upc[::2])
evensum = reduce(sum_, upc[1::2])
check = (evensum + oddsum * 3) % 10
if check == 0:
return 0
else:
return 10 - check
def build(self):
"""Builds the barcode pattern from 'self.upc'
:return: The pattern as string
:rtype: String
"""
code = _upc.EDGE[:]
for i, number in enumerate(self.upc[0:6]):
code += _upc.CODES['L'][int(number)]
code += _upc.MIDDLE
for number in self.upc[6:]:
code += _upc.CODES['R'][int(number)]
code += _upc.EDGE
return [code]
def to_ascii(self):
"""Returns an ascii representation of the barcode.
:rtype: String
"""
code = self.build()
for i, line in enumerate(code):
code[i] = line.replace('1', '|').replace('0', '_')
return '\n'.join(code)
def render(self, writer_options=None, text=None):
options = dict(module_width=0.33)
options.update(writer_options or {})
return Barcode.render(self, options, text)
UPCA = UniversalProductCodeA