
    ^h`                         S r SSKrSSKrSSKrSSKJr  SSKJrJr  SSK	J
r
JrJr  SSKJr  SSKJr  SSKJr  SS	KJr  \ " S
 S\
5      5       rg)z5Implementation of namespace-related magic functions.
    N)page)StdinNotImplementedError
UsageError)Magicsmagics_class
line_magic)skip_doctest)DEFAULT_ENCODING)read_py_file)get_py_filenamec                   D   \ rS rSrSr\SS j5       r\SS j5       r\\SS j5       5       r	\SS j5       r
\SS j5       r\SS	 j5       r\SS
 j5       r\\SS j5       5       r\\SS j5       5       r\\SS j5       5       r\SS j5       r\SS j5       r\SS j5       rSrg)NamespaceMagics!   zzMagics to manage various aspects of the user's namespace.

These include listing variables, introspecting into them, etc.
Nc                     Sn[         R                  " SU5      R                  5       u  pEpgU(       d  U(       d  U(       a  SnSU;   a  U R                  U5        gU R                  R                  SXcUS9  g)zhProvide detailed information about an object.

'%pinfo object' is just a synonym for object? or ?object.r   z(pinfo )?(\?*)(.*?)(\??$)   *pinfodetail_level
namespacesN)rematchgroupspsearchshell_inspect)selfparameter_sr   r   r   qmark1onameqmark2s           W/home/james-whalen/.local/lib/python3.13/site-packages/IPython/core/magics/namespace.pyr   NamespaceMagics.pinfo(   si      xx4[AHHJ 	"UFfL%<LLJJ+5   7    c                 <    U R                   R                  SUSUS9  g)zqProvide extra detailed information about an object.

'%pinfo2 object' is just a synonym for object?? or ??object.r   r   r   Nr   r   r   r   r   s      r"   pinfo2NamespaceMagics.pinfo2=   s$    
 	

G[q'1 	 	3r$   c                 <    U R                   R                  SX5        g)zPrint the call signature for any callable object.

If the object is a class, print the constructor information.

Examples
--------
::

  In [3]: %pdef urllib.urlopen
  urllib.urlopen(url, data=None, proxies=None)
pdefNr&   r'   s      r"   r+   NamespaceMagics.pdefE   s     	

F;;r$   c                 <    U R                   R                  SX5        g)zPrint the docstring for an object.

If the given object is a class, it will print both the class and the
constructor docstrings.pdocNr&   r'   s      r"   r.   NamespaceMagics.pdocU   s     	

F;;r$   c                 `    U(       d  [        S5      eU R                  R                  SX5        g)z;Print (or run through pager) the source code for an object.zMissing object name.psourceN)r   r   r   r'   s      r"   r1   NamespaceMagics.psource]   s'     344

