<s> import hashlib <EOL> import json <EOL> from pycoin . encoding import b2a_base58 <EOL> def deterministic_json_dumps ( obj ) : <EOL> """<STR_LIT>""" <EOL> return json . dumps ( obj , separators = ( '<STR_LIT:U+002C>' , '<STR_LIT::>' ) , sort_keys = True ) <EOL> class ColorSet ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , colormap , color_desc_list ) : <EOL> """<STR_LIT>""" <EOL> self . color_desc_list = color_desc_list <EOL> self . color_id_set = set ( ) <EOL> for color_desc in color_desc_list : <EOL> color_id = colormap . resolve_color_desc ( color_desc ) <EOL> self . color_id_set . add ( color_id ) <EOL> def __repr__ ( self ) : <EOL> return self . color_desc_list . __repr__ ( ) <EOL> def uncolored_only ( self ) : <EOL> return self . color_id_set == set ( [ <NUM_LIT:0> ] ) <EOL> def get_data ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . color_desc_list <EOL> def get_hash_string ( self ) : <EOL> """<STR_LIT>""" <EOL> json = deterministic_json_dumps ( sorted ( self . color_desc_list ) ) <EOL> return hashlib . sha256 ( json ) . hexdigest ( ) <EOL> def get_earliest ( self ) : <EOL> """<STR_LIT>""" <EOL> all_descs = self . get_data ( ) <EOL> if not len ( all_descs ) : <EOL> return "<STR_LIT>" <EOL> best = all_descs [ <NUM_LIT:0> ] <EOL> best_components = best . split ( '<STR_LIT::>' ) <EOL> for desc in all_descs [ <NUM_LIT:1> : ] : <EOL> components = desc . split ( '<STR_LIT::>' ) <EOL> if int ( components [ <NUM_LIT:3> ] ) < int ( best_components [ <NUM_LIT:3> ] ) : <EOL> best = desc <EOL> elif int ( components [ <NUM_LIT:3> ] ) == int ( best_components [ <NUM_LIT:3> ] ) : <EOL> if cmp ( components [ <NUM_LIT:1> ] , best_components [ <NUM_LIT:1> ] ) == - <NUM_LIT:1> : <EOL> best = desc <EOL> return best <EOL> def get_color_hash ( self ) : <EOL> """<STR_LIT>""" <EOL> return b2a_base58 ( self . get_hash_string ( ) . decode ( '<STR_LIT>' ) [ : <NUM_LIT:10> ] ) <EOL> def has_color_id ( self , color_id ) : <EOL> """<STR_LIT>""" <EOL> return ( color_id in self . color_id_set ) <EOL> def intersects ( self , other ) : <EOL> """<STR_LIT>""" <EOL> return len ( self . color_id_set & other . color_id_set ) > <NUM_LIT:0> <EOL> def equals ( self , other ) : <EOL> """<STR_LIT>""" <EOL> return self . color_id_set == other . color_id_set <EOL> @ classmethod <EOL> def from_color_ids ( cls , colormap , color_ids ) : <EOL> """<STR_LIT>""" <EOL> color_desc_list = [ colormap . find_color_desc ( color_id ) <EOL> for color_id in color_ids ] <EOL> return cls ( colormap , color_desc_list ) </s>
