Skip to content
This repository was archived by the owner on Nov 8, 2021. It is now read-only.

Commit dd28822

Browse files
authored
Code format. (#38)
* Code format. * Add logs in test init and cleanup. * Typo fix.
1 parent b5de7cc commit dd28822

File tree

9 files changed

+53
-50
lines changed

9 files changed

+53
-50
lines changed

cognitive_face/face.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,15 @@ def detect(image, face_id=True, landmarks=False, attributes=''):
3737
'returnFaceAttributes': attributes,
3838
}
3939

40-
return util.request('POST', url, headers=headers, params=params, json=json,
41-
data=data)
40+
return util.request(
41+
'POST', url, headers=headers, params=params, json=json, data=data)
4242

4343

44-
def find_similars(face_id, face_list_id=None, face_ids=None,
45-
max_candidates_return=20, mode='matchPerson'):
44+
def find_similars(face_id,
45+
face_list_id=None,
46+
face_ids=None,
47+
max_candidates_return=20,
48+
mode='matchPerson'):
4649
"""Given query face's `face_id`, to search the similar-looking faces from a
4750
`face_id` array or a `face_list_id`.
4851
@@ -100,7 +103,9 @@ def group(face_ids):
100103
return util.request('POST', url, json=json)
101104

102105

103-
def identify(face_ids, person_group_id, max_candidates_return=1,
106+
def identify(face_ids,
107+
person_group_id,
108+
max_candidates_return=1,
104109
threshold=None):
105110
"""Identify unknown faces from a person group.
106111

cognitive_face/face_list.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ def add_face(image, face_list_id, user_data=None, target_face=None):
3838
'targetFace': target_face,
3939
}
4040

41-
return util.request('POST', url, headers=headers, params=params, json=json,
42-
data=data)
41+
return util.request(
42+
'POST', url, headers=headers, params=params, json=json, data=data)
4343

4444

4545
def create(face_list_id, name=None, user_data=None):
@@ -57,7 +57,7 @@ def create(face_list_id, name=None, user_data=None):
5757
Returns:
5858
An empty response body.
5959
"""
60-
name = face_list_id if name is None else name
60+
name = name or face_list_id
6161
url = 'facelists/{}'.format(face_list_id)
6262
json = {
6363
'name': name,
@@ -68,23 +68,22 @@ def create(face_list_id, name=None, user_data=None):
6868

6969

7070
def delete_face(face_list_id, persisted_face_id):
71-
"""Delete an existing face from a face list (given by a
72-
`persisited_face_id` and a `face_list_id`). Persisted image related to the
73-
face will also be deleted.
71+
"""Delete an existing face from a face list (given by a `persisted_face_id`
72+
and a `face_list_id`). Persisted image related to the face will also be
73+
deleted.
7474
7575
Args:
7676
face_list_id: Valid character is letter in lower case or digit or '-'
7777
or '_', maximum length is 64.
78-
persisited_face_id: `persisited_face_id` of an existing face. Valid
78+
persisted_face_id: `persisted_face_id` of an existing face. Valid
7979
character is letter in lower case or digit or '-' or '_', maximum
8080
length is 64.
8181
8282
Returns:
8383
An empty response body.
8484
"""
85-
url = 'facelists/{}/persistedFaces/{}'.format(
86-
face_list_id, persisted_face_id
87-
)
85+
url = 'facelists/{}/persistedFaces/{}'.format(face_list_id,
86+
persisted_face_id)
8887

8988
return util.request('DELETE', url)
9089

cognitive_face/person.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
from . import util
88

99

10-
def add_face(image, person_group_id, person_id, user_data=None,
10+
def add_face(image,
11+
person_group_id,
12+
person_id,
13+
user_data=None,
1114
target_face=None):
1215
"""Add a representative face to a person for identification. The input face
1316
is specified as an image with a `target_face` rectangle. It returns a
@@ -40,8 +43,8 @@ def add_face(image, person_group_id, person_id, user_data=None,
4043
'targetFace': target_face,
4144
}
4245

43-
return util.request('POST', url, headers=headers, params=params, json=json,
44-
data=data)
46+
return util.request(
47+
'POST', url, headers=headers, params=params, json=json, data=data)
4548

4649

4750
def create(person_group_id, name, user_data=None):
@@ -100,8 +103,7 @@ def delete_face(person_group_id, person_id, persisted_face_id):
100103
An empty response body.
101104
"""
102105
url = 'persongroups/{}/persons/{}/persistedFaces/{}'.format(
103-
person_group_id, person_id, persisted_face_id
104-
)
106+
person_group_id, person_id, persisted_face_id)
105107

106108
return util.request('DELETE', url)
107109

