Sourcery Starbot ⭐ refactored ymt2/python-face-client#1
Sourcery Starbot ⭐ refactored ymt2/python-face-client#1SourceryAI wants to merge 1 commit intoymt2:masterfrom
Conversation
| 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') |
There was a problem hiding this comment.
Lines 11-22 refactored with the following changes:
- Use
withwhen opening file to ensure closure (ensure-file-closed) - Use named expression to simplify assignment and conditional (
use-named-expression) - Replace m.group(x) with m[x] for re.Match objects (
use-getitem-for-re-match-groups)
| # 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') |
There was a problem hiding this comment.
Function FaceClient.faces_detect refactored with the following changes:
- Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring) - Inline variable that is immediately returned (
inline-immediately-returned-variable)
| 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) |
There was a problem hiding this comment.
Function FaceClient.faces_status refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
| 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) |
There was a problem hiding this comment.
Function FaceClient.faces_recognize refactored with the following changes:
- Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring) - Add single value to dictionary directly rather than using update() [×2] (
simplify-dictionary-update) - Inline variable that is immediately returned (
inline-immediately-returned-variable)
| 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) |
There was a problem hiding this comment.
Function FaceClient.faces_train refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
| 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']}" | ||
| } | ||
| ) |
There was a problem hiding this comment.
Function FaceClient.__append_user_auth_data refactored with the following changes:
- Replace interpolated string formatting with f-string [×2] (
replace-interpolation-with-fstring)
| 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 |
There was a problem hiding this comment.
Function FaceClient.send_request refactored with the following changes:
- Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring) - Merge dictionary updates via the union operator (
dict-assign-update-to-union)
| 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}"' |
There was a problem hiding this comment.
Function Part.__init__ refactored with the following changes:
- Use x is None rather than x == None (
none-compare) - Replace interpolated string formatting with f-string [×2] (
replace-interpolation-with-fstring)
| 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)) |
There was a problem hiding this comment.
Function Part.get refactored with the following changes:
- Merge append into list declaration (
merge-list-append) - Merge consecutive list appends into a single extend (
merge-list-appends-into-extend) - Replace a for append loop with list extend (
for-append-to-extend) - Use f-string instead of string concatenation (
use-fstring-for-concatenation) - Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring)
| 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}' |
There was a problem hiding this comment.
Function Multipart.get refactored with the following changes:
- Use f-string instead of string concatenation [×2] (
use-fstring-for-concatenation) - Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring)
Thanks for starring sourcery-ai/sourcery ✨ 🌟 ✨
Here's your pull request refactoring your most popular Python repo.
If you want Sourcery to refactor all your Python repos and incoming pull requests install our bot.
Review changes via command line
To manually merge these changes, make sure you're on the
masterbranch, then run: