U C^@sZdZddlmZmZmZdZddlZddlZddl m m Z ddl mZmZmZmZejddkrddlmZmZmZmZmZmZeZn$ddlmZmZmZmZmZmZmZd(d d Zd d Zd dZd)ddZddZ d*ddZ!GdddeZ"GdddeZ#ddZ$Gddde%Z&Gdd d e&Z'Gd!d"d"e(Z)Gd#d$d$eZ*d+d&d'Z+dS),zBA collection of functions designed to help I/O with ascii files. )divisionabsolute_importprint_functionzrestructuredtext enN)asbytes asunicodebytes basestring)boolintfloatcomplexobjectstr)r r r r runicodercCs.t|tkr*|dkr |d}n ||}|S)anDecode 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. Nlatin1)typerdecode)lineencodingr5/tmp/pip-install-6_kvzl1k/numpy/numpy/lib/_iotools.py _decode_lines    rc Cs,z |dWnttfk r&YdSXdS)z2 Check whether obj behaves like a string. FT TypeError ValueErrorobjrrr_is_string_like.s  rc Cs,z |dWnttfk r&YdSXdS)z8 Check whether obj behaves like a bytes object. FTrrrrr_is_bytes_like9s  r!rFcCst|rV|dr(ddl}|||}n(|drFddl}||}n t||}d}nt|drj|}d}ntd|r~||fS|S) a Returns the filehandle corresponding to a string or a file. If the string ends in '.gz', the file is automatically unzipped. Parameters ---------- fname : string, filehandle Name of the file whose filehandle must be returned. flag : string, optional Flag indicating the status of the file ('r' for read, 'w' for write). return_opened : boolean, optional Whether to return the opening status of the file. z.gzrNz.bz2TseekFz%fname must be a string or file handle) rendswithgzipopenbz2BZ2Filefilehasattrr)fnameflagZ return_openedr%Zfhdr'Zopenedrrr_to_filehandleDs      r-cCs(|jpdD]}||jdk r dSq 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 rNTF)names)ndtypenamerrrhas_nested_fieldsfsr1cCsj|j}|dkr2|r*|jgtt|jS|jgSg}|D]&}|j|}t|d|}||q:|SdS)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.baser npprodshapefields flatten_dtypeextend)r/Z flatten_baser.typesfieldinfoZflat_dtrrrr7s!  r7c@sBeZdZdZddZdddZd d Zd d Zd dZddZ 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. cs fddS)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. csdd|DS)NcSsg|] }|qSr)strip).0_rrr sz...r)inputmethodrrr z(LineSplitter.autostrip..r)selfrCrrBr autostripszLineSplitter.autostripN#TcCst|}t|}||_|dks(t|tr8|p.d}|j}npt|dr|j}tdgt |}ddt |dd|ddD}n&t |r|j t |}}n |jd}}||_ |r|||_n||_||_dS)N__iter__rcSsg|]\}}t||qSrslice)r>ijrrrr@sz)LineSplitter.__init__..)rcomments isinstancer_delimited_splitterr*_variablewidth_splitterr3Zcumsumlistzipr _fixedwidth_splitter delimiterrF _handymanr)rErVrOrFrrWidxrrr__init__s( & zLineSplitter.__init__cCs8|jdk r||jd}|d}|s,gS||jS)z2Chop off comments, strip, and split at delimiter. Nrz )rOsplitr=rVrErrrrrQs   z LineSplitter._delimited_splittercsb|jdk r|jdds,gS|jfddtdtD}fdd|DS)Nrz csg|]}t||qSrrIr>rK)fixedrrr@sz5LineSplitter._fixedwidth_splitter..csg|] }|qSrrr>srrrr@s)rOrZr=rVrangelenrErZslicesr)r]rrrUs  z!LineSplitter._fixedwidth_splittercs:|jdk r|jds"gS|j}fdd|DS)Nrcsg|] }|qSrrr^r`rrr@ sz8LineSplitter._variablewidth_splitter..)rOrZrVrcrr`rrRs  z$LineSplitter._variablewidth_splittercCs|t||jSN)rWrrr[rrr__call__szLineSplitter.__call__)NrGTN) __name__ __module__ __qualname____doc__rFrYrQrUrRrerrrrr<s    r<c@s@eZdZdZdddgZedZddd Zdd d Zdd dZ dS) NameValidatora" Object to validate a list of strings to use as field names. The strings are stripped of any non alphanumeric character, and spaces are replaced by '_'. During instantiation, the user can define a list of names to exclude, as well as a list of invalid characters. Names in the exclusion list are appended a '_' character. Once an instance has been created, it can be called with a list of names, and a list of valid names will be created. The `__call__` method accepts an optional keyword "default" that sets the default name in case of ambiguity. By default this is 'f', so that names will default to `f0`, `f1`, etc. Parameters ---------- excludelist : sequence, optional A list of names to exclude. This list is appended to the default list ['return', 'file', 'print']. Excluded names are appended an underscore: for example, `file` becomes `file_` if supplied. deletechars : str, optional A string combining invalid characters that must be deleted from the names. case_sensitive : {True, False, 'upper', 'lower'}, optional * If True, field names are case-sensitive. * If False or 'upper', field names are converted to upper case. * If 'lower', field names are converted to lower case. The default value is True. replace_space : '_', optional Character(s) used in replacement of white spaces. Notes ----- Calling an instance of `NameValidator` is the same as calling its method `validate`. Examples -------- >>> validator = np.lib._iotools.NameValidator() >>> validator(['file', 'field2', 'with space', 'CaSe']) ('file_', 'field2', 'with_space', 'CaSe') >>> validator = np.lib._iotools.NameValidator(excludelist=['excl'], ... deletechars='q', ... case_sensitive=False) >>> validator(['excl', 'field2', 'no_q', 'with space', 'CaSe']) ('EXCL', 'FIELD2', 'NO_Q', 'WITH_SPACE', 'CASE') returnr)printz~!@#$%^&*()-=+~\|]}[{';: /?.>,.FucSs|Srd)upperrnrrrrD^r lcSs|Srd)lowerrnrrrrD`r z%unrecognized case_sensitive value %s.) r8defaultexcludelist excludelistdefaultdeletecharssetadd deletecharscase_converter startswithr replace_space)rEruryZcase_sensitiver|deletemsgrrrrYLs$      zNameValidator.__init__f%ic s`|dkr|dkrdSg}t|tr(|g}|dk rlt|}||krXt|dg||}n||krl|d|}|j|j}|j}|j}g}t} d} |D]} ||  } |r| d|} d fdd| D} | dkr|| } | |kr| d7} || } q| d7} n| |kr| d7} | | d} | dkr@| | d | n | | | d| | <qt|S) a Validate a list of strings as field names for a structured array. Parameters ---------- names : sequence of str Strings to be validated. defaultfmt : str, optional Default format string, used if validating a given string reduces its length to zero. nbfields : integer, optional Final number of validated names, used to expand or shrink the initial list of names. Returns ------- validatednames : list of str The list of validated field names. Notes ----- A `NameValidator` instance can be called directly, which is the same as calling `validate`. For examples, see `NameValidator`. Nrr csg|]}|kr|qSrr)r>cryrrr@sz*NameValidator.validate..rNr?z_%d)rPrrbrSryrurzr|dictr=replacejoingetappendtuple) rEr. defaultfmtnbfieldsZnbnamesrurzr|ZvalidatednamesseenZnbemptyitemZcntrrrvalidategsL           zNameValidator.validatecCs|j|||dS)N)rr)r)rEr.rrrrrreszNameValidator.__call__)NNNr?)rN)rN) rfrgrhrirtrwrvrYrrerrrrrjs3   GrjcCs,|}|dkrdS|dkr dStddS)a 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 TRUETFALSEFzInvalid booleanN)rqr)valuerrrstr2bools rc@seZdZdZdS)ConverterErrorzR Exception raised when an error occurs in a converter for string values. Nrfrgrhrirrrrrsrc@seZdZdZdS)ConverterLockErrorzR Exception raised when an attempt is made to upgrade a locked converter. Nrrrrrrsrc@seZdZdZdS)ConversionWarningz Warning issued when a string converter has a problem. Notes ----- In `genfromtxt` a `ConversionWarning` is issued if raising exceptions is explicitly suppressed with the "invalid_raise" keyword. Nrrrrrrs rc @seZdZdZejedfejedfgZ e ejj e ej j krRe ej edfe ejeejfejeejdfejejejfejedfejedfgee \ZZZeddZedd Zed d Z edd dZ!dddZ"ddZ#ddZ$ddZ%ddZ&ddZ'd ddZ(d S)!StringConverterab Factory class for function transforming a string into another object (int, float). After initialization, an instance can be called to transform a string into another object. If the string is recognized as representing a missing value, a default value is returned. Attributes ---------- func : function Function used for the conversion. default : any Default value to return when the input corresponds to a missing value. type : type Type of the output. _status : int Integer representing the order of the conversion. _mapper : sequence of tuples Sequence of tuples (dtype, function, default value) to evaluate in order. _locked : bool Holds `locked` parameter. Parameters ---------- dtype_or_func : {None, dtype, function}, optional If a `dtype`, specifies the input data type, used to define a basic function and a default value for missing data. For example, when `dtype` is float, the `func` attribute is set to `float` and the default value to `np.nan`. If a function, this function is used to convert a string to another object. In this case, it is recommended to give an associated default value as input. default : any, optional Value to return by default, that is, when the string to be converted is flagged as missing. If not given, `StringConverter` tries to supply a reasonable default value. missing_values : {None, sequence of str}, optional ``None`` or sequence of strings indicating a missing value. If ``None`` then missing values are indicated by empty entries. The default is ``None``. locked : bool, optional Whether the StringConverter should be locked to prevent automatic upgrade or not. Default is False. FrMyz???cCs t|jS)z(Returns the dtype of the input variable.)r3arraydtypeclsvalrrr _getdtype4szStringConverter._getdtypecCst|jjS)z4Returns the type of the dtype of the input variable.)r3rrrrrrr _getsubdtype:szStringConverter._getsubdtypecCs|jtjkr|S|jS)z9Returns dtype for datetime64 and type of dtype otherwise.)rr3Z datetime64)rrrrr _dtypeortypeDs zStringConverter._dtypeortypeNcCst|dr(|jd||||fdSt|drt|dttfr`|D]}|jd|qHdS|dkrxdgt|}n$t|}|dgt|t|t ||D]"\}}|jd||||fqdS)a Upgrade the mapper of a StringConverter by adding a new function and its corresponding default. The input function (or sequence of functions) and its associated default value (if any) is inserted in penultimate position of the mapper. The corresponding type is estimated from the dtype of the default value. Parameters ---------- func : var Function, or sequence of functions Examples -------- >>> import dateutil.parser >>> import datetime >>> dateparser = dateutil.parser.parse >>> defaultdate = datetime.date(2000, 1, 1) >>> StringConverter.upgrade_mapper(dateparser, default=defaultdate) rerMNrHr) r*_mapperinsertrrPrrSrbrrT)rfuncdefaultr?ZfctZdftrrrupgrade_mapperLs  zStringConverter.upgrade_mapperc CsBt||_|dkr6t|_d|_|p$d|_td}nzd|_t|}Wnrtk rt |dsxd}t|t |||_|dkrz|d}Wnt k rd}YnX| |}YnXd}t |jD]<\}\} } } t|j | r|}|dkr| |_n||_q q| } t |jD]B\}\} } } |j | kr|}| } |dkrL| |_n||_q\q| } |dkrtd}||_||_|jdkr| |_|j|jddkrt|j tjrtj|_n$t|j tjrtj|_n d d |_|dkrd h|_n*t|tr |d }tt|d g|_|j|_|||_ d|_||_dS) NrFr rezOThe input argument `dtype` is neither a function nor a dtype (got '%s' instead)0rMrNcSs tt|Srd)r r rnrrrrDr z*StringConverter.__init__..r,)r _lockedrr_statusrr3rrr*rrr enumeraterZ issubdtype issubclassZuint64int64missing_valuesrPrrZrwrS _strict_call_callingfunctionr_checked_initial_default) rEZ dtype_or_funcrrlockedrerrmsgrrKdeftyperZ default_defZ last_funcrrrrYvsv                 zStringConverter.__init__cCs,z ||WStk r&|jYSXdSrd)rrrrErrrr _loose_calls zStringConverter._loose_callcCszH||}|jtkrDztj||jdWntk rBtYnX|WStk r||jkr||j srd|_ |j YStd|YnXdS)N)rFzCannot convert string '%s') rr r3rr OverflowErrorrr=rrrr)rErZ new_valuerrrrs    zStringConverter._strict_callcCs ||Srd)rrrrrreszStringConverter.__call__cCsd|_z ||WStk r|jr4d}t|t|j}|j}||krZd}t|n||dkrn|d7}|j|\|_ |_ }||_|j dk r|j |_ n||_ | |YSXdS)a Find the best converter for a given string, and return the result. The supplied string `value` is converted by testing different converters in order. First the `func` method of the `StringConverter` instance is tried, if this fails other available converters are tried. The order in which these other converters are tried is determined by the `_status` attribute of the instance. Parameters ---------- value : str The string to convert. Returns ------- out : any The result of converting `value` with the appropriate converter. T*Converter is locked and cannot be upgraded*Could not find a valid conversion functionrNN)rrrrrrbrrrrrrrupgrade)rErr _statusmaxrrrrrrs(      zStringConverter.upgradecCsd|_t|ds|f}|j}z|D] }||q"Wntk r|jrTd}t|t|j}|j}||krvt dn||dkr|d7}|j|\|_ |_ }|j dk r|j |_ n||_ ||_||YnXdS)NTrHrrrN)rr*rrrrrbrrrrrrr iterupgrade)rErr_mrrrrrrrrs2     zStringConverter.iterupgraderc Cs||_||_|dk r.||_||||_n@z||p8d}Wnttfk rZd}YnX||||_|dkrt|_ n6t |s|g}t dd|Dstd|j |dS)a Set StringConverter attributes directly. Parameters ---------- func : function Conversion function. default : any, optional Value to return by default, that is, when the string to be converted is flagged as missing. If not given, `StringConverter` tries to supply a reasonable default value. testing_value : str, optional A string representing a standard input value of the converter. This string is used to help defining a reasonable default value. missing_values : {sequence of str, None}, optional Sequence of strings indicating a missing value. If ``None``, then the existing `missing_values` are cleared. The default is `''`. locked : bool, optional Whether the StringConverter should be locked to prevent automatic upgrade or not. Default is False. Notes ----- `update` takes the same parameters as the constructor of `StringConverter`, except that `func` does not accept a `dtype` whereas `dtype_or_func` in the constructor does. N1css|]}t|tVqdSrd)rPr)r>vrrr osz)StringConverter.update..z)missing_values must be strings or unicode)rrrrrrrrrwrr3iterableallupdate)rErrZ testing_valuerrtesterrrrr:s"   zStringConverter.update)N)NNNF)NNrF))rfrgrhrinxZbool_rintegerr rritemsizerrr8Zfloatingr nanZcomplexfloatingr Z longdoubleZunicode_rZstring_rrTZ _defaulttypeZ _defaultfuncZ _defaultfill classmethodrrrrrYrrrerrrrrrrrsB0        ) T-rrc Ksvzt|}Wnrtk rtf|}t|}|dkrHdgt|}nt|tr\|d}||||d}tt||d}YnX|dk rtf|}t|tr|d}|j dkrt |j gt|}|||d}tt t ||}n||t|j |d|_ np|j dk rrtf|}|j t ddtt|j Dkrb|d krb|dgt|j |d|_ n||j |d|_ |S) a6 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', '>> np.lib._iotools.easy_dtype("i4, f8", defaultfmt="field_%03i") dtype([('field_000', '>> np.lib._iotools.easy_dtype((int, float, float), names="a,b,c") dtype([('a', '>> np.lib._iotools.easy_dtype(float, names="a,b,c") dtype([('a', '.r)r3rrrjrbrPrrZrr.rrrSrTra)r/r.rZvalidationargsrrrrrr easy_dtypets<&             $r)N)r"F)F)Nr),ri __future__rrrZ __docformat__sysZnumpyr3Znumpy.core.numericcorenumericrZ numpy.compatrrrr version_infobuiltinsr r r r rrr __builtin__rrr!r-r1r7r<rjr Exceptionrr UserWarningrrrrrrrs8 $   " /d$