o
    ?e                     @   s   d Z ddlmZ ddlmZ ddlmZ ddlZdd ZG dd	 d	eZd
d Z	dZ
e
d Ze
d Ze
d Zdd Zdd Zdd Zdd ZdddZdd ZdS )z/Utility methods for the trackable dependencies.    )absolute_import)division)print_functionNc                 C   s    | sdS dd dd | D  S )Nzroot objectzroot..c                 S   s   g | ]}|j qS  name).0pr   r   l/home/www/facesmatcher.com/pyenv/lib/python3.10/site-packages/tensorflow/python/trackable/trackable_utils.py
<listcomp>   s    z*pretty_print_node_path.<locals>.<listcomp>join)pathr   r   r   pretty_print_node_path   s   r   c                       s   e Zd Z fddZ  ZS )CyclicDependencyErrorc                    s   || _ tt|   dS )z$Creates a CyclicDependencyException.N)leftover_dependency_mapsuperr   __init__)selfr   	__class__r   r   r       s   zCyclicDependencyError.__init__)__name__
__module____qualname__r   __classcell__r   r   r   r   r      s    r   c           
         s  t t |  D ]\}}|D ]	} | | qq	  |   }|r+td| g } fdd| D }|rb|d}|| t| | D ]} | }|	| |s_||  | qH|s8 rt t
}  D ]\}}	|	D ]	}|| | qsqmt|t|S )a*  Topologically sorts the keys of a map so that dependencies appear first.

  Uses Kahn's algorithm:
  https://en.wikipedia.org/wiki/Topological_sorting#Kahn's_algorithm

  Args:
    dependency_map: a dict mapping values to a list of dependencies (other keys
      in the map). All keys and dependencies must be hashable types.

  Returns:
    A sorted array of keys from dependency_map.

  Raises:
    CyclicDependencyError: if there is a cycle in the graph.
    ValueError: If there are values in the dependency map that are not keys in
      the map.
  z7Found values in the dependency map which are not keys: c                    s   g | ]}| vr|qS r   r   )r	   xZreverse_dependency_mapr   r   r   L   s    z'order_by_dependency.<locals>.<listcomp>r   )collectionsdefaultdictsetitemsaddkeys
ValueErrorpopappendremovelistr   reversed)
Zdependency_mapr   depsdepZunknown_keysZreversed_dependency_arrZto_visitedgesr   Zxsr   r   r   order_by_dependency'   s>   







r-   r   ZOPTIMIZER_SLOTZ
ATTRIBUTESZTENSORSc                 C   s   |  ttt  dtd S )N/S)replace_ESCAPE_CHARr   r   r   r   escape_local_namex   s   r2   c                 C   s   d dd | D S )z%Converts a list of nodes to a string.r.   c                 s   s    | ]}t |jV  qd S N)r2   r   )r	   Z	trackabler   r   r   	<genexpr>   s    z(object_path_to_string.<locals>.<genexpr>r   )Znode_path_arrr   r   r   object_path_to_string   s   r5   c                 C   s(   t |}|tkr
d}|  dt d| S )z>Returns the checkpoint key for a local attribute of an object. r.   )r2   SERIALIZE_TO_TENSORS_NAMEOBJECT_ATTRIBUTES_NAME)Zobject_pathZ
local_nameZ
key_suffixr   r   r   checkpoint_key   s   r9   c                 C   s   dt  }| d| | S )z=Substrings the checkpoint key to the start of "/.ATTRIBUTES".r.   N)r8   index)key
search_keyr   r   r   extract_object_name   s   r=   c                 C   sH   |pd}t d | }z| | |t| d W S  ty#   |  Y S w )zEReturns the substring after the "/.ATTIBUTES/" in the checkpoint key.r6   r.   N)r8   r:   lenr$   )r;   prefixr<   r   r   r   extract_local_name   s   r@   c                 C   s   |  dt  d| dt| S )z+Returns checkpoint key for a slot variable.r.   )_OPTIMIZER_SLOTS_NAMEr2   )Zvariable_pathZoptimizer_pathZ	slot_namer   r   r   slot_variable_key   s   rB   r3   )__doc__
__future__r   r   r   r   r   	Exceptionr   r-   r1   rA   r8   r7   r2   r5   r9   r=   r@   rB   r   r   r   r   <module>   s$   	;	

