File: _compat.py

package info (click to toggle)
python-anyqt 0.2.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 560 kB
  • sloc: python: 4,087; makefile: 192; sh: 3
file content (30 lines) | stat: -rw-r--r-- 679 bytes parent folder | download
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
from typing import Union
from functools import lru_cache

from AnyQt.QtCore import QObject, Property


@lru_cache(maxsize=200)
def _converter(type_: Union[type, str]):
    class Obj(QObject):
        _f = None
        def _set(self, val):
            self._f = val
        def _get(self):
            return self._f

        prop = Property(type_, _get, _set)

    def convert(value):
        inst = Obj()
        ok = inst.setProperty('prop', value)
        if ok:
            return inst.property('prop')
        else:
            return None
    return convert


def qvariant_cast(value, type_: Union[type, str]):
    converter = _converter(type_)
    return converter(value)