Sourcery Starbot ⭐ refactored iranzo/stampython#29
Sourcery Starbot ⭐ refactored iranzo/stampython#29SourceryAI wants to merge 2 commits intoiranzo:masterfrom
Conversation
SourceryAI
left a comment
There was a problem hiding this comment.
Due to GitHub API limits, only the first 60 comments can be shown.
| # if a database path was provided, override the one in alembic.ini | ||
| database = context.get_x_argument(as_dictionary=True).get('database') | ||
| if database: | ||
| url = "sqlite:///%s" % database | ||
| if database := context.get_x_argument(as_dictionary=True).get('database'): | ||
| url = f"sqlite:///{database}" |
There was a problem hiding this comment.
Function run_migrations_offline refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression) - Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring)
This removes the following comments ( why? ):
# if a database path was provided, override the one in alembic.ini
| # if a database path was provided, override the one in alembic.ini | ||
| database = context.get_x_argument(as_dictionary=True).get('database') | ||
| if database: | ||
| ini_section['sqlalchemy.url'] = "sqlite:///%s" % database | ||
| if database := context.get_x_argument(as_dictionary=True).get('database'): | ||
| ini_section['sqlalchemy.url'] = f"sqlite:///{database}" |
There was a problem hiding this comment.
Function run_migrations_online refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression) - Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring)
This removes the following comments ( why? ):
# if a database path was provided, override the one in alembic.ini
| triggers = [] | ||
| for each in newplug.init(): | ||
| triggers.append(each) | ||
| triggers = list(newplug.init()) |
There was a problem hiding this comment.
Function initplugins refactored with the following changes:
- Convert for loop into list comprehension (
list-comprehension) - Replace identity comprehension with call to collection constructor (
identity-comprehension)
| '-x', 'database=%s' % options.database, '--raiseerr', | ||
| 'upgrade', 'head', | ||
| '-x', | ||
| f'database={options.database}', | ||
| '--raiseerr', | ||
| 'upgrade', | ||
| 'head', |
There was a problem hiding this comment.
Function createorupdatedb refactored with the following changes:
- Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring)
| if "```" in text: | ||
| markdown = True | ||
| else: | ||
| markdown = False | ||
|
|
||
| texto = string.join(lines[0:maxlines], "\n") | ||
| markdown = "```" in text | ||
| texto = string.join(lines[:maxlines], "\n") | ||
| if markdown: | ||
| texto = "%s```" % texto | ||
| texto = f"{texto}```" |
There was a problem hiding this comment.
Function sendmessage refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression) - Replace if statement with if expression (
assign-if-exp) - Replace interpolated string formatting with f-string [×6] (
replace-interpolation-with-fstring) - Replace a[0:x] with a[:x] and a[x:len(a)] with a[x:] (
remove-redundant-slice-index) - Simplify boolean if expression (
boolean-if-exp-identity) - Remove unnecessary casts to int, str, float or bool (
remove-unnecessary-cast)
| if not admin: | ||
| if plugin.config.config(key='admin', gid=chat_id, default="") == "": | ||
| if plugin.config.config(key='admin', gid=chat_id, default="") == "": | ||
| if not admin: |
There was a problem hiding this comment.
Function is_owner_or_admin refactored with the following changes:
- Swap positions of nested conditionals (
swap-nested-ifs)
This removes the following comments ( why? ):
# If we're not admin and admin is empty, consider ourselves admin
| if plugin.config.gconfig(key='isolated', default=False, gid=gid): | ||
| # Isolated is defined for either channel or general bot config. | ||
| # need to check now if group is linked and if so, return that gid, | ||
| # and if not, return groupid | ||
|
|
||
| link = plugin.config.gconfig(key='link', default=False, gid=gid) | ||
| if link: | ||
| # This chat_id has 'link' defined against master, effective gid | ||
| # should be that one | ||
| return int(link) | ||
| else: | ||
| return gid | ||
| else: | ||
| if not plugin.config.gconfig(key='isolated', default=False, gid=gid): | ||
| # Non isolation configured, returning '0' as gid to use | ||
| return 0 | ||
| if link := plugin.config.gconfig(key='link', default=False, gid=gid): | ||
| # This chat_id has 'link' defined against master, effective gid | ||
| # should be that one | ||
| return int(link) | ||
| else: | ||
| return gid |
There was a problem hiding this comment.
Function geteffectivegid refactored with the following changes:
- Swap if/else branches (
swap-if-else-branches) - Remove unnecessary else after guard condition (
remove-unnecessary-else) - Use named expression to simplify assignment and conditional (
use-named-expression)
This removes the following comments ( why? ):
# Isolated is defined for either channel or general bot config.
# and if not, return groupid
# need to check now if group is linked and if so, return that gid,
| """ | ||
| triggers = ["^/admin"] | ||
| return triggers | ||
| return ["^/admin"] |
There was a problem hiding this comment.
Function init refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
| def run(message): # do not edit this line | ||
| def run(message): # do not edit this line |
There was a problem hiding this comment.
Function run refactored with the following changes:
- Replace a[0:x] with a[:x] and a[x:len(a)] with a[x:] (
remove-redundant-slice-index)
stampy/plugin/admin.py
Outdated
| token = texto.split(' ')[3] | ||
| if token: | ||
| if token := texto.split(' ')[3]: |
There was a problem hiding this comment.
Function admincommands refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression)
| token = ''.join((random.choice(charset)) for x in range(size)) | ||
| generatedtoken = "%s:%s" % (chat_id, token) | ||
| token = ''.join((random.choice(charset)) for _ in range(size)) | ||
| generatedtoken = f"{chat_id}:{token}" |
There was a problem hiding this comment.
Function changenmastertoken refactored with the following changes:
- Replace unused for index with underscore (
for-index-underscore) - Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring)
stampy/plugin/admin.py
Outdated
| if masterid: | ||
| if masterid := stampy.plugin.config.config( | ||
| key='link', default=False, gid=chat_id | ||
| ): |
There was a problem hiding this comment.
Function chanunlink refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression)
| masterid = stampy.plugin.config.config(key='link', default=False, gid=chat_id) | ||
|
|
||
| if masterid: | ||
| if masterid := stampy.plugin.config.config( | ||
| key='link', default=False, gid=chat_id | ||
| ): | ||
| # We've a master channel, report it | ||
| text = _("This channel %s is slave of channel %s") % (chat_id, masterid) | ||
| else: | ||
| # We nee to check database to see if we've any slave | ||
| sql = "SELECT id from config where key='link' and value='%s'" % chat_id | ||
| sql = f"SELECT id from config where key='link' and value='{chat_id}'" |
There was a problem hiding this comment.
Function chanshowslave refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression) - Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring)
| """ | ||
| triggers = ["^/alias"] | ||
| return triggers | ||
| return ["^/alias"] |
There was a problem hiding this comment.
Function init refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
| def run(message): # do not edit this line | ||
| def run(message): # do not edit this line |
There was a problem hiding this comment.
Function run refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression) - Replace a[0:x] with a[:x] and a[x:len(a)] with a[x:] (
remove-redundant-slice-index)
| """ | ||
| triggers = ["^/quit"] | ||
| return triggers | ||
| return ["^/quit"] |
There was a problem hiding this comment.
Function init refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
| def run(message): # do not edit this line | ||
| def run(message): # do not edit this line |
There was a problem hiding this comment.
Function run refactored with the following changes:
- Replace a[0:x] with a[:x] and a[x:len(a)] with a[x:] (
remove-redundant-slice-index)
| """ | ||
| triggers = ["^/cn"] | ||
| return triggers | ||
| return ["^/cn"] |
There was a problem hiding this comment.
Function init refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
stampy/plugin/chuck.py
Outdated
| def run(message): # do not edit this line | ||
| def run(message): # do not edit this line | ||
| """ | ||
| Executes plugin | ||
| :param message: message to run against | ||
| :return: | ||
| """ | ||
| text = stampy.stampy.getmsgdetail(message)["text"] | ||
| if text: | ||
| if text := stampy.stampy.getmsgdetail(message)["text"]: |
There was a problem hiding this comment.
Function run refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression)
| def help(message): # do not edit this line | ||
| def help(message): # do not edit this line | ||
| """ | ||
| Returns help for plugin | ||
| :param message: message to process | ||
| :return: help text | ||
| """ | ||
| commandtext = _("Use `/cn <word>` to get a random Chuck Norris quote based on word\n\n") | ||
| return commandtext | ||
| return _( | ||
| "Use `/cn <word>` to get a random Chuck Norris quote based on word\n\n" | ||
| ) |
There was a problem hiding this comment.
Function help refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
| logger.debug(msg=_L("Command: %s by %s" % (texto, who_un))) | ||
| logger.debug(msg=_L(f"Command: {texto} by {who_un}")) |
There was a problem hiding this comment.
Function cn refactored with the following changes:
- Replace interpolated string formatting with f-string [×2] (
replace-interpolation-with-fstring) - Replace if statement with if expression (
assign-if-exp)
| triggers = ["*"] | ||
|
|
||
| return triggers | ||
| return ["*"] |
There was a problem hiding this comment.
Function init refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
| def run(message): # do not edit this line | ||
| def run(message): # do not edit this line |
There was a problem hiding this comment.
Function run refactored with the following changes:
- Use f-string instead of string concatenation [×4] (
use-fstring-for-concatenation) - Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring)
| def help(message): # do not edit this line | ||
| def help(message): # do not edit this line | ||
| """ | ||
| Returns help for plugin | ||
| :param message: message to process | ||
| :return: help text | ||
| """ | ||
| commandtext = "" | ||
| return commandtext | ||
| return "" |
There was a problem hiding this comment.
Function help refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
|
|
||
| for comic in getcomics(): | ||
| triggers.extend(["/%s" % comic]) | ||
| triggers.extend([f"/{comic}"]) |
There was a problem hiding this comment.
Function init refactored with the following changes:
- Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring)
|
|
||
| triggers = ["^/espp"] | ||
| return triggers | ||
| return ["^/espp"] |
There was a problem hiding this comment.
Function init refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
| def run(message): # do not edit this line | ||
| def run(message): # do not edit this line | ||
| """ | ||
| Executes plugin | ||
| :param message: message to run against | ||
| :return: | ||
| """ | ||
| text = stampy.stampy.getmsgdetail(message)["text"] | ||
| if text: | ||
| if text.split()[0].lower()[0:5] == "/espp": | ||
| if text := stampy.stampy.getmsgdetail(message)["text"]: | ||
| if text.split()[0].lower()[:5] == "/espp": |
There was a problem hiding this comment.
Function run refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression) - Replace a[0:x] with a[:x] and a[x:len(a)] with a[x:] (
remove-redundant-slice-index)
| def help(message): # do not edit this line | ||
| def help(message): # do not edit this line | ||
| """ | ||
| Returns help for plugin | ||
| :param message: message to process | ||
| :return: help text | ||
| """ | ||
| commandtext = _("Use `/espp <amount>` to get estimated ESPP earnings\n\n") | ||
| return commandtext | ||
| return _("Use `/espp <amount>` to get estimated ESPP earnings\n\n") |
There was a problem hiding this comment.
Function help refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
stampy/plugin/espp.py
Outdated
| initial = float(stampy.plugin.config.gconfig(key="espp", default=False, gid=chat_id)) | ||
| if initial: | ||
| if initial := float( | ||
| stampy.plugin.config.gconfig(key="espp", default=False, gid=chat_id) | ||
| ): |
There was a problem hiding this comment.
Function espp refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression) - Replace if statement with if expression (
assign-if-exp) - Replace comparison with min/max call (
min-max-identity)
| triggers = ["^/feed"] | ||
|
|
||
| if botname == 'redken_bot': | ||
| stampy.stampy.cronme(name="feed", interval=5) | ||
|
|
||
| return triggers | ||
| return ["^/feed"] |
There was a problem hiding this comment.
Function init refactored with the following changes:
- Move assignment closer to its usage within a block (
move-assign-in-block) - Inline variable that is immediately returned (
inline-immediately-returned-variable)
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: