<s> import types <EOL> import ibis . compat as compat <EOL> from ibis . config import options <EOL> def guid ( ) : <EOL> try : <EOL> from ibis . comms import uuid4_hex <EOL> return uuid4_hex ( ) <EOL> except ImportError : <EOL> from uuid import uuid4 <EOL> guid = uuid4 ( ) <EOL> return guid . hex if compat . PY3 else guid . get_hex ( ) <EOL> def bytes_to_uint8_array ( val , width = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> if len ( val ) == <NUM_LIT:0> : <EOL> return '<STR_LIT:{}>' <EOL> lines = [ ] <EOL> line = '<STR_LIT:{>' + str ( ord ( val [ <NUM_LIT:0> ] ) ) <EOL> for x in val [ <NUM_LIT:1> : ] : <EOL> token = str ( ord ( x ) ) <EOL> if len ( line ) + len ( token ) > width : <EOL> lines . append ( line + '<STR_LIT:U+002C>' ) <EOL> line = token <EOL> else : <EOL> line += '<STR_LIT>' % token <EOL> lines . append ( line ) <EOL> return '<STR_LIT:\n>' . join ( lines ) + '<STR_LIT:}>' <EOL> def unique_by_key ( values , key ) : <EOL> id_to_table = { } <EOL> for x in values : <EOL> id_to_table [ key ( x ) ] = x <EOL> return compat . dict_values ( id_to_table ) <EOL> def indent ( text , spaces ) : <EOL> block = '<STR_LIT:U+0020>' * spaces <EOL> return '<STR_LIT:\n>' . join ( block + x for x in text . split ( '<STR_LIT:\n>' ) ) <EOL> def any_of ( values , t ) : <EOL> for x in values : <EOL> if isinstance ( x , t ) : <EOL> return True <EOL> return False <EOL> def all_of ( values , t ) : <EOL> for x in values : <EOL> if not isinstance ( x , t ) : <EOL> return False <EOL> return True <EOL> def promote_list ( val ) : <EOL> if not isinstance ( val , list ) : <EOL> val = [ val ] <EOL> return val <EOL> class IbisSet ( object ) : <EOL> def __init__ ( self , keys = None ) : <EOL> self . keys = keys or [ ] <EOL> @ classmethod <EOL> def from_list ( cls , keys ) : <EOL> return IbisSet ( keys ) <EOL> def __contains__ ( self , obj ) : <EOL> for other in self . keys : <EOL> if obj . equals ( other ) : <EOL> return True <EOL> return False <EOL> def add ( self , obj ) : <EOL> self . keys . append ( obj ) <EOL> class IbisMap ( object ) : <EOL> def __init__ ( self ) : <EOL> self . keys = [ ] <EOL> self . values = [ ] <EOL> def __contains__ ( self , obj ) : <EOL> for other in self . keys : <EOL> if obj . equals ( other ) : <EOL> return True <EOL> return False <EOL> def set ( self , key , value ) : <EOL> self . keys . append ( key ) <EOL> self . values . append ( value ) <EOL> def get ( self , key ) : <EOL> for k , v in zip ( self . keys , self . values ) : <EOL> if key . equals ( k ) : <EOL> return v <EOL> raise KeyError ( key ) <EOL> def is_function ( v ) : <EOL> return isinstance ( v , ( types . FunctionType , types . LambdaType ) ) <EOL> def adjoin ( space , * lists ) : <EOL> """<STR_LIT>""" <EOL> out_lines = [ ] <EOL> newLists = [ ] <EOL> lengths = [ max ( map ( len , x ) ) + space for x in lists [ : - <NUM_LIT:1> ] ] <EOL> lengths . append ( max ( map ( len , lists [ - <NUM_LIT:1> ] ) ) ) <EOL> maxLen = max ( map ( len , lists ) ) <EOL> for i , lst in enumerate ( lists ) : <EOL> nl = [ x . ljust ( lengths [ i ] ) for x in lst ] <EOL> nl . extend ( [ '<STR_LIT:U+0020>' * lengths [ i ] ] * ( maxLen - len ( lst ) ) ) <EOL> newLists . append ( nl ) <EOL> toJoin = zip ( * newLists ) <EOL> for lines in toJoin : <EOL> out_lines . append ( _join_unicode ( lines ) ) <EOL> return _join_unicode ( out_lines , sep = '<STR_LIT:\n>' ) <EOL> def _join_unicode ( lines , sep = '<STR_LIT>' ) : <EOL> try : <EOL> return sep . join ( lines ) <EOL> except UnicodeDecodeError : <EOL> sep = compat . unicode_type ( sep ) <EOL> return sep . join ( [ x . decode ( '<STR_LIT:utf-8>' ) if isinstance ( x , str ) else x <EOL> for x in lines ] ) <EOL> def deprecate ( f , message ) : <EOL> def g ( * args , ** kwargs ) : <EOL> print ( message ) <EOL> return f ( * args , ** kwargs ) <EOL> return g <EOL> def to_stdout ( x ) : <EOL> print ( x ) <EOL> def log ( msg ) : <EOL> if options . verbose : <EOL> ( options . verbose_log or to_stdout ) ( msg ) <EOL> class cache_readonly ( object ) : <EOL> def __init__ ( self , func = None , allow_setting = False ) : <EOL> if func is not None : <EOL> self . func = func <EOL> self . name = func . __name__ <EOL> self . allow_setting = allow_setting <EOL> def __call__ ( self , func , doc = None ) : <EOL> self . func = func <EOL> self . name = func . __name__ <EOL> return self <EOL> def __get__ ( self , obj , typ ) : <EOL> cache = getattr ( obj , '<STR_LIT>' , None ) <EOL> if cache is None : <EOL> try : <EOL> cache = obj . _cache = { } <EOL> except ( AttributeError ) : <EOL> return <EOL> if self . name in cache : <EOL> val = cache [ self . name ] <EOL> else : <EOL> val = self . func ( obj ) <EOL> cache [ self . name ] = val <EOL> return val <EOL> def __set__ ( self , obj , value ) : <EOL> if not self . allow_setting : <EOL> raise Exception ( "<STR_LIT>" % self . name ) <EOL> cache = getattr ( obj , '<STR_LIT>' , None ) <EOL> if cache is None : <EOL> try : <EOL> cache = obj . _cache = { } <EOL> except ( AttributeError ) : <EOL> return <EOL> cache [ self . name ] = value <EOL> def approx_equal ( a , b , eps ) : <EOL> assert abs ( a - b ) < eps <EOL> def implements ( f ) : <EOL> def decorator ( g ) : <EOL> g . __doc__ = f . __doc__ <EOL> return g <EOL> return decorator </s>
