ó
 ,ľ[c           @   sÖ  d  Z  d d l m Z d d l m Z d d l Z d d l Z d d l Z d d l m Z m	 Z	 d d l
 Z y d d l m Z Wn, e k
 rŽ d d l Z e j d  Z n Xy e Wn e k
 rÖ e Z e Z n Xd   Z d	   Z d d
  Z d   Z e j d d k Z e r#d   Z n	 d   Z d   Z d   Z d d  Z d d  Z d d  Z  d   Z! d   Z" d   Z# e$ d  Z% d   Z& d   Z' d d  Z( d e) f d     YZ* d d  Z+ d   Z, d S(    sÇ   
Miscellaneous Helpers for NetworkX.

These are not imported into the base networkx namespace but
can be accessed, for example, as

>>> import networkx
>>> networkx.utils.is_string_like('spam')
True
i˙˙˙˙(   t   defaultdict(   t   dequeN(   t   teet   chain(   t
   accumulatec         c   s_   t  |   } y t |  } Wn t k
 r0 d  SX| Vx" | D] } | | |  } | Vq= Wd  S(   N(   t   itert   nextt   StopIteration(   t   iterablet   funct   itt   totalt   element(    (    s2   lib/python2.7/site-packages/networkx/utils/misc.pyR   )   s    c         C   s   t  |  t  S(   s   Check if obj is string.(   t
   isinstancet
   basestring(   t   obj(    (    s2   lib/python2.7/site-packages/networkx/utils/misc.pyt   is_string_like@   s    c         C   s0   t  |  d  r t Sy t |   Wn t SXt S(   s:    Return True if obj is iterable with a well-defined len().t   __iter__(   t   hasattrt   Truet   lent   False(   R   (    (    s2   lib/python2.7/site-packages/networkx/utils/misc.pyR   E   s    c         C   s   t  |   s t |   r |  S| d k r2 g  } n  xD |  D]< } t  |  sX t |  rh | j |  q9 t | |  q9 W|  j |  S(   s@    Return flattened version of (possibly nested) iterable object. N(   R   R   t   Nonet   appendt   flattent	   __class__(   R   t   resultt   item(    (    s2   lib/python2.7/site-packages/networkx/utils/misc.pyR   P   s    	c         C   s;   t  |  t  s t Sx! |  D] } t  | t  s t Sq Wt S(   s(    Return True if list is a list of ints. (   R   t   listR   t   intR   (   t   intlistt   i(    (    s2   lib/python2.7/site-packages/networkx/utils/misc.pyt   is_list_of_ints^   s    i    i   c         C   s*   t  |  t  r |  St t |   d  Sd S(   s&   Return the string representation of t.s   unicode-escapeN(   R   t   unicodet   str(   t   x(    (    s2   lib/python2.7/site-packages/networkx/utils/misc.pyt   make_strj   s    
c         C   s
   t  |   S(   s&   Return the string representation of t.(   R"   (   R#   (    (    s2   lib/python2.7/site-packages/networkx/utils/misc.pyR$   y   s    c           C   s   t  t j    S(   s    Generate a unique node label.(   R"   t   uuidt   uuid1(    (    (    s2   lib/python2.7/site-packages/networkx/utils/misc.pyt   generate_unique_node~   s    c         C   si   d d l  m } i d g d 6d g d 6d g d 6d d	 d
 d g d 6} | t j |  g } | |  d S(   s   Opens `filename` using system's default program.

    Parameters
    ----------
    filename : str
        The path of the file to be opened.

    i˙˙˙˙(   t   callt   opent   darwins   xdg-opent   linuxt   linux2s   cmd.exes   /Ct   startt    t   win32N(   t
   subprocessR(   t   syst   platform(   t   filenameR(   t   cmdst   cmd(    (    s2   lib/python2.7/site-packages/networkx/utils/misc.pyt   default_opener   s    	

c         C   s9   y t  |  |  SWn! t t f k
 r4 t |  |  SXd S(   sP   Convert a dictionary of dictionaries to a numpy array
    with optional mapping.N(   t   dict_to_numpy_array2t   AttributeErrort	   TypeErrort   dict_to_numpy_array1(   t   dt   mapping(    (    s2   lib/python2.7/site-packages/networkx/utils/misc.pyt   dict_to_numpy_array   s    c         C   s  d d l  } | d k r~ t |  j    } x- |  j   D] \ } } | j | j    q7 Wt t | t t	 |     } n  t	 |  } | j
 | | f  } xg | j   D]Y \ } }	 xJ | j   D]< \ }
 } y |  | |
 | |	 | f <WqĹ t k
 r qĹ XqĹ WqŹ W| S(   sY   Convert a dictionary of dictionaries to a 2d numpy array
    with optional mapping.

    i˙˙˙˙N(   t   numpyR   t   sett   keyst   itemst   updatet   dictt   zipt   rangeR   t   zerost   KeyError(   R;   R<   R>   t   st   kt   vt   nt   at   k1R   t   k2t   j(    (    s2   lib/python2.7/site-packages/networkx/utils/misc.pyR7   Ą   s    $c         C   s˘   d d l  } | d k rN t |  j    } t t | t t |     } n  t |  } | j |  } x2 | j	   D]$ \ } } | | } |  | | | <qv W| S(   sT   Convert a dictionary of numbers to a 1d numpy array
    with optional mapping.

    i˙˙˙˙N(
   R>   R   R?   R@   RC   RD   RE   R   RF   RA   (   R;   R<   R>   RH   RK   RL   RM   R   (    (    s2   lib/python2.7/site-packages/networkx/utils/misc.pyR:   ˇ   s    $
c         C   s4   t  |  d  p t  |  d  } t |   |  k o3 | S(   sM   Returns True if and only if the given object is an iterator
    object.

    t   __next__R   (   R   R   (   R   t   has_next_attr(    (    s2   lib/python2.7/site-packages/networkx/utils/misc.pyt   is_iteratorČ   s    c         C   s+   t  |   r t d   n  t t |    S(   sš  Returns an arbitrary element of `iterable` without removing it.

    This is most useful for "peeking" at an arbitrary element of a set,
    but can be used for any list, dictionary, etc., as well::

        >>> arbitrary_element({3, 2, 1})
        1
        >>> arbitrary_element('hello')
        'h'

    This function raises a :exc:`ValueError` if `iterable` is an
    iterator (because the current implementation of this function would
    consume an element from the iterator)::

        >>> iterator = iter([1, 2, 3])
        >>> arbitrary_element(iterator)
        Traceback (most recent call last):
            ...
        ValueError: cannot return an arbitrary item from an iterator

    s0   cannot return an arbitrary item from an iterator(   RR   t
   ValueErrorR   R   (   R   (    (    s2   lib/python2.7/site-packages/networkx/utils/misc.pyt   arbitrary_elementŃ   s    c         C   s   t  |  d d d S(   s   Consume the iterator entirely.t   maxleni    N(   R   (   t   iterator(    (    s2   lib/python2.7/site-packages/networkx/utils/misc.pyt   consumeî   s    c         C   sS   t  |   \ } } t | d  } | t k rF t | t | | f   St | |  S(   s&   s -> (s0, s1), (s1, s2), (s2, s3), ...N(   R   R   R   R   RD   R   (   R   t   cyclicRL   t   bt   first(    (    s2   lib/python2.7/site-packages/networkx/utils/misc.pyt   pairwiseő   s
    c         C   sD   t  t  } x+ |  j   D] \ } } | | j |  q Wt |  S(   s	  Converts a many-to-one mapping into a one-to-many mapping.

    `many_to_one` must be a dictionary whose keys and values are all
    :term:`hashable`.

    The return value is a dictionary mapping values from `many_to_one`
    to sets of keys from `many_to_one` that have that value.

    For example::

        >>> from networkx.utils import groups
        >>> many_to_one = {'a': 1, 'b': 1, 'c': 2, 'd': 3, 'e': 3}
        >>> groups(many_to_one)  # doctest: +SKIP
        {1: {'a', 'b'}, 2: {'c'}, 3: {'d', 'e'}}

    (   R    R?   RA   t   addRC   (   t   many_to_onet   one_to_manyRJ   RI   (    (    s2   lib/python2.7/site-packages/networkx/utils/misc.pyt   groupsţ   s    c         C   s,   t  |  t t f  s |  St t t |    S(   sť   Converts lists to tuples.

    For example::

        >>> from networkx.utils import to_tuple
        >>> a_list = [1, 2, [1, 4]]
        >>> to_tuple(a_list)
        (1, 2, (1, 4))

    (   R   t   tupleR   t   mapt   to_tuple(   R#   (    (    s2   lib/python2.7/site-packages/networkx/utils/misc.pyRb     s    c         C   s   d d l  } |  d k s' |  | j k r4 | j j j St |  | j j  rM |  St |  t  rl | j j |   Sd } t | |    d S(   sŁ  Returns a numpy.random.RandomState instance depending on input.

    Parameters
    ----------
    random_state : int or RandomState instance or None  optional (default=None)
        If int, return a numpy.random.RandomState instance set with seed=int.
        if numpy.random.RandomState instance, return it.
        if None or numpy.random, return the global random number generator used
        by numpy.random.
    i˙˙˙˙NsA   %r cannot be used to generate a numpy.random.RandomState instance(	   R>   R   t   randomt   mtrandt   _randR   t   RandomStateR   RS   (   t   random_statet   npt   msg(    (    s2   lib/python2.7/site-packages/networkx/utils/misc.pyt   create_random_state%  s    t   PythonRandomInterfacec           B   s   e  Z y d d   Z Wn' e k
 r? d Z e j e e  n Xd   Z	 d   Z
 d d  Z d   Z d   Z d   Z d   Z d	   Z d
   Z d   Z RS(   c         C   s:   d d  l  } | d  k r- | j j j |  _ n  | |  _ d  S(   Ni˙˙˙˙(   R>   R   Rc   Rd   Re   t   _rng(   t   selft   rngR>   (    (    s2   lib/python2.7/site-packages/networkx/utils/misc.pyt   __init__>  s    s.   numpy not found, only random.random available.c         C   s   |  j  j   S(   N(   Rl   t   random_sample(   Rm   (    (    s2   lib/python2.7/site-packages/networkx/utils/misc.pyRc   G  s    c         C   s   | | | |  j  j   S(   N(   Rl   Rp   (   Rm   RL   RY   (    (    s2   lib/python2.7/site-packages/networkx/utils/misc.pyt   uniformJ  s    c         C   s   |  j  j | |  S(   N(   Rl   t   randint(   Rm   RL   RY   (    (    s2   lib/python2.7/site-packages/networkx/utils/misc.pyt	   randrangeM  s    c         C   s   | |  j  j d t |   S(   Ni    (   Rl   Rr   R   (   Rm   t   seq(    (    s2   lib/python2.7/site-packages/networkx/utils/misc.pyt   choiceP  s    c         C   s   |  j  j | |  S(   N(   Rl   t   normal(   Rm   t   mut   sigma(    (    s2   lib/python2.7/site-packages/networkx/utils/misc.pyt   gaussS  s    c         C   s   |  j  j |  S(   N(   Rl   t   shuffle(   Rm   Rt   (    (    s2   lib/python2.7/site-packages/networkx/utils/misc.pyRz   V  s    c         C   s%   |  j  j t |  d | f d t S(   Nt   sizet   replace(   Rl   Ru   R   R   (   Rm   Rt   RI   (    (    s2   lib/python2.7/site-packages/networkx/utils/misc.pyt   sample\  s    c         C   s   |  j  j | | d  S(   Ni   (   Rl   Rr   (   Rm   RL   RY   (    (    s2   lib/python2.7/site-packages/networkx/utils/misc.pyRr   _  s    c         C   s   |  j  j d |  S(   Ni   (   Rl   t   exponential(   Rm   t   scale(    (    s2   lib/python2.7/site-packages/networkx/utils/misc.pyt   expovariatec  s    c         C   s   |  j  j |  S(   N(   Rl   t   pareto(   Rm   t   shape(    (    s2   lib/python2.7/site-packages/networkx/utils/misc.pyt   paretovariateg  s    N(   t   __name__t
   __module__R   Ro   t   ImportErrorRi   t   warningst   warnt   ImportWarningRc   Rq   Rs   Ru   Ry   Rz   R}   Rr   R   R   (    (    (    s2   lib/python2.7/site-packages/networkx/utils/misc.pyRk   <  s   								c         C   sű   d d l  } yj d d l } |  | j  k r= t | j  j j  St |  | j  j  r\ t |   St |  t  ro |  St } Wn t k
 r t	 } n X|  d k s¨ |  | k rŻ | j St |  | j  rĹ |  St |  t  rá | j |   Sd } t | |    d S(   s  Returns a random.Random instance depending on input.

    Parameters
    ----------
    random_state : int or random number generator or None (default=None)
        If int, return a random.Random instance set with seed=int.
        if random.Random instance, return it.
        if None or the `random` package, return the global random number
        generator used by `random`.
        if np.random package, return the global numpy random number
        generator wrapped in a PythonRandomInterface class.
        if np.random.RandomState instance, return it wrapped in
        PythonRandomInterface
        if a PythonRandomInterface instance, return it
    i˙˙˙˙Ns6   %r cannot be used to generate a random.Random instance(   Rc   R>   Rk   Rd   Re   R   Rf   R   R   R   R   t   _instt   RandomR   RS   (   Rg   Rc   Rh   t	   has_numpyRi   (    (    s2   lib/python2.7/site-packages/networkx/utils/misc.pyt   create_py_random_stateu  s(    


c         C   s:   d d l  m } y d d  l } Wn | d   n Xd  S(   Ni˙˙˙˙(   t   SkipTests   NumPy not available(   t   noseR   R>   (   t   moduleR   R>   (    (    s2   lib/python2.7/site-packages/networkx/utils/misc.pyt   setup_module  s
    (-   t   __doc__t   collectionsR    R   R   R1   R%   t	   itertoolsR   R   t   networkxt   nxR   R   t   operatorR\   R   t	   NameErrorR"   R!   R   R   R   R   R    t   version_infot   PY2R$   R'   R6   R=   R7   R:   RR   RT   RW   R   R[   R_   Rb   Rj   t   objectRk   R   R   (    (    (    s2   lib/python2.7/site-packages/networkx/utils/misc.pyt   <module>
   sP   
			
										9(