Skip to content

Commit

Permalink
Improve variable naming and typing
Browse files Browse the repository at this point in the history
  • Loading branch information
sunarch committed Jul 2, 2023
1 parent 8fb2484 commit 489b2f4
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 68 deletions.
42 changes: 21 additions & 21 deletions src/libmonty_hexdump/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def main() -> None:

arguments.create_arguments(parser)

args = parser.parse_args()
args: Namespace = parser.parse_args()

if args.version:
print(f'{version.PROGRAM_NAME} {version.__version__}')
Expand All @@ -58,14 +58,14 @@ def main_lib(args: Namespace) -> None:

try:
stream, char_converter = arguments.stream(args.stream)
i_bytes_per_line = arguments.bytes_per_line(args.bytes_per_line)
i_sleep = arguments.sleep(args.sleep)
index_converter = arguments.index_converter(args.index_format)
bytes_per_line: int = arguments.bytes_per_line(args.bytes_per_line)
sleep_seconds: float = arguments.sleep(args.sleep)
index_converter: Callable = arguments.index_converter(args.index_format)
except ValueError as exc:
raise ValueError from exc

try:
run(stream, index_converter, char_converter, i_bytes_per_line, i_sleep)
run(stream, index_converter, char_converter, bytes_per_line, sleep_seconds)
except ValueError as exc:
raise ValueError from exc

Expand All @@ -74,7 +74,7 @@ def run(stream: Callable = None,
index_converter: Callable = None,
char_converter: Callable = None,
bytes_per_line: int = 0,
sleep: float = 0.1
sleep_seconds: float = 0.1
) -> None:
"""Run"""