Ik>r$   c           	         U R                   R                  SX5      nUS:X  aD   [        U5      n[
        R
                  " U R                   R                  [        USS95      5        gg! [         a  n[	        U5         SnAgSnAff = f)a  Print (or run through pager) the file where an object is defined.

The file opens at the line where the object definition begins. IPython
will honor the environment variable PAGER if set, and otherwise will
do its best to print the file in a convenient form.

If the given argument is not an object currently defined, IPython will
try to interpret it as a filename (automatically adding a .py extension
if needed). You can thus use %pfile as a syntax highlighting code
viewer.pfilez	not foundNF)skip_encoding_cookie)r   r   r   IOErrorprintr   
pycolorizer   )r   r   r   outfilenamemsgs         r"   r4   NamespaceMagics.pfiled   sw     jj!!'+B+*;7 IIdjj++LX],^_`   c
s   A( (
B2BBc           	         / SQnU R                  USSS9u  p4UR                  nU R                  nUR                  R                  nSnSU;   a  SnSU;   a  Sn	OSU;   a  Sn	OUR
                  (       + n	UR                  U" S	/ 5      5        U" S
/ 5      n
U Vs/ s H  oU
;  d  M
  UPM     nn U" XFR                  UU" S5      XS9  gs  snf !   UR                  5          g= f)a  Search for object in namespaces by wildcard.

%psearch [options] PATTERN [OBJECT TYPE]

Note: ? can be used as a synonym for %psearch, at the beginning or at
the end: both a*? and ?a* are equivalent to '%psearch a*'.  Still, the
rest of the command line must be unchanged (options come first), so
for example the following forms are equivalent

%psearch -i a* function
-i a* function?
?-i a* function

Arguments:

  PATTERN

  where PATTERN is a string containing * as a wildcard similar to its
  use in a shell.  The pattern is matched in all namespaces on the
  search path. By default objects starting with a single _ are not
  matched, many IPython generated objects have a single
  underscore. The default is case insensitive matching. Matching is
  also done on the attributes of objects and not only on the objects
  in a module.

  [OBJECT TYPE]

  Is the name of a python type from the types module. The name is
  given in lowercase without the ending type, ex. StringType is
  written string. By adding a type here only objects matching the
  given type are matched. Using all here makes the pattern match all
  types (this is the default).

Options:

  -a: makes the pattern match even objects whose names start with a
  single underscore.  These names are normally omitted from the
  search.

  -i/-c: make the pattern case insensitive/sensitive.  If neither of
  these options are given, the default is read from your configuration
  file, with the option ``InteractiveShell.wildcards_case_sensitive``.
  If this option is not specified in your configuration file, IPython's
  internal default is to do a case sensitive search.

  -e/-s NAMESPACE: exclude/search a given namespace.  The pattern you
  specify can be searched in any of the following namespaces:
  'builtin', 'user', 'user_global','internal', 'alias', where
  'builtin' and 'user' are the search defaults.  Note that you should
  not use quotes when specifying namespaces.

  -l: List all available object types for object matching. This function
  can be used without arguments.

  'Builtin' contains the python module builtin, 'user' contains all
  user data, 'alias' only contain the shell aliases and no python
  objects, 'internal' contains objects used by IPython.  The
  'user_global' namespace is only used by embedded IPython instances,
  and it contains module-level globals.  You can add namespaces to the
  search with -s or exclude them with -e (these options can be given
  more than once).

Examples
--------
::

  %psearch a*            -> objects beginning with an a
  %psearch -e builtin a* -> objects NOT in the builtin space starting in a
  %psearch a* function   -> all functions beginning with an a
  %psearch re.e*         -> objects beginning with an e in module re
  %psearch r*.e*         -> objects that start with e in modules starting in r
  %psearch r*.* string   -> all strings in modules beginning with r

Case sensitive search::

  %psearch -c a*         list all object beginning with lower case a

Show objects beginning with a single _::

  %psearch -a _*         list objects beginning with a single underscore

List available objects::

  %psearch -l            list all available object types
)
user_localuser_globalbuiltinzcias:e:lT)list_allFlicsea)show_allignore_case
list_typesN)	parse_optionsgetr   	inspectorr   wildcards_case_sensitiveextendns_tableshowtraceback)r   r   
def_searchoptsargsoptr   r   rJ   rI   
ns_excludenm	ns_searchs                r"   r   NamespaceMagics.psearch|   s    p >
 &&{:t&L	hh

//)) 
$;J $;KD[K#<<<K 	#c"+& #CZ",E*B*0DR*	E	"D	 X+V	 F	"!s   	C%C.C C!c                    U R                   R                  nU R                   R                  n[        5       nU Vs/ s H5  nUR	                  S5      (       a  M  X%   UR                  XT5      Ld  M3  UPM7     nnUR                  5       nU(       a:  [        U5      nU Vs/ s H"  n[        X%   5      R                  U;   d  M   UPM$     nnUR                  5         U$ s  snf s  snf )a  Return a sorted list of all interactive variables.

If arguments are given, only variables of types matching these
arguments are returned.

Examples
--------
Define two variables and list them with who_ls::

  In [1]: alpha = 123

  In [2]: beta = 'test'

  In [3]: %who_ls
  Out[3]: ['alpha', 'beta']

  In [4]: %who_ls int
  Out[4]: ['alpha']

  In [5]: %who_ls str
  Out[5]: ['beta']
_)r   user_nsuser_ns_hiddenobject
startswithrL   splitsettype__name__sort)	r   r   r\   r]   nonmatchingrC   r9   typelisttypesets	            r"   who_lsNamespaceMagics.who_ls   s    4 **$$22h" M7a||C( Z~'9'9!'II 7 M $$&(mG!JcT'*%5%>%>'%I1cCJ

M Ks   CC1CC!C!c                     U R                  U5      nU(       d  U(       a  [        S5        g[        S5        gSnU H)  n[        US-   SS9  US-  nUS	:  d  M  Sn[        5         M+     [        5         g)
a  Print all interactive variables, with some minimal formatting.

If any arguments are given, only variables whose type matches one of
these are printed.  For example::

  %who function str

will only list functions and strings, excluding all other types of
variables.  To find the proper type names, simply use type(var) at a
command line to see how python prints type names.  For example:

::

  In [1]: type('hello')\
  Out[1]: <type 'str'>

indicates that the type name for strings is 'str'.

``%who`` always excludes executed names loaded through your configuration
file and things which are internal to IPython.

This is deliberate, as typically you may load many modules and the
purpose of %who is to show you only what you've manually defined.

Examples
--------

Define two variables and list them with who::

  In [1]: alpha = 123

  In [2]: beta = 'test'

  In [3]: %who
  alpha   beta

  In [4]: %who int
  alpha

  In [5]: %who str
  beta
'No variables match your requested type.Interactive namespace is empty.Nr   	 endr      )rh   r7   )r   r   varlistcountrC   s        r"   whoNamespaceMagics.who  su    \ ++k*?@  78 A!D&c"QJEqy  	r$   c           
        ^ U R                  U5      nU(       d  U(       a  [        S5        g[        S5        g/ SQnSnS[        R                  ;   a   SSKJn  UR                  nSS	0mU4S
 jnU Vs/ s H  opR                  R                  U   PM     nn/ n	U Hk  n
U" U
5      nUS:X  aI  U	R                  TR                  [        U
R                  5      [        U
R                  5      5      5        MZ  U	R                  U5        Mm     SnSnSnSnSnSn[        [        [        [         U5      5      [!        U5      5      U-   n[        [        [        [         U	5      5      [!        U5      5      U-   n[        UR#                  U5      UR#                  U5      -   S-   U-   S-   SUU-   [!        U5      -   S-   -  -   5        SnSn[%        X(U	5       GHc  u  nnn[        UR'                  UUUUS9SS9  UU;   a"  [        S[        [!        U5      5      -   5        MJ  UU:X  a  [        UR(                  5      R+                  SS5      R+                  SS5      SS nUU:X  a'  UR,                  nUUR.                  -  nUR0                  nWS:  a  [        UUWWU4-  5        M  [        UUWWU4-  SS9  UU:  a  [        S UU-  < S!35        M  [        S UU-  < S"35        GM   [        U5      nUR+                  SS%5      n[!        U5      S&:  a  [        U5        GML  [        USS' S(-   US)S -   5        GMf     g! [         a     GNf = fs  snf ! [2         a    UR5                  [6        S#5      n N  S$[9        U5      -  n N= f)*a  Like %who, but gives some extra information about each variable.

The same type filtering of %who can be applied here.

For all variables, the type is printed. Additionally it prints:

  - For {},[],(): their length.

  - For numpy arrays, a summary with shape, number of
    elements, typecode and size in memory.

  - Everything else: a string representation, snipping their middle if
    too long.

Examples
--------
Define two variables and list them with whos::

  In [1]: alpha = 123

  In [2]: beta = 'test'

  In [3]: %whos
  Variable   Type        Data/Info
  --------------------------------
  alpha      int         123
  beta       str         test
rk   rl   N)dictlisttuplenumpyr   ndarrayzIPython.core.macro.MacroMacroc                 P   > [        U 5      R                  nTR                  X5      $ )N)rb   rc   rL   )vtnabbrevss     r"   	type_name'NamespaceMagics.whos.<locals>.type_name  s!    a!!B;;r%%r$   instanceVariableTypez	Data/Info   z{0:<{varwidth}}{1:<{typewidth}}z!%s: %s elems, type `%s`, %s bytesrn   
-r   i   i   )varwidth	typewidthro   zn=, xi (z kb)z Mb)backslashreplacez"<object with id %d (str() failed)>z\n2      z<...>i)rh   r7   sysmodulesrz   r|   rc   ImportErrorr   r\   appendrL   str	__class__maxmaplenljustzipformatshapereplacesizeitemsizedtypeUnicodeEncodeErrorencoder
   id)r   r   varnames	seq_typesndarray_typer|   r   nrr   rf   vvttvarlabel	typelabel	datalabelcolsepvformataformatr   r   kbMbvnamevarvtypevshapevsizevbytesvdtypevstrr   s                                 @r"   whosNamespaceMagics.whos^  sg   @ ;;{+?@  78
 .	 ckk!0)  '//
 .8	& 3;;(Q::%%a((;B2B:~S->-0->"@ A #  		68s3s8,-s8}=FCH-.I?&H	hnnX&)CC !#&(:3y>(I!(K#LM 	N "8H=OE#e'..I.V\_`	!d3s3x=(),&SYY//B7??CH2N,& XXE"3<</F YYFF?'VUFF$CCD'VUFF$CCM{6"9676"967Js8D ||D%0t9r>$K$s)g-ST
:;A  >U   <h * :::&6&8:DJ?"S'IDs*   L; 4"M$M;
M	M	 N3Nc           	         U R                  USSSS9u  p#SU;   a  SnO U R                  R                  SSS	9nU(       d  [	        S
5        gSU;   a0  U R                  R
                  nU R                  5        H  nXV	 M     O,[        U5      S:X  a  U R                  R                  SSU;   S9  U R                  nU R                  R
                  nU GH  nUR                  5       nUS:X  a@  [	        S[        US   5      -  5        U R                  R                  R                  5         MZ  US:X  a  [	        S5        U R                  R                  R                  S-   n	[        SU	5       H#  n
S[        U
5      -   nUR                  US5        M%     UR!                  [#        SSSS95        UR$                  nS/U	-  UR&                  SS& S/U	-  UR(                  SS& S=Ul        =Ul        =Ul        Ul        GM,  US:X  a@   SSKJn  [7        UR9                  5       5       H  u  p[;        X5      (       d  M  X^	 M     GMr  US:X  a  [	        S5        US   SS2	 GM  [	        SS S!9  [	        US"-   5        GM     [>        R@                  " 5         g! [         a    Sn GNif = f! [<         a    [	        S5         GM  f = f)#a  Resets the namespace by removing all names defined by the user, if
called without arguments, or by removing some types of objects, such
as everything currently in IPython's In[] and Out[] containers (see
the parameters for details).

Parameters
----------
-f
    force reset without asking for confirmation.
-s
    'Soft' reset: Only clears your namespace, leaving history intact.
    References to objects may be kept. By default (without this option),
    we do a 'hard' reset, giving you a new session and removing all
    references to objects from the current session.
--aggressive
    Try to aggressively remove modules from sys.modules ; this
    may allow you to reimport Python modules that have been updated and
    pick up changes, but can have unintended consequences.

in
    reset input history
out
    reset output history
dhist
    reset directory history
array
    reset only variables that are NumPy arrays

See Also
--------
reset_selective : invoked as ``%reset_selective``

Examples
--------
::

  In [6]: a = 1

  In [7]: a
  Out[7]: 1

  In [8]: 'a' in get_ipython().user_ns
  Out[8]: True

  In [9]: %reset -f

  In [1]: 'a' in get_ipython().user_ns
  Out[1]: False

  In [2]: %reset -f in
  Flushing input history

  In [3]: %reset -f dhist in
  Flushing directory history
  Flushing input history

Notes
-----
Calling this magic from clients that do not implement standard input,
such as the ipython notebook interface, will reset the namespace
without confirmation.
sf
aggressiverx   )modefTz=Once deleted, variables cannot be recovered. Proceed (y/[n])?r   defaultNothing done.NrE   r   F)new_sessionr   r9   z"Flushing output cache (%d entries)_ohinzFlushing input historyr   _ir   )r   _ii_iiiarrayr{   z-reset array only works if Numpy is available.dhistzFlushing directory history_dhzDon't know how to reset rn   ro   z", please run `%reset?` for details)!rK   r   
ask_yes_nor   r7   r\   rh   r   resetlowerdisplayhookflushprompt_countrangereprpopupdaterw   history_managerinput_hist_parsedinput_hist_rawr   r   r   _i00rz   r|   rx   items
isinstancer   gccollect)r   r   rS   rT   ansr\   rC   iptargetpcr   keyhmr|   r   vals                   r"   r   NamespaceMagics.reset  s   @ ''T<f'U
$;Cjj++O , 
 /"$;jj((G[[]J #Y!^JJLD<PR ZZ**$$F\\^F:S=PPQ

&&,,.4./ZZ++881<q"AtAw,CKKD) & tss<='' ,.$)$$Q'(*tby!!!$69999277"K- "&gmmo!6%c22 '
 "7 7"23EN1% 0c:fCCDO R 	

u , ^ # KIJJKs)   J 2J&;J&J#"J#&K ?K c                    U R                  US5      u  p#SU;   a  SnO U R                  R                  SSS9nU(       d  [	        S5        gU R                  R
                  nU(       d  [	        S5        g [        R                  " U5      nU R                  5        H  nUR                  U5      (       d  M  XX	 M     g! [         a    Sn Nf = f! [         a  n[        S	5      UeSnAff = f)
a  Resets the namespace by removing names defined by the user.

Input/Output history are left around in case you need them.

%reset_selective [-f] regex

No action is taken if regex is not included

Options
  -f : force reset without asking for confirmation.

See Also
--------
reset : invoked as ``%reset``

Examples
--------
We first fully reset the namespace so your output looks identical to
this example for pedagogical reasons; in practice you do not need a
full reset::

  In [1]: %reset -f

Now, with a clean namespace we can make a few variables and use
``%reset_selective`` to only delete names that match our regexp::

  In [2]: a=1; b=2; c=3; b1m=4; b2m=5; b3m=6; b4m=7; b2s=8

  In [3]: who_ls
  Out[3]: ['a', 'b', 'b1m', 'b2m', 'b2s', 'b3m', 'b4m', 'c']

  In [4]: %reset_selective -f b[2-3]m

  In [5]: who_ls
  Out[5]: ['a', 'b', 'b1m', 'b2s', 'b4m', 'c']

  In [6]: %reset_selective -f d

  In [7]: who_ls
  Out[7]: ['a', 'b', 'b1m', 'b2s', 'b4m', 'c']

  In [8]: %reset_selective -f c

  In [9]: who_ls
  Out[9]: ['a', 'b', 'b1m', 'b2s', 'b4m']

  In [10]: %reset_selective -f b

  In [11]: who_ls
  Out[11]: ['a']

Notes
-----
Calling this magic from clients that do not implement standard input,
such as the ipython notebook interface, will reset the namespace
without confirmation.
r   Tz>Once deleted, variables cannot be recovered. Proceed (y/[n])? r   r   r   Nz)No regex pattern specified. Nothing done.z*regex must be a string or compiled pattern)rK   r   r   r   r7   r\   r   compile	TypeErrorrh   search)	r   r   rS   regexr   r\   mrF   rC   s	            r"   reset_selectiveNamespaceMagics.reset_selective`  s    x ((S9$;Cjj++P , 
 /"**$$=>UJJu% [[]88A;;
 # ,   U LMSTTUs)   B> 6C >CC
C+C&&C+c                     U R                  US5      u  p# U R                  R                  USU;   5        g! [        [        4 a7  n[        [        U5      R                  S-   [        U5      -   5         SnAgSnAff = f)as  Delete a variable, trying to clear it from anywhere that
IPython's machinery has references to it. By default, this uses
the identity of the named object in the user namespace to remove
references held under other names. The object is also removed
from the output history.

Options
  -n : Delete the specified name from all namespaces, without
  checking their identity.
r   z: N)	rK   r   del_var	NameError
ValueErrorr7   rb   rc   r   )r   r   rS   varnamerF   s        r"   xdelNamespaceMagics.xdel  sm     **;s;	2JJw6:& 	2$q'""D(#a&011	2s   6 A=-A88A= )r   N)r   )rc   
__module____qualname____firstlineno____doc__r   r   r(   r	   r+   r.   r1   r4   r   rh   rt   r   r   r   r   __static_attributes__r   r$   r"   r   r   !   s;   
 7 7( 3 3 <  < < < ? ? a a. v" v"p %  %N <  <| z<  z<x A AF T$ T$l 2 2r$   r   )r   r   r   r   IPython.corer   IPython.core.errorr   r   IPython.core.magicr   r   r   IPython.testing.skipdoctestr	   IPython.utils.encodingr
   IPython.utils.openpyr   IPython.utils.pathr   r   r   r$   r"   <module>r     sM    
 	 
  C ? ? 4 3 - . e
2f e
2 e
2r$   