コード例 #1
0
def default_list_deserializer(obj: list,
                              cls: type = None,
                              *,
                              warn_on_fail: bool = False,
                              tasks: int = 1,
                              task_type: type = Process,
                              fork_inst: Type[StateHolder] = StateHolder,
                              **kwargs) -> list:
    """
    Deserialize a list by deserializing all items of that list.
    :param obj: the list that needs deserializing.
    :param cls: the type optionally with a generic (e.g. List[str]).
    :param warn_on_fail: if ``True``, will warn upon any failure and continue.
    :param tasks: the allowed number of tasks (threads or processes).
    :param task_type: the type that is used for multitasking.
    :param fork_inst: if given, it uses this fork of ``JsonSerializable``.
    :param kwargs: any keyword arguments.
    :return: a deserialized list instance.
    """
    cls_ = None
    kwargs_ = {**kwargs}
    cls_args = get_args(cls)
    if cls_args:
        cls_ = cls_args[0]
        # Mark the cls as 'inferred' so that later it is known where cls came
        # from and the precedence of classes can be determined.
        kwargs_['_inferred_cls'] = True

    if tasks == 1:
        result = _do_load(obj, cls_, warn_on_fail, fork_inst, kwargs_)
    elif tasks > 1:
        result = multi_task(load, obj, tasks, task_type, cls_, **kwargs_)
    else:
        raise JsonsError('Invalid number of tasks: {}'.format(tasks))
    return result
コード例 #2
0
def default_list_deserializer(obj: list,
                              cls: type = None,
                              *,
                              tasks: int = 1,
                              task_type: type = Process,
                              **kwargs) -> list:
    """
    Deserialize a list by deserializing all items of that list.
    :param obj: the list that needs deserializing.
    :param cls: the type optionally with a generic (e.g. List[str]).
    :param tasks: the allowed number of tasks (threads or processes).
    :param task_type: the type that is used for multitasking.
    :param kwargs: any keyword arguments.
    :return: a deserialized list instance.
    """
    cls_ = None
    kwargs_ = {**kwargs}
    cls_args = get_args(cls)
    if cls_args:
        cls_ = cls_args[0]
        # Mark the cls as 'inferred' so that later it is known where cls came
        # from and the precedence of classes can be determined.
        kwargs_['_inferred_cls'] = True

    if tasks == 1:
        result = [load(elem, cls=cls_, tasks=1, **kwargs_) for elem in obj]
    elif tasks > 1:
        result = multi_task(load, obj, tasks, task_type, cls_, **kwargs_)
    else:
        raise JsonsError('Invalid number of tasks: {}'.format(tasks))
    return result
コード例 #3
0
ファイル: default_iterable.py プロジェクト: taljaards/jsons
def default_iterable_serializer(obj: Iterable,
                                cls: type = None,
                                *,
                                strict: bool = False,
                                tasks: int = 1,
                                task_type: type = Process,
                                **kwargs) -> list:
    """
    Serialize the given ``obj`` to a list of serialized objects.
    :param obj: the iterable that is to be serialized.
    :param cls: the (subscripted) type of the iterable.
    :param strict: a bool to determine if the serializer should be strict
    (i.e. only dumping stuff that is known to ``cls``).
    :param tasks: the allowed number of tasks (threads or processes).
    :param task_type: the type that is used for multitasking.
    :param kwargs: any keyword arguments that may be given to the serialization
    process.
    :return: a list of which all elements are serialized.
    """
    # The meta kwarg store_cls is filtered out, because an iterable should have
    # its own -meta attribute.
    kwargs_ = {**kwargs, 'strict': strict}
    kwargs_.pop('_store_cls', None)
    if strict:
        cls_ = determine_cls(obj, cls)
        # cls_ = cls or get_type(obj)  # Get the List[T] type from the instance.
        subclasses = _get_subclasses(obj, cls_)
    else:
        subclasses = _get_subclasses(obj, None)

    if tasks < 2:
        result = [
            dump(elem, cls=subclasses[i], **kwargs_)
            for i, elem in enumerate(obj)
        ]
    else:
        zipped_objs = list(zip(obj, subclasses))
        result = multi_task(do_dump, zipped_objs, tasks, task_type, **kwargs_)

    return result