<s> from __future__ import print_function <EOL> __author__ = '<STR_LIT>' <EOL> import sys <EOL> import os <EOL> import datetime <EOL> import numpy as np <EOL> import inspect <EOL> import logging <EOL> import socket <EOL> try : <EOL> import zmq <EOL> except ImportError : <EOL> zmq = None <EOL> import pypet . compat as compat <EOL> from pypet . utils . decorators import deprecated <EOL> from pypet . utils . comparisons import nested_equal as nested_equal_new <EOL> def is_debug ( ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' in sys . modules <EOL> def flatten_dictionary ( nested_dict , separator ) : <EOL> """<STR_LIT>""" <EOL> flat_dict = { } <EOL> for key , val in nested_dict . items ( ) : <EOL> if isinstance ( val , dict ) : <EOL> new_flat_dict = flatten_dictionary ( val , separator ) <EOL> for flat_key , inval in new_flat_dict . items ( ) : <EOL> new_key = key + separator + flat_key <EOL> flat_dict [ new_key ] = inval <EOL> else : <EOL> flat_dict [ key ] = val <EOL> return flat_dict <EOL> def nest_dictionary ( flat_dict , separator ) : <EOL> """<STR_LIT>""" <EOL> nested_dict = { } <EOL> for key , val in flat_dict . items ( ) : <EOL> split_key = key . split ( separator ) <EOL> act_dict = nested_dict <EOL> final_key = split_key . pop ( ) <EOL> for new_key in split_key : <EOL> if not new_key in act_dict : <EOL> act_dict [ new_key ] = { } <EOL> act_dict = act_dict [ new_key ] <EOL> act_dict [ final_key ] = val <EOL> return nested_dict <EOL> class _Progressbar ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . _start_time = None <EOL> self . _start_index = None <EOL> self . _current_index = np . inf <EOL> self . _percentage_step = None <EOL> self . _total = None <EOL> self . _total_minus_one = None <EOL> self . _length = None <EOL> self . _norm_factor = None <EOL> self . _current_interval = None <EOL> def _reset ( self , index , total , percentage_step , length ) : <EOL> """<STR_LIT>""" <EOL> self . _start_time = datetime . datetime . now ( ) <EOL> self . _start_index = index <EOL> self . _current_index = index <EOL> self . _percentage_step = percentage_step <EOL> self . _total = float ( total ) <EOL> self . _total_minus_one = total - <NUM_LIT:1> <EOL> self . _length = length <EOL> self . _norm_factor = total * percentage_step / <NUM_LIT> <EOL> self . _current_interval = int ( ( index + <NUM_LIT:1.0> ) / self . _norm_factor ) <EOL> def _get_remaining ( self , index ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> current_time = datetime . datetime . now ( ) <EOL> time_delta = current_time - self . _start_time <EOL> try : <EOL> total_seconds = time_delta . total_seconds ( ) <EOL> except AttributeError : <EOL> total_seconds = ( ( time_delta . microseconds + <EOL> ( time_delta . seconds + <EOL> time_delta . days * <NUM_LIT> * <NUM_LIT> ) * <NUM_LIT:10> ** <NUM_LIT:6> ) / <NUM_LIT> ** <NUM_LIT:6> ) <EOL> remaining_seconds = int ( ( self . _total - self . _start_index - <NUM_LIT:1.0> ) * <EOL> total_seconds / float ( index - self . _start_index ) - <EOL> total_seconds ) <EOL> remaining_delta = datetime . timedelta ( seconds = remaining_seconds ) <EOL> remaining_str = '<STR_LIT>' + str ( remaining_delta ) <EOL> except ZeroDivisionError : <EOL> remaining_str = '<STR_LIT>' <EOL> return remaining_str <EOL> def __call__ ( self , index , total , percentage_step = <NUM_LIT:5> , logger = '<STR_LIT>' , log_level = logging . INFO , <EOL> reprint = False , time = True , length = <NUM_LIT:20> , fmt_string = None , reset = False ) : <EOL> """<STR_LIT>""" <EOL> reset = ( reset or <EOL> index <= self . _current_index or <EOL> total != self . _total ) <EOL> if reset : <EOL> self . _reset ( index , total , percentage_step , length ) <EOL> statement = None <EOL> indexp1 = index + <NUM_LIT:1.0> <EOL> next_interval = int ( indexp1 / self . _norm_factor ) <EOL> ending = index >= self . _total_minus_one <EOL> if next_interval > self . _current_interval or ending or reset : <EOL> if time : <EOL> remaining_str = self . _get_remaining ( index ) <EOL> else : <EOL> remaining_str = '<STR_LIT>' <EOL> if ending : <EOL> statement = '<STR_LIT:[>' + '<STR_LIT:=>' * self . _length + '<STR_LIT>' <EOL> else : <EOL> bars = int ( ( indexp1 / self . _total ) * self . _length ) <EOL> spaces = self . _length - bars <EOL> percentage = indexp1 / self . _total * <NUM_LIT> <EOL> if reset : <EOL> statement = ( '<STR_LIT:[>' + '<STR_LIT:=>' * bars + <EOL> '<STR_LIT:U+0020>' * spaces + '<STR_LIT:]>' + '<STR_LIT>' % percentage + '<STR_LIT:%>' ) <EOL> else : <EOL> statement = ( '<STR_LIT:[>' + '<STR_LIT:=>' * bars + <EOL> '<STR_LIT:U+0020>' * spaces + '<STR_LIT:]>' + '<STR_LIT>' % percentage + '<STR_LIT:%>' + <EOL> remaining_str ) <EOL> if fmt_string : <EOL> statement = fmt_string % statement <EOL> if logger == '<STR_LIT>' : <EOL> if reprint and not ending : <EOL> print ( statement , end = '<STR_LIT:\r>' ) <EOL> else : <EOL> print ( statement ) <EOL> elif logger is not None : <EOL> if isinstance ( logger , compat . base_type ) : <EOL> logger = logging . getLogger ( logger ) <EOL> logger . log ( msg = statement , level = log_level ) <EOL> self . _current_interval = next_interval <EOL> self . _current_index = index <EOL> return statement <EOL> _progressbar = _Progressbar ( ) <EOL> def progressbar ( index , total , percentage_step = <NUM_LIT:10> , logger = '<STR_LIT>' , log_level = logging . INFO , <EOL> reprint = True , time = True , length = <NUM_LIT:20> , fmt_string = None , reset = False ) : <EOL> """<STR_LIT>""" <EOL> return _progressbar ( index = index , total = total , percentage_step = percentage_step , <EOL> logger = logger , log_level = log_level , reprint = reprint , <EOL> time = time , length = length , fmt_string = fmt_string , reset = reset ) <EOL> def get_matching_kwargs ( func , kwargs ) : <EOL> """<STR_LIT>""" <EOL> if inspect . isclass ( func ) : <EOL> func = func . __init__ <EOL> try : <EOL> argspec = inspect . getargspec ( func ) <EOL> if argspec . keywords is not None : <EOL> return kwargs . copy ( ) <EOL> else : <EOL> matching_kwargs = dict ( ( k , kwargs [ k ] ) for k in argspec . args if k in kwargs ) <EOL> return matching_kwargs <EOL> except TypeError : <EOL> return { } <EOL> def result_sort ( result_list , start_index = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> if len ( result_list ) < <NUM_LIT:2> : <EOL> return result_list <EOL> to_sort = result_list [ start_index : ] <EOL> minmax = [ x [ <NUM_LIT:0> ] for x in to_sort ] <EOL> minimum = min ( minmax ) <EOL> maximum = max ( minmax ) <EOL> sorted_list = [ None for _ in range ( minimum , maximum + <NUM_LIT:1> ) ] <EOL> for elem in to_sort : <EOL> key = elem [ <NUM_LIT:0> ] - minimum <EOL> sorted_list [ key ] = elem <EOL> idx_count = start_index <EOL> for elem in sorted_list : <EOL> if elem is not None : <EOL> result_list [ idx_count ] = elem <EOL> idx_count += <NUM_LIT:1> <EOL> return result_list <EOL> def format_time ( timestamp ) : <EOL> """<STR_LIT>""" <EOL> format_string = '<STR_LIT>' <EOL> formatted_time = datetime . datetime . fromtimestamp ( timestamp ) . strftime ( format_string ) <EOL> return formatted_time <EOL> def port_to_tcp ( port = None ) : <EOL> """<STR_LIT>""" <EOL> domain_name = socket . getfqdn ( ) <EOL> try : <EOL> addr_list = socket . getaddrinfo ( domain_name , None ) <EOL> except Exception : <EOL> addr_list = socket . getaddrinfo ( '<STR_LIT:localhost>' , None ) <EOL> family , socktype , proto , canonname , sockaddr = addr_list [ <NUM_LIT:0> ] <EOL> address = '<STR_LIT>' + sockaddr [ <NUM_LIT:0> ] <EOL> if port is None : <EOL> port = ( ) <EOL> if not isinstance ( port , int ) : <EOL> context = zmq . Context ( ) <EOL> socket_ = context . socket ( zmq . REP ) <EOL> port = socket_ . bind_to_random_port ( address , * port ) <EOL> socket_ . close ( ) <EOL> context . term ( ) <EOL> return address + '<STR_LIT::>' + str ( port ) <EOL> def racedirs ( path ) : <EOL> """<STR_LIT>""" <EOL> if os . path . isfile ( path ) : <EOL> raise IOError ( '<STR_LIT>' ) <EOL> while True : <EOL> try : <EOL> if os . path . isdir ( path ) : <EOL> break <EOL> os . makedirs ( path ) <EOL> except EnvironmentError as exc : <EOL> if exc . errno != <NUM_LIT> : <EOL> raise </s>
