<s> from __future__ import unicode_literals , absolute_import , division , print_function <EOL> from collections import namedtuple , Mapping , Iterable , OrderedDict as odict <EOL> from pathlib import Path <EOL> from configparser import ConfigParser <EOL> from . hand import Range <EOL> from . constants import Position <EOL> _Situation = namedtuple ( '<STR_LIT>' , '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> _Spot = namedtuple ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> _POSITIONS = { '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' } <EOL> class Strategy ( Mapping ) : <EOL> def __init__ ( self , strategy , source = '<STR_LIT>' ) : <EOL> self . _config = ConfigParser ( default_section = '<STR_LIT>' , interpolation = None ) <EOL> self . _config . read_string ( strategy , source ) <EOL> self . _situations = odict ( ) <EOL> for name in self . _config . sections ( ) : <EOL> values = dict . fromkeys ( _Situation . _fields , None ) <EOL> for key , val in self . _config [ name ] . items ( ) : <EOL> if ( not val ) or ( key not in _Situation . _fields ) : <EOL> continue <EOL> elif key in _POSITIONS : <EOL> values [ key ] = Range ( val ) <EOL> else : <EOL> values [ key ] = val <EOL> self . _situations [ name ] = _Situation ( ** values ) <EOL> self . _tuple = tuple ( self . _situations . values ( ) ) <EOL> @ classmethod <EOL> def from_file ( cls , filename ) : <EOL> strategy = Path ( filename ) . open ( encoding = '<STR_LIT:utf-8>' ) . read ( ) <EOL> return cls ( strategy , source = filename ) <EOL> def __getattr__ ( self , name ) : <EOL> return self . _config [ '<STR_LIT>' ] [ name ] <EOL> def __iter__ ( self ) : <EOL> return iter ( self . _situations ) <EOL> def items ( self ) : <EOL> return self . _situations . items ( ) <EOL> def keys ( self ) : <EOL> return self . _situations . keys ( ) <EOL> def get ( self , key , default = None ) : <EOL> return self . _situations . get ( key , default ) <EOL> def __getitem__ ( self , key ) : <EOL> if isinstance ( key , unicode ) : <EOL> return self . _situations . __getitem__ ( key ) <EOL> elif isinstance ( key , int ) : <EOL> return self . _tuple [ key ] <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def values ( self ) : <EOL> return self . _situations . values ( ) <EOL> def __contains__ ( self , key ) : <EOL> return self . _situations . __contains__ ( key ) <EOL> def __len__ ( self ) : <EOL> return len ( self . _situations ) <EOL> def get_first_spot ( self , situation = <NUM_LIT:0> ) : <EOL> situation = self [ situation ] <EOL> for posindex , position in enumerate ( Position ) : <EOL> range = getattr ( situation , position . name . lower ( ) ) <EOL> if range : <EOL> return _Spot ( position , range , posindex ) </s>
