-
Notifications
You must be signed in to change notification settings - Fork 0
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 refactored master branch #1
base: master
Are you sure you want to change the base?
Conversation
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.
Due to GitHub API limits, only the first 60 comments can be shown.
if len(string) <= length: | ||
return string | ||
else: | ||
return string[:length-4] + " ..." | ||
return string if len(string) <= length else f"{string[:length - 4]} ..." |
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 fit_str
refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp
) - Use f-string instead of string concatenation (
use-fstring-for-concatenation
)
VERSION = ("Planet/%s +http://www.planetplanet.org" % __version__) | ||
VERSION = f"Planet/{__version__} +http://www.planetplanet.org" | ||
|
||
# Default User-Agent header to send when retreiving feeds | ||
USER_AGENT = VERSION + " " + feedparser.USER_AGENT | ||
USER_AGENT = f"{VERSION} {feedparser.USER_AGENT}" |
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.
Lines 45-48
refactored with the following changes:
- Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring
) - Use f-string instead of string concatenation [×2] (
use-fstring-for-concatenation
)
info[key + "_iso"] = time.strftime(TIMEFMT_ISO, date) | ||
info[key + "_822"] = time.strftime(TIMEFMT_822, date) | ||
info[f"{key}_iso"] = time.strftime(TIMEFMT_ISO, date) | ||
info[f"{key}_822"] = time.strftime(TIMEFMT_822, date) |
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 template_info
refactored with the following changes:
- Use f-string instead of string concatenation [×2] (
use-fstring-for-concatenation
)
time.gmtime(time.time()-86400*activity_threshold) | ||
time.gmtime(time.time()-86400*activity_threshold) |
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 Planet.gather_channel_info
refactored with the following changes:
- Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring
)
item_info["channel_" + k] = v | ||
item_info[f"channel_{k}"] = v | ||
|
||
# Check for the start of a new day | ||
if prev_date[:3] != newsitem.date[:3]: | ||
prev_date = newsitem.date | ||
item_info["new_date"] = time.strftime(new_date_format, | ||
newsitem.date) | ||
|
||
# Check for the start of a new channel | ||
if item_info.has_key("new_date") \ | ||
or prev_channel != newsitem._channel: | ||
or prev_channel != newsitem._channel: | ||
prev_channel = newsitem._channel | ||
item_info["new_channel"] = newsitem._channel.url | ||
|
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 Planet.gather_items_info
refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation
)
for other_key in ("updated", "modified", "published", "issued", "created"): | ||
if self.has_key(other_key): | ||
date = self.get_as_date(other_key) | ||
break | ||
else: | ||
date = None | ||
|
||
date = next( | ||
( | ||
self.get_as_date(other_key) | ||
for other_key in ( | ||
"updated", | ||
"modified", | ||
"published", | ||
"issued", | ||
"created", | ||
) | ||
if self.has_key(other_key) | ||
), | ||
None, | ||
) | ||
if date is not None: | ||
if date > self._channel.updated: | ||
date = self._channel.updated | ||
# elif date < self._channel.last_updated: | ||
# date = self._channel.updated | ||
date = min(date, self._channel.updated) |
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 NewsItem.get_date
refactored with the following changes:
- Use the built-in function
next
instead of a for-loop (use-next
) - Replace comparison with min/max call (
min-max-identity
)
This removes the following comments ( why? ):
# date = self._channel.updated
# elif date < self._channel.last_updated:
for key in ("content", "tagline", "summary"): | ||
if self.has_key(key) and self.key_type(key) != self.NULL: | ||
return self.get_as_string(key) | ||
|
||
return "" | ||
return next( | ||
( | ||
self.get_as_string(key) | ||
for key in ("content", "tagline", "summary") | ||
if self.has_key(key) and self.key_type(key) != self.NULL | ||
), | ||
"", | ||
) |
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 NewsItem.get_content
refactored with the following changes:
- Use the built-in function
next
instead of a for-loop (use-next
)
base = tuple(parts[0:i]) | ||
base = tuple(parts[:i]) |
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 relativize.collect_and_tally
refactored with the following changes:
- Replace a[0:x] with a[:x] and a[x:len(a)] with a[x:] (
remove-redundant-slice-index
)
if chunks[i] in ['amp', 'lt', 'gt', 'apos', 'quot']: | ||
chunks[i] ='&' + chunks[i] +';' | ||
elif chunks[i] in name2codepoint: | ||
chunks[i]=unichr(name2codepoint[chunks[i]]) | ||
else: | ||
chunks[i]='&' + chunks[i] + ';' | ||
if chunks[i] in ['amp', 'lt', 'gt', 'apos', 'quot']: | ||
chunks[i] = f'&{chunks[i]};' | ||
elif chunks[i] in name2codepoint: | ||
chunks[i]=unichr(name2codepoint[chunks[i]]) | ||
else: | ||
chunks[i] = f'&{chunks[i]};' |
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 retype
refactored with the following changes:
- Use f-string instead of string concatenation [×4] (
use-fstring-for-concatenation
)
if self._root: | ||
return key | ||
else: | ||
return self._id + " " + key | ||
return key if self._root else f"{self._id} {key}" |
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 CachedInfo.cache_key
refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp
) - Use f-string instead of string concatenation [×2] (
use-fstring-for-concatenation
)
if self._root: | ||
keys_key = " keys" | ||
else: | ||
keys_key = self._id | ||
|
||
keys_key = " keys" if self._root else self._id |
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 CachedInfo.cache_read
refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp
) - Use f-string instead of string concatenation (
use-fstring-for-concatenation
)
del(self._cache[cache_key + " type"]) | ||
del self._cache[f"{cache_key} type"] | ||
continue | ||
|
||
keys.append(key) | ||
self._cache[cache_key] = self._value[key] | ||
self._cache[cache_key + " type"] = self._type[key] | ||
|
||
if self._root: | ||
keys_key = " keys" | ||
else: | ||
keys_key = self._id | ||
self._cache[f"{cache_key} type"] = self._type[key] | ||
|
||
keys_key = " keys" if self._root else self._id |
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 CachedInfo.cache_write
refactored with the following changes:
- Use f-string instead of string concatenation [×2] (
use-fstring-for-concatenation
) - Replace if statement with if expression (
assign-if-exp
)
if self._root: | ||
keys_key = " keys" | ||
else: | ||
keys_key = self._id | ||
|
||
if self._cache.has_key(keys_key): | ||
keys = self._cache[keys_key].split(" ") | ||
del(self._cache[keys_key]) | ||
else: | ||
keys_key = " keys" if self._root else self._id | ||
if not self._cache.has_key(keys_key): | ||
return | ||
|
||
keys = self._cache[keys_key].split(" ") | ||
del(self._cache[keys_key]) | ||
for key in keys: | ||
cache_key = self.cache_key(key) | ||
del(self._cache[cache_key]) | ||
del(self._cache[cache_key + " type"]) | ||
del self._cache[f"{cache_key} type"] |
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 CachedInfo.cache_clear
refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp
) - Swap if/else branches (
swap-if-else-branches
) - Remove unnecessary else after guard condition (
remove-unnecessary-else
) - Use f-string instead of string concatenation (
use-fstring-for-concatenation
)
func = getattr(self, "set_" + key) | ||
func = getattr(self, f"set_{key}") | ||
except AttributeError: | ||
pass | ||
else: | ||
return func(key, value) | ||
|
||
if value == None: | ||
if value is None: | ||
return self.set_as_null(key, value) | ||
else: | ||
try: | ||
return self.set_as_string(key, value) | ||
except TypeError: | ||
return self.set_as_date(key, value) | ||
try: | ||
return self.set_as_string(key, value) | ||
except TypeError: | ||
return self.set_as_date(key, value) |
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 CachedInfo.set
refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation
) - Remove unnecessary else after guard condition (
remove-unnecessary-else
) - Use x is None rather than x == None (
none-compare
)
func = getattr(self, "get_" + key) | ||
func = getattr(self, f"get_{key}") | ||
except AttributeError: | ||
pass | ||
else: | ||
return func(key) | ||
|
||
try: | ||
func = getattr(self, "get_as_" + self._type[key]) | ||
func = getattr(self, f"get_as_{self._type[key]}") |
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 CachedInfo.get
refactored with the following changes:
- Use f-string instead of string concatenation [×2] (
use-fstring-for-concatenation
)
href = attrsD.get('url', attrsD.get('uri', attrsD.get('href', None))) | ||
if href: | ||
if href := attrsD.get('url', attrsD.get('uri', attrsD.get('href', None))): |
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 _FeedParserMixin._itsAnHrefDamnIt
refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression
)
versionmap = {'0.91': 'rss091u', | ||
'0.92': 'rss092', | ||
'0.93': 'rss093', | ||
'0.94': 'rss094'} | ||
if not self.version: | ||
attr_version = attrsD.get('version', '') | ||
version = versionmap.get(attr_version) | ||
if version: | ||
versionmap = {'0.91': 'rss091u', | ||
'0.92': 'rss092', | ||
'0.93': 'rss093', | ||
'0.94': 'rss094'} | ||
if version := versionmap.get(attr_version): |
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 _FeedParserMixin._start_rss
refactored with the following changes:
- Move assignments closer to their usage (
move-assign
) - Use named expression to simplify assignment and conditional (
use-named-expression
)
versionmap = {'0.1': 'atom01', | ||
'0.2': 'atom02', | ||
'0.3': 'atom03'} | ||
if not self.version: | ||
attr_version = attrsD.get('version') | ||
version = versionmap.get(attr_version) | ||
if version: | ||
self.version = version | ||
else: | ||
self.version = 'atom' | ||
versionmap = {'0.1': 'atom01', | ||
'0.2': 'atom02', | ||
'0.3': 'atom03'} | ||
self.version = version if (version := versionmap.get(attr_version)) else 'atom' |
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 _FeedParserMixin._start_feed
refactored with the following changes:
- Move assignments closer to their usage (
move-assign
) - Use named expression to simplify assignment and conditional (
use-named-expression
) - Replace if statement with if expression (
assign-if-exp
)
context = self.sourcedata | ||
return self.sourcedata | ||
elif self.inentry: | ||
context = self.entries[-1] | ||
return self.entries[-1] | ||
else: | ||
context = self.feeddata | ||
return context | ||
return self.feeddata |
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 _FeedParserMixin._getContext
refactored with the following changes:
- Lift return into if (
lift-return-into-if
)
context.setdefault(prefix + '_detail', FeedParserDict()) | ||
context[prefix + '_detail'][key] = value | ||
context.setdefault(f'{prefix}_detail', FeedParserDict()) | ||
context[f'{prefix}_detail'][key] = value |
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 _FeedParserMixin._save_author
refactored with the following changes:
- Use f-string instead of string concatenation [×2] (
use-fstring-for-concatenation
)
detail = context.get('%s_detail' % key) | ||
if detail: | ||
if detail := context.get(f'{key}_detail'): | ||
name = detail.get('name') | ||
email = detail.get('email') | ||
if name and email: | ||
context[key] = '%s (%s)' % (name, email) | ||
context[key] = f'{name} ({email})' |
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 _FeedParserMixin._sync_author_detail
refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression
) - Replace interpolated string formatting with f-string [×5] (
replace-interpolation-with-fstring
)
id = self._getAttribute(attrsD, 'rdf:about') | ||
if id: | ||
if id := self._getAttribute(attrsD, 'rdf:about'): |
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 _FeedParserMixin._start_item
refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression
)
value = self._getAttribute(attrsD, 'rdf:resource') | ||
if value: | ||
if value := self._getAttribute(attrsD, 'rdf:resource'): |
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 _FeedParserMixin._start_cc_license
refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression
)
value = self._getAttribute(attrsD, 'rdf:resource') | ||
if value: | ||
if value := self._getAttribute(attrsD, 'rdf:resource'): |
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 _FeedParserMixin._start_admin_errorreportsto
refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression
)
href = attrsD.get('href') | ||
if href: | ||
if href := attrsD.get('href'): |
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 _FeedParserMixin._start_enclosure
refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression
)
if ((code / 100) == 3) and (code != 304): | ||
if code == 300: |
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 _FeedURLHandler.http_error_default
refactored with the following changes:
- Remove redundant conditional (
remove-redundant-if
) - Simplify numeric comparison (
simplify-numeric-comparison
)
url_file_stream_or_string = '%s://%s%s' % (urltype, realhost, rest) | ||
url_file_stream_or_string = f'{urltype}://{realhost}{rest}' |
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 _open_resource
refactored with the following changes:
- Replace interpolated string formatting with f-string [×2] (
replace-interpolation-with-fstring
)
if ordinal: | ||
ordinal = int(ordinal) | ||
else: | ||
ordinal = 0 | ||
ordinal = int(ordinal) if ordinal else 0 |
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 _parse_date_iso8601
refactored with the following changes:
- Replace if statement with if expression [×2] (
assign-if-exp
) - Swap positions of nested conditionals [×2] (
swap-nested-ifs
) - Hoist nested repeated code outside conditional statements [×2] (
hoist-similar-statement-from-if
) - Swap if/else branches (
swap-if-else-branches
) - Merge else clause's nested if statement into elif (
merge-else-if-into-elif
)
This removes the following comments ( why? ):
# ordinals are NOT normalized by mktime, we simulate them
# by setting month=1, day=ordinal
# see above
hour = '0' + hour | ||
hour = f'0{hour}' |
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 _parse_date_nate
refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation
)
day = '0' + day | ||
day = f'0{day}' | ||
hour = m.group(4) | ||
if len(hour) == 1: | ||
hour = '0' + hour | ||
hour = f'0{hour}' |
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 _parse_date_hungarian
refactored with the following changes:
- Use f-string instead of string concatenation [×2] (
use-fstring-for-concatenation
)
Branch
master
refactored by Sourcery.If you're happy with these changes, merge this Pull Request using the Squash and merge strategy.
See our documentation here.
Run Sourcery locally
Reduce the feedback loop during development by using the Sourcery editor plugin:
Review changes via command line
To manually merge these changes, make sure you're on the
master
branch, then run:Help us improve this pull request!