wL ix JUdZddlmZddlZddlZddlZddlZddlZddlm Z ddl m Z ddl m Z ddlmZddlmZdd lmZdd lmZgd Zd Zeej2d ZdXdZdZdddefdZdddefdZGddZGddeZ dZ!Gdde Z"GddZ#Gdde#Z$Gd d!e#Z%Gd"d#e#Z&Gd$d%Z'Gd&d'Z(Gd(d)Z)Gd*d+Z*d,Z+d-Z,d.Z-d/Z.d0Z/Gd1d2Z0d3Z1d4Z2d5Z3d6Z4d7Z5d8Z6ee7d9< e8Z9iee4e?e,d:d;e@e,dd?eBe-d>d?eCe-d@dAeDe/ee1ee3eje5eje5eje4eje2eje4eje4e9e6iZJeejZLeLeAur e.dBd?eJeL<e.dCdAeJej<e4eJeN<e4eJeO<e4eJeP<iZQee7dD<dEZRdFZSeAjeUeVddGdeWeXge4ZYdHZZdIZ[dJZ\dKZ]dLZ^eSdMdNeZeSdMdOe[eSdMdPe\eSdMdQe]eSdMdRe^e_dSk(r ddTl`maZaGdUdVZbeebdGWyy#e:$re;Z9Y{wxYw)Ya Python advanced pretty printer. This pretty printer is intended to replace the old `pprint` python module which does not allow developers to provide their own pretty print callbacks. This module is based on ruby's `prettyprint.rb` library by `Tanaka Akira`. Example Usage ------------- To directly print the representation of an object use `pprint`:: from pretty import pprint pprint(complex_object) To get a string of the output use `pretty`:: from pretty import pretty string = pretty(complex_object) Extending --------- The pretty library allows developers to add pretty printing rules for their own objects. This process is straightforward. All you have to do is to add a `_repr_pretty_` method to your object and call the methods on the pretty printer passed:: class MyObject(object): def _repr_pretty_(self, p, cycle): ... Here's an example for a class with a simple constructor:: class MySimpleObject: def __init__(self, a, b, *, c=None): self.a = a self.b = b self.c = c def _repr_pretty_(self, p, cycle): ctor = CallExpression.factory(self.__class__.__name__) if self.c is None: p.pretty(ctor(a, b)) else: p.pretty(ctor(a, b, c=c)) Here is an example implementation of a `_repr_pretty_` method for a list subclass:: class MyList(list): def _repr_pretty_(self, p, cycle): if cycle: p.text('MyList(...)') else: with p.group(8, 'MyList([', '])'): for idx, item in enumerate(self): if idx: p.text(',') p.breakable() p.pretty(item) The `cycle` parameter is `True` if pretty detected a cycle. You *have* to react to that or the result is an infinite loop. `p.text()` just adds non breaking text to the output, `p.breakable()` either adds a whitespace or breaks here. If you pass it an argument it's used instead of the default space. `p.pretty` prettyprints another object using the pretty print method. The first parameter to the `group` function specifies the extra indentation of the next line. In this example the next item will either be on the same line (if the items are short enough) or aligned with the right edge of the opening bracket of `MyList`. If you just want to indent something you can use the group function without open / close parameters. You can also use this code:: with p.indent(2): ... Inheritance diagram: .. inheritance-diagram:: IPython.lib.pretty :parts: 3 :copyright: 2007 by Armin Ronacher. Portions (c) 2009 by Robert Kern. :license: BSD License. )contextmanagerN)deque) signature)StringIO)warn)undoc)PYPY)Dict) prettypprint PrettyPrinterRepresentationPrinterfor_typefor_type_by_nameRawTextRawStringLiteralCallExpressionic@ t|||S#t$r|cYSwxYw)zzSafe version of getattr. Same as getattr, but will return ``default`` on any Exception, rather than raising. )getattr Exception)objattrdefaults X/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/IPython/lib/pretty.py _safe_getattrrws* sD'** s  ct|} t|S#t$r( t|tcYS#t$r|cYcYSwxYwwxYw)z Sort the given items for pretty printing. Since some predictable sorting is better than no sorting at all, we sort on the string representation if normal sorting fails. )key)listsortedrstr)itemss r_sorted_for_pprintr#sQ KEe}  %S) ) L s)  A 5A  AA AA FO ct}t|||||}|j||j|j S)z3 Pretty print the object's representation. max_seq_length)rrr flushgetvalue)rverbose max_widthnewliner(streamprinters rr r s@ZF#FGYXfgG NN3 MMO ?? cttj||||}|j||j tjj |tjj y)z, Like `pretty` but print to stdout. r'N)rsysstdoutr r)write)rr+r,r-r(r/s rr r sR$CJJG\jkG NN3 MMOJJWJJr0c.eZdZedZeddZy)_PrettyPrinterBasec#K|xj|z c_ d|xj|zc_y#|xj|zc_wxYww)z/with statement support for indenting/dedenting.N) indentation)selfindents rr:z_PrettyPrinterBase.indentsC F" '     & D   & sA 3A A  A c#K|j|| d|j||y#|j||wxYww)z8like begin_group / end_group but for the with statement.N) begin_group end_group)r9r:opencloses rgroupz_PrettyPrinterBase.groups< & *  NN65 )DNN65 )sA-AAAN)rrr)__name__ __module__ __qualname__rr:r@r0rr6r6s(''**r0r6cZeZdZdZddefdZdZdZdZddZ d Z dd Z d Z dd Z d Zy)r a Baseclass for the `RepresentationPrinter` prettyprinter that is used to generate pretty reprs of objects. Contrary to the `RepresentationPrinter` this printer knows nothing about the default pprinters or the `_repr_pretty_` callback method. r$r%c||_||_||_||_d|_d|_t |_td}|g|_ t||_ d|_ yNr) outputr,r-r( output_width buffer_widthrbufferGroup group_stack GroupQueue group_queuer8)r9rHr,r-r( root_groups r__init__zPrettyPrinter.__init__s` " ,g 1X &<%j1r0cr|jrq|jj}|j|j|j|_|xj |j zc_|jrq|jrt|jdtr|jj}|j|j|j|_|xj |j zc_|jrt|jdtryyyyrG) breakablesrKpopleftrHrIrJwidth isinstanceText)r9r@xs r_break_one_groupzPrettyPrinter._break_one_groups ##%A !d6G6G HD     ( kkjQ> ##%A !d6G6G HD     ( kkjQ>k>kr0c|j|j|jzkrV|jj }|sy|j ||j|j|jzkrUyyN)r,rIrJrOdeqrYr9r@s r_break_outer_groupsz!PrettyPrinter._break_outer_groupssfnnt0043D3DDD$$((*E  ! !% ( nnt0043D3DDDr0ct|}|jr||jd}t|ts%t}|jj ||j |||xj |z c_|jy|jj||xj|z c_ y)zAdd literal text to the output.N) lenrKrVrWappendaddrJr^rHr4rI)r9rrUtexts rrdzPrettyPrinter.textsC ;;;;r?DdD)v ""4( HHS%    &   $ $ & KK  c "    & r0ct|}|jd}|jrv|j|jj |j |jj d|jz|j|_d|_ y|jjt||||xj|z c_ |jy)z Add a breakable separator to the output. This does not mean that it will automatically break here. If no breaking on this position takes place the `sep` is inserted which default to one space. r` rN)rarM want_breakr)rHr4r-r8rIrJrKrb Breakabler^)r9seprUr@s r breakablezPrettyPrinter.breakables C  $    JJL KK  dll + KK  cD$4$44 5 $ 0 0D  !D  KK  yeT: ;    &   $ $ &r0cH|jj}|r|j||j|jj |j |jj d|jz|j|_d|_ y)z_ Explicitly insert a newline into the output, maintaining correct indentation. rfrN) rOr\rYr)rHr4r-r8rIrJr]s rbreak_zPrettyPrinter.break_s{  $$&   ! !% (  $,,' # 0 001 ,,r0c |r|j|t|jdjdz}|jj ||j j ||xj|z c_y)z Begin a group. The first parameter specifies the indentation for the next line (usually the width of the opening text), the second the opening text. All parameters are optional. r`N)rdrLrMdepthrbrOenqr8)r9r:r>r@s rr<zPrettyPrinter.begin_groupsk  IIdOd&&r*00145 & U# F"r0c#Kt|D]Z\}}|jrC||jk\r4|jd|j|jdy||f\yw)z>like enumerate, but with an upper limit on the number of items,...N) enumerater(rdrj)r9seqidxrXs r _enumeratezPrettyPrinter._enumerates`n FC""sd.A.A'A #  % q&L  sA*A,c|xj|zc_|jj}|js|jj ||r|j |yy)z0End a group. See `begin_group` for more details.N)r8rMpoprSrOremoverd)r9dedentr?r@s rr=zPrettyPrinter.end_group)sX F"  $$&    # #E *  IIe  r0c|jD];}|xj|j|j|jz c_=|jjd|_y)z&Flush data that is left in the buffer.rN)rKrIrHclearrJ)r9datas rr)zPrettyPrinter.flush2sUKK MD   T[[$:K:K!L L  M r0N)rf)rr)rArBrC__doc__MAX_SEQ_LENGTHrQrYr^rdrjrlr<rwr=r)rDr0rr r sB*,T. )) ''&  #r0r ct|ds/ t|j|tfi}|jdd}|S|j}|S#t $r|g}Y|SwxYw)z| Get a reasonable method resolution order of a class and its superclasses for both old-style and new-style classes. __mro__rnr`)hasattrtyperAobjectr TypeError) obj_classmros r_get_mror:sz 9i ( *Y//)V1DbII ##Ab)C J J +C J sA AAc2eZdZdZddddddefdZdZdZy) ra Special pretty printer that has a `pretty` method that calls the pretty printer for a python object. This class stores processing data on `self` so you must *never* use this class in a threaded environment. Always lock it or reinstanciate it. Instances also have a verbose flag callbacks can access to control their output. For example the default instance repr prints all attributes and methods that are not prefixed by an underscore if the printer is in verbose mode. Fr$r%Nc tj|||||||_g|_|tj }||_|tj }||_|tj }||_ y)Nr') r rQr+stack_singleton_pprinterscopysingleton_pprinters_type_pprinterstype_pprinters_deferred_type_pprintersdeferred_pprinters) r9rHr+r,r-rrrr(s rrQzRepresentationPrinter.__init__\s tVYP^_   &"6";";"= #6  !,113N,  %!9!>!>!@ "4r0ct|}||jv}|jj||j t |ddxs t |} |j |}|||||j|jjS#ttf$rYnwxYwt|D]a}||jvrC|j||||c|j|jjS|j|}|6||||c|j|jjSd|jvrM|j}t!|r6||||c|j|jjS|t"usd|jvst!t |dds+t%|||c|j|jjSt'||||j|jjS#|j|jjwxYw)zPretty print the given object. __class__N _repr_pretty___repr__)idrrbr<rrrr=ryrKeyErrorrr_in_deferred_types__dict__rcallabler _repr_pprint_default_pprint)r9robj_idcyclerr/clsmeths rr zRepresentationPrinter.prettyms&C$**$ &! - %c;=JcI 1226:sD%0H NN  JJNN Qx(   * B$---34..s3CuEE: NN  JJNN 7#55c:G*&sD%880 NN  JJNN '+cll:##4D'~'+Cu'= = NN  JJNN  v- *cll : !)sJ)M N#/T5#AA NN  JJNN C B<#3e4 NN  JJNN  NN  JJNN sZI !B$0 I $B63I 5B668I I "0I =I I I /I ( I ,J ct|dd}t|dd}||f}d}||jvr*|jj|}||j|<|S)a Check if the given class is specified in the deferred type registry. Returns the printer from the registry if it exists, and None if the class is not in the registry. Successful matches will be moved to the regular type registry for future use. rBNrA)rrryr)r9rmodnamerr/s rrz(RepresentationPrinter._in_deferred_typessgCt4S*d3Dk $)) )--11#6G'.D   $r0)rArBrCrrrQr rrDr0rrrMs* (-D $%5"3jr0rceZdZdZy) Printablec|Sr[rDr9r.rIs rrHzPrintable.outputsr0N)rArBrCrHrDr0rrrsr0rceZdZdZdZdZy)rWc g|_d|_yrG)objsrUr9s rrQz Text.__init__s  r0cd|jD]}|j|||jzSr[)rr4rU)r9r.rIrs rrHz Text.outputs099 C LL  djj((r0cd|jj||xj|z c_yr[)rrbrU)r9rrUs rrczText.adds"  e r0N)rArBrCrQrHrcrDr0rrWrWs) r0rWceZdZdZdZy)rhc||_||_||_|j|_|jd|_|j j j|y)Nr`)rrUr r8rMr@rSrb)r9rurUr s rrQzBreakable.__init__sO  !--''+  $$T*r0c|jjj|jjrO|j |j j |j d|jz|jS|jjs/|j jj|j|j |j||jzS)Nrf) r@rSrTrgr4r r-r8rOrzrrUrs rrHzBreakable.outputs %%' :: LL,, - LLt/// 0## #zz$$ KK # # * *4:: 6 TXXdjj((r0N)rArBrCrQrHrDr0rrhrhs + )r0rhceZdZdZy)rLc>||_t|_d|_y)NF)rorrSrg)r9ros rrQzGroup.__init__s 'r0N)rArBrCrQrDr0rrLrLs r0rLc$eZdZdZdZdZdZy)rNcBg|_|D]}|j|yr[)queuerp)r9groupsr@s rrQzGroupQueue.__init__s#  E HHUO r0c|j}|t|jdz kDr7|jjg|t|jdz kDr7|j|j|y)Nrn)rorarrb)r9r@ros rrpzGroupQueue.enqs` c$**o)) JJ  b !c$**o)) 5  'r0c|jD]M}tt|D]!\}}|js||=d|_|ccS|D] }d|_ |dd=Oy)NT)rrtreversedrSrg)r9rrvr@s rr\zGroupQueue.deqsnZZ E'8 ! U##c '+E$ L  !  (#'  (a r0ct |j|jj|y#t$rYywxYwr[)rrorz ValueErrorr]s rrzzGroupQueue.removes4  JJu{{ # * *5 1   s (+ 77N)rArBrCrQrpr\rzrDr0rrNrNs ( r0rNceZdZdZdZdZy)rz Object such that ``p.pretty(RawText(value))`` is the same as ``p.text(value)``. An example usage of this would be to show a list as binary numbers, using ``p.pretty([RawText(bin(i)) for i in integers])``. c||_yr[valuer9rs rrQzRawText.__init__  r0c:|j|jyr[)rdr)r9prs rrzRawText._repr_pretty_s tzzr0NrArBrCrrQrrDr0rrrs r0rc,eZdZdZdZedZdZy)rzY Object which emits a line-wrapped call expression in the form `__name(*args, **kwargs)` c2|}||_||_||_yr[)rargskwargs)_CallExpression__self_CallExpression__namerrr9s rrQzCallExpression.__init__s   r0cfd}|S)Ncg|i|Sr[rD)rrrrs rinnerz%CallExpression.factory..inner!st-d-f- -r0rD)rrrs`` rfactoryzCallExpression.factorys  . r0cdfd}|jdz}jt||d5|jD]}|j ||j j D]F\}}||dz}jt||5j |dddH dddy#1swY\xYw#1swYyxYw)NFcPr!jdjdy)NrrT)rdrj)rstartedsrnew_itemz.CallExpression._repr_pretty_..new_item*ss  Gr0()=)rr@rarr rr") r9rrrprefixargarg_name arg_prefixrs ` @rrzCallExpression._repr_pretty_%s S WWS[&# . "yy    "&!2!2!4 " # %^ WWS_j9"HHSM"" "  " """ " "s$A1C'C 9 C C CC"N)rArBrCrrQ classmethodrrrDr0rrrs#c "r0rceZdZdZdZdZy)rz/ Wrapper that shows a string with a `r` prefix c||_yr[rrs rrQzRawStringLiteral.__init__?rr0ct|j}|dddvr|dd}d}nd}||jddz}|j|y)NrnuUurrz\\\)reprrreplacerd)r9rr base_reprrs rrzRawStringLiteral._repr_pretty_BsV$ Ra=D !!" IFFY..vt<<  yr0NrrDr0rrr=s9r0rcHt|ddxs t|}t|ddtjurt |||y|j dd|j ||jdt|z|r|jdn|jrd}t|D]}|jd r t||}t|tj r=|s|jd |j#|j||jd t%|dz}|xj&|z c_|j ||xj&|zc_d }|j)dd y#t$rYwxYw)zw The default print function. Used if an object does not provide one and it's none of the builtin objects. rNrrn)rrrrrr<r rdrr+dir startswithrAttributeErrorrVtypes MethodTyperjrar8r=)rrrklassfirstrrsteps rrrMsV #{D 1 >T#YEUJ-V__DS!U#MM!SHHUOFF:3    v s8 C>>#&#C-EeU%5%56FF3K s s 3x!| %  % # $KK3&s F F! F!cfd}|S)z| Factory that returns a pprint function useful for sequences. Used by the default pprint for tuples and lists. c|r|jdzzSt}|j||j|D]9\}}|r!|jd|j |j |;t|dk(r!t |tr|jd|j|y)Nrsrrrn) rdrar<rwrjr rVtupler=)rrrrrvrXendstarts rrz$_seq_pprinter_factory..innerxs 66%%-#-. .5z dE"ll3' FCs  HHQK   s8q=ZU3 FF3K D#r0rDrrrs`` r_seq_pprinter_factoryrs  Lr0cfd}|S)zP Factory that returns a pprint function useful for sets and frozensets. c|r|jdzzSt|dk(r(|jt|jdzyt}|j ||j rt||j k\s t |}n|}|j|D]9\}}|r!|jd|j|j|;|j|y)Nrsrz()rr) rdrarrAr<r(r#rwrjr r=) rrrrr"rvrXrrs rrz$_set_pprinter_factory..inners 66%%-#-. . s8q= FF49%%, -u:D MM$ &$$SQ5E5E)E*3/,,u- QFF3KKKM   KKc "r0rDrs`` r_set_pprinter_factoryrs#( Lr0cfd}|S)zj Factory that returns a pprint function used by the default pprint of dicts and dict proxies. c|r|jdSt}|j||j}|j |D]^\}}|r!|jd|j |j ||jd|j ||`|j|y)Nz{...}rrz: )rdrar<keysrwrjr r=) rrrrrrvrrrs rrz%_dict_pprinter_factory..inners 66'? "5z dE"xxz T* HCs  HHSM FF4L HHSX    D#r0rDrs`` r_dict_pprinter_factoryrrr0cb|jdd|j|j|jd|j t r.|j j}|j||urdn|n|j|j|jddy)zThe pprint for the super type.z*+ *S\\*+r0ctt|Dcgc]}dt|vs|c}ddtgk7rt|||yt |dd} |j }t |ts td |dvr|j|y|j|dz|zycc}w#t$r!|j}t |tsd}Y\wxYw) z!The pprint for classes and types.rNrnrBz Try __name__z)N __builtin__builtins exceptions.) rrvarsrrrCrVr!rrArd)rrrmrrs r _type_pprintr&sDI&@a*Q*?@!DNS!U# \4 0C $$$N+ +% == t  sSy4 'A $||$$#D$sB.B.'B33'CCct|}|j}|j5t|D](\}}|r|j |j |* dddy#1swYyxYw)z9A pprint that just redirects to the normal repr function.N)r splitlinesr@rtrlrd)rrrrHlinesrv output_lines rrrsj#YF    E  )% 0 C FF;     s 7A--A6ct|d|j}|j}|r |dvr|dz|z} |tt |z}|j d|zy#t $r|}Y"wxYw)z4Base pprint for all functions and builtin functions.rC)r r!r"r#z N)rrArBr!rrrd)rrrrrfunc_defs r_function_pprintr-sw ncll ;D ..C sCCSy4Ys^,,xFF?X %& xsA A-,A-c  t|jd|jj}|jjdvr|jjd|}|j t |gt|ddy)zBase pprint for all exceptions.rC)r"r!r#rrDN)rrrArBr r)rrrrs r_exception_pprintr/(sd 3==.#--2H2H ID }}'AA--22D9HH^D <73#; <=r0_exception_baserr[]{}z frozenset({z})zenviron{zmappingproxy({rcHtj|d}| |t|<|S)z0 Add a pretty printer for a given type. N)rget)typfuncoldfuncs rrr_s+!!#t,G # Nr0cP||f}tj|d}| |t|<|S)z| Add a pretty printer for a type specified by the module and name of a type rather than the type object itself. N)rr6) type_module type_namer8rr9s rrris6  "C&**35G (, % Nr0Tctj|jj}|r!|j |t dy|j ||j t|yNrs)rrrrAr rdefault_factorydictrrrcls_ctors r_defaultdict_pprintrC{sQ%%cmm&<&<=H  '%.)* #--tCy9:r0c<tj|jj}|r!|j |t dyt |r/|j |t|jy|j |yr>) rrrrAr rrarr"rAs r_ordereddict_pprintrEse%%cmm&<&<=H  '%.)* S $syy{+,- r0cNtj|jj}|r!|j |t dy|j -|j |t||j y|j |t|y)Nrs)maxlen)rrrrAr rrGrrAs r _deque_pprintrHsn%%cmm&<&<=H  '%.)*   $s)CJJ78 $s)$%r0c<tj|jj}|r!|j |t dyt |r/|j |t|jy|j |yr>) rrrrAr rrar@ most_commonrAs r_counter_pprintrKsf%%cmm&<&<=H  '%.)* S $s0123 r0ctj|jj}|r!|j |t dy|j ||j yr>)rrrrAr rr~rAs r_userlist_pprintrMsI%%cmm&<&<=H  '%.)* #((#$r0 collections defaultdict OrderedDictrCounterUserList__main__) randrangeceZdZdZdZy)Foocd|_tjd|_tj t dtdd|_d|_ dd|g|_ y)Nrnz\s+(gg@blubblah) foorcompilebarr@fromkeysrangerTrZheherrs rrQz Foo.__init__sKDHzz&)DH eBi1b1ABDI$DI.DIr0ctdy)Nr\)printrs rget_fooz Foo.get_foos %Lr0N)rArBrCrQrdrDr0rrVrVs  / r0rV)r+r[)cr contextlibrdatetimeosrr2rrNrinspectriorwarningsrIPython.utils.decoratorsrIPython.utils.py3compatr typingr __all__rrr]_re_pattern_typerr#r r r6r rrrrWrhrLrNrrrrrrrrr rrr&rr-r/__annotations__ BaseExceptionr0 NameErrorrintfloatr!rrr@set frozensetsuper FunctionTypeBuiltinFunctionTyperSimpleNamespace timedeltarenviron _env_typeMappingProxyTypeslicer`bytesrrrr_maprEllipsisNotImplementedrrCrErHrKrMrArandomrTrVrDr0rrs]~& *( U  2'  T.T.**(}&}@&eMeP 9  ) ), I >  %"%"P   #L,6,   ,<,!<   '> #O     5c3 ?   5c3 ?   6sC @  5c3 ? 5mT J  2   0  0    =   !"  #$ 1%,    D!7 C!HOI*@AQSW*X&&'%%%"$$# }}SdD%&4.6&78DF;&% /BC /BC7 ?; ,<= z    35$o O s$JJ"!J"