
    -hj                        S r SSKJr  SSKrSSKrSSKJr  SSKJr  SSKJr  SSKJ	r	  / S	Qr
S
 r " S S\5      r " S S\R                  " \5      5      r " S S\5      rS r " S S\R                  " \\5      5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r\r\r\r\r\r\R:                  " 5       4S jrS rS r S r!S  r"g)!aA  Visitor/traversal interface and library functions.

SQLAlchemy schema and expression constructs rely on a Python-centric
version of the classic "visitor" pattern as the primary way in which
they apply functionality.  The most common use of this pattern
is statement compilation, where individual expression classes match
up to rendering methods that produce a string result.   Beyond this,
the visitor system is also used to inspect expressions for various
information and patterns, as well as for the purposes of applying
transformations to expressions.

Examples of how the visit system is used can be seen in the source code
of for example the ``sqlalchemy.sql.util`` and the ``sqlalchemy.sql.compiler``
modules.  Some background on clause adaption is also at
https://techspot.zzzeek.org/2008/01/23/expression-transformations/ .

    )dequeN   )exc)util)langhelpers)symbol)	iteratetraverse_usingtraversecloned_traversereplacement_traverseTraversibleTraversibleTypeExternalTraversalInternalTraversalc                 X  ^ U R                   nSU R                  ;   a  U R                  U l        g[	        U[
        R                  R                  5      (       d#  [        R                  " SU R                  -  5      eSU-  n[        R                  " U5      mU4S jnU=U l        U l        g)z`Generate a _compiler_dispatch() external traversal on classes with a
__visit_name__ attribute.

_compiler_dispatchNz>__visit_name__ on class %s must be a string at the class levelvisit_%sc                 |   >  T" U5      nU" U 40 UD6$ ! [          a  nUR                  " X40 UD6s SnA$ SnAff = f)zgLook for an attribute named "visit_<visit_name>" on the
visitor, and call it with the same kw params.

N)AttributeErrorvisit_unsupported_compilation)selfvisitorkwmetherrgetters        lC:\Users\ROHAN GUPTA\OneDrive\Desktop\mathbuddy-assessment\venv\Lib\site-packages\sqlalchemy/sql/visitors.pyr   7_generate_compiler_dispatch.<locals>._compiler_dispatchG   sM    
	$'?D
 ###	  	J88IbII	Js    
;6;;)__visit_name____dict__r   _original_compiler_dispatch
isinstancer   compatstring_typesr   InvalidRequestError__name__operator
attrgetter)cls
visit_namenamer   r   s       @r   _generate_compiler_dispatchr-   0   s    
 ##Js||+ +.*@*@'j$++":":;;%%Lll
 	

 
"D  &F$ 	C'    c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )r   Y   a  Metaclass which assigns dispatch attributes to various kinds of
"visitable" classes.

Attributes include:

* The ``_compiler_dispatch`` method, corresponding to ``__visit_name__``.
  This is called "external traversal" because the caller of each visit()
  method is responsible for sub-traversing the inner elements of each
  object. This is appropriate for string compilers and other traversals
  that need to call upon the inner elements in a specific pattern.

* internal traversal collections ``_children_traversal``,
  ``_cache_key_traversal``, ``_copy_internals_traversal``, generated from
  an optional ``_traverse_internals`` collection of symbols which comes
  from the :class:`.InternalTraversal` list of symbols.  This is called
  "internal traversal" MARKMARK

c                 \   > US:w  a  SU;   a  [        U 5        [        [        U ]  XU5        g )Nr   r    )r-   superr   __init__)r*   clsnamebasesclsdict	__class__s       r   r3   TraversibleType.__init__m   s,    m#7*+C0os,WWEr.    )r'   
__module____qualname____firstlineno____doc__r3   __static_attributes____classcell__r7   s   @r   r   r   Y   s    &F Fr.   r   c                   R    \ rS rSrSrS r\R                  " S5      SS j5       rSr	g)	r   u   z]Base class for visitable objects, applies the
:class:`.visitors.TraversibleType` metaclass.