@@ -139,16 +141,15 @@ def get_face(person_group_id, person_id, persisted_face_id):
139141
`user_data`).
140142
"""
141143
url = 'persongroups/{}/persons/{}/persistedFaces/{}'.format(
142-
person_group_id, person_id, persisted_face_id
143-
)
144+
person_group_id, person_id, persisted_face_id)
144145

145146
return util.request('GET', url)
146147

147148

148149
def lists(person_group_id, start=None, top=None):
149150
"""List `top` persons in a person group with `person_id` greater than
150151
`start`, and retrieve person information (including `person_id`, `name`,
151-
`user_data` and `persisited_face_ids` of registered faces of the person).
152+
`user_data` and `persisted_face_ids` of registered faces of the person).
152153
153154
Args:
154155
person_group_id: `person_group_id` of the target person group.
@@ -207,8 +208,7 @@ def update_face(person_group_id, person_id, persisted_face_id, user_data=None):
207208
An empty response body.
208209
"""
209210
url = 'persongroups/{}/persons/{}/persistedFaces/{}'.format(
210-
person_group_id, person_id, persisted_face_id
211-
)
211+
person_group_id, person_id, persisted_face_id)
212212
json = {
213213
'userData': user_data,
214214
}

cognitive_face/person_group.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def create(person_group_id, name=None, user_data=None):
2222
Returns:
2323
An empty response body.
2424
"""
25-
name = person_group_id if name is None else name
25+
name = name or person_group_id
2626
url = 'persongroups/{}'.format(person_group_id)
2727
json = {
2828
'name': name,

cognitive_face/tests/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ def setUpModule():
2626
- Set Base URL.
2727
- Setup needed data for unitests.
2828
"""
29+
print("setUpModule Begin.")
2930
CF.Key.set(config.KEY)
3031
CF.BaseUrl.set(config.BASE_URL)
3132
util.DataStore.setup_person_group()
3233
util.DataStore.setup_face_list()
3334
util.DataStore.setup_face()
35+
print("setUpModule End.")
3436

3537

3638
def tearDownModule():
@@ -39,5 +41,7 @@ def tearDownModule():
3941
4042
- Remove all the created persisted data.
4143
"""
44+
print("tearDownModule Begin.")
4245
CF.util.clear_face_lists()
4346
CF.util.clear_person_groups()
47+
print("tearDownModule End.")

cognitive_face/tests/test_face_list.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ def test_face(self):
2626
self.assertIsInstance(res, dict)
2727
util.wait()
2828

29-
persisited_face_id = res['persistedFaceId']
29+
persisted_face_id = res['persistedFaceId']
3030

3131
res = CF.face_list.delete_face(
3232
util.DataStore.face_list_id,
33-
persisited_face_id,
33+
persisted_face_id,
3434
)
3535
print(res)
3636
self.assertIsInstance(res, dict)

cognitive_face/tests/test_person.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ def test_face(self):
3131
self.assertIsInstance(res, dict)
3232
util.wait()
3333

34-
persisited_face_id = res['persistedFaceId']
34+
persisted_face_id = res['persistedFaceId']
3535

3636
res = CF.person.update_face(
3737
util.DataStore.person_group_id,
3838
util.DataStore.person_id['Dad'],
39-
persisited_face_id,
39+
persisted_face_id,
4040
'TempUserData',
4141
)
4242
print(res)
@@ -46,7 +46,7 @@ def test_face(self):
4646
res = CF.person.delete_face(
4747
util.DataStore.person_group_id,
4848
util.DataStore.person_id['Dad'],
49-
persisited_face_id,
49+
persisted_face_id,
5050
)
5151
print(res)
5252
self.assertIsInstance(res, dict)

cognitive_face/util.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,19 @@ class CognitiveFaceException(Exception):
2424
code: error code.
2525
msg: error message.
2626
"""
27+
2728
def __init__(self, status_code, code, msg):
2829
super(CognitiveFaceException, self).__init__()
2930
self.status_code = status_code
3031
self.code = code
3132
self.msg = msg
3233

3334
def __str__(self):
34-
return (
35-
'Error when calling Cognitive Face API:\n'
36-
'\tstatus_code: {}\n'
37-
'\tcode: {}\n'
38-
'\tmessage: {}\n'
39-
).format(self.status_code, self.code, self.msg)
35+
return ('Error when calling Cognitive Face API:\n'
36+
'\tstatus_code: {}\n'
37+
'\tcode: {}\n'
38+
'\tmessage: {}\n').format(self.status_code, self.code,
39+
self.msg)
4040

4141

4242
class Key(object):
@@ -56,7 +56,6 @@ def get(cls):
5656

5757

5858
class BaseUrl(object):
59-
6059
@classmethod
6160
def set(cls, base_url):
6261
cls.base_url = base_url
@@ -82,8 +81,8 @@ def request(method, url, data=None, json=None, headers=None, params=None):
8281
headers['Content-Type'] = 'application/json'
8382
headers['Ocp-Apim-Subscription-Key'] = Key.get()
8483

85-
response = requests.request(method, url, params=params, data=data,
86-
json=json, headers=headers)
84+
response = requests.request(
85+
method, url, params=params, data=data, json=json, headers=headers)
8786

8887
# Handle result and raise custom exception when something wrong.
8988
result = None
@@ -92,14 +91,11 @@ def request(method, url, data=None, json=None, headers=None, params=None):
9291
try:
9392
error_msg = response.json()['error']
9493
except:
95-
raise CognitiveFaceException(
96-
response.status_code,
97-
response.status_code,
98-
response.text)
99-
raise CognitiveFaceException(
100-
response.status_code,
101-
error_msg.get('code'),
102-
error_msg.get('message'))
94+
raise CognitiveFaceException(response.status_code,
95+
response.status_code, response.text)
96+
raise CognitiveFaceException(response.status_code,
97+
error_msg.get('code'),
98+
error_msg.get('message'))
10399

104100
# Prevent `response.json()` complains about empty response.
105101
if response.text:

setup.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,4 @@ def readme():
3737
'Topic :: Scientific/Engineering :: Image Recognition',
3838
],
3939
test_suite='nose.collector',
40-
tests_require=['nose'],
41-
)
40+
tests_require=['nose'])

0 commit comments

Comments
 (0)