module documentation

A collection of functions designed to help I/O with ascii files.
Class ​Conversion​Warning Warning issued when a string converter has a problem.
Class ​Converter​Error Exception raised when an error occurs in a converter for string values.
Class ​Converter​Lock​Error Exception raised when an attempt is made to upgrade a locked converter.
Class ​Line​Splitter Object to split a string at a given delimiter or at given places.
Class ​Name​Validator Object to validate a list of strings to use as field names.
Class ​String​Converter Factory class for function transforming a string into another object (int, float).
Function ​_decode​_line Decode bytes from binary input streams.
Function ​_is​_bytes​_like Check whether obj behaves like a bytes object.
Function ​_is​_string​_like Check whether obj behaves like a string.
Function easy​_dtype Convenience function to create a np.dtype object.
Function flatten​_dtype Unpack a structured data-type by collapsing nested fields and/or fields with a shape.
Function has​_nested​_fields Returns whether one or several fields of a dtype are nested.
Function str2bool Tries to transform a string supposed to represent a boolean to a boolean.
def _decode_line(line, encoding=None):

Decode bytes from binary input streams.

Defaults to decoding from 'latin1'. That differs from the behavior of np.compat.asunicode that decodes from 'ascii'.

Parameters

line : str or bytes
Line to be decoded.
encoding : str
Encoding used to decode line.

Returns

decoded_line : str

def _is_bytes_like(obj):
Check whether obj behaves like a bytes object.
def _is_string_like(obj):
Check whether obj behaves like a string.
def easy_dtype(ndtype, names=None, defaultfmt='f%i', **validationargs):

Convenience function to create a np.dtype object.

The function processes the input dtype and matches it with the given names.

Parameters

ndtype : var
Definition of the dtype. Can be any string or dictionary recognized by the np.dtype function, or a sequence of types.
names : str or sequence, optional
Sequence of strings to use as field names for a structured dtype. For convenience, names can be a string of a comma-separated list of names.
defaultfmt : str, optional
Format string used to define missing names, such as "f%i" (default) or "fields_%02i".
validationargs : optional
A series of optional arguments used to initialize a NameValidator.

Examples

>>> np.lib._iotools.easy_dtype(float)
dtype('float64')
>>> np.lib._iotools.easy_dtype("i4, f8")
dtype([('f0', '<i4'), ('f1', '<f8')])
>>> np.lib._iotools.easy_dtype("i4, f8", defaultfmt="field_%03i")
dtype([('field_000', '<i4'), ('field_001', '<f8')])
>>> np.lib._iotools.easy_dtype((int, float, float), names="a,b,c")
dtype([('a', '<i8'), ('b', '<f8'), ('c', '<f8')])
>>> np.lib._iotools.easy_dtype(float, names="a,b,c")
dtype([('a', '<f8'), ('b', '<f8'), ('c', '<f8')])
def flatten_dtype(ndtype, flatten_base=False):

Unpack a structured data-type by collapsing nested fields and/or fields with a shape.

Note that the field names are lost.

Parameters

ndtype : dtype
The datatype to collapse
flatten_base : bool, optional
If True, transform a field with a shape into several fields. Default is False.

Examples

>>> dt = np.dtype([('name', 'S4'), ('x', float), ('y', float),
...                ('block', int, (2, 3))])
>>> np.lib._iotools.flatten_dtype(dt)
[dtype('S4'), dtype('float64'), dtype('float64'), dtype('int64')]
>>> np.lib._iotools.flatten_dtype(dt, flatten_base=True)
[dtype('S4'),
 dtype('float64'),
 dtype('float64'),
 dtype('int64'),
 dtype('int64'),
 dtype('int64'),
 dtype('int64'),
 dtype('int64'),
 dtype('int64')]
def has_nested_fields(ndtype):

Returns whether one or several fields of a dtype are nested.

Parameters

ndtype : dtype
Data-type of a structured array.

Raises

AttributeError
If ndtype does not have a names attribute.

Examples

>>> dt = np.dtype([('name', 'S4'), ('x', float), ('y', float)])
>>> np.lib._iotools.has_nested_fields(dt)
False
def str2bool(value):

Tries to transform a string supposed to represent a boolean to a boolean.

Parameters

value : str
The string that is transformed to a boolean.

Returns

boolval : bool
The boolean representation of value.

Raises

ValueError
If the string is not 'True' or 'False' (case independent)

Examples

>>> np.lib._iotools.str2bool('TRUE')
True
>>> np.lib._iotools.str2bool('false')
False