-
Notifications
You must be signed in to change notification settings - Fork 0
Sourcery refactored master branch #2
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
base: master
Are you sure you want to change the base?
Conversation
| f0 = open(pathTo2+"/Genotype_fasta-headers-rename.log", "a+") # change appropriately how do you want to call log table | ||
| fname=glob.glob(pathTo1+"/assemblerNameGenotype*.fasta") # change appropriately pattern in your file naming | ||
| for i in range (len(fname)): | ||
| vname=fname[i].split("/") | ||
| vname=vname[len(vname)-1] | ||
| idname=re.sub('\.fasta$', '', vname) | ||
| f4 = open(fname[i], "r") | ||
| f5 = open(pathTo2+"/"+str(idname) + "_renamed.fasta", "w") | ||
| line4=f4.readline() | ||
| count=1 | ||
| while line4: | ||
| line4=str.rstrip(line4) | ||
| if '>' in line4: | ||
| line44=">"+str(idname)+"_"+str(count) | ||
| f5.write("%s\n" % (line44)) | ||
| f0.write("%s\t" % (line4)) | ||
| f0.write("%s\n" % (line44)) | ||
| count=count+1 | ||
| else: | ||
| f5.write("%s\n" % (line4)) | ||
| line4 = f4.readline() | ||
| f4.close() | ||
| f5.close() | ||
| f0.close() | ||
| with open(f"{pathTo2}/Genotype_fasta-headers-rename.log", "a+") as f0: | ||
| fname = glob.glob(f"{pathTo1}/assemblerNameGenotype*.fasta") | ||
| for item in fname: | ||
| vname = item.split("/") | ||
| vname=vname[len(vname)-1] | ||
| idname=re.sub('\.fasta$', '', vname) | ||
| with open(item, "r") as f4: | ||
| f5 = open(f"{pathTo2}/{str(idname)}_renamed.fasta", "w") | ||
| line4=f4.readline() | ||
| count=1 | ||
| while line4: | ||
| line4=str.rstrip(line4) | ||
| if '>' in line4: | ||
| line44 = f">{str(idname)}_{str(count)}" | ||
| f5.write("%s\n" % (line44)) | ||
| f0.write("%s\t" % (line4)) | ||
| f0.write("%s\n" % (line44)) | ||
| count=count+1 | ||
| else: | ||
| f5.write("%s\n" % (line4)) | ||
| line4 = f4.readline() | ||
| f5.close() |
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 12-35 refactored with the following changes:
- Use f-string instead of string concatenation [×8] (
use-fstring-for-concatenation) - Use
withwhen opening file to ensure closure [×2] (ensure-file-closed) - Replace index in for loop with direct reference (
for-index-replacement)
This removes the following comments ( why? ):
# change appropriately how do you want to call log table
# change appropriately pattern in your file naming
| def prRed(skk): print("\033[91m {}\033[00m" .format(skk)) | ||
| def prGreen(skk): print("\033[92m {}\033[00m" .format(skk)) | ||
| def prYellow(skk): print("\033[93m {}\033[00m" .format(skk)) | ||
| def prLightPurple(skk): print("\033[94m {}\033[00m" .format(skk)) | ||
| def prPurple(skk): print("\033[95m {}\033[00m" .format(skk)) | ||
| def prCyan(skk): print("\033[96m {}\033[00m" .format(skk)) | ||
| def prLightGray(skk): print("\033[97m {}\033[00m" .format(skk)) | ||
| def prBlack(skk): print("\033[98m {}\033[00m" .format(skk)) | ||
| def prBlue(skk): print("\033[94m {}\033[00m" .format(skk)) | ||
| def prRed(skk): | ||
| print(f"\033[91m {skk}\033[00m") |
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 prRed refactored with the following changes:
- Replace call to format with f-string (
use-fstring-for-formatting)
| def prGreen(skk): print("\033[92m {}\033[00m" .format(skk)) | ||
| def prYellow(skk): print("\033[93m {}\033[00m" .format(skk)) | ||
| def prLightPurple(skk): print("\033[94m {}\033[00m" .format(skk)) | ||
| def prPurple(skk): print("\033[95m {}\033[00m" .format(skk)) | ||
| def prCyan(skk): print("\033[96m {}\033[00m" .format(skk)) | ||
| def prLightGray(skk): print("\033[97m {}\033[00m" .format(skk)) | ||
| def prBlack(skk): print("\033[98m {}\033[00m" .format(skk)) | ||
| def prBlue(skk): print("\033[94m {}\033[00m" .format(skk)) | ||
| def prRed(skk): | ||
| print(f"\033[91m {skk}\033[00m") | ||
| def prGreen(skk): | ||
| print(f"\033[92m {skk}\033[00m") |
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 prGreen refactored with the following changes:
- Replace call to format with f-string (
use-fstring-for-formatting)
| def prYellow(skk): print("\033[93m {}\033[00m" .format(skk)) | ||
| def prLightPurple(skk): print("\033[94m {}\033[00m" .format(skk)) | ||
| def prPurple(skk): print("\033[95m {}\033[00m" .format(skk)) | ||
| def prCyan(skk): print("\033[96m {}\033[00m" .format(skk)) | ||
| def prLightGray(skk): print("\033[97m {}\033[00m" .format(skk)) | ||
| def prBlack(skk): print("\033[98m {}\033[00m" .format(skk)) | ||
| def prBlue(skk): print("\033[94m {}\033[00m" .format(skk)) | ||
| def prRed(skk): | ||
| print(f"\033[91m {skk}\033[00m") | ||
| def prGreen(skk): | ||
| print(f"\033[92m {skk}\033[00m") | ||
| def prYellow(skk): | ||
| print(f"\033[93m {skk}\033[00m") |
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 prYellow refactored with the following changes:
- Replace call to format with f-string (
use-fstring-for-formatting)
| def prLightPurple(skk): print("\033[94m {}\033[00m" .format(skk)) | ||
| def prPurple(skk): print("\033[95m {}\033[00m" .format(skk)) | ||
| def prCyan(skk): print("\033[96m {}\033[00m" .format(skk)) | ||
| def prLightGray(skk): print("\033[97m {}\033[00m" .format(skk)) | ||
| def prBlack(skk): print("\033[98m {}\033[00m" .format(skk)) | ||
| def prBlue(skk): print("\033[94m {}\033[00m" .format(skk)) | ||
| def prRed(skk): | ||
| print(f"\033[91m {skk}\033[00m") | ||
| def prGreen(skk): | ||
| print(f"\033[92m {skk}\033[00m") | ||
| def prYellow(skk): | ||
| print(f"\033[93m {skk}\033[00m") | ||
| def prLightPurple(skk): | ||
| print(f"\033[94m {skk}\033[00m") |
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 prLightPurple refactored with the following changes:
- Replace call to format with f-string (
use-fstring-for-formatting)
| def prPurple(skk): print("\033[95m {}\033[00m" .format(skk)) | ||
| def prCyan(skk): print("\033[96m {}\033[00m" .format(skk)) | ||
| def prLightGray(skk): print("\033[97m {}\033[00m" .format(skk)) | ||
| def prBlack(skk): print("\033[98m {}\033[00m" .format(skk)) | ||
| def prBlue(skk): print("\033[94m {}\033[00m" .format(skk)) | ||
| def prRed(skk): | ||
| print(f"\033[91m {skk}\033[00m") | ||
| def prGreen(skk): | ||
| print(f"\033[92m {skk}\033[00m") | ||
| def prYellow(skk): | ||
| print(f"\033[93m {skk}\033[00m") | ||
| def prLightPurple(skk): | ||
| print(f"\033[94m {skk}\033[00m") | ||
| def prPurple(skk): | ||
| print(f"\033[95m {skk}\033[00m") |
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 prPurple refactored with the following changes:
- Replace call to format with f-string (
use-fstring-for-formatting)
| def prCyan(skk): print("\033[96m {}\033[00m" .format(skk)) | ||
| def prLightGray(skk): print("\033[97m {}\033[00m" .format(skk)) | ||
| def prBlack(skk): print("\033[98m {}\033[00m" .format(skk)) | ||
| def prBlue(skk): print("\033[94m {}\033[00m" .format(skk)) | ||
| def prRed(skk): | ||
| print(f"\033[91m {skk}\033[00m") | ||
| def prGreen(skk): | ||
| print(f"\033[92m {skk}\033[00m") | ||
| def prYellow(skk): | ||
| print(f"\033[93m {skk}\033[00m") | ||
| def prLightPurple(skk): | ||
| print(f"\033[94m {skk}\033[00m") | ||
| def prPurple(skk): | ||
| print(f"\033[95m {skk}\033[00m") | ||
| def prCyan(skk): | ||
| print(f"\033[96m {skk}\033[00m") |
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 prCyan refactored with the following changes:
- Replace call to format with f-string (
use-fstring-for-formatting)
| def prLightGray(skk): print("\033[97m {}\033[00m" .format(skk)) | ||
| def prBlack(skk): print("\033[98m {}\033[00m" .format(skk)) | ||
| def prBlue(skk): print("\033[94m {}\033[00m" .format(skk)) | ||
| def prRed(skk): | ||
| print(f"\033[91m {skk}\033[00m") | ||
| def prGreen(skk): | ||
| print(f"\033[92m {skk}\033[00m") | ||
| def prYellow(skk): | ||
| print(f"\033[93m {skk}\033[00m") | ||
| def prLightPurple(skk): | ||
| print(f"\033[94m {skk}\033[00m") | ||
| def prPurple(skk): | ||
| print(f"\033[95m {skk}\033[00m") | ||
| def prCyan(skk): | ||
| print(f"\033[96m {skk}\033[00m") | ||
| def prLightGray(skk): | ||
| print(f"\033[97m {skk}\033[00m") |
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 prLightGray refactored with the following changes:
- Replace call to format with f-string (
use-fstring-for-formatting)
| def prBlack(skk): print("\033[98m {}\033[00m" .format(skk)) | ||
| def prBlue(skk): print("\033[94m {}\033[00m" .format(skk)) | ||
| def prRed(skk): | ||
| print(f"\033[91m {skk}\033[00m") | ||
| def prGreen(skk): | ||
| print(f"\033[92m {skk}\033[00m") | ||
| def prYellow(skk): | ||
| print(f"\033[93m {skk}\033[00m") | ||
| def prLightPurple(skk): | ||
| print(f"\033[94m {skk}\033[00m") | ||
| def prPurple(skk): | ||
| print(f"\033[95m {skk}\033[00m") | ||
| def prCyan(skk): | ||
| print(f"\033[96m {skk}\033[00m") | ||
| def prLightGray(skk): | ||
| print(f"\033[97m {skk}\033[00m") | ||
| def prBlack(skk): | ||
| print(f"\033[98m {skk}\033[00m") |
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 prBlack refactored with the following changes:
- Replace call to format with f-string (
use-fstring-for-formatting)
| def prBlue(skk): print("\033[94m {}\033[00m" .format(skk)) | ||
| def prRed(skk): | ||
| print(f"\033[91m {skk}\033[00m") | ||
| def prGreen(skk): | ||
| print(f"\033[92m {skk}\033[00m") | ||
| def prYellow(skk): | ||
| print(f"\033[93m {skk}\033[00m") | ||
| def prLightPurple(skk): | ||
| print(f"\033[94m {skk}\033[00m") | ||
| def prPurple(skk): | ||
| print(f"\033[95m {skk}\033[00m") | ||
| def prCyan(skk): | ||
| print(f"\033[96m {skk}\033[00m") | ||
| def prLightGray(skk): | ||
| print(f"\033[97m {skk}\033[00m") | ||
| def prBlack(skk): | ||
| print(f"\033[98m {skk}\033[00m") | ||
| def prBlue(skk): | ||
| print(f"\033[94m {skk}\033[00m") |
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 prBlue refactored with the following changes:
- Replace call to format with f-string (
use-fstring-for-formatting)
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 two Python scripts, aiming to improve code readability and maintainability. It applies context management for file operations and updates string formatting to use f-strings.
General PR suggestions
- Ensure that all file operations are performed using context managers (
withstatements) to guarantee that files are properly closed after their operations are complete, especially in the case of exceptions. - Precompile regular expressions that are used within loops to enhance performance, as the pattern does not change during iterations.
- When using f-strings, avoid unnecessary calls to
str()for variables, as f-strings automatically handle the conversion to strings. - Consider using built-in path manipulation functions like
os.path.basenamefor extracting file names from paths to make the code more robust and platform-independent. - Review the consistency of error handling and resource management across the codebase to ensure that the refactoring maintains or improves the current standards.
Your trial expires on December 19, 2023. Please email [email protected] to continue using Sourcery ✨
| vname=vname[len(vname)-1] | ||
| idname=re.sub('\.fasta$', '', vname) | ||
| with open(item, "r") as f4: | ||
| f5 = open(f"{pathTo2}/{str(idname)}_renamed.fasta", "w") |
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 opened as 'f5' should be managed using a 'with' statement to ensure it is properly closed even if an error occurs.
| f5 = open(f"{pathTo2}/{str(idname)}_renamed.fasta", "w") | |
| + with open(f"{pathTo2}/{str(idname)}_renamed.fasta", "w") as f5: |
| for item in fname: | ||
| vname = item.split("/") | ||
| vname=vname[len(vname)-1] | ||
| idname=re.sub('\.fasta$', '', vname) |
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): Consider precompiling the regular expression outside of the loop for better performance, as it does not change per iteration.
| idname=re.sub('\.fasta$', '', vname) | |
| Outside the loop: pattern = re.compile('\\.fasta$') |
| while line4: | ||
| line4=str.rstrip(line4) | ||
| if '>' in line4: | ||
| line44 = f">{str(idname)}_{str(count)}" |
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 'str' function is redundant when using formatted string literals (f-strings).
| line44 = f">{str(idname)}_{str(count)}" | |
| + line44 = f">{idname}_{count}" |
| fname = glob.glob(f"{pathTo1}/assemblerNameGenotype*.fasta") | ||
| for item in fname: | ||
| vname = item.split("/") | ||
| vname=vname[len(vname)-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.
suggestion (llm): Using 'os.path.basename' would be a more robust method for extracting the file name from a path.
| vname=vname[len(vname)-1] | |
| + vname = os.path.basename(item) |
Branch
masterrefactored 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
masterbranch, then run:Help us improve this pull request!