-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sourcery Starbot ⭐ refactored ymt2/python-face-client #1
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,7 +73,7 @@ def faces_detect(self, urls=None, file=None, aggressive=False): | |
if file: | ||
# Check if the file exists | ||
if not os.path.exists(file): | ||
raise IOError('File %s does not exist' % (file)) | ||
raise IOError(f'File {file} does not exist') | ||
|
||
data = {'file': file} | ||
else: | ||
|
@@ -82,8 +82,7 @@ def faces_detect(self, urls=None, file=None, aggressive=False): | |
if aggressive: | ||
data['detector'] = 'Aggressive' | ||
|
||
response = self.send_request('faces/detect', data) | ||
return response | ||
return self.send_request('faces/detect', data) | ||
|
||
def faces_status(self, uids=None, namespace=None): | ||
""" | ||
|
@@ -95,14 +94,13 @@ def faces_status(self, uids=None, namespace=None): | |
raise AttributeError('Missing user IDs') | ||
|
||
(facebook_uids, twitter_uids) = \ | ||
self.__check_user_auth_credentials(uids) | ||
self.__check_user_auth_credentials(uids) | ||
|
||
data = {'uids': uids} | ||
self.__append_user_auth_data(data, facebook_uids, twitter_uids) | ||
self.__append_optional_arguments(data, namespace=namespace) | ||
|
||
response = self.send_request('faces/status', data) | ||
return response | ||
return self.send_request('faces/status', data) | ||
Comment on lines
-98
to
+103
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
def faces_recognize(self, uids=None, urls=None, file=None, train=None, | ||
namespace=None): | ||
|
@@ -120,25 +118,24 @@ def faces_recognize(self, uids=None, urls=None, file=None, train=None, | |
raise AttributeError('Missing required arguments') | ||
|
||
(facebook_uids, twitter_uids) = \ | ||
self.__check_user_auth_credentials(uids) | ||
self.__check_user_auth_credentials(uids) | ||
|
||
data = {'uids': uids} | ||
|
||
if file: | ||
# Check if the file exists | ||
if not os.path.exists(file): | ||
raise IOError('File %s does not exist' % (file)) | ||
raise IOError(f'File {file} does not exist') | ||
|
||
data.update({'file': file}) | ||
data['file'] = file | ||
else: | ||
data.update({'urls': urls}) | ||
data['urls'] = urls | ||
|
||
self.__append_user_auth_data(data, facebook_uids, twitter_uids) | ||
self.__append_optional_arguments(data, train=train, | ||
namespace=namespace) | ||
|
||
response = self.send_request('faces/recognize', data) | ||
return response | ||
return self.send_request('faces/recognize', data) | ||
Comment on lines
-123
to
+138
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
def faces_train(self, uids=None, namespace=None): | ||
""" | ||
|
@@ -151,18 +148,17 @@ def faces_train(self, uids=None, namespace=None): | |
raise AttributeError('Missing user IDs') | ||
|
||
(facebook_uids, twitter_uids) = \ | ||
self.__check_user_auth_credentials(uids) | ||
self.__check_user_auth_credentials(uids) | ||
|
||
data = {'uids': uids} | ||
self.__append_user_auth_data(data, facebook_uids, twitter_uids) | ||
self.__append_optional_arguments(data, namespace=namespace) | ||
|
||
response = self.send_request('faces/train', data) | ||
return response | ||
return self.send_request('faces/train', data) | ||
Comment on lines
-154
to
+157
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
### Methods for managing face tags ### | ||
def tags_get(self, uids=None, urls=None, pids=None, order='recent', \ | ||
limit=5, together=False, filter=None, namespace=None): | ||
limit=5, together=False, filter=None, namespace=None): | ||
Comment on lines
-165
to
+161
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
""" | ||
Returns saved tags in one or more photos, or for the specified | ||
User ID(s). | ||
|
@@ -173,7 +169,7 @@ def tags_get(self, uids=None, urls=None, pids=None, order='recent', \ | |
http://developers.face.com/docs/api/tags-get/ | ||
""" | ||
(facebook_uids, twitter_uids) = \ | ||
self.__check_user_auth_credentials(uids) | ||
self.__check_user_auth_credentials(uids) | ||
|
||
data = {'uids': uids, | ||
'urls': urls, | ||
|
@@ -183,8 +179,7 @@ def tags_get(self, uids=None, urls=None, pids=None, order='recent', \ | |
self.__append_optional_arguments(data, pids=pids, filter=filter, | ||
namespace=namespace) | ||
|
||
response = self.send_request('tags/get', data) | ||
return response | ||
return self.send_request('tags/get', data) | ||
|
||
def tags_add(self, url=None, x=None, y=None, width=None, uid=None, | ||
tagger_id=None, label=None, password=None): | ||
|
@@ -208,11 +203,10 @@ def tags_add(self, url=None, x=None, y=None, width=None, uid=None, | |
self.__append_user_auth_data(data, facebook_uids, twitter_uids) | ||
self.__append_optional_arguments(data, label=label, password=password) | ||
|
||
response = self.send_request('tags/add', data) | ||
return response | ||
return self.send_request('tags/add', data) | ||
Comment on lines
-211
to
+206
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
def tags_save(self, tids=None, uid=None, tagger_id=None, label=None, \ | ||
password=None): | ||
password=None): | ||
Comment on lines
-215
to
+209
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
""" | ||
Saves a face tag. Use this method to save tags for training the | ||
face.com index, or for future use of the faces.detect and tags.get | ||
|
@@ -231,8 +225,7 @@ def tags_save(self, tids=None, uid=None, tagger_id=None, label=None, \ | |
self.__append_optional_arguments(data, tagger_id=tagger_id, | ||
label=label, password=password) | ||
|
||
response = self.send_request('tags/save', data) | ||
return response | ||
return self.send_request('tags/save', data) | ||
|
||
def tags_remove(self, tids=None, password=None): | ||
""" | ||
|
@@ -245,8 +238,7 @@ def tags_remove(self, tids=None, password=None): | |
|
||
data = {'tids': tids} | ||
|
||
response = self.send_request('tags/remove', data) | ||
return response | ||
return self.send_request('tags/remove', data) | ||
Comment on lines
-248
to
+241
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
### Account management methods ### | ||
def account_limits(self): | ||
|
@@ -269,10 +261,7 @@ def account_users(self, namespaces=None): | |
if not namespaces: | ||
raise AttributeError('Missing namespaces argument') | ||
|
||
response = self.send_request('account/users', | ||
{'namespaces': namespaces}) | ||
|
||
return response | ||
return self.send_request('account/users', {'namespaces': namespaces}) | ||
Comment on lines
-272
to
+264
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
def __check_user_auth_credentials(self, uids): | ||
# Check if needed credentials are provided | ||
|
@@ -293,9 +282,11 @@ def __check_user_auth_credentials(self, uids): | |
|
||
def __append_user_auth_data(self, data, facebook_uids, twitter_uids): | ||
if facebook_uids: | ||
data.update({'user_auth': 'fb_user:%s,fb_session:%s' % | ||
(self.facebook_credentials['fb_user'], | ||
self.facebook_credentials['fb_session'])}) | ||
data.update( | ||
{ | ||
'user_auth': f"fb_user:{self.facebook_credentials['fb_user']},fb_session:{self.facebook_credentials['fb_session']}" | ||
} | ||
) | ||
Comment on lines
-296
to
+289
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
if twitter_uids: | ||
# If both user/password and OAuth credentials are provided, use | ||
|
@@ -309,25 +300,26 @@ def __append_user_auth_data(self, data, facebook_uids, twitter_uids): | |
self.twitter_credentials['twitter_oauth_token']))} | ||
) | ||
else: | ||
data.update({'user_auth': | ||
'twitter_user:%s,twitter_password:%s' % | ||
(self.twitter_credentials['twitter_user'], | ||
self.twitter_credentials['twitter_password'])}) | ||
data.update( | ||
{ | ||
'user_auth': f"twitter_user:{self.twitter_credentials['twitter_user']},twitter_password:{self.twitter_credentials['twitter_password']}" | ||
} | ||
) | ||
|
||
def __append_optional_arguments(self, data, **kwargs): | ||
for key, value in kwargs.iteritems(): | ||
if value: | ||
data.update({key: value}) | ||
|
||
def send_request(self, method=None, parameters=None): | ||
url = '%s/%s' % (API_URL, method) | ||
url = f'{API_URL}/{method}' | ||
|
||
data = {'api_key': self.api_key, | ||
'api_secret': self.api_secret, | ||
'format': self.format} | ||
|
||
if parameters: | ||
data.update(parameters) | ||
data |= parameters | ||
Comment on lines
-323
to
+322
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
# Local file is provided, use multi-part form | ||
if 'file' in parameters: | ||
|
@@ -355,7 +347,7 @@ def send_request(self, method=None, parameters=None): | |
response_data = json.loads(response) | ||
|
||
if 'status' in response_data and \ | ||
response_data['status'] == 'failure': | ||
response_data['status'] == 'failure': | ||
raise FaceError(response_data['error_code'], | ||
response_data['error_message']) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,15 +48,14 @@ def __init__(self, name, filename, body, headers): | |
self._body = body | ||
# We respect any content type passed in, but otherwise set it here. | ||
# We set the content disposition now, overwriting any prior value. | ||
if self._filename == None: | ||
self._headers[Part.CONTENT_DISPOSITION] = \ | ||
('form-data; name="%s"' % self._name) | ||
if self._filename is None: | ||
self._headers[Part.CONTENT_DISPOSITION] = f'form-data; name="{self._name}"' | ||
self._headers.setdefault(Part.CONTENT_TYPE, | ||
Part.DEFAULT_CONTENT_TYPE) | ||
else: | ||
self._headers[Part.CONTENT_DISPOSITION] = \ | ||
('form-data; name="%s"; filename="%s"' % | ||
(self._name, self._filename)) | ||
self._headers[ | ||
Part.CONTENT_DISPOSITION | ||
] = f'form-data; name="{self._name}"; filename="{self._filename}"' | ||
Comment on lines
-51
to
+58
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
self._headers.setdefault(Part.CONTENT_TYPE, | ||
mimetypes.guess_type(filename)[0] | ||
or Part.DEFAULT_CONTENT_TYPE) | ||
|
@@ -71,12 +70,9 @@ def get(self): | |
@return: Lines of this part. | ||
@rtype: list | ||
''' | ||
lines = [] | ||
lines.append('--' + Part.BOUNDARY) | ||
for (key, val) in self._headers.items(): | ||
lines.append('%s: %s' % (key, val)) | ||
lines.append('') | ||
lines.append(self._body) | ||
lines = [f'--{Part.BOUNDARY}'] | ||
lines.extend(f'{key}: {val}' for key, val in self._headers.items()) | ||
lines.extend(('', self._body)) | ||
Comment on lines
-74
to
+75
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
return lines | ||
|
||
|
||
|
@@ -142,8 +138,8 @@ def get(self): | |
all = [] | ||
for part in self.parts: | ||
all += part.get() | ||
all.append('--' + Part.BOUNDARY + '--') | ||
all.append(f'--{Part.BOUNDARY}--') | ||
all.append('') | ||
# We have to return the content type, since it specifies the boundary. | ||
content_type = 'multipart/form-data; boundary=%s' % Part.BOUNDARY | ||
content_type = f'multipart/form-data; boundary={Part.BOUNDARY}' | ||
Comment on lines
-145
to
+144
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
return content_type, Part.CRLF.join(all) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,18 +8,14 @@ | |
r'__version__ = (\(.*?\))') | ||
|
||
cwd = os.path.dirname(os.path.abspath(__file__)) | ||
fp = open(os.path.join(cwd, 'face_client', '__init__.py')) | ||
|
||
version = None | ||
for line in fp: | ||
match = version_re.search(line) | ||
if match: | ||
version = eval(match.group(1)) | ||
break | ||
else: | ||
raise Exception('Cannot find version in __init__.py') | ||
fp.close() | ||
|
||
with open(os.path.join(cwd, 'face_client', '__init__.py')) as fp: | ||
version = None | ||
for line in fp: | ||
if match := version_re.search(line): | ||
version = eval(match[1]) | ||
break | ||
else: | ||
raise Exception('Cannot find version in __init__.py') | ||
Comment on lines
-11
to
+18
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
||
setup(name='face_client', | ||
version='.' . join(map(str, version)), | ||
description='face.com face recognition Python API client library', | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function
FaceClient.faces_detect
refactored with the following changes:replace-interpolation-with-fstring
)inline-immediately-returned-variable
)