Skip to content

Commit

Permalink
update to match changes in typedparser
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-ging committed Jan 12, 2024
1 parent b9dbfed commit 3e7a7ad
Show file tree
Hide file tree
Showing 13 changed files with 55 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
docs/
/dist*/

*.bat
*.h5
Expand Down
2 changes: 1 addition & 1 deletion src/packg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
from .misc import format_exception

__all__ = ["Const", "format_exception"]
__version__ = "0.7.1"
__version__ = "0.7.2"
2 changes: 1 addition & 1 deletion src/packg/autocomplete.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ _packg() {
_init_completion || return
# complete first argument with script
if [ $COMP_CWORD -eq 1 ]; then
opts="caching constclass debugging dtime iotools.compressed iotools.file_indexer iotools.file_reader iotools.git_root_finder iotools.git_status_checker iotools.jsonext iotools.jsonext_encoder iotools.misc iotools.numpyext iotools.pathspec_matcher iotools.tomlext iotools.yamlext log magic misc multiproc.multiproc_fn multiproc.multiproc_producer_consumer packaging paths run.cleanup run.create_autocomplete run.show_env stats strings strings.abbreviations strings.base64utils strings.create_strings strings.hasher strings.quote_urlparse system.systemcall testing.fixture_webserver testing.import_from_source testing.setup_tests tqdmu typext web"
opts="caching constclass debugging dtime iotools.compressed iotools.file_indexer iotools.file_reader iotools.git_root_finder iotools.git_status_checker iotools.jsonext iotools.jsonext_encoder iotools.misc iotools.numpyext iotools.pathspec_matcher iotools.tomlext iotools.yamlext log magic maths misc multiproc.multiproc_fn multiproc.multiproc_producer_consumer packaging paths run.cleanup run.create_autocomplete run.print_paths run.show_env stats strings.abbreviations strings.base64tools strings.hasher strings.quote_urlparse system.systemcall testing.fixture_webserver testing.import_from_source testing.setup_tests tqdmext typext web"
COMPREPLY=( $( compgen -W "${opts}" -- "${cur}") )
return 0
fi
Expand Down
6 changes: 6 additions & 0 deletions src/packg/iotools/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,9 @@ def format_b_in_mb(size_b: int) -> str:

def format_b_in_gb(size_b: int) -> str:
return f"{size_b / 1024 ** 3:.3f}GB"


def format_bytes_human_readable(size_b: int) -> str:
for scale, unit in enumerate(["B", "KB", "MB", "GB", "TB"]):
if size_b < 1024 ** (scale + 1):
return f"{size_b / 1024 ** scale:.3f}{unit}"
1 change: 0 additions & 1 deletion src/packg/maths.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ def clip_rectangle_coords(rectangle_coords: tuple[int, int, int, int], w: int, h
return x1, y1, x2, y2



def round_half_up(x: float) -> int:
"""
Python/numpy do bankers rounding (round to even). This method rounds 0.5 up to 1 instead.
Expand Down
4 changes: 2 additions & 2 deletions src/packg/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def suppress_stdout_stderr():
yield err, out


def convert_unsigned_int_to_bytes(int_input:int, length:int=4) -> bytes:
def convert_unsigned_int_to_bytes(int_input: int, length: int = 4) -> bytes:
"""
convert integer to bytes
Expand All @@ -40,7 +40,7 @@ def convert_unsigned_int_to_bytes(int_input:int, length:int=4) -> bytes:
return int(int_input).to_bytes(length, "big")


def convert_bytes_to_unsigned_int(bytes_input:bytes) -> int:
def convert_bytes_to_unsigned_int(bytes_input: bytes) -> int:
"""
convert bytes to integer
Expand Down
3 changes: 2 additions & 1 deletion src/packg/run/cleanup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
from loguru import logger

from packg.log import SHORTEST_FORMAT, configure_logger, get_logger_level_from_args
from typedparser import VerboseQuietArgs, add_argument, define, TypedParser
from typedparser import VerboseQuietArgs, add_argument, TypedParser
from attrs import define


@define
Expand Down
3 changes: 2 additions & 1 deletion src/packg/run/create_autocomplete.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

from packg.log import SHORTEST_FORMAT, configure_logger, get_logger_level_from_args
from packg.packaging import create_bash_autocomplete_script
from typedparser import VerboseQuietArgs, add_argument, define, TypedParser
from typedparser import VerboseQuietArgs, add_argument, TypedParser
from attrs import define


@define
Expand Down
16 changes: 9 additions & 7 deletions src/packg/system/systemcall.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
"""Helper to run system commands and process their output."""
import subprocess
from typing import Tuple
from typing import Tuple, Optional


def systemcall(call: str, verbose: bool = False) -> Tuple[str, str, int]:
def systemcall(
call: str, verbose: bool = False, decode: Optional[str] = "utf-8"
) -> Tuple[str, str, int]:
"""Run a command with subprocess.Popen and process the output. This call
is synchronous so output will only returned once the command is done.
Expand All @@ -18,9 +20,9 @@ def systemcall(call: str, verbose: bool = False) -> Tuple[str, str, int]:
with subprocess.Popen(call, stdout=pipe, stderr=pipe, shell=True) as process:
out, err = process.communicate()
retcode = process.poll()
charset = "utf-8"
out = out.decode(charset)
err = err.decode(charset)
if decode is not None:
out = out.decode(decode)
err = err.decode(decode)
if verbose:
print(f"out {out} err {err} ret {retcode}")
return out, err, retcode
Expand Down Expand Up @@ -48,7 +50,7 @@ def assert_command_worked(errmsg: str, cmd: str, out: str, err: str, retcode: in


def systemcall_with_assert(
call: str, errmsg: str = "none", verbose: bool = False
call: str, errmsg: str = "none", verbose: bool = False, decode: Optional[str] = "utf-8"
) -> Tuple[str, str, int]:
"""Run a command and assert it worked
Expand All @@ -60,6 +62,6 @@ def systemcall_with_assert(
Returns:
stdout, stderr, returncode
"""
out, err, retcode = systemcall(call, verbose=verbose)
out, err, retcode = systemcall(call, verbose=verbose, decode=decode)
assert_command_worked(errmsg, call, out, err, retcode)
return out, err, retcode
29 changes: 29 additions & 0 deletions tests/packg/test_iotools_format_bytes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from packg.iotools.misc import format_bytes_human_readable


def test_format_bytes_small():
assert format_bytes_human_readable(0) == "0.000B"
assert format_bytes_human_readable(100) == "100.000B"
assert format_bytes_human_readable(512) == "512.000B"
assert format_bytes_human_readable(-512) == "-512.000B"


def test_format_bytes_kilobytes():
assert format_bytes_human_readable(1024) == "1.000KB"
assert format_bytes_human_readable(2048) == "2.000KB"
assert format_bytes_human_readable(1536) == "1.500KB"


def test_format_bytes_megabytes():
assert format_bytes_human_readable(1048576) == "1.000MB"
assert format_bytes_human_readable(1572864) == "1.500MB"


def test_format_bytes_gigabytes():
assert format_bytes_human_readable(1073741824) == "1.000GB"
assert format_bytes_human_readable(1610612736) == "1.500GB"


def test_format_bytes_terabytes():
assert format_bytes_human_readable(1099511627776) == "1.000TB"
assert format_bytes_human_readable(1649267441664) == "1.500TB"
1 change: 0 additions & 1 deletion tests/packg/test_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,3 @@ def test_silence_stdlib_loggers():
assert stream.getvalue() == ""

print(f"Done")

2 changes: 1 addition & 1 deletion tests/packg/test_system_systemcall.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ def main():
test_systemcall()


if __name__ == '__main__':
if __name__ == "__main__":
main()
1 change: 1 addition & 0 deletions tests/packg/test_web_download_file_mocked.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

class _MockHTTPResponse:
"""Mock for urllib3.HTTPResponse"""

def __init__(self, data, headers=None, status=200):
if headers is None:
headers = {}
Expand Down

0 comments on commit 3e7a7ad

Please sign in to comment.