c                     U $ Nr9   )r*   keys     r   __class_getitem__Traversible.__class_getitem__{   s    
r.   zsqlalchemy.sql.traversalsr9   c                   ^^ [         R                  R                  n U R                  nUR
                  R                  n[        R                  R                  UU4S jU" XS5       5       5      $ ! [         a    / s $ f = f)a  Return immediate child :class:`.visitors.Traversible`
elements of this :class:`.visitors.Traversible`.

This is used for visit traversal.

\**kw may contain flags that change the collection that is
returned, for example to return a subset of items in order to
cut down on larger traversals, or to return child items from a
different context (such as schema-level collections instead of
clause-level).

c              3   V   >#    U  H  u  pnUT;  d  M  Uc  M  U" U40 TD6v   M      g 7frD   r9   ).0attrnameobjr   r   
omit_attrss       r   	<genexpr>+Traversible.get_children.<locals>.<genexpr>   s?      -
(#t z)	  /2	 DOO(s   )))!_generated_get_children_traversal)
r   	preloadedsql_traversals_traverse_internalsr   _get_childrenrun_generated_dispatch	itertoolschainfrom_iterable)r   rM   r   
traversalstraverse_internalsdispatchs    ``   r   get_childrenTraversible.get_children   s}     ^^22
	!%!9!9
 ++BB,, -
'/*M(-
 
 	
  	I	s   A1 1B ?B N)r9   )
r'   r:   r;   r<   r=   rF   r   preload_moduler\   r>   r9   r.   r   r   r   u   s,    
 
45
 6
