<s> from __future__ import absolute_import , print_function , unicode_literals , division <EOL> from bisect import bisect_left <EOL> from collections import defaultdict <EOL> from datetime import datetime <EOL> from functools import wraps <EOL> import json <EOL> from sc2reader . log_utils import loggable <EOL> def plugin ( func ) : <EOL> @ wraps ( func ) <EOL> def wrapper ( ** options ) : <EOL> @ wraps ( func ) <EOL> def call ( * args , ** kwargs ) : <EOL> opt = kwargs . copy ( ) <EOL> opt . update ( options ) <EOL> return func ( * args , ** opt ) <EOL> return call <EOL> return wrapper <EOL> class JSONDateEncoder ( json . JSONEncoder ) : <EOL> def default ( self , obj ) : <EOL> if isinstance ( obj , datetime ) : <EOL> return obj . strftime ( "<STR_LIT>" ) <EOL> return json . JSONEncoder . default ( self , obj ) <EOL> class GameState ( dict ) : <EOL> def __init__ ( self , initial_state ) : <EOL> self . _frames = list ( ) <EOL> self . _frameset = set ( ) <EOL> self [ <NUM_LIT:0> ] = initial_state <EOL> self . locked = False <EOL> def __getitem__ ( self , frame ) : <EOL> if frame in self : <EOL> return super ( GameState , self ) . __getitem__ ( frame ) <EOL> key = bisect_left ( self . _frames , frame ) <EOL> if key == len ( self . _frames ) or self . _frames [ key ] > frame : <EOL> prev_frame = self . _frames [ key - <NUM_LIT:1> ] <EOL> else : <EOL> prev_frame = self . _frames [ key ] <EOL> if self . locked : <EOL> state = self [ prev_frame ] <EOL> else : <EOL> state = self [ prev_frame ] <EOL> if hasattr ( state , '<STR_LIT>' ) : <EOL> state = state . copy ( ) <EOL> self [ frame ] = state <EOL> return state <EOL> def __setitem__ ( self , frame , value ) : <EOL> if frame not in self . _frameset : <EOL> self . _frames . insert ( bisect_left ( self . _frames , frame ) , frame ) <EOL> self . _frameset . add ( frame ) <EOL> super ( GameState , self ) . __setitem__ ( frame , value ) <EOL> @ loggable <EOL> class UnitSelection ( object ) : <EOL> def __init__ ( self , objects = None ) : <EOL> self . objects = objects or list ( ) <EOL> def select ( self , new_objects ) : <EOL> new_set = set ( self . objects + list ( new_objects ) ) <EOL> self . objects = sorted ( new_set , key = lambda obj : obj . id ) <EOL> def deselect ( self , mode , data ) : <EOL> """<STR_LIT>""" <EOL> size = len ( self . objects ) <EOL> if mode == '<STR_LIT:None>' : <EOL> return True <EOL> elif mode == '<STR_LIT>' : <EOL> """<STR_LIT>""" <EOL> mask = data <EOL> if len ( mask ) < size : <EOL> mask = mask + [ False ] * ( len ( self . objects ) - len ( mask ) ) <EOL> self . logger . debug ( "<STR_LIT>" . format ( mask ) ) <EOL> self . objects = [ obj for ( slct , obj ) in filter ( lambda slct_obj : not slct_obj [ <NUM_LIT:0> ] , zip ( mask , self . objects ) ) ] <EOL> return len ( mask ) <= size <EOL> elif mode == '<STR_LIT>' : <EOL> """<STR_LIT>""" <EOL> clean_data = list ( filter ( lambda i : i < size , data ) ) <EOL> self . objects = [ self . objects [ i ] for i in range ( len ( self . objects ) ) if i not in clean_data ] <EOL> return len ( clean_data ) == len ( data ) <EOL> elif mode == '<STR_LIT>' : <EOL> """<STR_LIT>""" <EOL> clean_data = list ( filter ( lambda i : i < size , data ) ) <EOL> self . objects = [ self . objects [ i ] for i in clean_data ] <EOL> return len ( clean_data ) == len ( data ) <EOL> else : <EOL> return False <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT:U+002CU+0020>' . join ( str ( obj ) for obj in self . objects ) <EOL> def copy ( self ) : <EOL> return UnitSelection ( self . objects [ : ] ) <EOL> class PlayerSelection ( defaultdict ) : <EOL> def __init__ ( self ) : <EOL> super ( PlayerSelection , self ) . __init__ ( UnitSelection ) <EOL> def copy ( self ) : <EOL> new = PlayerSelection ( ) <EOL> for bank , selection in self . items ( ) : <EOL> new [ bank ] = selection <EOL> return new </s>
