-
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 #2
base: master
Are you sure you want to change the base?
Conversation
f1 = open(param1, "r") | ||
a1 = f1.readlines() | ||
f2 = open(param2, "r") | ||
a2 = f2.readlines() | ||
f1.close() | ||
with open(param1, "r") as f1: | ||
a1 = f1.readlines() | ||
f2 = open(param2, "r") | ||
a2 = f2.readlines() | ||
f2.close() | ||
record_ids1 = list() | ||
record_ids2 = list() | ||
record_ids1 = [] | ||
record_ids2 = [] | ||
record_ids1 = list(map(str.strip, a1)) | ||
record_ids2 = list(map(str.strip, a2)) | ||
|
||
with open(param4, 'w') as outFile: | ||
for record in SeqIO.parse(param3, 'fasta'): | ||
# print(record.id) | ||
if record.id not in record_ids1: | ||
SeqIO.write(record, outFile, 'fasta') | ||
else: | ||
if record.id in record_ids1: |
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 13-41
refactored with the following changes:
- Use
with
when opening file to ensure closure (ensure-file-closed
) - Replace
list()
with[]
[×2] (list-literal
) - Swap if/else to remove empty if body (
remove-pass-body
) - Hoist repeated code outside conditional statement (
hoist-statement-from-if
)
This removes the following comments ( why? ):
# print(record.description)
# print(record)
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.
PR Type: Refactoring
Summary of PR: This PR introduces refactoring changes to a script that processes FASTA files, aiming to improve code readability and maintainability by using context managers for file operations and simplifying list initializations.
General PR suggestions
- Ensure that all file operations are consistently using context managers to guarantee that files are properly closed even if errors occur.
- Verify the logic changes related to the handling of record IDs are intentional and preserve the desired functionality of the script.
- Remove any redundant code, such as unnecessary variable reassignments or setting a variable to None before immediately reassigning it.
- Confirm that the changes in the behavior of writing records to the output file are correct and align with the expected outcomes of the script.
Your trial expires on December 19, 2023. Please email tim@sourcery.ai to continue using Sourcery ✨
f1.close() | ||
with open(param1, "r") as f1: | ||
a1 = f1.readlines() | ||
f2 = open(param2, "r") |
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.
issue (llm): The file 'param2' is opened without a context manager, which may lead to the file not being closed if an error occurs before the close call. It's recommended to use a 'with' statement for opening files to ensure they are properly closed.
f2 = open(param2, "r") | |
+ with open(param2, "r") as f2: |
f2.close() | ||
record_ids1 = list() | ||
record_ids2 = list() | ||
record_ids1 = [] |
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.
nitpick (llm): The reassignment of 'record_ids1' to an empty list is redundant since it's immediately re-populated with the stripped lines from 'a1'. The same applies to 'record_ids2'.
record_ids1 = [] | |
-record_ids1 = [] |
if record.id not in record_ids1: | ||
SeqIO.write(record, outFile, 'fasta') | ||
else: | ||
if record.id in record_ids1: |
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.
suggestion (llm): The logic has been inverted here, which changes the behavior of the script. Previously, records not in 'record_ids1' were written to the output file. Now, only records in 'record_ids1' are being modified and written. Ensure this is the intended behavior.
if record.id in record_ids1: | |
- if record.id not in record_ids1: |
# print(record.description) | ||
# print(record) | ||
SeqIO.write(record, outFile, 'fasta') | ||
SeqIO.write(record, outFile, 'fasta') |
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.
thought (llm): The 'SeqIO.write' function is now called outside of the conditional block, which means it will execute for every record. This is a change in behavior from the original code, where it was only called if the record ID was not in 'record_ids1'. Confirm that this change is intentional and that it won't cause any undesired side effects.
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!