r.   r   c                   (   ^  \ rS rSrU 4S jrSrU =r$ )_InternalTraversalType   c                 |  > U R                   S;   a  0 nUR                  5        HO  u  pVUR                  S5      (       d  M  UR                  SS5      nUR                  nX;  d   U5       eU=XF'   XH'   MQ     [        U S5      (       a  UR                  U R                  5        X@l        [        [        U ]+  XU5        g )N)r   ExtendedInternalTraversaldp_visit__dispatch_lookup)r'   items
startswithreplacer,   hasattrupdaterf   r2   r`   r3   )
r*   r4   r5   r6   lookuprE   sym	visit_keysym_namer7   s
            r   r3   _InternalTraversalType.__init__   s    <<MMF#MMO>>%(( #E8 <I"xxH#1;8;15>>FK&"2 , s.//c223#) $c3GGLr.   r9   )r'   r:   r;   r<   r3   r>   r?   r@   s   @r   r`   r`      s    M Mr.   r`   c                    / nU HD  u  pEU R                  U5      nU(       d  M  [        R                  U   nUR                  XG45        MF     SSR	                  S U 5       5      -   S-   nSU-  U-   S-   n	[
        R                  " U	0 U5      $ )Nz    return [
z, 
c              3   D   #    U  H  u  pS U< SU< SU< S3v   M     g7f)z	        (z, self.z
, visitor.)Nr9   )rJ   rK   r+   s      r   rN   '_generate_dispatcher.<locals>.<genexpr>   s'       -2(Hj Xz3,1s    z
    ]
zdef %s(self, visitor):

)r[   rc   rf   appendjoinr   _exec_code_in_env)
r   internal_dispatchmethod_namenamesrK   	visit_symr   r+   code	meth_texts
             r   _generate_dispatcherr      s    E0	*42CCINJLL(/0	  1 
KK  -2 	
 	 	 ,k9TADHI((BDDr.   c                   r   \ rS rSrSrS rS rS r\" S5      r	 \" S5      r
 \" S5      r \" S	5      r \" S
5      r \" S5      r \" S5      r \" S5      r\" S5      r\" S5      r \" S5      r \" S5      r \" S5      r \" S5      r \" S5      r \" S5      r \" S5      r \" S5      r \" S5      r \" S5      r \" S5      r \" S5      r \" S5      r \" S5      r  \" S5      r! \" S5      r"\" S 5      r#\" S!5      r$ \" S"5      r% \" S#5      r& \" S$5      r' \" S%5      r( \" S&5      r)S'r*g())r      a,  Defines visitor symbols used for internal traversal.

The :class:`.InternalTraversal` class is used in two ways.  One is that
it can serve as the superclass for an object that implements the
various visit methods of the class.   The other is that the symbols
themselves of :class:`.InternalTraversal` are used within
the ``_traverse_internals`` collection.   Such as, the :class:`.Case`
object defines ``_traverse_internals`` as ::

    _traverse_internals = [
        ("value", InternalTraversal.dp_clauseelement),
        ("whens", InternalTraversal.dp_clauseelement_tuples),
        ("else_", InternalTraversal.dp_clauseelement),
    ]

Above, the :class:`.Case` class indicates its internal state as the
attributes named ``value``, ``whens``, and ``else_``.    They each
link to an :class:`.InternalTraversal` method which indicates the type
of datastructure referred towards.

Using the ``_traverse_internals`` structure, objects of type
:class:`.InternalTraversible` will have the following methods automatically
implemented:

* :meth:`.Traversible.get_children`

* :meth:`.Traversible._copy_internals`

* :meth:`.Traversible._gen_cache_key`

Subclasses can also implement these methods directly, particularly for the
:meth:`.Traversible._copy_internals` method, when special steps
are needed.

.. versionadded:: 1.4

c                 8    U R                   U   n[        XS5      $ )zaGiven a method from :class:`.InternalTraversal`, return the
corresponding method on a subclass.

N)rf   getattr)r   visit_symbolr,   s      r   r[   InternalTraversal.dispatch   s!    
 $$\2t4((r.   c                      UR                   R                  U   nU" X5      $ ! [         a    U R                  UR                   X#5      n N0f = frD   )r7   r!   KeyErrorgenerate_dispatch)r   targetry   generate_dispatcher_name
dispatchers        r   rU   (InternalTraversal.run_generated_dispatch   sZ    		))223KLJ &''  	
 //  "3J	s   # &AAc                 6    [        XU5      n[        XU5        U$ rD   )r   setattr)r   
target_clsry   r   r   s        r   r   #InternalTraversal.generate_dispatch  s&     *%=

 	
jAr.   HCHLCEFCCTSCLCTEOWCCOSSLANBOTPDDOCDMDAKPODDPSTHSJMESHUKDML_OVDML_VDML_MVPAr9   N)+r'   r:   r;   r<   r=   r[   rU   r   r   dp_has_cache_keydp_has_cache_key_listdp_clauseelement)dp_fromclause_canonical_column_collectiondp_clauseelement_tuplesdp_clauseelement_listdp_clauseelement_tupledp_executable_optionsdp_with_context_optionsdp_fromclause_ordered_set	dp_stringdp_string_listdp_anon_name
dp_booleandp_operatordp_typedp_plain_dictdp_dialect_optionsdp_string_clauseelement_dictdp_string_multi_dictdp_annotations_keydp_plain_objdp_named_ddl_elementdp_prefix_sequencedp_table_hint_listdp_setup_join_tupledp_memoized_select_entitiesdp_statement_hint_listdp_unknown_structuredp_dml_ordered_valuesdp_dml_valuesdp_dml_multi_valuesdp_propagate_attrsr>   r9   r.   r   r   r      s   $L)( d|/"4L8d|<06t-	 %Um
 #4L $D\ #4L$Tl &tKsI D\N"$<L J +K SkG 4LM  ,#)$< 
 "$<
   $<L "$<  
  
 !,"(,#D\ "$< #8,+ 7OM
 !*
  )r.   r   c                       \ rS rSrSr\" S5      r \" S5      r \" S5      r \" S5      r	 \" S5      r
 \" S5      rS	rg
)rc   i  aG  Defines additional symbols that are useful in caching applications.

Traversals for :class:`_expression.ClauseElement` objects only need to use
those symbols present in :class:`.InternalTraversal`.  However, for
additional caching use cases within the ORM, symbols dealing with the
:class:`.HasCacheKey` class are added here.

IGISMMTHTILr9   N)r'   r:   r;   r<   r=   r   	dp_ignoredp_inspectabledp_multidp_multi_listdp_has_cache_key_tuplesdp_inspectable_listr>   r9   r.   r   rc   rc     si     tI D\N% c{H 4LM' %Tl
 !,r.   rc   c                   h    \ rS rSrSr0 rS rS rS r\	R                  S 5       r\S 5       rS rS	rg
)r   i  zBase class for visitor objects which can traverse externally using
the :func:`.visitors.traverse` function.

