commit 070acc7bfdb6506603a58d7f35f53e56d8fb55ab
Author: José Matos <[email protected]>
Date: Thu Jul 17 22:17:04 2025 +0100
Simplify code
Keep the logic but simplify the code:
* There is no need to redirect the standard input and outputs.
* String split removes newlines so that is unneeded.
* Use string constants to avoid repeating the same code.
* Use chained comparison operator and use the appropriated values for
the number of spaces (that value is between 1 and 8).
* We are only concerned about the parity of the position and so use the
integer remainder operator, not the divmod since we do not use the
quotient.
* Use augmented assignments whenever possible (supported since Python
2.2).
* Use formatted strings (makes the code easier to read).
---
lib/scripts/fen2ascii.py | 48 ++++++++++++++++++------------------------------
1 file changed, 18 insertions(+), 30 deletions(-)
diff --git a/lib/scripts/fen2ascii.py b/lib/scripts/fen2ascii.py
index 3bbdbc4769..44239083d1 100644
--- a/lib/scripts/fen2ascii.py
+++ b/lib/scripts/fen2ascii.py
@@ -9,43 +9,31 @@
# This script will convert a chess position in the FEN
# format to an ascii representation of the position.
-import os
import sys
-os.close(0)
-os.close(1)
-sys.stdin = open(sys.argv[1])
-sys.stdout = open(sys.argv[2], "w")
+fout = open(sys.argv[2], "w")
-line = sys.stdin.readline()
-if line[-1] == "\n":
- line = line[:-1]
-
-line = line.split(" ")[0]
-comp = line.split("/")
+placement = open(sys.argv[1]).readline().split()[0]
+row_codes = placement.split("/")
cont = 1
-margin = " " * 6
+MARGIN = " " * 6
+RULE = f"+{'-' * 15}+"
-print(margin + " +" + "-" * 15 + "+")
+print(f"{MARGIN} {RULE}", file=fout)
for i in range(8):
- cont = cont + 1
- tmp = ""
- for j in comp[i]:
- if j >= "0" and j <= "9":
- for k in range(int(j)):
- cont = cont + 1
- x, mod = divmod(cont, 2)
- if mod:
- tmp = tmp + "| "
- else:
- tmp = tmp + "|*"
+ cont += 1
+ row = ""
+ for p in row_codes[i]:
+ if "1" <= p <= "8":
+ for k in range(int(p)):
+ cont += 1
+ row += "| " if (cont % 2) else "|*"
else:
- tmp = tmp + "|" + j
- cont = cont + 1
+ row += "|" + p
+ cont += 1
- row = 8 - i
- print(margin, row, tmp + "|")
+ print(f"{MARGIN} {8 - i} {row}|", file=fout)
-print(margin + " +" + "-" * 15 + "+")
-print(margin + " a b c d e f g h ")
+print(f"{MARGIN} {RULE}", file=fout)
+print(f"{MARGIN} a b c d e f g h ", file=fout)
--
lyx-cvs mailing list
[email protected]
https://lists.lyx.org/mailman/listinfo/lyx-cvs