-
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
if args[0] == "on": | ||
debugon() | ||
logging.getLogger().setLevel(logging.DEBUG) | ||
logger.remove() | ||
logger.add(sys.stdout, level="INFO") | ||
logger.info("输出等级设置为 DEBUG.") | ||
return await matcher.send( | ||
manager.process_generic_event("DebugOn", event=event) | ||
) | ||
else: | ||
if args[0] != "on": | ||
return await matcher.send("错误, 我无法解析你的指令.") | ||
debugon() | ||
logging.getLogger().setLevel(logging.DEBUG) | ||
logger.remove() | ||
logger.add(sys.stdout, level="INFO") | ||
logger.info("输出等级设置为 DEBUG.") | ||
return await matcher.send( | ||
manager.process_generic_event("DebugOn", event=event) | ||
) |
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 debughandler
refactored with the following changes:
- Swap if/else branches (
swap-if-else-branches
) - Remove unnecessary else after guard condition (
remove-unnecessary-else
)
arg = list(filter(None, args.split(" "))) | ||
|
||
if len(arg) >= 1: | ||
if arg := list(filter(None, args.split(" "))): |
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 superuser_handler
refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression
) - Simplify sequence length comparison (
simplify-len-comparison
) - Merge else clause's nested if statement into elif (
merge-else-if-into-elif
) - Swap if/else branches (
swap-if-else-branches
) - Simplify logical expression using De Morgan identities (
de-morgan
)
reply = "从商店搜索到以下插件:\n" | ||
reply += "Noctisynth 官方插件:\n" | ||
|
||
reply = "从商店搜索到以下插件:\n" + "Noctisynth 官方插件:\n" |
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 bothandler
refactored with the following changes:
- Replace assignment and augmented assignment with single assignment (
merge-assign-and-aug-assign
)
got = messages.get(args) | ||
if got: | ||
if got := messages.get(args): | ||
return got | ||
|
||
for key, alias in messages.keys.items(): | ||
relation = [] | ||
for alia in alias: | ||
if alia: | ||
relation.append(similar(alia, args)) | ||
|
||
relation = [similar(alia, args) for alia in alias if alia] |
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 help_message
refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression
) - Inline variable that is immediately returned (
inline-immediately-returned-variable
) - Move assignment closer to its usage within a block (
move-assign-in-block
) - Replace manual loop counter with call to enumerate (
convert-to-enumerate
) - Use str.join() instead of for loop (
use-join
) - Convert for loop into list comprehension (
list-comprehension
)
if len(args) == 0: | ||
if not args: |
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 DefaultRA.__call__
refactored with the following changes:
- Simplify sequence length comparison (
simplify-len-comparison
) - Remove redundant conditional (
remove-redundant-if
) - Merge append into list declaration (
merge-list-append
) - Inline variable that is immediately returned (
inline-immediately-returned-variable
)
if "official" in result.keys(): | ||
official = result["official"] | ||
else: | ||
official = {} | ||
|
||
return official | ||
return result["official"] if "official" in result.keys() else {} |
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 get_official_plugins
refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp
) - Inline variable that is immediately returned (
inline-immediately-returned-variable
)
if "community" in result.keys(): | ||
community = result["community"] | ||
else: | ||
community = {} | ||
|
||
return community | ||
return result["community"] if "community" in result.keys() else {} |
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 get_community_plugins
refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp
) - Inline variable that is immediately returned (
inline-immediately-returned-variable
)
for name, method in vars(templates).items(): | ||
for method in vars(templates).values(): |
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 _load_template_methods
refactored with the following changes:
- Replace calls to
dict.items
withdict.values
when the keys are not used (replace-dict-items-with-values
)
logger.error(f"请确保您的回复配置文件包含了正确的键和相应的值。如果您不确定如何正确配置文件,请参考文档或向管理员寻求帮助。") | ||
logger.error("请确保您的回复配置文件包含了正确的键和相应的值。如果您不确定如何正确配置文件,请参考文档或向管理员寻求帮助。") |
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 _load_reply_file
refactored with the following changes:
- Replace f-string with no interpolated values with string (
remove-redundant-fstring
)
logger.error(f"初始化回复配置时出现异常。") | ||
logger.error("初始化回复配置时出现异常。") |
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 init_yaml_file
refactored with the following changes:
- Replace f-string with no interpolated values with string (
remove-redundant-fstring
)
if response and is_enable: | ||
result = self._handle_generic_event(response, **kwargs) | ||
if result: | ||
return result | ||
else: | ||
break | ||
if response and is_enable: | ||
result = self._handle_generic_event(response, **kwargs) | ||
if result: | ||
return result | ||
else: | ||
break |
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 ReplyRegistryManager.process_generic_event
refactored with the following changes:
- Hoist conditional out of nested conditional (
hoist-if-from-if
)
if len(result) == 0: | ||
return None | ||
return result | ||
return None if len(result) == 0 else result |
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 ReplyRegistryManager.process_message_event
refactored with the following changes:
- Lift code into else after jump in control flow (
reintroduce-else
) - Replace if statement with if expression (
assign-if-exp
)
logger.error(f"请确保您的回复配置文件包含了正确的键和相应的值。如果您不确定如何正确配置文件,请参考文档或向管理员寻求帮助。") | ||
logger.error("请确保您的回复配置文件包含了正确的键和相应的值。如果您不确定如何正确配置文件,请参考文档或向管理员寻求帮助。") |
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 ReplyRegistry.disable_event
refactored with the following changes:
- Replace f-string with no interpolated values with string (
remove-redundant-fstring
)
logger.error(f"请确保您的回复配置文件包含了正确的键和相应的值。如果您不确定如何正确配置文件,请参考文档或向管理员寻求帮助。") | ||
logger.error("请确保您的回复配置文件包含了正确的键和相应的值。如果您不确定如何正确配置文件,请参考文档或向管理员寻求帮助。") |
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 ReplyRegistry.enable_event
refactored with the following changes:
- Replace f-string with no interpolated values with string (
remove-redundant-fstring
)
logger.error(f"请确保您的回复配置文件包含了正确的键和相应的值。如果您不确定如何正确配置文件,请参考文档或向管理员寻求帮助。") | ||
logger.error("请确保您的回复配置文件包含了正确的键和相应的值。如果您不确定如何正确配置文件,请参考文档或向管理员寻求帮助。") |
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 ReplyRegistry.toggle
refactored with the following changes:
- Replace f-string with no interpolated values with string (
remove-redundant-fstring
)
if split[0]: | ||
self.a = int(split[0]) | ||
else: | ||
self.a = 1 | ||
|
||
self.a = int(split[0]) if split[0] else 1 |
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 PunishDice.parse
refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp
)
for roll_string in roll_strings.keys(): | ||
for roll_string in roll_strings: |
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 344-344
refactored with the following changes:
- Remove unnecessary call to keys() (
remove-dict-keys
)
if judge: | ||
self.judge = self.judge_dict[judge] | ||
else: | ||
self.judge = None | ||
self.judge = self.judge_dict[judge] if judge else 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 Docimasy.__init__
refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp
)
if self.judge > 0: | ||
return True | ||
else: | ||
return False | ||
return self.judge > 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 Docimasy.__bool__
refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp
) - Simplify boolean if expression (
boolean-if-exp-identity
) - Remove unnecessary casts to int, str, float or bool (
remove-unnecessary-cast
)
toadd = toadd.lstrip("\n") | ||
if toadd: | ||
if self.detail: | ||
self.detail = self.detail.strip("\n") + "\n" + toadd | ||
else: | ||
self.detail = toadd | ||
if toadd := toadd.lstrip("\n"): | ||
self.detail = self.detail.strip("\n") + "\n" + toadd if self.detail else toadd |
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 Docimasy.__add__
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
)
message = str(message).lower() if lower else str(message) | ||
message = message.lower() if lower else message | ||
msg = translate_punctuation( | ||
re.sub("\s+", " ", re.sub(regex, "", message)).strip(" ") | ||
) | ||
if msg.startswith("/"): | ||
msg = "." + msg[1:] | ||
msg = f".{msg[1:]}" |
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 format_str
refactored with the following changes:
- Remove unnecessary casts to int, str, float or bool [×2] (
remove-unnecessary-cast
) - Use f-string instead of string concatenation (
use-fstring-for-concatenation
)
mentions = [] | ||
try: | ||
message = json.loads(event.json())["message"] | ||
except KeyError: | ||
return [] | ||
|
||
for mention in message: | ||
if mention["type"] == "at": | ||
mentions.append(mention["data"]["qq"]) | ||
|
||
return mentions | ||
return [ | ||
mention["data"]["qq"] for mention in message if mention["type"] == "at" | ||
] |
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 get_mentions
refactored with the following changes:
- Move assignment closer to its usage within a block (
move-assign-in-block
) - Convert for loop into list comprehension (
list-comprehension
) - Inline variable that is immediately returned (
inline-immediately-returned-variable
)
for _, obj in vars(main).items(): | ||
for obj in vars(main).values(): |
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 get_handlers
refactored with the following changes:
- Replace calls to
dict.items
withdict.values
when the keys are not used (replace-dict-items-with-values
)
if raw_json["card"]: | ||
return raw_json["card"] | ||
else: | ||
return raw_json["nickname"] | ||
return raw_json["card"] if raw_json["card"] else raw_json["nickname"] | ||
except: | ||
from .plugins import modes | ||
|
||
cards = modes[get_mode(event)].__cards__ | ||
got = cards.get(event, qid=get_user_id(event)) | ||
if got: | ||
if got := cards.get(event, qid=get_user_id(event)): |
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 get_user_card
refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp
) - Use named expression to simplify assignment and conditional (
use-named-expression
)
got = cards.get(event, qid=get_user_id(event)) | ||
if got: | ||
if got := cards.get(event, qid=get_user_id(event)): |
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 get_user_nickname
refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression
)
if not path.exists(): | ||
return "欧若可" | ||
|
||
return path.open(mode="r").read() | ||
return "欧若可" if not path.exists() else path.open(mode="r").read() |
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 get_name
refactored with the following changes:
- Lift code into else after jump in control flow (
reintroduce-else
) - Replace if statement with if expression (
assign-if-exp
)
status_text = BOT_STATUS_FILE.read_text(encoding="utf-8") | ||
if status_text: | ||
if status_text := BOT_STATUS_FILE.read_text(encoding="utf-8"): |
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 load_status
refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression
)
iter_args = [arg for arg in args] | ||
iter_args = list(args) |
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 CommandParser.shlex
refactored with the following changes:
- Replace identity comprehension with call to collection constructor (
identity-comprehension
) - Merge else clause's nested if statement into elif (
merge-else-if-into-elif
) - Lift code into else after jump in control flow (
reintroduce-else
) - Simplify sequence length comparison (
simplify-len-comparison
)
else: | ||
if module.__type__ == "plugin": | ||
module_type = "插件" | ||
elif module.__type__ == "library": | ||
module_type = "库" | ||
|
||
if hasattr(module, "__nbcommands__"): | ||
commands: dict = module.__nbcommands__ | ||
else: | ||
commands = {} | ||
if module.__type__ == "library": | ||
module_type = "库" | ||
|
||
elif module.__type__ == "plugin": | ||
module_type = "插件" | ||
commands = module.__nbcommands__ if hasattr(module, "__nbcommands__") else {} | ||
if commands and not hasattr(module, "__nbhandler__"): | ||
logger.error(f"{module_type} {folder.name} 配置异常, 导入失败.") | ||
continue | ||
|
||
if hasattr(module, "__nbhandler__"): | ||
handlers = module.__nbhandler__ | ||
else: | ||
handlers = {} | ||
|
||
handlers = module.__nbhandler__ if hasattr(module, "__nbhandler__") else {} |
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 modules
refactored with the following changes:
- Swap if/else branches [×2] (
swap-if-else-branches
) - Remove unnecessary else after guard condition (
remove-unnecessary-else
) - Simplify conditional into switch-like form [×9] (
switch
) - Replace if statement with if expression [×2] (
assign-if-exp
)
if old_length == 5: | ||
old_pre_release = old_tuple.group(4)[0] | ||
old_pre_version = int(old_tuple.group(5)) | ||
elif old_length == 3: | ||
if old_length == 3: | ||
old_pre_release = "s" | ||
old_pre_version = 1 | ||
elif old_length == 5: | ||
old_pre_release = old_tuple.group(4)[0] | ||
old_pre_version = int(old_tuple.group(5)) | ||
else: | ||
return False | ||
|
||
if new_length == 5: | ||
new_pre_release = new_tuple.group(4)[0] | ||
new_pre_version = int(new_tuple.group(5)) | ||
elif new_length == 3: | ||
if new_length == 3: | ||
new_pre_release = "s" | ||
new_pre_version = 1 |
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 require_update
refactored with the following changes:
- Simplify conditional into switch-like form [×7] (
switch
) - Merge duplicate blocks in conditional [×2] (
merge-duplicate-blocks
) - Remove redundant conditional [×2] (
remove-redundant-if
)
e4f4ef0
to
007c882
Compare
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!