3
���h�x � @ s� d Z dZddlZddljjZddlm Z m
Z
mZ d!dd�Zdd� Z
d d
� Zdd� Zd"dd�ZG dd� d�ZG dd� d�Zdd� ZG dd� de�ZG dd� de�ZG dd� de�ZG dd� d�Zd#dd �ZdS )$zBA collection of functions designed to help I/O with ascii files.
zrestructuredtext en� N)�asbytes� asunicode�bytesc C s. t | �tkr*|dkr | jd�} n
| j|�} | S )an 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.
Returns
-------
decoded_line : unicode
Unicode in Python 2, a str (unicode) in Python 3.
N�latin1)�typer �decode)�line�encoding� r
�3/tmp/pip-build-5_djhm0z/numpy/numpy/lib/_iotools.py�_decode_line s
r c C s* y| d W n t tfk
r$ dS X dS )z2
Check whether obj behaves like a string.
� FT)� TypeError�
ValueError)�objr
r
r �_is_string_like% s
r c C s* y| d W n t tfk
r$ dS X dS )z8
Check whether obj behaves like a bytes object.
� FT)r r )r r
r
r �_is_bytes_like0 s
r c C s* x$| j p
f D ]}| | j dk rdS qW dS )a�
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
NTF)�names)�ndtype�namer
r
r �has_nested_fields; s r Fc C sn | j }|dkr2|r*| jgttj| j�� S | jgS g }x.|D ]&}| j| }t|d |�}|j|� q<W |S dS )aX
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')]
Nr ) r �base�int�np�prod�shape�fields�
flatten_dtype�extend)r Zflatten_baser �types�field�infoZflat_dtr
r
r r V s !
r c @ sB e Zd ZdZdd� Zddd�Zd d
� Zdd� Zd
d� Zdd� Z dS )�LineSplittera�
Object to split a string at a given delimiter or at given places.
Parameters
----------
delimiter : str, int, or sequence of ints, optional
If a string, character used to delimit consecutive fields.
If an integer or a sequence of integers, width(s) of each field.
comments : str, optional
Character used to mark the beginning of a comment. Default is '#'.
autostrip : bool, optional
Whether to strip each individual field. Default is True.
c s � fdd�S )a�
Wrapper to strip each member of the output of `method`.
Parameters
----------
method : function
Function that takes a single argument and returns a sequence of
strings.
Returns
-------
wrapped : function
The result of wrapping `method`. `wrapped` takes a single input
argument and returns a list of strings that are stripped of
white-space.
c s dd� � | �D �S )Nc S s g | ]}|j � �qS r
)�strip)�.0�_r
r
r �
<listcomp>� s z<LineSplitter.autostrip.<locals>.<lambda>.<locals>.<listcomp>r
)�input)�methodr
r �<lambda>� s z(LineSplitter.autostrip.<locals>.<lambda>r
)�selfr) r
)r) r � autostrip� s zLineSplitter.autostripN�#Tc C s� t |�}t |�}|| _|d ks(t|t�r8|p.d }| j}npt|d�r�| j}tjdgt |� �}dd� t
|d d� |dd � �D �}n&t|�r�| jt|� }}n| jd }}|| _
|r�| j|�| _n|| _|| _d S )N�__iter__r c S s g | ]\}}t ||��qS r
)�slice)r% �i�jr
r
r r' |