Skip to content

Commit 4c8c62b

Browse files
committed
Now, instead of a dict, quirc.decode returns object with the same attributes as dict keys
1 parent 5fa3f81 commit 4c8c62b

4 files changed

Lines changed: 33 additions & 15 deletions

File tree

docs/source/usage/high-level.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ It is very simple in use::
77
from PIL import Image
88

99
for code in quirc.decode(Image.open('images/qr-code.png')):
10-
print code['text']
10+
print code
1111

1212
`quirc.decode` function returns generator, which contains results of QR codes recognition.
13-
Each item is `dict` object with those keys:
13+
Each item is an object with those attributes:
1414

1515
corners
1616
Tuple with the four corners of the QR code, from top left, clockwise

docs/source/usage/low-level.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ Low-level API
33

44
Low-level API fully copies the C API and contains all in the `quirc.api` module.
55

6+
**Warning**: you will need to use *ctypes* here manually.
7+
68
.. automodule:: quirc.api
79

810

@@ -47,3 +49,4 @@ Usage
4749
print ctypes.string_at(data.payload, data.payload_len)
4850

4951
api.destroy(obj)
52+

quirc/base.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,21 @@
1212
except ImportError:
1313
from PIL import Image
1414

15+
class Code(object):
16+
"""Structure for storing extracted QR code data"""
17+
18+
__slots__ = ('corners', 'size', 'version', 'ecc_level', 'data_type', 'text')
19+
20+
def __init__(self, corners, size, version, ecc_level, data_type, text):
21+
self.corners = corners
22+
self.size = size
23+
self.version = version
24+
self.ecc_level = ecc_level
25+
self.data_type = data_type
26+
self.text = text
27+
28+
def __repr__(self):
29+
return self.text
1530

1631
def decode(image):
1732
"""Recognize image and return generator with all the available QR codes
@@ -48,14 +63,14 @@ def decode(image):
4863
api.extract(obj, i, code)
4964
api.decode(code, data)
5065

51-
yield {
52-
'corners': tuple([(corner.x, corner.y) for corner in code.corners]),
53-
'size': code.size,
54-
'version': data.version,
55-
'ecc_level': data.ecc_level,
56-
'data_type': data.data_type,
57-
'text': ctypes.string_at(data.payload, data.payload_len),
58-
}
66+
yield Code(
67+
tuple([(corner.x, corner.y) for corner in code.corners]),
68+
code.size,
69+
data.version,
70+
data.ecc_level,
71+
data.data_type,
72+
ctypes.string_at(data.payload, data.payload_len),
73+
)
5974

6075
api.destroy(obj)
6176

tests/test_quirc.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ def test_pil(self):
4040

4141
code = result[0]
4242

43-
self.assertEqual(code['data_type'], 4)
44-
self.assertEqual(code['ecc_level'], 0)
45-
self.assertEqual(code['size'], 29)
43+
self.assertEqual(code.data_type, 4)
44+
self.assertEqual(code.ecc_level, 0)
45+
self.assertEqual(code.size, 29)
4646

47-
self.assertTupleEqual(code['corners'], ((16, 16), (132, 16), (132, 132), (16, 132)))
48-
self.assertEqual(code['text'], 'https://github.com/svartalf/python-quirc')
47+
self.assertTupleEqual(code.corners, ((16, 16), (132, 16), (132, 132), (16, 132)))
48+
self.assertEqual(code.text, str(code), 'https://github.com/svartalf/python-quirc')

0 commit comments

Comments
 (0)