Skip to content

Commit 85fabf8

Browse files
author
Hugo Osvaldo Barrera
committed
Linting and similar cleanups
1 parent 5341366 commit 85fabf8

11 files changed

Lines changed: 101 additions & 63 deletions

File tree

barcode/__init__.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,26 @@
1818
from barcode.version import version # noqa: F401
1919

2020

21-
__BARCODE_MAP = dict(
22-
ean8=EAN8,
23-
ean13=EAN13,
24-
ean=EAN13,
25-
gtin=EAN14,
26-
ean14=EAN14,
27-
jan=JAN,
28-
upc=UPCA,
29-
upca=UPCA,
30-
isbn=ISBN13,
31-
isbn13=ISBN13,
32-
gs1=ISBN13,
33-
isbn10=ISBN10,
34-
issn=ISSN,
35-
code39=Code39,
36-
pzn=PZN,
37-
code128=Code128,
38-
itf=ITF,
39-
gs1_128=Gs1_128,
40-
)
21+
__BARCODE_MAP = {
22+
'ean8': EAN8,
23+
'ean13': EAN13,
24+
'ean': EAN13,
25+
'gtin': EAN14,
26+
'ean14': EAN14,
27+
'jan': JAN,
28+
'upc': UPCA,
29+
'upca': UPCA,
30+
'isbn': ISBN13,
31+
'isbn13': ISBN13,
32+
'gs1': ISBN13,
33+
'isbn10': ISBN10,
34+
'issn': ISSN,
35+
'code39': Code39,
36+
'pzn': PZN,
37+
'code128': Code128,
38+
'itf': ITF,
39+
'gs1_128': Gs1_128,
40+
}
4141

4242
PROVIDED_BARCODES = list(__BARCODE_MAP)
4343
PROVIDED_BARCODES.sort()

barcode/charsets/code128.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
)
2626

2727
ALL = set(_common + _charset_a + _charset_b)
28-
A = dict(((c, i) for i, c in enumerate(_charset_a)))
29-
B = dict(((c, i) for i, c in enumerate(_charset_b)))
28+
A = {c: i for i, c in enumerate(_charset_a)}
29+
B = {c: i for i, c in enumerate(_charset_b)}
3030
C = {'TO_B': 100, 'TO_A': 101, '\xf1': 102}
3131

