wL irJ jdZddlZddlZddlZddlZddlZddlZddlmZddlm Z ddl m Z m Z m Z mZmZmZmZmZmZmZmZmZej.dkrddlmZnddl mZGdd eZGd d eZd5d ed edededef dZ deee efde efdZ!d6dedededefdZ"dedefdZ#dedefdZ$dedefdZ%GddeZ&Gdd eZ'Gd!d"e'Z( d7d#e ed$ed%edee efd&Z)d'e ed%ed(ed)ede eeff d*Z*ed+Z+d,e e+d-ed.e+de+fd/Z, d8d0e ed1ed2ed3edef d4Z-y)9z~ Utilities for working with strings and text. Inheritance diagram: .. inheritance-diagram:: IPython.utils.text :parts: 3 N) Formatter)Path) ListDictTupleOptionalcastSequenceMappingAnyUnionCallableIteratorTypeVar) )SelfceZdZUdZeeed<eed<eeed<deefdZe exZ Z defdZ e e xZ ZdefdZe exZZdeefd Ze exZZy ) LSStringa`String derivative with a special access attributes. These are normal strings, but with the special attributes: .l (or .list) : value as list (split on newlines). .n (or .nlstr): original value (the string itself). .s (or .spstr): value as whitespace-separated string. .p (or .paths): list of path objects (requires path.py package) Any values which require transformations are computed only once and cached. Such strings are very useful to efficiently interact with the shell, which typically only understands whitespace-separated options for commands._LSString__list_LSString__spstr_LSString__pathsreturnc~ |jS#t$r%|jd|_|jcYSwxYwN )rAttributeErrorsplitselfs X/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/IPython/utils/text.pyget_listzLSString.get_list<s: ;;  **T*DK;;   +<<c |jS#t$r&|jdd|_|jcYSwxYw)Nr )rrreplacers r! get_spstrzLSString.get_spstrEs< <<  <<S1DL<<  s ,==c|SNrs r! get_nlstrzLSString.get_nlstrN c |jS#t$rb|jdDcgc]-}tjj |s#t |/ncc}wc}|_|jcYSwxYwr)rrrospathexistsrr ps r! get_pathszLSString.get_pathsSs\ <<  -1ZZ-=SPQARDGSSDL<<  s A9$A AA98A9N)__name__ __module__ __qualname____doc__rstr__annotations__rr"propertyllistr'sspstrrr+nnlstrr4r3pathsr*r-r!rr(s M I L $Z$s)!!A 3 ##A4##A 4: ##Ar-rc ^eZdZUdZeed<eed<eeed<defdZ e e xZ Z defdZ e e xZZdefdZe exZZdeefd Ze exZZ dd eeeegej4ed zffd ed eedefdZdeedeeefdZ dd eeededefdZ y )SListaList derivative with a special access attributes. These are normal lists, but with the special attributes: * .l (or .list) : value as list (the list itself). * .n (or .nlstr): value as a string, joined on newlines. * .s (or .spstr): value as a string, joined on spaces. * .p (or .paths): list of path objects (requires path.py package) Any values which require transformations are computed only once and cached. _SList__spstr _SList__nlstr _SList__pathsrc|Sr)r*rs r!r"zSList.get_listzr,r-c~ |jS#t$r%dj||_|jcYSwxYw)Nr%)rErjoinrs r!r'zSList.get_spstrs9 <<  88D>DL<<  r#c~ |jS#t$r%dj||_|jcYSwxYwr)rFrrJrs r!r+zSList.get_nlstrs9 <<  99T?DL<<  r#c |jS#t$rS|Dcgc]-}tjj |s#t |/ncc}wc}|_|jcYSwxYwr))rGrr/r0r1rr2s r!r4zSList.get_pathssR <<  -1GRWW^^A5FDGGGDL<<  s  A*$A A A*)A*Npatternprunefieldc *dtdtffd }ttrfd}n}|s.t||Dcgc]}|||s|c}St||Dcgc]}|||r|c}Scc}wcc}w)aReturn all strings matching 'pattern' (a regex or callable) This is case-insensitive. If prune is true, return all items NOT matching the pattern. If field is specified, the match must occur in the specified whitespace-separated field. Examples:: a.grep( lambda x: x.startswith('C') ) a.grep('Cha.*log', prune=1) a.grep('chm', field=-1) r>rcZ|S|j} |}|S#t$rYywxYw)N)r IndexError)r>partstgtrOs r! match_targetz SList.grep..match_targets=}GGIE El   s  **cNtj|tjSr))research IGNORECASE)xrMs r!zSList.grep..sbiiBMMBr-)r9 isinstancetype)r rMrNrOrVpredels ` ` r!grepz SList.greps* C C  gs #BDD4:DKbDb9I4JrKL L4:DOb\"=M8NrOP PLOsB B ,BBfieldsclt|dk(r|Dcgc]}|jc}St}|Dcgc]}|jc}D]C}g}|D]} |j|||s$|jdj |E|Scc}wcc}w#t$rYVwxYw)a{Collect whitespace-separated fields from string list Allows quick awk-like usage of string lists. Example data (in var a, created by 'a = !ls -l'):: -rwxrwxrwx 1 ville None 18 Dec 14 2006 ChangeLog drwxrwxrwx+ 6 ville None 0 Oct 24 18:05 IPython * ``a.fields(0)`` is ``['-rwxrwxrwx', 'drwxrwxrwx+']`` * ``a.fields(1,0)`` is ``['1 -rwxrwxrwx', '6 drwxrwxrwx+']`` (note the joining by space). * ``a.fields(-1)`` is ``['ChangeLog', 'IPython']`` IndexErrors are ignored. Without args, fields() just split()'s the strings. rr%)lenrrDappendrSrJ)r rbr`resf linepartsfds r!rbz SList.fieldss& v;! )-.2BHHJ. .g&*+1779+ 0BI $$RV,   388I./ 0 /, "sBB" B'' B32B3numsc|+|Dcgc]}t|gj||g!}}n|Dcgc]}||g}}|rctt|D]L}dj ||dDcgc]}|j s|c}} t |}|||d<N|jt||D cgc]} | d c} Scc}wcc}wcc}w#t$rd}YXwxYwcc} w)zsort by specified fields (see fields()) Example:: a.sort(1, nums = True) Sorts a by second field, in numerical order (so that 21 > 3) rRr) rDrbrangerdrJisdigitint ValueErrorsortr^) r rOrjlinedsuichnumstrr@ts r!rqz SList.sorts  DHIDE4&M((/$7ICI-12TD4=2C2 3s8_ s1vay!IBJJL"!IJF AAq    tDz-A1Q4-..J2"J"A .s.$C C-C C  C% C6% C32C3)FN)NF)!r5r6r7r8r9r:rrrr"r;r<r=r'r>r?r+r@rAr4r3rBr rr rXMatchboolrrorarbrqr*r-r!rDrDisG L L $Z$!!A 3 ##A 3 ##A 4: ##A # &QsHcUBHHSMD,@%@AAB&Q&Q} &Q  &QP"d3i"DcO"L&*/S "//  /r-rDinstrnspacesntabsflattenrcBd|zd|zz}|r%tjdtj}n$tjdtj}tj|||}|j t j |zr|dt| S|S)aIndent a string a given number of spaces or tabstops. indent(str, nspaces=4, ntabs=0) -> indent str by ntabs+nspaces. Parameters ---------- instr : basestring The string to be indented. nspaces : int (default: 4) The number of spaces to be indented. ntabs : int (default: 0) The number of tabs to be indented. flatten : bool (default: False) Whether to scrub existing indentation. If True, all lines will be aligned to the same indentation. If False, existing indentation will be strictly increased. Returns ------- str : string indented by ntabs and nspaces.  r%z^\s*^N)rXcompile MULTILINEsubendswithr/lineseprd)rzr{r|r}indpatoutstrs r!indentrs. ,w &Cjj",,/jjr||, VVCe $F rzz#~&jCy!! r-argc,t|tr|gS|S)aAlways return a list of strings, given a string or list of strings as input. Examples -------- :: In [7]: list_strings('A single string') Out[7]: ['A single string'] In [8]: list_strings(['A single string in a list']) Out[8]: ['A single string in a list'] In [9]: list_strings(['A','list','of','strings']) Out[9]: ['A', 'list', 'of', 'strings'] )r]r9)rs r! list_stringsr+s$#su  r-txtwidthmarkc|s||zd|S|t|z dz t|zdz}|dkrd}||z}|d|d|S)aReturn the input string centered in a 'marquee'. Examples -------- :: In [16]: marquee('A test',40) Out[16]: '**************** A test ****************' In [17]: marquee('A test',40,'-') Out[17]: '---------------- A test ----------------' In [18]: marquee('A test',40,' ') Out[18]: ' A test ' Nrr%rd)rrrnmarkmarkss r!marqueerCsY" U FU## 3s8^A D )1 ,E qy% JEs5 ))r-strngcrtjdtj}|jd|}|S)zTFormat a string for screen printing. This removes some latex-type format codes.z\\$rR)rXrrr)rpar_res r! format_screenr\s- ZZr|| ,F JJr% E Lr-textc|jdrtj|S|jdd}t |dk(rtj|S|\}}tj|}dj ||gS)zEquivalent of textwrap.dedent that ignores unindented first line. This means it will still dedent strings like: '''foo is a bar ''' For use in wrap_paragraphs. rrl) startswithtextwrapdedentrrdrJ)rsplitsfirstrests r!rrfsv tt$$ZZQ F 6{at$$KE4 ??4 D 99eT] ##r-c |j}d}t|D]>}tt|dkDrn%|d}|tj vs|dk(r|dz }>ndj |Dcgc]}||d c}}|Scc}w)aNStrip leading email quotation characters ('>'). Removes any combination of leading '>' interspersed with whitespace that appears *identically* in all lines of the input text. Parameters ---------- text : str Examples -------- Simple uses:: In [2]: strip_email_quotes('> > text') Out[2]: 'text' In [3]: strip_email_quotes('> > text\n> > more') Out[3]: 'text\nmore' Note how only the common prefix that appears in all lines is stripped:: In [4]: strip_email_quotes('> > text\n> > more\n> more...') Out[4]: '> text\n> more\nmore...' So if any line has no quote marks ('>'), then none are stripped from any of them :: In [5]: strip_email_quotes('> > text\n> > more\nlast different') Out[5]: '> > text\n> > more\nlast different' rrl>rN) splitlinesziprdsetstring whitespacerJ)rlines strip_len characters prefix_charlns r!strip_email_quotesrs@ OO EI5k  s: ! #  m &++ +{c/A NI   99u5bn5 6D K6s+ Bc 2eZdZdZdedededeeeffdZy) EvalFormattera+A String Formatter that allows evaluation of simple expressions. Note that this version interprets a `:` as specifying a format string (as per standard string formatting), so if slicing is required, you must explicitly create a slice. Note that on Python 3.14+ this version interprets `[]` as indexing operator so you need to use generators instead of list comprehensions, for example: `list(i for i in range(10))`. This is to be used in templating cases, such as the parallel batch script templates, where simple arithmetic on arguments is useful. Examples -------- :: In [1]: f = EvalFormatter() In [2]: f.format('{n//4}', n=8) Out[2]: '2' In [3]: f.format("{greeting[slice(2,4)]}", greeting="Hello") Out[3]: 'll' nameargskwargsrc$t|||}||fSr))eval)r rrrvs r! get_fieldzEvalFormatter.get_fields vv &$wr-N)r5r6r7r8r9r rrr*r-r!rrs02cceCHor-rc8eZdZdZdedeedeeefdefdZy)FullEvalFormatteraiA String Formatter that allows evaluation of simple expressions. Any time a format key is not found in the kwargs, it will be tried as an expression in the kwargs namespace. Note that this version allows slicing using [1:2], so you cannot specify a format string. Use :class:`EvalFormatter` to permit format strings. Examples -------- :: In [1]: f = FullEvalFormatter() In [2]: f.format('{n//4}', n=8) Out[2]: '2' In [3]: f.format('{list(range(5))[2:4]}') Out[3]: '[2, 3]' In [4]: f.format('{3*2}') Out[4]: '6' format_stringrrrcDg}|j|D]z\}}}}|r|j|||rdj||g}t|t |} |j | |} |j|j | d|dj|S)N:rR)parsererJrdict convert_field format_field) r rrrresult literal_text field_name format_spec conversionobjs r!vformatzFullEvalFormatter.vformatsAE B  : =L*k: l+%!$:{*C!DJ:tF|4((j9 d//R893 :6wwvr-N) r5r6r7r8r9r r r rr*r-r!rrs90   (0  ?FsCx?P   r-rc deZdZdZej dZdedee e e e e ffdZ defdZ y)DollarFormatteraFormatter allowing Itpl style $foo replacement, for names and attribute access only. Standard {foo} replacement also works, and allows full evaluation of its arguments. Examples -------- :: In [1]: f = DollarFormatter() In [2]: f.format('{n//4}', n=8) Out[2]: '2' In [3]: f.format('23 * 76 is $result', result=23*76) Out[3]: '23 * 76 is 1748' In [4]: f.format('$a or {b}', a=1, b=2) Out[4]: '1 or 2' z,(.*?)\$(\$?[\w\.]+)(?=([^']*'[^']*')*[^']*$) fmt_stringrc#HKtj||D]\}}}}d}d}|jj|D]N}|j dd\} } | j dr || | zz }n || z| ddfd}|j }P|||dz|||fyw)NrrRrlr$)rr#_dollar_pattern_ignore_single_quotefinditergrouprend) r r literal_txtrrr continue_fromrmnew_txt new_fields r!rzDollarFormatter.parse)s@I *A  [ >C !  (]^44j+zZ Z# [sB B"cy)Nzr*rs r!__repr__zDollarFormatter.__repr__=s"r-N) r5r6r7r8rXrrr9rrr rrr*r-r!rrsR&+5"**7+'[[sCc7I1J(K[(###r-rr<max_rows row_firstc#K|rZt||zt||zdkDz}t|D]+}t|t||Dcgc]}|| c}-ytdt||D] }||||zycc}ww)z5Yield successive max_rows-sized column chunks from l.rN)rdrm)r<rrncolsrtjs r! _col_chunksrEsQ8#A(9A(=>u :A!&q#a&%!89A1Q49 9 :q#a&(+ &AAq8|% % &:sA B  B3B rlistseparator_size displaywidthc tdt|dzD]L}tttt |||}t |}t|}|||dz zz|ksLn|dz r |z |dz znddS)z4Calculate optimal info to columnize a list of stringrlr) num_columnsoptimal_separator_widthr column_widths)rmrdr=mapmaxrsum)rrrrr col_widths sumlengthrs r! _find_optimalrRs!SZ!^,#c;uh #JKL  O J ~3 3| C   !UZ]^U^ y(@eai'Pef ' r-Tmylistrtdefaultc,|t|k\r|S||S)z2return list item number, or default if don't existr)rrtrs r!_get_or_defaultrfsCKayr-list_last_sepsepwrap_item_withct|dk(ry|r|Dcgc] }||| }}t|dk(r|dS|jd|ddD||dScc}w)a Return a string with a natural enumeration of items >>> get_text_list(['a', 'b', 'c', 'd']) 'a, b, c and d' >>> get_text_list(['a', 'b', 'c'], ' or ') 'a, b or c' >>> get_text_list(['a', 'b', 'c'], ', ') 'a, b, c' >>> get_text_list(['a', 'b'], ' or ') 'a or b' >>> get_text_list(['a']) 'a' >>> get_text_list([]) '' >>> get_text_list(['a', 'b'], wrap_item_with="`") '`a` and `b`' >>> get_text_list(['a', 'b', 'c', 'd'], " = ", sep=" + ") 'a + b + c = d' rrRrlc3 K|]}|ywr)r*).0rts r! z get_text_list..s'q's N)rdrJ)rrrritems r! get_text_listrns}. 5zQ ,T>B   5zQQx 'E#2J''%)   sA)rF)rRN*)F)z and z, rR).r8r/rXrsysrwarningsrpathlibrtypingrrrrr r r r r rrr version_infotyping_extensionsrr9rr=rDroryrrrrrrrrrrrrrrr*r-r!rs     g&2$s2$B\/D\/~ #   $ SV FeCcN+S 0**#*#**2$$$6/S/S/dID9 9x-#'-#j49 & Cy & &,0 & d3i & 9!%7:JM #s(^" CLDGaAWY  9  # 47 PS  r-