bgx dZdZddlZddlmcmZddlm Z m Z ddZ dZ dZ dZdd ZGd d ZGd dZdZGddeZGddeZGddeZGddZddZdS)zBA collection of functions designed to help I/O with ascii files. zrestructuredtext enN)asbytes asunicodecdt|tur|d}||}|S)akDecode 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 Nlatin1)typebytesdecode)lineencodings I/opt/cloudlinux/venv/lib64/python3.11/site-packages/numpy/lib/_iotools.py _decode_liner s6$ DzzU  H{{8$$ KcD |dzn#ttf$rYdSwxYwdS)z2 Check whether obj behaves like a string. FT TypeError ValueErrorobjs r _is_string_liker%s> b z "uu 4 cD |dzn#ttf$rYdSwxYwdS)z8 Check whether obj behaves like a bytes object. rFTrrs r _is_bytes_liker0s> c z "uu 4rc>|jpdD]}||jdSdS)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)ndtypenames r has_nested_fieldsr;s8* " $<  )44 * 5rFc|j}|9|r/|jgttj|jzS|jgSg}|D]:}|j|}t|d|}||;|S)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) rbaseintnpprodshapefields flatten_dtypeextend)r flatten_basertypesfieldinfoflat_dts r r'r'VsB LE }  >K=3rwv|'<'<#=#== = } " "E='D#DG\::G LL ! ! ! ! rc<eZdZdZdZ d dZdZdZd Zd 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. cfdS)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.d|DS)Nc6g|]}|Sr)strip).0_s r z...s ???Aaggii???rr)inputmethods r z(LineSplitter.autostrip..s?????rr)selfr8s `r autostripzLineSplitter.autostrips$@????rN#Tc&t|}t|}||_|t|tr |pd}|j}nt |drW|j}tjdgt|z}dt|dd|ddD}n/t|r|j t|}}n |jd}}||_ |r|||_n||_||_dS)N__iter__rc4g|]\}}t||Srslice)r4ijs r r6z)LineSplitter.__init__..s$JJJ!Qq!JJJr)r comments isinstancestr_delimited_splitterhasattr_variablewidth_splitterr#cumsumlistzipr"_fixedwidth_splitter delimiterr; _handymanr )r:rPrFr;r rQidxs r __init__zLineSplitter.__init__s ++ ))    *Y"<"< !)TI0II Y + + F4I)QC$y//122CJJ3s3B3xQRR3I3IJJJII ^^ F-s9~~"YY'+&> Y"  '!^^I66DNN&DN  rc|j ||jd}|d}|sgS||jS)z2Chop off comments, strip, and split at delimiter. Nrz )rFsplitr3rPr:r s r rIz LineSplitter._delimited_splittersT = $::dm,,Q/Dzz'"" Izz$.)))rc|j |jddsgS|jfdt dt D}fd|DS)Nrz c6g|]}t||zSrr@)r4rBfixeds r r6z5LineSplitter._fixedwidth_splitter..s'JJJ!%1u9%%JJJrc g|] }| Srrr4sr s r r6z5LineSplitter._fixedwidth_splitter..(((AQ(((r)rFrUr3rPrangelen)r:r slicesrYs ` @r rOz!LineSplitter._fixedwidth_splitters = $::dm,,Q/Dzz&!! IJJJJuQD 5/I/IJJJ((((((((rc|j |jdsgS|j}fd|DS)Nrc g|] }| Srrr[s r r6z8LineSplitter._variablewidth_splitter..r]r)rFrUrP)r:r r`s ` r rKz$LineSplitter._variablewidth_splittersQ = $::dm,,Q/D I((((((((rcR|t||jSN)rQr r rVs r __call__zLineSplitter.__call__s ~~l4??@@@r)Nr<TN) __name__ __module__ __qualname____doc__r;rSrIrOrKrerrr r/r/s  @@@(@D!!!!:***))))))AAAAArr/cLeZdZdZgdZedZ d dZd dZd d Z 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') )returnfileprintz~!@#$%^&*()-=+~\|]}[{';: /?.>,.,sArFuc*|Srd)upperrrs r r9z(NameValidator.__init__...AGGIIrlc*|Srd)lowerrrs r r9z(NameValidator.__init__..0rwrz%unrecognized case_sensitive value %s.) r(defaultexcludelist excludelistdefaultdeletecharssetadd deletecharscase_converter startswithr replace_space)r:r|rcase_sensitiverdeletemsgs r rSzNameValidator.__init__s  K42333&  ,FF%%F 3!  "$(>(>"-+D  %%.*C*CC*H*H%"5"5D    & &s + + ""5"5D  9NJCS// !*rf%ic ||dSg}t|tr|g}|?t|}||krt|dg||z zz}n||kr |d|}|j |j}|j}|j}g}t} d} |D]} ||  } |r| d|} d fd| D} | dkr|| z} | |vr| dz } || z} | |v| dz } n | |vr| dz } | | d} | dkr| | d| zzn| | | dz| | <t|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 cg|]}|v| Srr)r4crs r r6z*NameValidator.validate..ls#DDD!q /C/CA/C/C/CrrEr5z_%d)rGrHr_rMrr|rrdictr3replacejoingetappendtuple)r:r defaultfmtnbfieldsnbnamesr|rrvalidatednamesseennbemptyitemcntrs @r validatezNameValidator.validate7s6 M tE eS ! ! IE  %jjG(""U rdh.@&AAH$$ixi(& & ,* vv ! !D!>$''--//D 8||C7777DDDDtDDDEEDrzz!G+emmqLG%/Demm1 $$ ((4##CQww%%dUS[&89999%%d+++qDJJ^$$$rc2||||S)N)rr)r)r:rrrs r rezNameValidator.__call__}s}}UzH}MMMr)NNNr5rN) rfrgrhrir{r~r}rSrrerrr rkrks11f544CDD5947++++6D%D%D%D%LNNNNNNrrkch|}|dkrdS|dkrdStd)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 boolean)rvr)values r str2boolrs>6 KKMME t '  u*+++rceZdZdZdS)ConverterErrorzR Exception raised when an error occurs in a converter for string values. Nrfrgrhrirrr rr DrrceZdZdZdS)ConverterLockErrorzR Exception raised when an attempt is made to upgrade a locked converter. Nrrrr rrrrrceZdZdZdS)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. Nrrrr rrs Drrc eZdZdZejedfejedfgZ ej ejj ej ej j kre ej edfe ejeejfejeejdzfejejejfejedfejeejfejeejdzfejedfejedfgedZedZedZedd Z ed Z! dd Z"d Z#dZ$dZ%dZ&dZ'dZ( 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. FrDyz???c4tj|jS)z(Returns the dtype of the input variable.)r#arraydtypeclsvals r _getdtypezStringConverter._getdtypesx}}""rc>tj|jjS)z4Returns the type of the dtype of the input variable.)r#rrrrs r _getsubdtypezStringConverter._getsubdtype sx}}"''rc>|jtjkr|S|jS)z9Returns dtype for datetime64 and type of dtype otherwise.)rr# datetime64)rrs r _dtypeortypezStringConverter._dtypeortypes  : & &LzrNc|t|dr3|jd||||fdSt|drt |dt t fr"|D]}|jd|dS|dgt|z}nEt |}|dgt|t|z zt||D]8\}}|jd||||f7dSdS)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) rerDNr>r) rJ_mapperinsertrrGrrMr_rrN)rfuncdefaultr5fctdfts r upgrade_mapperzStringConverter.upgrade_mappersZ2 4 $ $ J K  rC$4$4W$=$=tW#M N N N F T: & & J$q'E4=11 ..AK&&r1----&3t99,w--vTS\\)ABCCCg.. J JS ""2(8(8(=(=sC'HIIII J J J Jrct|jD]\}\}}}|j|kr ||||ffcSt|jD],\}\}}}tj|j|r ||||ffcS-t rd) enumeraterrr# issubdtype LookupError)rrrBdeftyper default_defs r _find_map_entryzStringConverter._find_map_entryGs09/E/E 7 7 +A+{zW$$7D+66666%09/E/E 7 7 +A+{}UZ11 77D+66666 7rct||_|2t|_d|_|pd|_t jd}n d|_t j|}n#t$r{t|ds!d}t|t|z||_|) |d}n#t$rd}YnwxYw| |}YnwxYw | |\|_\}}} || |_n7||_n/#t$r"||_|jd\}}}d|_YnwxYw|j||_|j|jddkrjt!|j t jrt j|_n9t!|j t jrt j|_nd |_| d h|_nOt)|t*r|d }t/t1|d gz|_|j|_|||_ d|_||_dS) NrFboolrezOThe input argument `dtype` is neither a function nor a dtype (got '%s' instead)0rDrEc:tt|Srd)r"floatrrs r r9z*StringConverter.__init__..s#eAhh--rr,)r_lockedrr_statusrr#rrrJrrrrrr issubclassuint64int64missing_valuesrGrHrUr~rM _strict_call_callingfunctionr_checked_initial_default) r: dtype_or_funcrrlockedrerrmsgr5rrs r rSzStringConverter.__init__UsF||   DIDL"+eDLHV$$EE 0  // 0 0 0}j99BIF#FT--@-@$@AAA) ?'"&))C..%'''"&'w// 0$ +7;7K7KE7R7R4 4q$ ?#.DLL#*DLL ! ! !& !\"- 4   !y   yDLOA...ej")448 " DII BH558 "DII 7 7DI  !#%$D  .#.. ;!/!5!5c!:!:"%d>&:&:bT&A"B"BD  $ 1%%e,,  'sI A&&AC++CC+ C C+CC+*C+/!D"")E Ec\ ||S#t$r |jcYSwxYwrd)rrrr:rs r _loose_callzStringConverter._loose_callsB 99U## #   <    s ++cV ||}|jtur2 tj||jn#t $rt wxYw|S#t $rE||jvr|j sd|_ |j cYSt d|zwxYw)N)rFzCannot convert string '%s') rr"r#rr OverflowErrorrr3rrrr)r:r new_values r rzStringConverter._strict_calls C %((I yC%HU$)44444$%%%$$%  C C C{{}} 333|*$)DM|###9EABB B  Cs(#AAAAA:B(B(c,||Srd)rrs r rezStringConverter.__call__s$$U+++rc2|jrd}t|t|j}|j}||krd}t |||dz kr|dz }|j|\|_|_}||_|j|j|_ dS||_ dS)Nz*Converter is locked and cannot be upgradedz*Could not find a valid conversion functionrE) rrr_rrrrrrr)r:r _statusmaxrrs r _do_upgradezStringConverter._do_upgrades < -AF$V,, ,&& , j AF (( ( zA~ % % qLG(, W(=% 49g  ,0DLLL"DLLLrcd|_ ||S#t$r,|||cYSwxYw)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)rrrrupgraders r rzStringConverter.upgradesi*  '$$U++ + ' ' '      <<&& & & & 's3AAcd|_t|ds|f}|j} |D] }||dS#t$r-|||YdSwxYw)NTr>)rrJrrr iterupgrade)r:rr_ms r rzStringConverter.iterupgrades uj)) HE(  $ ! ! R     ! ! $ $ $         U # # # # # # $s53A,+A,rc||_||_|5||_||||_nU ||pd}n#t tf$rd}YnwxYw||||_|t|_ dStj |s|g}td|Dst d|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. N1c3@K|]}t|tVdSrd)rGrH)r4vs r z)StringConverter.update..1s,BBaz!S))BBBBBBrz)missing_values must be strings or unicode)rrrrrrrrr~rr#iterableallupdate)r:rr testing_valuerrtesters r rzStringConverter.updates+>    "DL))$..*A*ABBDII m2s33z*    ))$..*@*@AADI  !#&%%D   ;~.. 2"0!1BB>BBBBB M KLLL   & &~ 6 6 6 6 6s AA+*A+rd)NNNF)NNrF)*rfrgrhrinxbool_rint_r"rritemsizerrr(float64rnan complex128complex longdoubleintegerfloatingcomplexfloatingstr_rbytes_r classmethodrrrrrrSrrrerrrrrrr rrsJ..^(E*b!$G rx!HBHRX$6$6$???#r*+++ NNRZ/]GRVb[9]BM26:Zb)[%0'"&2+>Wi/Y/    ##[#(([(  [ &J&J&J[&JP  [ IMG(G(G(G(R   CCC2,,,###('''8 $ $ $8<).777777777777rrrc 4 tj|}|td i|}t|tr|d}|jct|jgt|z}|||}tjtt||}n`||t|j||_n9|jtd i|}tdtt|jD}|j|kr/|dkr)|dgt|jz||_n||j||_n#t$rtd i|}t|}|dgt|z}n*t|tr|d}||||}tjt||}YnwxYw|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', '.zs&"O"O519"O"O"O"O"O"Orrr)formatsrr)r#rrkrGrHrUrrrr_rMrNr^rr)rrrvalidationargsrrnumbered_namesrs r easy_dtyper6sAL$M&!!  $66~66H%%% ) C((|# E :;; :>>>$s5'':':";";<< (xFL8I8I3= ? ? ? \ %$66~66H""O"OeC 22v;; =D3v;;&EE s # # %KK$$EjIII$vU;;;<<=F MsE<rs& ++++++++46,,,,^]A]A]A]A]A]A]A]A@YNYNYNYNYNYNYNYNx!,!,!,H     Y                    q7q7q7q7q7q7q7q7h KKKKKKr