o
    h#                     @   s   d dl Z d dlZd dlZd dlmZ ddgZeddddddG dd dZd	d
 Ze jj	
e G dd deZeg e eejdddZdS )    N)	dataclassConfigconfigFT)initeqslotskw_only
match_argsc                   @   s   e Zd ZdZd,ddZdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd ZeZd-ddZd d! Zd"d# Zd$d% Zd&d' Zd(d) Zed*d+ ZdS ).r   a  The base class for NetworkX configuration.

    There are two ways to use this to create configurations. The first is to
    simply pass the initial configuration as keyword arguments to ``Config``:

    >>> cfg = Config(eggs=1, spam=5)
    >>> cfg
    Config(eggs=1, spam=5)

    The second--and preferred--way is to subclass ``Config`` with docs and annotations.

    >>> class MyConfig(Config):
    ...     '''Breakfast!'''
    ...
    ...     eggs: int
    ...     spam: int
    ...
    ...     def _check_config(self, key, value):
    ...         assert isinstance(value, int) and value >= 0
    >>> cfg = MyConfig(eggs=1, spam=5)

    Once defined, config items may be modified, but can't be added or deleted by default.
    ``Config`` is a ``Mapping``, and can get and set configs via attributes or brackets:

    >>> cfg.eggs = 2
    >>> cfg.eggs
    2
    >>> cfg["spam"] = 42
    >>> cfg["spam"]
    42

    Subclasses may also define ``_check_config`` (as done in the example above)
    to ensure the value being assigned is valid:

    >>> cfg.spam = -1
    Traceback (most recent call last):
        ...
    AssertionError

    If a more flexible configuration object is needed that allows adding and deleting
    configurations, then pass ``strict=False`` when defining the subclass:

    >>> class FlexibleConfig(Config, strict=False):
    ...     default_greeting: str = "Hello"
    >>> flexcfg = FlexibleConfig()
    >>> flexcfg.name = "Mr. Anderson"
    >>> flexcfg
    FlexibleConfig(default_greeting='Hello', name='Mr. Anderson')
    Tc                 C   s
   || _ d S N)_strict)clsstrict r   Q/var/www/html/env_mimamsha/lib/python3.10/site-packages/networkx/utils/configs.py__init_subclass__=      
zConfig.__init_subclass__c                 K   st   | }| t u rt| j| fddd |D i} td| j| jddd| } | js(t| _|| _t	| }|j
di | |S )N__annotations__c                 S   s   i | ]}|t jqS r   )typingAny.0keyr   r   r   
<dictcomp>G   s    z"Config.__new__.<locals>.<dictcomp>FT)r   reprr   r   r	   r   )r   type__name__r   r   _flexible_repr__repr___orig_classobject__new____init__)r   kwargs
orig_classinstancer   r   r   r    @   s,   
zConfig.__new__c                 C   s   dS )zCCheck whether config value is valid. This is useful for subclasses.Nr   selfr   valuer   r   r   _check_configW   s    zConfig._check_configc                 C   s
   | j  S r
   )__dataclass_fields__keysr&   r   r   r   __dir__[   r   zConfig.__dir__c                 C   s<   | j r|| jvrtd|| || t| || d S )NzInvalid config name: )r   r)   AttributeErrorr(   r   __setattr__r%   r   r   r   r.   ^   s   zConfig.__setattr__c                 C   s&   | j rtd|dt| | d S )Nz3Configuration items can't be deleted (can't delete z).)r   	TypeErrorr   __delattr__r&   r   r   r   r   r0   d   s
   
zConfig.__delattr__c                 C   s   | j r|| jv S || jv S r
   )r   r)   __dict__r1   r   r   r   __contains__l   s   zConfig.__contains__c                 C      t | jr| jS | jS r
   )iterr   r)   r2   r+   r   r   r   __iter__q      zConfig.__iter__c                 C   r4   r
   )lenr   r)   r2   r+   r   r   r   __len__t   r7   zConfig.__len__c                 C   r4   r
   )reversedr   r)   r2   r+   r   r   r   __reversed__w   r7   zConfig.__reversed__c              
   C   s2   zt | |W S  ty } zt|j d d }~ww r
   )getattrr-   KeyErrorargsr&   r   errr   r   r   __getitem__{   s   zConfig.__getitem__c              
   C   s8   z	|  || W d S  ty } zt|j d d }~ww r
   )r.   r-   r=   r>   )r&   r   r'   r@   r   r   r   __setitem__   s   zConfig.__setitem__c              
   C   s6   z|  | W d S  ty } zt|j d d }~ww r
   )r0   r-   r=   r>   r?   r   r   r   __delitem__   s   zConfig.__delitem__Nc                 C   s   t | ||S r
   )r<   )r&   r   defaultr   r   r   get      z
