Skip to content

utils

get_value_type(val)

Got a little hacky so as to avoid dependency on external libraries. Current method is to check if the dependent library is already imported, if they are, then we can reference them.

Note: - Watch out for error here if the Executor tests fail. TODO - We currently just silently ignore cases we cant handle

Source code in lineapy/utils/utils.py
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
def get_value_type(val: Any) -> Optional[ValueType]:
    """
    Got a little hacky so as to avoid dependency on external libraries.
    Current method is to check if the dependent library is already imported,
    if they are, then we can reference them.

    Note:
    - Watch out for error here if the Executor tests fail.
    TODO
    - We currently just silently ignore cases we cant handle
    """
    if isinstance(val, (list, str, int)):
        return ValueType.array
    if "pandas" in sys.modules:
        import pandas

        if isinstance(val, pandas.core.frame.DataFrame):
            return ValueType.dataset  # FIXME
        if isinstance(val, pandas.core.series.Series):
            return ValueType.dataset  # FIXME

    if "PIL" in sys.modules:
        import PIL.PngImagePlugin

        if hasattr(PIL, "PngImagePlugin"):
            if isinstance(val, PIL.PngImagePlugin.PngImageFile):
                return ValueType.chart

        import PIL.Image

        if isinstance(val, PIL.Image.Image):
            return ValueType.chart

    return None

remove_duplicates(xs)

Remove all duplicate items, maintaining order.

Source code in lineapy/utils/utils.py
117
118
119
120
121
122
123
124
125
126
127
def remove_duplicates(xs: Iterable[T]) -> Iterable[T]:
    """
    Remove all duplicate items, maintaining order.
    """
    seen_: Set[int] = set()
    for x in xs:
        h = hash(x)
        if h in seen_:
            continue
        seen_.add(h)
        yield x

remove_value(xs, x)

Remove all items equal to x.

Source code in lineapy/utils/utils.py
130
131
132
133
134
135
136
def remove_value(xs: Iterable[T], x: T) -> Iterable[T]:
    """
    Remove all items equal to x.
    """
    for y in xs:
        if x != y:
            yield y

Was this helpful?

Help us improve docs with your feedback!