Skip to content

Commit 52aaf05

Browse files
committed
Make __init__ defaults immutable tuples, move key_map to modual constant, make duplicated response keys constants.
1 parent a720bff commit 52aaf05

1 file changed

Lines changed: 28 additions & 23 deletions

File tree

vision/google/cloud/vision/annotations.py

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,21 @@
2121
from google.cloud.vision.safe import SafeSearchAnnotation
2222

2323

24+
FACE_ANNOTATIONS = 'faceAnnotations'
25+
IMAGE_PROPERTIES_ANNOTATION = 'imagePropertiesAnnotation'
26+
SAFE_SEARCH_ANNOTATION = 'safeSearchAnnotation'
27+
28+
_KEY_MAP = {
29+
FACE_ANNOTATIONS: 'faces',
30+
IMAGE_PROPERTIES_ANNOTATION: 'properties',
31+
'labelAnnotations': 'labels',
32+
'landmarkAnnotations': 'landmarks',
33+
'logoAnnotations': 'logos',
34+
SAFE_SEARCH_ANNOTATION: 'safe_searches',
35+
'textAnnotations': 'texts'
36+
}
37+
38+
2439
class Annotations(object):
2540
"""Helper class to bundle annotation responses.
2641
@@ -51,15 +66,15 @@ class Annotations(object):
5166
:param texts: List of
5267
:class:`~google.cloud.vision.entity.EntityAnnotation`.
5368
"""
54-
def __init__(self, faces=None, properties=None, labels=None,
55-
landmarks=None, logos=None, safe_searches=None, texts=None):
56-
self.faces = faces or ()
57-
self.properties = properties or ()
58-
self.labels = labels or ()
59-
self.landmarks = landmarks or ()
60-
self.logos = logos or ()
61-
self.safe_searches = safe_searches or ()
62-
self.texts = texts or ()
69+
def __init__(self, faces=(), properties=(), labels=(), landmarks=(),
70+
logos=(), safe_searches=(), texts=()):
71+
self.faces = faces
72+
self.properties = properties
73+
self.labels = labels
74+
self.landmarks = landmarks
75+
self.logos = logos
76+
self.safe_searches = safe_searches
77+
self.texts = texts
6378

6479
@classmethod
6580
def from_api_repr(cls, response):
@@ -72,18 +87,8 @@ def from_api_repr(cls, response):
7287
:returns: An instance of ``Annotations`` with detection types loaded.
7388
"""
7489
annotations = {}
75-
key_map = {
76-
'faceAnnotations': 'faces',
77-
'imagePropertiesAnnotation': 'properties',
78-
'labelAnnotations': 'labels',
79-
'landmarkAnnotations': 'landmarks',
80-
'logoAnnotations': 'logos',
81-
'safeSearchAnnotation': 'safe_searches',
82-
'textAnnotations': 'texts'
83-
}
84-
8590
for feature_type, annotation in response.items():
86-
curr_feature = annotations.setdefault(key_map[feature_type], [])
91+
curr_feature = annotations.setdefault(_KEY_MAP[feature_type], [])
8792
curr_feature.extend(
8893
_entity_from_response_type(feature_type, annotation))
8994
return cls(**annotations)
@@ -100,13 +105,13 @@ def _entity_from_response_type(feature_type, results):
100105
:class:`~google.cloud.vision.safe.SafeSearchAnnotation`.
101106
"""
102107
detected_objects = []
103-
if feature_type == 'faceAnnotations':
108+
if feature_type == FACE_ANNOTATIONS:
104109
detected_objects.extend(
105110
Face.from_api_repr(face) for face in results)
106-
elif feature_type == 'imagePropertiesAnnotation':
111+
elif feature_type == IMAGE_PROPERTIES_ANNOTATION:
107112
detected_objects.append(
108113
ImagePropertiesAnnotation.from_api_repr(results))
109-
elif feature_type == 'safeSearchAnnotation':
114+
elif feature_type == SAFE_SEARCH_ANNOTATION:
110115
detected_objects.append(SafeSearchAnnotation.from_api_repr(results))
111116
else:
112117
for result in results:

0 commit comments

Comments
 (0)