Direct usage of the :func:`.visitors.traverse` function is usually
preferred.

c                     U R                    H/  n[        USUR                  -  S 5      nU(       d  M&  U" U40 UD6s  $    g )Nr   )visitor_iteratorr   r    )r   rL   r   vr   s        r   traverse_single!ExternalTraversal.traverse_single  s>    &&A1j3+=+==tDDtC2& 'r.   c                 ,    [        XR                  5      $ )zQTraverse the given expression structure, returning an iterator
of all elements.

)r	   __traverse_options__r   rL   s     r   r	   ExternalTraversal.iterate  s    
 s5566r.   c                 B    [        XR                  U R                  5      $ 2Traverse and visit the given expression structure.)r   r   _visitor_dictr   s     r   r   ExternalTraversal.traverse  s     668J8JKKr.   c                 ~    0 n[        U 5       H+  nUR                  S5      (       d  M  [        X5      XSS  '   M-     U$ )Nre      )dirrh   r   )r   visitorsr,   s      r   r   ExternalTraversal._visitor_dict  s>    IDx((%,T%8ab"  r.   c              #   T   #    U nU(       a  Uv   [        USS5      nU(       a  M  gg7f)z8Iterate through this visitor and each 'chained' visitor._nextN)r   )r   r   s     r   r   "ExternalTraversal.visitor_iterator#  s)      G7D)A as   "((c                 B    [        U R                  5      S   nXl        U $ )z'Chain' an additional ClauseVisitor onto this ClauseVisitor.

The chained visitor will receive all visit events after this one.

)listr   r   )r   r   tails      r   rW   ExternalTraversal.chain,  s#     D))*2.
r.   r9   N)r'   r:   r;   r<   r=   r   r   r	   r   r   memoized_propertyr   propertyr   rW   r>   r9   r.   r   r   r     sR     '7L
 
  * *r.   r   c                   $    \ rS rSrSrS rS rSrg)CloningExternalTraversali7  zBase class for visitor objects which can traverse using
the :func:`.visitors.cloned_traverse` function.

Direct usage of the :func:`.visitors.cloned_traverse` function is usually
preferred.


c                 N    U Vs/ s H  o R                  U5      PM     sn$ s  snf )zPApply cloned traversal to the given list of elements, and return
the new list.

)r   )r   list_xs      r   copy_and_process)CloningExternalTraversal.copy_and_processA  s#    
 +00%Qa %000s   "c                 B    [        XR                  U R                  5      $ r   )r   r   r   r   s     r   r   !CloningExternalTraversal.traverseH  s"     **D,>,>
 	
r.   r9   N)r'   r:   r;   r<   r=   r  r   r>   r9   r.   r   r   r   7  s    1
r.   r   c                   $    \ rS rSrSrS rS rSrg)ReplacingExternalTraversaliP  zBase class for visitor objects which can traverse using
the :func:`.visitors.replacement_traverse` function.

Direct usage of the :func:`.visitors.replacement_traverse` function is
usually preferred.

c                     g)zReceive pre-copied elements during a cloning traversal.