Config.getc                 C      t j| S r
   )collectionsabc	ItemsViewr+   r   r   r   items   rF   zConfig.itemsc                 C   rG   r
   )rH   rI   KeysViewr+   r   r   r   r*      rF   zConfig.keysc                 C   rG   r
   )rH   rI   
ValuesViewr+   r   r   r   values   rF   zConfig.valuesc                 C   s*   t |tstS | j|jko|  | kS r
   )
isinstancer   NotImplementedr   rK   )r&   otherr   r   r   __eq__   s   
zConfig.__eq__c                 C   s   | j | jt| ffS r
   )_deserializer   dictr+   r   r   r   
__reduce__   s   zConfig.__reduce__c                 C   s   | di |S )Nr   r   )r   r"   r   r   r   rS      s   zConfig._deserialize)Tr
   )r   
__module____qualname____doc__r   r    r(   r,   r.   r0   r3   r6   r9   r;   rA   rB   rC   _ipython_key_completions_rE   rK   r*   rN   rR   rU   staticmethodrS   r   r   r   r   r   	   s0    
2
c                 C   s,   | j j dddd | j D  d S )N(, c                 s   s"    | ]\}}| d |V  qdS )=Nr   )r   r   valr   r   r   	<genexpr>   s     z!_flexible_repr.<locals>.<genexpr>))	__class__rW   joinr2   rK   r+   r   r   r   r      s   r   c                   @   s6   e Zd ZU dZee ed< eed< eed< dd Z	dS )NetworkXConfiga?  Configuration for NetworkX that controls behaviors such as how to use backends.

    Attribute and bracket notation are supported for getting and setting configurations:

    >>> nx.config.backend_priority == nx.config["backend_priority"]
    True

    Parameters
    ----------
    backend_priority : list of backend names
        Enable automatic conversion of graphs to backend graphs for algorithms
        implemented by the backend. Priority is given to backends listed earlier.
        Default is empty list.

    backends : Config mapping of backend names to backend Config
        The keys of the Config mapping are names of all installed NetworkX backends,
        and the values are their configurations as Config mappings.

    cache_converted_graphs : bool
        If True, then save converted graphs to the cache of the input graph. Graph
        conversion may occur when automatically using a backend from `backend_priority`
        or when using the `backend=` keyword argument to a function call. Caching can
        improve performance by avoiding repeated conversions, but it uses more memory.
        Care should be taken to not manually mutate a graph that has cached graphs; for
        example, ``G[u][v][k] = val`` changes the graph, but does not clear the cache.
        Using methods such as ``G.add_edge(u, v, weight=val)`` will clear the cache to
        keep it consistent. ``G.__networkx_cache__.clear()`` manually clears the cache.
        Default is False.

    Notes
    -----
    Environment variables may be used to control some default configurations:

    - NETWORKX_BACKEND_PRIORITY: set `backend_priority` from comma-separated names.
    - NETWORKX_CACHE_CONVERTED_GRAPHS: set `cache_converted_graphs` to True if nonempty.

    This is a global configuration. Use with caution when using from multiple threads.
    backend_prioritybackendscache_converted_graphsc                    s>  ddl m   |dkrBt|trtdd |D s!t|d| fdd|D  }r@d	ttt|}t	d
|d| d S |dkrt|t
r_tdd |D r_tdd | D sht|d| fdd|D  }rd	ttt|}t	d
|d| d S |dkrt|tst|d|d S d S )N   re   rd   c                 s       | ]}t |tV  qd S r
   rO   strr   xr   r   r   r_          z/NetworkXConfig._check_config.<locals>.<genexpr>z- config must be a list of backend names; got c                       h | ]}| vr|qS r   r   rl   rh   r   r   	<setcomp>       z/NetworkXConfig._check_config.<locals>.<setcomp>r\   zUnknown backend when setting z: re   c                 s   ri   r
   rj   r   r   r   r   r_      rn   c                 s   ri   r
   )rO   r   )r   r^   r   r   r   r_      rn   z1 config must be a Config of backend configs; got c                    ro   r   r   rl   rh   r   r   rp      rq   rf   z# config must be True or False; got )re   rO   listallr/   rb   mapr   sorted
ValueErrorr   rN   bool)r&   r   r'   missingr   rh   r   r(      s:   
zNetworkXConfig._check_configN)
r   rV   rW   rX   rr   rk   r   r   rw   r(   r   r   r   r   rc      s   
 'rc   NETWORKX_CACHE_CONVERTED_GRAPHS )rd   re   rf   )rH   osr   dataclassesr   __all__r   r   rI   Mappingregisterrc   rw   environrE   r   r   r   r   r   <module>   s      "	I