3232
CODES = (

barcode/charsets/itf.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,15 @@
55

66
START = 'NnNn'
77
STOP = 'WnN'
8-
CODES = ('NNWWN', 'WNNNW', 'NWNNW', 'WWNNN', 'NNWNW', 'WNWNN', 'NWWNN', 'NNNWW', 'WNNWN', 'NWNWN')
8+
CODES = (
9+
'NNWWN',
10+
'WNNNW',
11+
'NWNNW',
12+
'WWNNN',
13+
'NNWNW',
14+
'WNWNN',
15+
'NWWNN',
16+
'NNNWW',
17+
'WNNWN',
18+
'NWNWN',
19+
)

barcode/codex.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def check_code(code, name, allowed):
3232

3333

3434
class Code39(Barcode):
35-
"""Initializes a new Code39 instance.
35+
r"""Initializes a new Code39 instance.
3636
3737
:parameters:
3838
code : String
@@ -62,7 +62,7 @@ def get_fullcode(self):
6262
return self.code
6363

6464
def calculate_checksum(self):
65-
check = sum([code39.MAP[x][0] for x in self.code]) % 43
65+
check = sum(code39.MAP[x][0] for x in self.code) % 43
6666
for k, v in code39.MAP.items():
6767
if check == v[0]:
6868
return k
@@ -75,7 +75,7 @@ def build(self):
7575
return [code39.MIDDLE.join(chars)]
7676

7777
def render(self, writer_options=None, text=None):
78-
options = dict(module_width=MIN_SIZE, quiet_zone=MIN_QUIET_ZONE)
78+
options = {'module_width': MIN_SIZE, 'quiet_zone': MIN_QUIET_ZONE}
7979
options.update(writer_options or {})
8080
return Barcode.render(self, options, text)
8181

@@ -110,7 +110,7 @@ def get_fullcode(self):
110110
return 'PZN-{0}'.format(self.pzn)
111111

112112
def calculate_checksum(self):
113-
sum_ = sum([int(x) * int(y) for x, y in enumerate(self.pzn, start=2)])
113+
sum_ = sum(int(x) * int(y) for x, y in enumerate(self.pzn, start=2))
114114
checksum = sum_ % 11
115115
if checksum == 10:
116116
raise BarcodeError('Checksum can not be 10 for PZN.')
@@ -254,7 +254,7 @@ def build(self):
254254
return [code]
255255

256256
def render(self, writer_options=None, text=None):
257-
options = dict(module_width=MIN_SIZE, quiet_zone=MIN_QUIET_ZONE)
257+
options = {'module_width': MIN_SIZE, 'quiet_zone': MIN_QUIET_ZONE}
258258
options.update(writer_options or {})
259259
return Barcode.render(self, options, text)
260260

barcode/ean.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,18 @@
2121

2222

2323
# EAN13 Specs (all sizes in mm)
24-
SIZES = dict(SC0=0.27, SC1=0.297, SC2=0.33, SC3=0.363, SC4=0.396, SC5=0.445,
25-
SC6=0.495, SC7=0.544, SC8=0.61, SC9=0.66)
24+
SIZES = {
25+
'SC0': 0.27,
26+
'SC1': 0.297,
27+
'SC2': 0.33,
28+
'SC3': 0.363,
29+
'SC4': 0.396,
30+
'SC5': 0.445,
31+
'SC6': 0.495,
32+
'SC7': 0.544,
33+
'SC8': 0.61,
34+
'SC9': 0.66
35+
}
2636

2737

2838
class EuropeanArticleNumber13(Barcode):
@@ -107,7 +117,7 @@ def to_ascii(self):
107117
return '\n'.join(code)
108118

109119
def render(self, writer_options=None, text=None):
110-
options = dict(module_width=SIZES['SC2'])
120+
options = {'module_width': SIZES['SC2']}
111121
options.update(writer_options or {})
112122
return Barcode.render(self, options, text)
113123

barcode/isxn.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,9 @@ def __init__(self, isbn, writer=None):
7676
InternationalStandardBookNumber13.__init__(self, '978' + isbn, writer)
7777

7878
def _calculate_checksum(self):
79-
tmp = sum([x * int(y) for x, y in enumerate(self.isbn10[:9],
80-
start=1)]) % 11
79+
tmp = sum(
80+
x * int(y) for x, y in enumerate(self.isbn10[:9], start=1)
81+
) % 11
8182
if tmp == 10:
8283
return 'X'
8384
else:
@@ -112,8 +113,9 @@ def __init__(self, issn, writer=None):
112113
EuropeanArticleNumber13.__init__(self, self.make_ean(), writer)
113114

114115
def _calculate_checksum(self):
115-
tmp = 11 - sum([x * int(y) for x, y in
116-
enumerate(reversed(self.issn[:7]), start=2)]) % 11
116+
tmp = 11 - sum(
117+
x * int(y) for x, y in enumerate(reversed(self.issn[:7]), start=2)
118+
) % 11
117119
if tmp == 10:
118120
return 'X'
119121
else:

barcode/itf.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ def build(self):
7373
return [raw]
7474

7575
def render(self, writer_options, text=None):
76-
options = dict(module_width=MIN_SIZE/self.narrow,
77-
quiet_zone=MIN_QUIET_ZONE)
76+
options = {
77+
'module_width': MIN_SIZE/self.narrow,
78+
'quiet_zone': MIN_QUIET_ZONE,
79+
}
7880
options.update(writer_options or {})
7981
return Barcode.render(self, options, text)

barcode/pybarcode.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ def create_barcode(args, parser):
4747
'barcodes.'.format(bc=args.barcode)
4848
)
4949
if args.type != 'SVG':
50-
opts = dict(format=args.type)
50+
opts = {'format': args.type}
5151
writer = ImageWriter()
5252
else:
53-
opts = dict(compress=args.compress)
53+
opts = {'compress': args.compress}
5454
writer = SVGWriter()
5555
out = os.path.normpath(os.path.abspath(args.output))
5656
name = barcode.generate(args.barcode, args.code, writer, out, opts,

barcode/upc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def build(self):
7979
"""
8080
code = _upc.EDGE[:]
8181

82-
for i, number in enumerate(self.upc[0:6]):
82+
for _i, number in enumerate(self.upc[0:6]):
8383
code += _upc.CODES['L'][int(number)]
8484

8585
code += _upc.MIDDLE
@@ -103,7 +103,7 @@ def to_ascii(self):
103103
return '\n'.join(code)
104104

105105
def render(self, writer_options=None, text=None):
106-
options = dict(module_width=0.33)
106+
options = {'module_width': 0.33}
107107
options.update(writer_options or {})
108108
return Barcode.render(self, options, text)
109109

barcode/writer.py

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,12 @@ class BaseWriter(object):
7777

7878
def __init__(self, initialize=None, paint_module=None, paint_text=None,
7979
finish=None):
80-
self._callbacks = dict(
81-
initialize=initialize, paint_module=paint_module,
82-
paint_text=paint_text, finish=finish
83-
)
80+
self._callbacks = {
81+
'initialize': initialize,
82+
'paint_module': paint_module,
83+
'paint_text': paint_text,
84+
'finish': finish,
85+
}
8486
self.module_width = 10
8587
self.module_height = 10
8688
self.font_size = 10
@@ -111,7 +113,10 @@ def calculate_size(self, modules_per_line, number_of_lines, dpi=300):
111113
height = 2.0 + self.module_height * number_of_lines
112114
number_of_text_lines = len(self.text.splitlines())
113115
if self.font_size and self.text:
114-
height += pt2mm(self.font_size) / 2 * number_of_text_lines + self.text_distance
116+
height += (
117+
pt2mm(self.font_size) / 2
118+
* number_of_text_lines + self.text_distance
119+
)
115120
height += self.text_line_distance * (number_of_text_lines - 1)
116121
return int(mm2px(width, dpi)), int(mm2px(height, dpi))
117122

@@ -235,27 +240,36 @@ def _init(self, code):
235240
width, height = self.calculate_size(len(code[0]), len(code), self.dpi)
236241
self._document = create_svg_object()
237242
self._root = self._document.documentElement
238-
attributes = dict(width=SIZE.format(width), height=SIZE.format(height))
243+
attributes = {
244+
'width': SIZE.format(width),
245+
'height': SIZE.format(height),
246+
}
239247
_set_attributes(self._root, **attributes)
240248
self._root.appendChild(self._document.createComment(COMMENT))
241249
# create group for easier handling in 3rd party software
242250
# like corel draw, inkscape, ...
243251
group = self._document.createElement('g')
244-
attributes = dict(id='barcode_group')
252+
attributes = {'id': 'barcode_group'}
245253
_set_attributes(group, **attributes)
246254
self._group = self._root.appendChild(group)
247255
background = self._document.createElement('rect')
248-
attributes = dict(width='100%', height='100%',
249-
style='fill:{0}'.format(self.background))
256+
attributes = {
257+
'width': '100%',
258+
'height': '100%',
259+
'style': 'fill:{0}'.format(self.background)
260+
}
250261
_set_attributes(background, **attributes)
251262
self._group.appendChild(background)
252263

253264
def _create_module(self, xpos, ypos, width, color):
254265
element = self._document.createElement('rect')
255-
attributes = dict(x=SIZE.format(xpos), y=SIZE.format(ypos),
256-
width=SIZE.format(width),
257-
height=SIZE.format(self.module_height),
258-
style='fill:{0};'.format(color))
266+
attributes = {
267+
'x': SIZE.format(xpos),
268+
'y': SIZE.format(ypos),
269+
'width': SIZE.format(width),
270+
'height': SIZE.format(self.module_height),
271+
'style': 'fill:{0};'.format(color)
272+
}
259273
_set_attributes(element, **attributes)
260274
self._group.appendChild(element)
261275

@@ -268,14 +282,14 @@ def _create_text(self, xpos, ypos):
268282
barcodetext = self.text
269283
for subtext in barcodetext.split('\n'):
270284
element = self._document.createElement('text')
271-
attributes = dict(
272-
x=SIZE.format(xpos),
273-
y=SIZE.format(ypos),
274-
style='fill:{0};font-size:{1}pt;text-anchor:middle;'.format(
285+
attributes = {
286+
'x': SIZE.format(xpos),
287+
'y': SIZE.format(ypos),
288+
'style': 'fill:{0};font-size:{1}pt;text-anchor:middle;'.format(
275289
self.foreground,
276290
self.font_size,
277291
)
278-
)
292+
}
279293
_set_attributes(element, **attributes)
280294
text_element = self._document.createTextNode(subtext)
281295
element.appendChild(text_element)
@@ -328,7 +342,7 @@ def _paint_module(self, xpos, ypos, width, color):
328342

329343
def _paint_text(self, xpos, ypos):
330344
for subtext in self.text.split('\n'):
331-
font = ImageFont.truetype(FONT, self.font_size * 2)
345+
font = ImageFont.truetype(FONT, self.font_size * 2)
332346
width, height = font.getsize(subtext)
333347
# determine the maximum width of each line
334348
pos = (mm2px(xpos, self.dpi) - width // 2,

0 commit comments

Comments
 (0)