Skip to content

Commit 434f37d

Browse files
loran425loran425
authored andcommitted
Add upc charset
remove upc dependence on ean
1 parent 7b33d96 commit 434f37d

2 files changed

Lines changed: 83 additions & 12 deletions

File tree

barcode/charsets/upc.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from __future__ import unicode_literals
4+
5+
6+
EDGE = '101'
7+
MIDDLE = '01010'
8+
CODES = {
9+
'L': ('0001101', '0011001', '0010011', '0111101', '0100011',
10+
'0110001', '0101111', '0111011', '0110111', '0001011'),
11+
'R': ('1110010', '1100110', '1101100', '1000010', '1011100',
12+
'1001110', '1010000', '1000100', '1001000', '1110100')
13+
}

barcode/upc.py

Lines changed: 70 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,95 @@
88
"""
99
__docformat__ = 'restructuredtext en'
1010

11-
from barcode.ean import EuropeanArticleNumber13
11+
from barcode.base import Barcode
12+
from barcode.charsets import upc as _upc
13+
from barcode.errors import *
1214

13-
14-
class UniversalProductCodeA(EuropeanArticleNumber13):
15-
"""Initializes new UPC-A barcode. Can be rendered as EAN-13 by passing
16-
`True` to the `make_ean` argument.
15+
class UniversalProductCodeA(Barcode):
16+
"""Initializes new UPC-A barcode.
1717
1818
:parameters:
1919
upc : String
2020
The upc number as string.
2121
writer : barcode.writer Instance
2222
The writer to render the barcode (default: SVGWriter).
23-
make_ean : Boolean
24-
Render barcode as EAN-13 with leading 0 (default: False).
2523
"""
2624

2725
name = 'UPC-A'
2826

2927
digits = 11
3028

31-
def __init__(self, upc, writer=None, make_ean=False):
32-
if make_ean:
33-
UniversalProductCodeA.digits = 12
34-
upc = '0' + upc
29+
def __init__(self, upc, writer=None):
30+
upc = upc[:self.digits]
31+
if not upc.isdigit():
32+
raise IllegalCharacterError('UPC code can only contain numbers.')
33+
if len(upc) != self.digits:
34+
raise NumberOfDigitsError('UPC must have {0} digits, not '
35+
'{1}.'.format(self.digits, len(upc)))
3536
self.upc = upc
36-
EuropeanArticleNumber13.__init__(self, upc, writer)
37+
self.upc = '{}{}'.format(upc, self.calculate_checksum())
38+
self.writer = writer or Barcode.default_writer()
3739

3840
def __unicode__(self):
3941
return self.upc
4042

4143
__str__ = __unicode__
4244

45+
def get_fullcode(self):
46+
return self.upc
47+
48+
def calculate_checksum(self):
49+
"""Calculates the checksum for UPCA/UPC codes
50+
51+
:return: The checksum for 'self.upc'
52+
:rtype: Integer
53+
"""
54+
def sum_(x, y): return int(x) + int(y)
55+
upc = self.upc[0:self.digits]
56+
oddsum = reduce(sum_, upc[::2])
57+
evensum = reduce(sum_, upc[1::2])
58+
check = (evensum + oddsum * 3) % 10
59+
if check == 0:
60+
return 0
61+
else:
62+
return 10 - check
63+
64+
65+
def build(self):
66+
"""Builds the barcode pattern from 'self.upc'
67+
68+
:return: The pattern as string
69+
:rtype: String
70+
"""
71+
print self.upc
72+
code = _upc.EDGE
73+
74+
for i, number in enumerate(self.upc[0:6]):
75+
code += _upc.CODES['L'][int(number)]
76+
77+
code += _upc.MIDDLE
78+
79+
for number in self.upc[6:]:
80+
code += _upc.CODES['R'][int(number)]
81+
82+
code += _upc.EDGE
83+
84+
return [code]
85+
86+
def to_ascii(self):
87+
"""Returns an ascii representation of the barcode.
88+
89+
:rtype: String
90+
"""
91+
92+
code = self.build()
93+
for i, line in enumerate(code):
94+
code[i] = line.replace('1', '|').replace('0', '_')
95+
return '\n'.join(code)
96+
97+
def render(self, writer_options=None):
98+
options = dict(module_width=0.33)
99+
options.update(writer_options or {})
100+
return Barcode.render(self, options)
43101

44102
UPCA = UniversalProductCodeA

0 commit comments

Comments
 (0)