File: __init__.py

package info (click to toggle)
orange3 3.40.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 15,912 kB
  • sloc: python: 162,745; ansic: 622; makefile: 322; sh: 93; cpp: 77
file content (190 lines) | stat: -rw-r--r-- 5,460 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
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
47
48
49
50
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import inspect
import sys
from collections import deque
from contextlib import contextmanager
from enum import Enum, IntEnum
from typing import (
    TypeVar, Callable, Any, Iterable, Optional, Hashable, Type, Union
)
from xml.sax.saxutils import escape

from AnyQt.QtCore import QObject, Qt

from Orange.data.variable import TimeVariable
from Orange.util import deepgetattr, ftry  # pylint: disable=unused-import


def vartype(var):
    if var.is_discrete:
        return 1
    elif var.is_continuous:
        if isinstance(var, TimeVariable):
            return 4
        return 2
    elif var.is_string:
        return 3
    else:
        return 0


def progress_bar_milestones(count, iterations=100):
    return {int(i * count / float(iterations)) for i in range(iterations)}


def getdeepattr(obj, attr, *arg, **kwarg):
    if isinstance(obj, dict):
        return obj.get(attr)
    return deepgetattr(obj, attr, *arg, **kwarg)


def to_html(s):
    return s.replace("<=", "≤").replace(">=", "≥"). \
        replace("<", "&lt;").replace(">", "&gt;").replace("=\\=", "≠")

getHtmlCompatibleString = to_html


def get_variable_values_sorted(variable):
    """
    Return a list of sorted values for given attribute, if all its values can be
    cast to int's.
    """
    if variable.is_continuous:
        return []
    try:
        return sorted(variable.values, key=int)
    except ValueError:
        return variable.values


def dumpObjectTree(obj, _indent=0):
    """
    Dumps Qt QObject tree. Aids in debugging internals.
    See also: QObject.dumpObjectTree()
    """
    assert isinstance(obj, QObject)
    print('{indent}{type} "{name}"'.format(indent=' ' * (_indent * 4),
                                           type=type(obj).__name__,
                                           name=obj.objectName()),
          file=sys.stderr)
    for child in obj.children():
        dumpObjectTree(child, _indent + 1)


def getmembers(obj, predicate=None):
    """Return all the members of an object in a list of (name, value) pairs sorted by name.

    Behaves like inspect.getmembers. If a type object is passed as a predicate,
    only members of that type are returned.
    """

    if isinstance(predicate, type):
        def mypredicate(x):
            return isinstance(x, predicate)
    else:
        mypredicate = predicate
    return inspect.getmembers(obj, mypredicate)


def qname(type_: type) -> str:
    """Return the fully qualified name for a `type_`."""
    return "{0.__module__}.{0.__qualname__}".format(type_)


_T1 = TypeVar("_T1")  # pylint: disable=invalid-name
_E = TypeVar("_E", bound=Enum)  # pylint: disable=invalid-name
_A = TypeVar("_A")  # pylint: disable=invalid-name
_B = TypeVar("_B")  # pylint: disable=invalid-name


def apply_all(seq, op):
    # type: (Iterable[_T1], Callable[[_T1], Any]) -> None
    """Apply `op` on all elements of `seq`."""
    # from itertools recipes `consume`
    deque(map(op, seq), maxlen=0)


def unique_everseen(iterable, key=None):
    # type: (Iterable[_T1], Optional[Callable[[_T1], Hashable]]) -> Iterable[_T1]
    """
    Return an iterator over unique elements of `iterable` preserving order.

    If `key` is supplied it is used as a substitute for determining
    'uniqueness' of elements.

    Parameters
    ----------
    iterable : Iterable[T]
    key : Callable[[T], Hashable]

    Returns
    -------
    unique : Iterable[T]
    """
    seen = set()
    if key is None:
        key = lambda t: t
    for el in iterable:
        el_k = key(el)
        if el_k not in seen:
            seen.add(el_k)
            yield el


def enum_get(etype: Type[_E], name: str, default: _T1) -> Union[_E, _T1]:
    """
    Return an Enum member by `name`. If no such member exists in `etype`
    return `default`.
    """
    try:
        return etype[name]
    except LookupError:
        return default


def instance_tooltip(domain, row, skip_attrs=()):
    def show_part(_point_data, singular, plural, max_shown, _vars):
        cols = [escape('{} = {}'.format(var.name, _point_data[var]))
                for var in _vars[:max_shown + len(skip_attrs)]
                if _vars is domain.class_vars
                or var not in skip_attrs][:max_shown]
        if not cols:
            return ""
        n_vars = len(_vars)
        if n_vars > max_shown:
            cols[-1] = f'... and {n_vars - max_shown + 1} others'
        return \
            "<b>{}</b>:<br/>".format(singular if n_vars < 2 else plural) \
            + "<br/>".join(cols)

    parts = (("Class", "Classes", 4, domain.class_vars),
             ("Meta", "Metas", 4, domain.metas),
             ("Feature", "Features", 10, domain.attributes))
    return "<br/>".join(show_part(row, *columns) for columns in parts)


def enum2int(enum: Union[Enum, IntEnum]) -> int:
    """
    PyQt5 uses IntEnum like object for settings, for example SortOrder while
    PyQt6 uses Enum. PyQt5's IntEnum also does not support value attribute.
    This function transform both settings objects to int.

    Parameters
    ----------
    enum
        IntEnum like object or Enum object with Qt's settings

    Returns
    -------
    Settings transformed to int
    """
    return int(enum) if isinstance(enum, int) else enum.value


@contextmanager
def disconnected(signal, slot, connection_type=Qt.AutoConnection):
    signal.disconnect(slot)
    try:
        yield
    finally:
        signal.connect(slot, connection_type)