Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 32 additions & 40 deletions face_client/face_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Copy link
Author

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:


data = {'file': file}
else:
Expand All @@ -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):
"""
Expand All @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function FaceClient.faces_status refactored with the following changes:


def faces_recognize(self, uids=None, urls=None, file=None, train=None,
namespace=None):
Expand All @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function FaceClient.faces_recognize refactored with the following changes:


def faces_train(self, uids=None, namespace=None):
"""
Expand All @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function FaceClient.faces_train refactored with the following changes:


### 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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function FaceClient.tags_get refactored with the following changes:

"""
Returns saved tags in one or more photos, or for the specified
User ID(s).
Expand All @@ -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,
Expand All @@ -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):
Expand All @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function FaceClient.tags_add refactored with the following changes:


def tags_save(self, tids=None, uid=None, tagger_id=None, label=None, \
password=None):
password=None):
Comment on lines -215 to +209
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function FaceClient.tags_save refactored with the following changes:

"""
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
Expand All @@ -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):
"""
Expand All @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function FaceClient.tags_remove refactored with the following changes:


### Account management methods ###
def account_limits(self):
Expand All @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function FaceClient.account_users refactored with the following changes:


def __check_user_auth_credentials(self, uids):
# Check if needed credentials are provided
Expand All @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function FaceClient.__append_user_auth_data refactored with the following changes:


if twitter_uids:
# If both user/password and OAuth credentials are provided, use
Expand All @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function FaceClient.send_request refactored with the following changes:


# Local file is provided, use multi-part form
if 'file' in parameters:
Expand Down Expand Up @@ -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'])

Expand Down
24 changes: 10 additions & 14 deletions face_client/multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Part.__init__ refactored with the following changes:

self._headers.setdefault(Part.CONTENT_TYPE,
mimetypes.guess_type(filename)[0]
or Part.DEFAULT_CONTENT_TYPE)
Expand All @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Part.get refactored with the following changes:

return lines


Expand Down Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Multipart.get refactored with the following changes:

return content_type, Part.CRLF.join(all)
20 changes: 8 additions & 12 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 11-22 refactored with the following changes:

setup(name='face_client',
version='.' . join(map(str, version)),
description='face.com face recognition Python API client library',
Expand Down