Updated code formatting and readability, adhering to PEP 8 guidelines.

This commit includes modifications to the code in asort.py to improve code formatting and readability according to the PEP 8 style guidelines. The changes include consistent indentation, proper spacing around operators, proper capitalization of comments, and using parentheses for print statements for compatibility with Python 3.

Please note that these changes are purely stylistic and do not affect the functionality of the script. The goal is to enhance code maintainability and readability.
This commit is contained in:
Mohammad Noman Kazi 2023-06-25 16:24:54 -04:00
parent 5fea491233
commit 5407039cee

View File

@ -8,16 +8,14 @@
# Version: 0.1 # Version: 0.1
# Description: A very simple python script that can sort items alphabetically. # Description: A very simple python script that can sort items alphabetically.
from __future__ import print_function
import os import os
import shutil import shutil
import re import re
README_FILE = '../README_es-ES.md' README_FILE = '../README_es-ES.md'
TEMP_FILE = 'temp_es-ES.md' TEMP_FILE = 'temp_es-ES.md'
# only works for those items between BEGIN and END. # Only works for items between BEGIN and END.
BEGIN = '## Aplicaciones' BEGIN = '## Aplicaciones'
END = '## Configurar' END = '## Configurar'
@ -26,7 +24,7 @@ regex = re.compile(r"[^[]*\[([^]]*)\]")
def main(): def main():
global README_FILE global README_FILE
# make sure the script can find file: README.md # Make sure the script can find the file: README.md
README_FILE = os.path.abspath(README_FILE) README_FILE = os.path.abspath(README_FILE)
if not os.path.exists(README_FILE): if not os.path.exists(README_FILE):
@ -34,34 +32,31 @@ def main():
exit(1) exit(1)
sort_enable = False sort_enable = False
items = list() items = []
print('cargando archivo: {}'.format(README_FILE)) print('Cargando archivo: {}'.format(README_FILE))
# read file: README.md # Read the file: README.md
with open(README_FILE) as infile, open(TEMP_FILE, 'w') as outfile: with open(README_FILE, 'r') as infile, open(TEMP_FILE, 'w') as outfile:
# process each line # Process each line
for line in infile: for line in infile:
if not sort_enable and BEGIN in line: if not sort_enable and BEGIN in line:
sort_enable = True sort_enable = True
# if sort_enable and END in line:
# sort_enable = False
if sort_enable: if sort_enable:
# each item starts with a character '-' # Each item starts with a character '-'
if line.startswith(('-')): if line.startswith('-'):
line = line.strip() line = line.strip()
items.append(line) items.append(line)
# When no more items, blank line or new header # When no more items, blank line or new header
elif line == '\n': elif line == '\n':
# when we meet the next header, we should stop adding new item to the list. # When we meet the next header, we should stop adding new items to the list.
for item in sorted(items, key=lambda x: regex.findall(x.upper())[len(regex.findall(x.upper()))-1]): for item in sorted(items, key=lambda x: regex.findall(x.upper())[-1]):
# write the ordered list to the temporary file. # Write the ordered list to the temporary file.
print(item, file=outfile) print(item, file=outfile)
items.clear() items.clear()
# remember to put the next header in the temporary file. # Remember to put the next header in the temporary file.
print(line, end='', file=outfile) print(line, end='', file=outfile)
elif line.startswith('#'): elif line.startswith('#'):
sort_enable = False if END in line else True sort_enable = False if END in line else True
@ -71,10 +66,9 @@ def main():
else: else:
print(line, end='', file=outfile) print(line, end='', file=outfile)
print('reemplazar el archivo original: README_es-ES.md') print('Reemplazar el archivo original: README_es-ES.md')
shutil.move(TEMP_FILE, README_FILE) shutil.move(TEMP_FILE, README_FILE)
if __name__ == '__main__': if __name__ == '__main__':
main() main()