Skip to content

helpers

dependenpy printer module.

PrintMixin ¤

Bases: object

Print mixin class.

Source code in dependenpy/helpers.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class PrintMixin(object):
    """Print mixin class."""

    def print(self, format: str | None = TEXT, output: IO = sys.stdout, **kwargs: Any):  # noqa: A002,A003
        """
        Print the object in a file or on standard output by default.

        Args:
            format: output format (csv, json or text).
            output: descriptor to an opened file (default to standard output).
            **kwargs: additional arguments.
        """
        if format is None:
            format = TEXT

        if format != TEXT:
            kwargs.pop("zero", "")

        if format == TEXT:
            print(self._to_text(**kwargs), file=output)
        elif format == CSV:
            print(self._to_csv(**kwargs), file=output)
        elif format == JSON:
            print(self._to_json(**kwargs), file=output)

    def _to_text(self, **kwargs):
        raise NotImplementedError

    def _to_csv(self, **kwargs):
        raise NotImplementedError

    def _to_json(self, **kwargs):
        raise NotImplementedError

print(format=TEXT, output=sys.stdout, **kwargs) ¤

Print the object in a file or on standard output by default.

Parameters:

Name Type Description Default
format str | None

output format (csv, json or text).

TEXT
output IO

descriptor to an opened file (default to standard output).

sys.stdout
**kwargs Any

additional arguments.

{}
Source code in dependenpy/helpers.py
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
def print(self, format: str | None = TEXT, output: IO = sys.stdout, **kwargs: Any):  # noqa: A002,A003
    """
    Print the object in a file or on standard output by default.

    Args:
        format: output format (csv, json or text).
        output: descriptor to an opened file (default to standard output).
        **kwargs: additional arguments.
    """
    if format is None:
        format = TEXT

    if format != TEXT:
        kwargs.pop("zero", "")

    if format == TEXT:
        print(self._to_text(**kwargs), file=output)
    elif format == CSV:
        print(self._to_csv(**kwargs), file=output)
    elif format == JSON:
        print(self._to_json(**kwargs), file=output)

guess_depth(packages) ¤

Guess the optimal depth to use for the given list of arguments.

Parameters:

Name Type Description Default
packages list[str]

List of packages.

required

Returns:

Type Description
int

Guessed depth to use.

Source code in dependenpy/helpers.py
49
50
51
52
53
54
55
56
57
58
59
60
61
def guess_depth(packages: list[str]) -> int:
    """
    Guess the optimal depth to use for the given list of arguments.

    Args:
        packages: List of packages.

    Returns:
        Guessed depth to use.
    """
    if len(packages) == 1:
        return packages[0].count(".") + 2
    return min(package.count(".") for package in packages) + 1