If the method returns a new element, the element is used
instead of creating a simple copy of the element.  Traversal
will halt on the newly returned element if it is re-encountered.
Nr9   )r   elems     r   ri   "ReplacingExternalTraversal.replaceY  s     r.   c                 >   ^  U 4S jn[        UT R                  U5      $ )r   c                 \   > TR                    H  nUR                  U 5      nUc  M  Us  $    g rD   )r   ri   )r
  r   er   s      r   ri   4ReplacingExternalTraversal.traverse.<locals>.replacee  s*    **IIdO=H +r.   )r   r   )r   rL   ri   s   `  r   r   #ReplacingExternalTraversal.traverseb  s    	 $C)B)BGLLr.   r9   N)r'   r:   r;   r<   r=   ri   r   r>   r9   r.   r   r  r  P  s    	Mr.   r  c              #      #    U v   U R                   " S0 UD6nU(       d  g[        U/5      nU(       aH  UR                  5       nU H(  nUv   UR                  UR                   " S0 UD65        M*     U(       a  MG  gg7f)a  Traverse the given expression structure, returning an iterator.

Traversal is configured to be breadth-first.

The central API feature used by the :func:`.visitors.iterate`
function is the
:meth:`_expression.ClauseElement.get_children` method of
:class:`_expression.ClauseElement` objects.  This method should return all
the :class:`_expression.ClauseElement` objects which are associated with a
particular :class:`_expression.ClauseElement` object. For example, a
:class:`.Case` structure will refer to a series of
:class:`_expression.ColumnElement` objects within its "whens" and "else\_"
member variables.

:param obj: :class:`_expression.ClauseElement` structure to be traversed

:param opts: dictionary of iteration options.   This dictionary is usually
 empty in modern usage.

Nr9   )r\   r   popleftrv   )rL   optschildrenstack
t_iteratorts         r   r	   r	   v  sm     * I'$'H8*E
]]_
AGLL/$/0  %s   A7A=;A=c                 r    U  H0  nUR                  UR                  S5      nU(       d  M(  U" U5        M2     U$ )a  Visit the given expression structure using the given iterator of
objects.

:func:`.visitors.traverse_using` is usually called internally as the result
of the :func:`.visitors.traverse` function.

:param iterator: an iterable or sequence which will yield
 :class:`_expression.ClauseElement`
 structures; the iterator is assumed to be the
 product of the :func:`.visitors.iterate` function.

:param obj: the :class:`_expression.ClauseElement`
 that was used as the target of the
 :func:`.iterate` function.

:param visitors: dictionary of visit functions.  See :func:`.traverse`
 for details on this dictionary.

.. seealso::

    :func:`.traverse`


N)getr    )iteratorrL   r   r   r   s        r   r
   r
     s7    2 ||F11484L  Jr.   c                 ,    [        [        X5      X5      $ )a  Traverse and visit the given expression structure using the default
iterator.

 e.g.::

    from sqlalchemy.sql import visitors

    stmt = select(some_table).where(some_table.c.foo == 'bar')

    def visit_bindparam(bind_param):
        print("found bound value: %s" % bind_param.value)

    visitors.traverse(stmt, {}, {"bindparam": visit_bindparam})

The iteration of objects uses the :func:`.visitors.iterate` function,
which does a breadth-first traversal using a stack.

:param obj: :class:`_expression.ClauseElement` structure to be traversed

:param opts: dictionary of iteration options.   This dictionary is usually
 empty in modern usage.

:param visitors: dictionary of visit functions.   The dictionary should
 have strings as keys, each of which would correspond to the
 ``__visit_name__`` of a particular kind of SQL expression object, and
 callable functions  as values, each of which represents a visitor function
 for that kind of object.

)r
   r	   rL   r  r   s      r   r   r     s    < '#,c<<r.   c                    ^^^^^ 0 m[        TR                  S/ 5      5      mUU4S jnUUUU4S jmU b  T" U 4SU0TD6n SmU $ )a|  Clone the given expression structure, allowing modifications by
visitors.

Traversal usage is the same as that of :func:`.visitors.traverse`.
The visitor functions present in the ``visitors`` dictionary may also
modify the internals of the given structure as the traversal proceeds.

The central API feature used by the :func:`.visitors.cloned_traverse`
and :func:`.visitors.replacement_traverse` functions, in addition to the
:meth:`_expression.ClauseElement.get_children`
function that is used to achieve
the iteration, is the :meth:`_expression.ClauseElement._copy_internals`
method.
For a :class:`_expression.ClauseElement`
structure to support cloning and replacement
traversals correctly, it needs to be able to pass a cloning function into
its internal members in order to make copies of them.

.. seealso::

    :func:`.visitors.traverse`

    :func:`.visitors.replacement_traverse`

stop_onc                    > [        U TT5      $ rD   )r   r  s    r   deferred_copy_internals0cloned_traverse.<locals>.deferred_copy_internals  s    sD(33r.   c                 X  > U T;   a  U $ [        U 5      T;  a  SU;   a  US   " U 5      nUb  UT[        U 5      '   U$ U R                  " SST0UD6=T[        U 5      '   nUR                  " SST0UD6  TR                  UR                  S 5      nU(       a  U" U5        T[        U 5         $ )Nri   cloner9   )id_clone_copy_internalsr  r    )r
  r   newelemr   r#  clonedr  r   s       r   r#  cloned_traverse.<locals>.clone  s    7?K$xv%? mD1G*+2r$x(&-1[[-Ku-K-KKr$x 7'':e:r:||G$:$:DAM"T(##r.   Nr   )setr  )rL   r  r   r   r#  r(  r  s    `` @@@r   r   r     s^    6 F$((9b)*G4$ $& 
)@
DH
 EJr.   c                    ^^^^^ 0 mTR                  S/ 5       Vs1 s H  n[        U5      iM     snmUU4S jnUUUU4S jmU b  T" U 4SU0TD6n SmU $ s  snf )a  Clone the given expression structure, allowing element
replacement by a given replacement function.

This function is very similar to the :func:`.visitors.cloned_traverse`
function, except instead of being passed a dictionary of visitors, all
elements are unconditionally passed into the given replace function.
The replace function then has the option to return an entirely new object
which will replace the one given.  If it returns ``None``, then the object
is kept in place.

The difference in usage between :func:`.visitors.cloned_traverse` and
:func:`.visitors.replacement_traverse` is that in the former case, an
already-cloned object is passed to the visitor function, and the visitor
function can then manipulate the internal state of the object.
In the case of the latter, the visitor function should only return an
entirely different object, or do nothing.

The use case for :func:`.visitors.replacement_traverse` is that of
replacing a FROM clause inside of a SQL structure with a different one,
as is a common use case within the ORM.

r  c                    > [        U TT5      $ rD   )r   )rL   r  ri   s    r   r   5replacement_traverse.<locals>.deferred_copy_internals1  s    #Cw77r.   c                 L  > [        U 5      T;   d  SU R                  ;   a  U $ T" U 5      nUb  TR                  [        U5      5        U$ [        U 5      nUT;  aF  SU;   a  US   " U 5      nUb  UTU'   U$ U R                  " S0 UD6=TU'   nUR                  " SST0UD6  TU   $ )Nno_replacement_traverseri   r#  r9   )r$  _annotationsaddr%  r&  )r
  r   r'  id_elemr#  r(  ri   r  s       r   r#  #replacement_traverse.<locals>.clone4  s    tH(D,=,==KdmG"BwK(
 T(&( B"$Y-"5"..5F7O#*N040Ab0AAF7Og++>%>2>g&r.   Nr   )r  r$  )rL   r  ri   r  r   r#  r(  r  s    ``  @@@r   r   r     st    0 F"hhy"565r!u56G8' '6 
)@
DH
 EJK 7s   A)#r=   collectionsr   rV   r(    r   r   r   r   __all__r-   typer   with_metaclassr   r`   r   objectr   rc   r   r   r  	VisitableVisitableTypeClauseVisitorCloningVisitorReplacingCloningVisitorimmutabledictr	   r
   r   r   r   r9   r.   r   <module>r@     s  $       
&RFd F8(
$%%o6 (
VMT M"E0H)++,BFK H)V& 1 &R7 7t
0 
2M!9 M> 	!)4  ((*  1F@=B9x>r.   