<s> from __future__ import unicode_literals <EOL> import os <EOL> import signal <EOL> import sys <EOL> import threading <EOL> from wcwidth import wcwidth <EOL> from six . moves import range <EOL> __all__ = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> class Callback ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , func = None ) : <EOL> assert func is None or callable ( func ) <EOL> self . _handlers = [ func ] if func else [ ] <EOL> def fire ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> for handler in self . _handlers : <EOL> handler ( * args , ** kwargs ) <EOL> def __iadd__ ( self , handler ) : <EOL> """<STR_LIT>""" <EOL> self . _handlers . append ( handler ) <EOL> return self <EOL> def __isub__ ( self , handler ) : <EOL> """<STR_LIT>""" <EOL> self . _handlers . remove ( handler ) <EOL> return self <EOL> def __or__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> assert isinstance ( other , Callback ) <EOL> def call_both ( ) : <EOL> self . fire ( ) <EOL> other . fire ( ) <EOL> return Callback ( call_both ) <EOL> class DummyContext ( object ) : <EOL> """<STR_LIT>""" <EOL> def __enter__ ( self ) : <EOL> pass <EOL> def __exit__ ( self , * a ) : <EOL> pass <EOL> class _CharSizesCache ( dict ) : <EOL> """<STR_LIT>""" <EOL> def __missing__ ( self , string ) : <EOL> if len ( string ) == <NUM_LIT:1> : <EOL> result = max ( <NUM_LIT:0> , wcwidth ( string ) ) <EOL> else : <EOL> result = sum ( max ( <NUM_LIT:0> , wcwidth ( c ) ) for c in string ) <EOL> if len ( string ) < <NUM_LIT> : <EOL> self [ string ] = result <EOL> return result <EOL> _CHAR_SIZES_CACHE = _CharSizesCache ( ) <EOL> def get_cwidth ( string ) : <EOL> """<STR_LIT>""" <EOL> return _CHAR_SIZES_CACHE [ string ] <EOL> def suspend_to_background_supported ( ) : <EOL> """<STR_LIT>""" <EOL> return hasattr ( signal , '<STR_LIT>' ) <EOL> def is_windows ( ) : <EOL> """<STR_LIT>""" <EOL> return sys . platform . startswith ( '<STR_LIT>' ) <EOL> def is_conemu_ansi ( ) : <EOL> """<STR_LIT>""" <EOL> return is_windows ( ) and os . environ . get ( '<STR_LIT>' , '<STR_LIT>' ) == '<STR_LIT>' <EOL> def in_main_thread ( ) : <EOL> """<STR_LIT>""" <EOL> return threading . current_thread ( ) . __class__ . __name__ == '<STR_LIT>' <EOL> def take_using_weights ( items , weights ) : <EOL> """<STR_LIT>""" <EOL> assert isinstance ( items , list ) <EOL> assert isinstance ( weights , list ) <EOL> assert all ( isinstance ( i , int ) for i in weights ) <EOL> assert len ( items ) == len ( weights ) <EOL> assert len ( items ) > <NUM_LIT:0> <EOL> already_taken = [ <NUM_LIT:0> for i in items ] <EOL> item_count = len ( items ) <EOL> max_weight = max ( weights ) <EOL> i = <NUM_LIT:0> <EOL> while True : <EOL> adding = True <EOL> while adding : <EOL> adding = False <EOL> for item_i , item , weight in zip ( range ( item_count ) , items , weights ) : <EOL> if already_taken [ item_i ] < i * weight / float ( max_weight ) : <EOL> yield item <EOL> already_taken [ item_i ] += <NUM_LIT:1> <EOL> adding = True <EOL> i += <NUM_LIT:1> </s>
