How to use the typedload.dataloader function in typedload

To help you get started, we’ve selected a few typedload examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ltworf / typedload / tests / test_legacytuples_dataloader.py View on Github external
def test_union(self):
        loader = dataloader.Loader()
        loader.basiccast = False
        assert loader.load(1, Optional[Union[int, str]]) == 1
        assert loader.load('a', Optional[Union[int, str]]) == 'a'
        assert loader.load(None, Optional[Union[int, str]]) == None
        assert type(loader.load(1, Optional[Union[int, float]])) == int
        assert type(loader.load(1.0, Optional[Union[int, float]])) == float
        with self.assertRaises(ValueError):
            loader.load('', Optional[Union[int, float]])

        loader.basiccast = True
        assert type(loader.load(1, Optional[Union[int, float]])) == int
        assert type(loader.load(1.0, Optional[Union[int, float]])) == float
        assert loader.load(None, Optional[str]) is None
github ltworf / typedload / tests / test_legacytuples_dataloader.py View on Github external
def test_nested(self):
        A = NamedTuple('A', [('a', int)])
        B = NamedTuple('B', [('a', A)])

        loader = dataloader.Loader()
        r = B(A(1))
        assert loader.load({'a': {'a': 1}}, B) == r
        with self.assertRaises(TypeError):
            loader.load({'a': {'a': 1}}, A)
github ltworf / typedload / tests / test_dataclass.py View on Github external
def test_ComplicatedUnion(self):
        @dataclass
        class A:
            a: int
        @dataclass
        class B:
            a: str
        @dataclass
        class C:
            val: Union[A, B]
        loader = dataloader.Loader()
        loader.basiccast = False
        assert type(loader.load({'val': {'a': 1}}, C).val) == A
        assert type(loader.load({'val': {'a': '1'}}, C).val) == B
github ltworf / typedload / tests / test_dataloader.py View on Github external
def test_fail(self):
        class A(NamedTuple):
            a: int
            q: str
        loader = dataloader.Loader()
        with self.assertRaises(ValueError):
            loader.load({'a': 3}, A)
github ltworf / localslackirc / rocket.py View on Github external
def __init__(self, url: str, token: str, previous_status: Optional[bytes]) -> None:
        self.url = url
        self.token  = token
        self._call_id = 100
        self._internalevents: List[Dict[str, Any]] = []
        self._channels: List[Channel] = []
        self._users: Dict[str, User] = {}
        self._id_prefix = 'lsi' + str(uuid.uuid1()) + '_'

        # WORKAROUND
        # The NamedTuple module can't have a field called _id, so I rename it to id_
        self.loader = typedload.dataloader.Loader()
        index = self.loader.index(ChannelUsers)
        _original_handler = self.loader.handlers[index][1]
        def _namedtuplehandler(l: typedload.dataloader.Loader, d, t):
            if '_id' in d:
                d['id_'] = d['_id']
                del d['_id']
            return  _original_handler(l, d, t)
        self.loader.handlers[index] = (self.loader.handlers[index][0], _namedtuplehandler)
        self._connect()
github ltworf / typedload / typedload / __init__.py View on Github external
def load(value: Any, type_: Type[T], **kwargs) -> T:
    """
    Quick function call to load data into a type.

    It is useful to avoid creating the Loader object,
    in case only the default parameters are used.
    """
    from . import dataloader
    loader = dataloader.Loader(**kwargs)
    return loader.load(value, type_)