Expand All @@ -89,37 +89,37 @@ def run(stream: Callable = None,

print('')

i_extra_width = 0
extra_width: int = 0

if bytes_per_line <= 0:
i_cols = terminal.get_cols()
column_count: int = terminal.get_cols()

full_width = False
is_full_width: bool = False
if bytes_per_line <= -1:
full_width = True
is_full_width: bool = True

bytes_per_line = width.determine_count_per_line(i_cols, full_width)
bytes_per_line: int = width.determine_count_per_line(column_count, is_full_width)

if full_width:
i_extra_width = i_cols - width.min_line_length(bytes_per_line)
if is_full_width:
extra_width: int = column_count - width.min_line_length(bytes_per_line)

i_offset = 0
offset: int = 0

lines.print_header(bytes_per_line, index_converter, i_extra_width)
lines.print_header(bytes_per_line, index_converter, extra_width)
print('')

for b_unit in stream(bytes_per_line):
for bytes_unit in stream(bytes_per_line):

try:
lines.print_data(b_unit,
lines.print_data(bytes_unit,
bytes_per_line,
i_offset,
offset,
index_converter,
char_converter,
i_extra_width)
time.sleep(sleep)
extra_width)
time.sleep(sleep_seconds)

i_offset += bytes_per_line
offset += bytes_per_line

except KeyboardInterrupt:
break
Expand Down
33 changes: 17 additions & 16 deletions src/libmonty_hexdump/components/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"""

# imports: library
from typing import Tuple, Callable, Union
from argparse import ArgumentParser
from typing import Callable, Union

# imports: dependencies
from libmonty.formatting import char_str, number_str
Expand All @@ -18,7 +19,7 @@
from libmonty_hexdump.components import streams


def create_arguments(parser_hexer):
def create_arguments(parser_hexer: ArgumentParser) -> None:
"""Create arguments"""

parser_hexer.add_argument('-s', '--stream',
Expand All @@ -42,25 +43,25 @@ def create_arguments(parser_hexer):
dest='index_format')


def stream(source: Union[Callable, str]) -> Tuple[Callable, Callable]:
def stream(source: Union[Callable, str]) -> (Callable, Callable):
"""Stream source"""

if isinstance(source, Callable):
f_stream = source
f_char_converter = char_str.byte_to_compact_printable_with_dots
f_stream: Callable = source
f_char_converter: Callable = char_str.byte_to_compact_printable_with_dots

else:
if source == 'random':
f_stream = streams.random_data
f_char_converter = char_str.byte_to_compact_printable_with_dots
f_stream: Callable = streams.random_data
f_char_converter: Callable = char_str.byte_to_compact_printable_with_dots

else:
try:
f_stream = streams.create_from_file(source)
f_stream: Callable = streams.create_from_file(source)
except FileNotFoundError as exc:
raise ValueError(str(exc)) from exc

f_char_converter = char_str.byte_to_compact_printable_with_frames
f_char_converter: Callable = char_str.byte_to_compact_printable_with_frames

return f_stream, f_char_converter

Expand All @@ -77,7 +78,7 @@ def bytes_per_line(count: int) -> int:
def sleep(speed: Union[float, int, str]) -> float:
"""Sleep"""

d_speeds = {
speeds: dict[str, float] = {
'f': 0.01,
'fast': 0.01,
'm': 0.05,
Expand All @@ -89,15 +90,15 @@ def sleep(speed: Union[float, int, str]) -> float:
}

if isinstance(speed, int):
speed = float(speed)
speed: float = float(speed)

if isinstance(speed, (float, int)):
if speed <= 0:
raise ValueError

elif isinstance(speed, str):
try:
speed = d_speeds[speed]
speed: float = speeds[speed]
except KeyError:
print(f'Bad value for \'sleep\': \'{speed}\'')
speed = 0.01
Expand All @@ -109,7 +110,7 @@ def sleep(speed: Union[float, int, str]) -> float:
def index_converter(converter: Union[Callable, str]) -> Callable:
"""Index converter"""

d_index_formats = {
index_formats: dict[str, Callable] = {
'h': number_str.hexadecimal,
'hex': number_str.hexadecimal,
'hexadecimal': number_str.hexadecimal,
Expand All @@ -127,9 +128,9 @@ def index_converter(converter: Union[Callable, str]) -> Callable:
return converter

try:
return d_index_formats[converter]
return index_formats[converter]
except KeyError:
print(f'Value for index format not recognized: \'{converter}\'')
converter = 'hexadecimal'
converter: str = 'hexadecimal'
print(f'Using default value for index format: \'{converter}\'')
return d_index_formats[converter]
return index_formats[converter]
54 changes: 28 additions & 26 deletions src/libmonty_hexdump/components/lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,24 @@ def print_header(bytes_per_line: int,
) -> None:
"""Print header"""

s_counter = f'Offset ({index_converter(-1)})'
s_line = f' {s_counter:^{COUNTER_DIGITS + extra_width}} '
counter_text: str = f'Offset ({index_converter(-1)})'
line: str = f' {counter_text:^{COUNTER_DIGITS + extra_width}} '

try:
b_unit = bytes(range(bytes_per_line))
bytes_unit: bytes = bytes(range(bytes_per_line))
except ValueError:
if bytes_per_line > 256:
full = bytes(range(256)) * (bytes_per_line // 256)
fraction = bytes(range(bytes_per_line % 256))
b_unit = full + fraction
full: bytes = bytes(range(256)) * (bytes_per_line // 256)
fraction: bytes = bytes(range(bytes_per_line % 256))
bytes_unit: bytes = full + fraction
else:
raise

s_line += _part_bytes(b_unit, bytes_per_line, index_converter)
line += _part_bytes(bytes_unit, bytes_per_line, index_converter)

s_line += 'Decoded text'
line += 'Decoded text'

print(s_line, flush=True)
print(line, flush=True)


def print_data(b_unit: bytes,
Expand All @@ -54,14 +54,16 @@ def print_data(b_unit: bytes,
) -> None:
"""Print data"""

s = construct(b_unit,
bytes_per_line,
offset,
index_converter,
char_converter,
extra_width)
text: str = construct(
b_unit,
bytes_per_line,
offset,
index_converter,
char_converter,
extra_width
)

print(s, flush=True)
print(text, flush=True)


def construct(b_unit: bytes,
Expand All @@ -73,13 +75,13 @@ def construct(b_unit: bytes,
) -> str:
"""Construct"""

s_counter = _part_counter(offset, COUNTER_DIGITS + extra_width, index_converter)
counter_text: str = _part_counter(offset, COUNTER_DIGITS + extra_width, index_converter)

s_bytes = _part_bytes(b_unit, bytes_per_line, number_str.hexadecimal)
bytes_text: str = _part_bytes(b_unit, bytes_per_line, number_str.hexadecimal)

s_chars = _part_chars(b_unit, char_converter)
chars_text: str = _part_chars(b_unit, char_converter)

return s_counter + s_bytes + s_chars
return counter_text + bytes_text + chars_text


def _part_counter(offset: int = 0,
Expand All @@ -97,18 +99,18 @@ def _part_bytes(b_unit: bytes,
) -> str:
"""Part: bytes"""

s_bytes = ' '.join(map(lambda b: number_converter(b, 2), b_unit))
bytes_text: str = ' '.join(map(lambda b: number_converter(b, 2), b_unit))

if len(b_unit) < bytes_per_line:
s_format = '{:<' + str((bytes_per_line * 3) - 1) + '}'
s_bytes = s_format.format(s_bytes)
format_string: str = '{:<' + str((bytes_per_line * 3) - 1) + '}'
bytes_text: str = format_string.format(bytes_text)

return s_bytes + ' '
return bytes_text + ' '


def _part_chars(b_unit: bytes,
def _part_chars(bytes_unit: bytes,
char_converter: Callable = char_str.pseudo,
) -> str:
"""Part: chars"""

return ''.join(map(char_converter, b_unit))
return ''.join(map(char_converter, bytes_unit))
6 changes: 3 additions & 3 deletions src/libmonty_hexdump/components/width.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def determine_count_per_line(cols: int = 80,
def min_line_length(bytes_per_line: int) -> int:
"""Minimum line length"""

i_line = len(lines.construct(bytes(bytes_per_line), bytes_per_line))
i_line_end = 1
line_length: int = len(lines.construct(bytes(bytes_per_line), bytes_per_line))
line_end_character_offset: int = 1

return i_line + i_line_end
return line_length + line_end_character_offset
4 changes: 2 additions & 2 deletions src/libmonty_hexdump/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
"""General info
"""

PROGRAM_NAME = 'libmonty-hexdump'
PROGRAM_NAME: str = 'libmonty-hexdump'

__version__ = '0.1.1'
__version__: str = '0.1.1'

0 comments on commit 489b2f4

Please sign in to comment.