L iEATdZddlmZddlmZmZmZddlZddlm Z ddl m Z m Z ddl mZmZddlmZmZmZmZmZmZmZmZmZd Zed e Zed e Zed ZedZ ddZddZ GddeeefZ!eddZ"eddZ"eddZ"dZ"ddZ#y)a Cycler ====== Cycling through combinations of values, producing dictionaries. You can add cyclers:: from cycler import cycler cc = (cycler(color=list('rgb')) + cycler(linestyle=['-', '--', '-.'])) for d in cc: print(d) Results in:: {'color': 'r', 'linestyle': '-'} {'color': 'g', 'linestyle': '--'} {'color': 'b', 'linestyle': '-.'} You can multiply cyclers:: from cycler import cycler cc = (cycler(color=list('rgb')) * cycler(linestyle=['-', '--', '-.'])) for d in cc: print(d) Results in:: {'color': 'r', 'linestyle': '-'} {'color': 'r', 'linestyle': '--'} {'color': 'r', 'linestyle': '-.'} {'color': 'g', 'linestyle': '-'} {'color': 'g', 'linestyle': '--'} {'color': 'g', 'linestyle': '-.'} {'color': 'b', 'linestyle': '-'} {'color': 'b', 'linestyle': '--'} {'color': 'b', 'linestyle': '-.'} ) annotations)HashableIterable GeneratorN)reduce)productcycle)muladd) TypeVarGenericCallableUnionDictListAnyoverloadcastz0.12.1K)boundLVUc|gk7rtt|ni}|tt|ni}t|j}t|j}||zr t d||zS)a Helper function to compose cycler keys. Parameters ---------- left, right : iterable of dictionaries or None The cyclers to be composed. Returns ------- keys : set The keys in the composition of the two cyclers. z"Can not compose overlapping cycles)nextitersetkeys ValueError)leftrightl_peekr_peekl_keyr_keys U/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/cycler/__init__.py _process_keysr'>sl".2RZd4j)RF.3.?d5k*RF &E &E u}=>> 5=c|j|jk7rJtdj|j|jz|j|jz ttt t tttff|jttt t tttff|jttfd|jDS)aS Concatenate `Cycler`\s, as if chained using `itertools.chain`. The keys must match exactly. Examples -------- >>> num = cycler('a', range(3)) >>> let = cycler('a', 'abc') >>> num.concat(let) cycler('a', [0, 1, 2, 'a', 'b', 'c']) Returns ------- `Cycler` The concatenated cycler. zBKeys do not match: Intersection: {both!r} Disjoint: {just_one!r})bothjust_onec3HK|]}t|||zywN_cycler).0k_l_rs r& zconcat..ts$Ea2a52a5=1Es") rrformatrrrrrrrby_keyrr )r r!r2r3s @@r&concatr7Xs$ yyEJJ ''-vYY+dii%**6L(.(   d1d5A;''($++- 8B d1d5A;''(%,,. 9B #E499E FFr(ceZdZdZdZ d ddZdZeddZddZ e ddZ dd Z d d Z d!d Zed!d Zed"d ZdZed!dZed"dZdZd#dZd$dZd%dZd&dZdZd'dZd'dZd(dZeZd)dZeZy)*Cyclera Composable cycles. This class has compositions methods: ``+`` for 'inner' products (zip) ``+=`` in-place ``+`` ``*`` for outer products (`itertools.product`) and integer multiplication ``*=`` in-place ``*`` and supports basic slicing via ``[]``. Parameters ---------- left, right : Cycler or None The 'left' and 'right' cyclers. op : func or None Function which composes the 'left' and 'right' cyclers. ct|Sr-)r selfs r&__call__zCycler.__call__s T{r(Nct|tr1t|j|j|j|_n2|)|Dcgc]}t j |c}|_ng|_t|tr1t|j|j|j|_nd|_t |j|j|_||_ycc}w)zf Semi-private init. Do not use this directly, use `cycler` function instead. N) isinstancer9_left_right_opcopyr'_keys)r<r r!opvs r&__init__zCycler.__init__s dF #:@ DKK;DJ 1551$))A,5DJDJ eV $/5 U\\5990DKDK*4::t{{C 6sC)c||jvSr-)rD)r<r1s r& __contains__zCycler.__contains__sDJJr(c,t|jS)z!The keys this Cycler knows about.)rrDr;s r&rz Cycler.keyss4::r(c J||k(ry||jvrtd|d|d|d||jvrtd|d|d|d|jj||jj ||j 5||j j vr|j j||yt|jtr|jj||y|jDcgc] }|||i c}|_ ycc}w)a Change a key in this cycler to a new name. Modification is performed in-place. Does nothing if the old key is the same as the new key. Raises a ValueError if the new key is already a key. Raises a KeyError if the old key isn't a key. NzCan't replace z with , z is already a keyz is not a key) rDrKeyErrorremover rAr change_keyr?r@r9)r<oldnewentrys r&rOzCycler.change_keys #:  $**  VC53%7HI  djj  VC53%}E  # s ;; "sdkk.>.>'> KK " "3 , F + JJ ! !#s + :>D3c +DDJDs D cZ|d}tfd|D|_h|_|S)a Class method to create 'base' Cycler objects that do not have a 'right' or 'op' and for which the 'left' object is not another Cycler. Parameters ---------- label : hashable The property key. itr : iterable Finite length iterable of the property values. Returns ------- `Cycler` New 'base' cycler. Nc3&K|]}|i ywr-)r0rFlabels r&r4z$Cycler._from_iter..s1%1s)listr@rD)clsrVitrrets ` r& _from_iterzCycler._from_iters.( I1S11 G  r(cttr7|j}ttfd|j DSt d)Nc3BK|]\}}t||ywr-r.)r0r1rFkeys r&r4z%Cycler.__getitem__..s Mtq!1S6 2Mz+Can only use slices with Cycler.__getitem__)r?slicer6rr itemsr)r<r^transs ` r& __getitem__zCycler.__getitem__s= c5 !KKME#Mu{{}MN NJK Kr(c#<K|j|jD]}t|y|j t d|j|j|jD]-\}}i}|j ||j ||/yw)Nz=Operation cannot be None when both left and right are defined)rAr@dictrB TypeErrorupdate)r<r abouts r&__iter__zCycler.__iter__s ;;   !4j  !xxST[[9 1 1  1   sBBc lt|t|k7r#tdt|dt|ttttt t ftttff|tttt t ftttff|tS)z Pair-wise combine two equal length cyclers (zip). Parameters ---------- other : Cycler z&Can only add equal length cycles, not z and ) lenrr9rrrrrrzipr<others r&__add__zCycler.__add__s t9E "8T 5U U  ad U1a4[014 8 ad U1a4[015 9   r(cyr-rUros r&__mul__zCycler.__mul__$ r(cyr-rUros r&rszCycler.__mul__(rtr(c ttr{tttttt ftt tff|ttttt ftt tfftSttr7|j}ttfd|jDStS)z Outer product of two cyclers (`itertools.product`) or integer multiplication. Parameters ---------- other : Cycler or int c3BK|]\}}t||zywr-r.)r0r1rFrps r&r4z!Cycler.__mul__..>s F1gaU+Fr_)r?r9rrrrrrrintr6rr raNotImplemented)r<rprbs ` r&rszCycler.__mul__,s eV $VE!Q$Kq!t45t<VE!Q$Kq!t45u=  s #KKMEF F " !r(cyr-rUros r&__rmul__zCycler.__rmul__Crtr(cyr-rUros r&r{zCycler.__rmul__Grtr(c ||zSr-rUros r&r{zCycler.__rmul__Ks e|r(ctttti}|jt |j St |j }t |j}||j||Sr-)rnminrr rArmr@rB)r<op_dictl_lenr_lens r&__len__zCycler.__len__Ns\>A3QT=U ;; tzz? "DJJDKK  wtxx ..r(c t|ts tdtj|}t |||_||_t|_t|j |j|j|_ |S)z In-place pair-wise combine two equal length cyclers (zip). Parameters ---------- other : Cycler z"Cannot += with a non-Cycler object) r?r9rfrCr'rDr@rnrBrAr<rpold_selfs r&__iadd__zCycler.__iadd__Xsd%(@A A99T?"8U3  U[[%,, B  r(c t|ts tdtj|}t |||_||_t|_t|j |j|j|_ |S)z In-place outer product of two cyclers (`itertools.product`). Parameters ---------- other : Cycler z"Cannot *= with a non-Cycler object) r?r9rfrCr'rDr@rrBrArs r&__imul__zCycler.__imul__jsd%(@A A99T?"8U3  U[[%,, B  r(ct|tsyt|t|k7ry|j|jz ryt dt ||DS)NFc3,K|] \}}||k(ywr-rU)r0rhris r&r4z Cycler.__eq__..s7da167s)r?r9rmrallrnros r&__eq__z Cycler.__eq__|sM%( t9E " 99uzz !7c$&6777r(c2tdtdi}|j7|jj t fd|D}dd|dS|j |jd}d}|j|j||j S) N+*c3(K|] }| ywr-rUr0rFlabs r&r4z"Cycler.__repr__..s,!qv,zcycler(rL)?z({left!r} {op} {right!r}))r rEr!) rnrrArpoprWgetrBr5r@)r<op_maprYrEmsgrs @r&__repr__zCycler.__repr__ssGS) ;; ))--/C,t,,CSG2cWA. .DHHc*B-C::4::"DKK:H Hr(cd}t|jt}|D] }|d|dz } t|D]}|dz }|D]}|d||dz }|dz }!|d z }|S) Nz)r^zzzzz
z
z
)sortedrreprr)r<output sorted_keysr^dr1s r& _repr_html_zCycler._repr_html_sTYYD1  *C SG5) )F *d A f F  /D1.. / g F   * r(c|j}|Dcic] }|t}}|D] }|D]}||j||"|Scc}w)a Values by key. This returns the transposed values of the cycler. Iterating over a `Cycler` yields dicts with a single value for each key, this method returns a `dict` of `list` which are the values for the given key. The returned value can be used to create an equivalent `Cycler` using only `+`. Returns ------- transpose : dict dict of lists of the values for each key. )rrWappend)r<rr1rjrs r&r6z Cycler.by_keysd*yy48 9qDF 9 9 $A $A ad# $ $ !:sA cl|j}ttd|jDS)z Simplify the cycler into a sum (but no products) of cyclers. Returns ------- simple : Cycler c3:K|]\}}t||ywr-r.r0r1rFs r&r4z"Cycler.simplify..sDdaGAqMD)r6rr ra)r<rbs r&simplifyzCycler.simplifys( cDekkmDEEr()NN)r *Cycler[K, V] | Iterable[dict[K, V]] | Noner!zCycler[K, V] | NonerEr)returnset[K])rPrrQrrNonerVrrY Iterable[V]r Cycler[K, V])r^r`rr)rz!Generator[dict[K, V], None, None])rpz Cycler[L, U]rzCycler[K | L, V | U])rprxrr)rrx)rprrr)rpzCycler[K, V] | intrr)rpobjectrbool)rstr)rzdict[K, list[V]])rr)__name__ __module__ __qualname____doc__r=rGrIpropertyrrO classmethodr[rcrkrqrrsr{rrrr__hash__rrr6 _transposerr7rUr(r&r9r9ws6 &* 8#  @"EH0L  $    ".    /$$8H I <JF Fr(r9cyr-rU)args r&cyclerrr(c yr-rU)kwargss r&rrrr(cyr-rU)rVrYs r&rrrr(cV|r |r tdt|dk(r,t|dts tdt|dSt|dk(rt |St|dkDr td|r%t t d|jDStd) a Create a new `Cycler` object from a single positional argument, a pair of positional arguments, or the combination of keyword arguments. cycler(arg) cycler(label1=itr1[, label2=iter2[, ...]]) cycler(label, itr) Form 1 simply copies a given `Cycler` object. Form 2 composes a `Cycler` as an inner product of the pairs of keyword arguments. In other words, all of the iterables are cycled simultaneously, as if through zip(). Form 3 creates a `Cycler` from a label and an iterable. This is useful for when the label cannot be a keyword argument (e.g., an integer or a name that has a space in it). Parameters ---------- arg : Cycler Copy constructor for Cycler (does a shallow copy of iterables). label : name The property key. In the 2-arg form of the function, the label can be any hashable object. In the keyword argument form of the function, it must be a valid python identifier. itr : iterable Finite length iterable of the property values. Can be a single-property `Cycler` that would be like a key change, but as a shallow copy. Returns ------- cycler : Cycler New `Cycler` for the given property zEcycler() can only accept positional OR keyword arguments -- not both.rzDIf only one positional argument given, it must be a Cycler instance.zdOnly a single Cycler can be accepted as the lone positional argument. Use keyword arguments instead.c3:K|]\}}t||ywr-r.rs r&r4zcycler..sEdaGAqMErz4Must have at least a positional OR keyword arguments)rfrmr?r9r/rr ra)argsrs r&rrsL  S   4yA~$q'6*( d1g Ta~ TQ B  cEfllnEFF J KKr(ct|trB|j}t|dk7r d}t ||j fd|D}tj ||S)aD Create a new `Cycler` object from a property name and iterable of values. Parameters ---------- label : hashable The property key. itr : iterable Finite length iterable of the property values. Returns ------- cycler : Cycler New `Cycler` for the given property rz2Can not create Cycler from a multi-property Cyclerc3(K|] }| ywr-rUrs r&r4z_cycler..;s#!qv#r)r?r9rrmrrr[)rVrYrrrs @r&r/r/"s\ #vxx t9>FCS/ !hhj$s#   UC ((r()r z#Cycler[K, V] | Iterable[dict[K, V]]r!rrr)r rr!z Cycler[K, U]rzCycler[K, V | U])rrrr)rrrzCycler[str, V]r)$r __future__rcollections.abcrrrrC functoolsr itertoolsrr operatorr r typingr r rrrrrrr __version__rrrrr'r7r9rr/rUr(r&rs(V#99 $UUU  Cx  Cx  CL CL - 5 4G>YWQT]Yx       =L@)r(