Skip to content

Commit ae99579

Browse files
committed
sum: Refactor algorithm selection
1 parent 3b05327 commit ae99579

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

userland/utilities/sum.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ def sum_sysv(data: bytes) -> str:
2323
return f"{checksum:03} {-(len(data) // -512)}"
2424

2525

26-
SUM_ALGORITHMS = {"bsd": sum_bsd, "sysv": sum_sysv}
27-
2826
parser = core.ExtendedOptionParser(
2927
usage="%prog [OPTION] [FILE]...",
3028
)
@@ -33,15 +31,15 @@ def sum_sysv(data: bytes) -> str:
3331
"-r",
3432
dest="algorithm",
3533
action="store_const",
36-
const="bsd",
37-
default="bsd",
38-
help="use the BSD (16-bit) checksum algorithm (1KiB blocks)",
34+
const=sum_bsd,
35+
default=sum_bsd,
36+
help="use the BSD (16-bit) checksum algorithm (1KiB blocks) (default)",
3937
)
4038
parser.add_option(
4139
"-s",
4240
dest="algorithm",
4341
action="store_const",
44-
const="sysv",
42+
const=sum_sysv,
4543
help="use the System V sum algorithm (512B blocks)",
4644
)
4745

@@ -52,11 +50,11 @@ def python_userland_sum(opts, args: list[str]) -> int:
5250

5351
for name in args or ["-"]:
5452
if name == "-":
55-
print(SUM_ALGORITHMS[opts.algorithm](sys.stdin.buffer.read()))
53+
print(opts.algorithm(sys.stdin.buffer.read()))
5654
else:
5755
try:
5856
with open(name, "rb") as f:
59-
print(f"{SUM_ALGORITHMS[opts.algorithm](f.read())} {name}")
57+
print(f"{opts.algorithm(f.read())} {name}")
6058
except OSError as e:
6159
failed = True
6260
core.perror(e)

0 commit comments

Comments
 (0)