예제 #1
0
def buffer_from_example(example,
                        leading_dims,
                        share_memory=False,
                        use_NatSchema=None):
    """Allocates memory and returns it in `namedarraytuple` with same
    structure as ``examples``, which should be a `namedtuple` or
    `namedarraytuple`. Applies the same leading dimensions ``leading_dims`` to
    every entry, and otherwise matches their shapes and dtypes. The examples
    should have no leading dimensions.  ``None`` fields will stay ``None``.
    Optionally allocate on OS shared memory. Uses ``build_array()``.
    
    New: can use NamedArrayTuple types by the `use_NatSchema` flag, which
    may be easier for pickling/unpickling when using spawn instead
    of fork. If use_NatSchema is None, the type of ``example`` will be used to
    infer what type to return (this is the default)
    """
    if example is None:
        return
    if use_NatSchema is None:
        use_NatSchema = isinstance(example, (NamedTuple, NamedArrayTuple))
    try:
        if use_NatSchema:
            buffer_type = NamedArrayTupleSchema_like(example)
        else:
            buffer_type = namedarraytuple_like(example)
    except TypeError:  # example was not a namedtuple or namedarraytuple
        return build_array(example, leading_dims, share_memory)
    return buffer_type(*(buffer_from_example(v,
                                             leading_dims,
                                             share_memory=share_memory,
                                             use_NatSchema=use_NatSchema)
                         for v in example))
예제 #2
0
def buffer_from_example(example, leading_dims, shared_memory=False):
    try:
        buffer_type = namedarraytuple_like(example)
    except TypeError:  # example was not a namedtuple or namedarraytuple
        return build_array(example, leading_dims, shared_memory)
    return buffer_type(*(buffer_from_example(v, leading_dims, shared_memory)
                         for v in example))
예제 #3
0
def buffer_from_example(example, leading_dims, share_memory=False):
    ''' Based on given example, build a object in memory that serves as buffer,
    and return the object with the same alignment as example.
    '''
    if example is None:
        return
    try:
        buffer_type = namedarraytuple_like(example)
    except TypeError:  # example was not a namedtuple or namedarraytuple
        return build_array(example, leading_dims, share_memory)
    return buffer_type(*(buffer_from_example(v, leading_dims, share_memory)
                         for v in example))
예제 #4
0
def buffer_from_example(example, leading_dims, share_memory=False):
    """Allocates memory and returns it in `namedarraytuple` with same
    structure as ``examples``, which should be a `namedtuple` or
    `namedarraytuple`. Applies the same leading dimensions ``leading_dims`` to
    every entry, and otherwise matches their shapes and dtypes. The examples
    should have no leading dimensions.  ``None`` fields will stay ``None``.
    Optionally allocate on OS shared memory. Uses ``build_array()``.
    """
    if example is None:
        return
    try:
        buffer_type = namedarraytuple_like(example)
    except TypeError:  # example was not a namedtuple or namedarraytuple
        return build_array(example, leading_dims, share_memory)
    return buffer_type(*(buffer_from_example(v, leading_dims, share_memory)
        for v in example))