ó
§¥¨Wc           @   sA   d  Z  d d l Z d d l Z d e f d „  ƒ  YZ d „  Z d S(   sC   
Created on 2015-03-27

@author: Ian McDowell (sortof, not really)
iÿÿÿÿNt   suppress_stdout_stderrc           B   s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   s±  
    A context manager for doing a "deep suppression" of stdout and stderr in 
    Python, i.e. will suppress all print, even if the print originates in a 
    compiled C/Fortran sub-function.
       This will not suppress raised exceptions, since exceptions are printed
    to stderr just before a script exits, and after the context manager has
    exited (at least, I think that is why it lets exceptions through).      
    
    c         C   sY   g  t  d ƒ D] } t j t j t j ƒ ^ q |  _ t j d ƒ t j d ƒ f |  _ d  S(   Ni   i   (   t   ranget   ost   opent   devnullt   O_RDWRt   null_fdst   dupt   save_fds(   t   selft   x(    (    s   DP_GP/utils.pyt   __init__   s    4c         C   s2   t  j |  j d d ƒ t  j |  j d d ƒ d  S(   Ni    i   i   (   R   t   dup2R   (   R	   (    (    s   DP_GP/utils.pyt	   __enter__   s    c         G   sZ   t  j |  j d d ƒ t  j |  j d d ƒ t  j |  j d ƒ t  j |  j d ƒ d  S(   Ni    i   i   (   R   R   R   t   closeR   (   R	   t   _(    (    s   DP_GP/utils.pyt   __exit__$   s    (   t   __name__t
   __module__t   __doc__R   R   R   (    (    (    s   DP_GP/utils.pyR       s   			c            s(   d „  ‰  ‡  f d †  } t  |  d | ƒS(   sª    
    Sort the given iterable in the way that humans expect, e.g. 'gene1' before 'gene10'.
    from http://stackoverflow.com/questions/5491913/sorting-list-in-python
    c         S   s   |  j  ƒ  r t |  ƒ S|  S(   N(   t   isdigitt   int(   t   text(    (    s   DP_GP/utils.pyt   <lambda>1   s    c            s)   g  t  j d |  ƒ D] } ˆ  | ƒ ^ q S(   Ns   ([0-9]+)(   t   ret   split(   t   keyt   c(   t   convert(    s   DP_GP/utils.pyR   2   s    R   (   t   sorted(   t   lt   alphanum_key(    (   R   s   DP_GP/utils.pyt   sorted_nicely,   s    	(   R   R   R   t   objectR    R    (    (    (    s   DP_GP/utils.pyt   <module>   s   