signature:
def ppdict ( dict_to_print , br = '\n' , html = False , key_align = 'l' , sort_keys = True , key_preffix = '' , key_suffix = '' , value_prefix = '' , value_suffix = '' , left_margin = 3 , indent = 2 ) :
    """Indent representation of a dict"""

implementation:
if dict_to_print :
    if sort_keys :
        dic = dict_to_print . copy ( )
        keys = list ( dic . keys ( ) )
        keys . sort ( )
        dict_to_print = OrderedDict ( )
        for k in keys :
            dict_to_print [ k ] = dic [ k ]
    tmp = [ '{' ]
    ks = [ type ( x ) == str and "'%s'" % x or x for x in dict_to_print . keys ( ) ]
    vs = [ type ( x ) == str and "'%s'" % x or x for x in dict_to_print . values ( ) ]
    max_key_len = max ( [ len ( str ( x ) ) for x in ks ] )
    for i in range ( len ( ks ) ) :
        k = { 1 : str ( ks [ i ] ) . ljust ( max_key_len ) , key_align == 'r' : str ( ks [ i ] ) . rjust ( max_key_len ) } [ 1 ]
        v = vs [ i ]
        tmp . append ( ' ' * indent + '{}{}{}:{}{}{},' . format ( key_preffix , k , key_suffix , value_prefix , v , value_suffix ) )
    tmp [ - 1 ] = tmp [ - 1 ] [ : - 1 ]
    # remove the ' , ' in the last item
    tmp . append ( '}' )
    if left_margin :
        tmp = [ ' ' * left_margin + x for x in tmp ]
    if html :
        return '<code>{}</code>' . format ( br . join ( tmp ) . replace ( ' ' , ' ' ) )
    else :
        return br . join ( tmp )
else :
    return '{}'


signature:
def eq_ ( result , expected , msg = None ) :
    """Shadow of the Nose builtin which presents easier to read multiline output ."""

implementation:
params = { 'expected' : expected , 'result' : result }
aka = """

--------------------------------- aka -----------------------------------------

Expected:
%(expected)r

Got:
%(result)r
""" % params
default_msg = """
Expected:
%(expected)s

Got:
%(result)s
""" % params
if ( ( repr ( result ) != six . text_type ( result ) ) or ( repr ( expected ) != six . text_type ( expected ) ) ) :
    default_msg += aka
assertion_msg = msg or default_msg
# This assert will bubble up to Nose ' s failure handling , which at some
# point calls explicit str ( ) - which will UnicodeDecodeError on any non
# ASCII text .
# To work around this , we make sure Unicode strings become bytestrings
# beforehand , with explicit encode .
if isinstance ( assertion_msg , six . text_type ) :
    assertion_msg = assertion_msg . encode ( 'utf-8' )
assert result == expected , assertion_msg


signature:
def _assert_contains ( haystack , needle , invert , escape = False ) :
    """Test for existence of ` ` needle ` ` regex within ` ` haystack ` ` .
 Say ` ` escape ` ` to escape the ` ` needle ` ` if you aren ' t really using the
 regex feature & have special characters in it ."""

implementation:
myneedle = re . escape ( needle ) if escape else needle
matched = re . search ( myneedle , haystack , re . M )
if ( invert and matched ) or ( not invert and not matched ) :
    raise AssertionError ( "'%s' %sfound in '%s'" % ( needle , "" if invert else "not " , haystack ) )


signature:
def parse_config_file ( ) :
    """Find the . splunk _ logger config file in the current directory , or in the
 user ' s home and parse it . The one in the current directory has precedence .
 : return : A tuple with :
 - project _ id
 - access _ token"""

implementation:
for filename in ( '.splunk_logger' , os . path . expanduser ( '~/.splunk_logger' ) ) :
    project_id , access_token , api_domain = _parse_config_file_impl ( filename )
    if project_id is not None and access_token is not None and api_domain is not None :
        return project_id , access_token , api_domain
else :
    return None , None , None


signature:
def _parse_config_file_impl ( filename ) :
    """Format for the file is :
 credentials :
 project _ id : . . .
 access _ token : . . .
 api _ domain : . . .
 : param filename : The filename to parse
 : return : A tuple with :
 - project _ id
 - access _ token
 - api _ domain"""

implementation:
try :
    doc = yaml . load ( file ( filename ) . read ( ) )
    project_id = doc [ "credentials" ] [ "project_id" ]
    access_token = doc [ "credentials" ] [ "access_token" ]
    api_domain = doc [ "credentials" ] [ "api_domain" ]
    return project_id , access_token , api_domain
except :
    return None , None , None


signature:
def dem_url_dia ( dt_day = '2015-06-22' ) :
    """Obtiene las urls de descarga de los datos de demanda energética de un día concreto ."""

implementation:
def _url_tipo_dato ( str_dia , k ) :
    url = SERVER + '/archives/{}/download_json?locale=es' . format ( D_TIPOS_REQ_DEM [ k ] )
    if type ( str_dia ) is str :
        return url + '&date=' + str_dia
    else :
        return url + '&date=' + str_dia . date ( ) . isoformat ( )
urls = [ _url_tipo_dato ( dt_day , k ) for k in D_TIPOS_REQ_DEM . keys ( ) ]
return urls


signature:
def dem_procesa_datos_dia ( key_day , response ) :
    """Procesa los datos descargados en JSON ."""

implementation:
dfs_import , df_import , dfs_maxmin , hay_errores = [ ] , None , [ ] , 0
for r in response :
    tipo_datos , data = _extract_func_json_data ( r )
    if tipo_datos is not None :
        if ( 'IND_MaxMin' in tipo_datos ) and data :
            df_import = _import_daily_max_min ( data )
            dfs_maxmin . append ( df_import )
        elif data :
            df_import = _import_json_ts_data ( data )
            dfs_import . append ( df_import )
    if tipo_datos is None or df_import is None :
        hay_errores += 1
if hay_errores == 4 : # No hay nada , salida temprana sin retry :
    print_redb ( '** No hay datos para el día {}!' . format ( key_day ) )
    return None , - 2
else : # if hay _ errores < 3:
# TODO formar datos incompletos ! ! ( max - min con NaN ' s , etc . )
    data_import = { }
    if dfs_import :
        data_import [ KEYS_DATA_DEM [ 0 ] ] = dfs_import [ 0 ] . join ( dfs_import [ 1 ] )
    if len ( dfs_maxmin ) == 2 :
        data_import [ KEYS_DATA_DEM [ 1 ] ] = dfs_maxmin [ 0 ] . join ( dfs_maxmin [ 1 ] )
    elif dfs_maxmin :
        data_import [ KEYS_DATA_DEM [ 1 ] ] = dfs_maxmin [ 0 ]
    if not data_import :
        print_err ( 'DÍA: {} -> # ERRORES: {}' . format ( key_day , hay_errores ) )
        return None , - 2
    return data_import , 0


signature:
def dem_data_dia ( str_dia = '2015-10-10' , str_dia_fin = None ) :
    """Obtiene datos de demanda energética en un día concreto o un intervalo , accediendo directamente a la web ."""

implementation:
params = { 'date_fmt' : DATE_FMT , 'usar_multithread' : False , 'num_retries' : 1 , "timeout" : 10 , 'func_procesa_data_dia' : dem_procesa_datos_dia , 'func_url_data_dia' : dem_url_dia , 'data_extra_request' : { 'json_req' : False , 'headers' : HEADERS } }
if str_dia_fin is not None :
    params [ 'usar_multithread' ] = True
    data , hay_errores , str_import = get_data_en_intervalo ( str_dia , str_dia_fin , ** params )
else :
    data , hay_errores , str_import = get_data_en_intervalo ( str_dia , str_dia , ** params )
if not hay_errores :
    return data
else :
    print_err ( str_import )
    return None


signature:
def flag_inner_classes ( obj ) :
    """Mutates any attributes on ` ` obj ` ` which are classes , with link to ` ` obj ` ` .
 Adds a convenience accessor which instantiates ` ` obj ` ` and then calls its
 ` ` setup ` ` method .
 Recurses on those objects as well ."""

implementation:
for tup in class_members ( obj ) :
    tup [ 1 ] . _parent = obj
    tup [ 1 ] . _parent_inst = None
    tup [ 1 ] . __getattr__ = my_getattr
    flag_inner_classes ( tup [ 1 ] )


signature:
def autohide ( obj ) :
    """Automatically hide setup ( ) and teardown ( ) methods , recursively ."""

implementation:
# Members on obj
for name , item in six . iteritems ( vars ( obj ) ) :
    if callable ( item ) and name in ( 'setup' , 'teardown' ) :
        item = hide ( item )
# Recurse into class members
for name , subclass in class_members ( obj ) :
    autohide ( subclass )


signature:
def trap ( func ) :
    """Replace sys . std ( out | err ) with a wrapper during execution , restored after .
 In addition , a new combined - streams output ( another wrapper ) will appear at
 ` ` sys . stdall ` ` . This stream will resemble what a user sees at a terminal ,
 i . e . both out / err streams intermingled ."""

implementation:
@ wraps ( func )
def wrapper ( * args , ** kwargs ) : # Use another CarbonCopy even though we ' re not cc ' ing ; for our " write
# bytes , return strings on py3 " behavior . Meh .
    sys . stdall = CarbonCopy ( )
    my_stdout , sys . stdout = sys . stdout , CarbonCopy ( cc = sys . stdall )
    my_stderr , sys . stderr = sys . stderr , CarbonCopy ( cc = sys . stdall )
    try :
        return func ( * args , ** kwargs )
    finally :
        sys . stdout = my_stdout
        sys . stderr = my_stderr
        del sys . stdall
return wrapper


signature:
def pvpc_url_dia ( dt_day ) :
    """Obtiene la url de descarga de los datos de PVPC de un día concreto .
 Anteriormente era : ' http : / / www . esios . ree . es / Solicitar ? fileName = pvpcdesglosehorario _ ' + str _ dia
 + ' & fileType = xml & idioma = es ' , pero ahora es en JSON y requiere token _ auth en headers ."""

implementation:
if type ( dt_day ) is str :
    return SERVER + '/archives/70/download_json?locale=es' + '&date=' + dt_day
else :
    return SERVER + '/archives/70/download_json?locale=es' + '&date=' + dt_day . date ( ) . isoformat ( )


signature:
def pvpc_calc_tcu_cp_feu_d ( df , verbose = True , convert_kwh = True ) :
    """Procesa TCU , CP , FEU diario .
 : param df :
 : param verbose :
 : param convert _ kwh :
 : return :"""

implementation:
if 'TCU' + TARIFAS [ 0 ] not in df . columns : # Pasa de € / MWh a € / kWh :
    if convert_kwh :
        cols_mwh = [ c + t for c in COLS_PVPC for t in TARIFAS if c != 'COF' ]
        df [ cols_mwh ] = df [ cols_mwh ] . applymap ( lambda x : x / 1000. )
    # Obtiene columnas TCU , CP , precio día
    gb_t = df . groupby ( lambda x : TARIFAS [ np . argmax ( [ t in x for t in TARIFAS ] ) ] , axis = 1 )
    for k , g in gb_t :
        if verbose :
            print ( 'TARIFA {}' . format ( k ) )
            print ( g . head ( ) )
        # Cálculo de TCU
        df [ 'TCU{}' . format ( k ) ] = g [ k ] - g [ 'TEU{}' . format ( k ) ]
        # Cálculo de CP
        # cols _ cp = [ c + k for c in [ ' FOS ' , ' FOM ' , ' INT ' , ' PCAP ' , ' PMH ' , ' SAH ' ] ]
        cols_cp = [ c + k for c in COLS_PVPC if c not in [ '' , 'COF' , 'TEU' ] ]
        df [ 'CP{}' . format ( k ) ] = g [ cols_cp ] . sum ( axis = 1 )
        # Cálculo de PERD - - > No es posible así , ya que los valores base ya vienen con PERD
        # dfs _ pvpc [ k ] [ ' PERD { } ' . format ( k ) ] = dfs _ pvpc [ k ] [ ' TCU { } ' . format ( k ) ] / dfs _ pvpc [ k ] [ ' CP { } ' . format ( k ) ]
        # dfs _ pvpc [ k ] [ ' PERD { } ' . format ( k ) ] = dfs _ pvpc [ k ] [ ' INT { } ' . format ( k ) ] / 1.92
        # Cálculo de FEU diario
        cols_k = [ 'TEU' + k , 'TCU' + k , 'COF' + k ]
        g = df [ cols_k ] . groupby ( 'TEU' + k )
        pr = g . apply ( lambda x : x [ 'TCU' + k ] . dot ( x [ 'COF' + k ] ) / x [ 'COF' + k ] . sum ( ) )
        pr . name = 'PD_' + k
        df = df . join ( pr , on = 'TEU' + k , rsuffix = '_r' )
        df [ 'PD_' + k ] += df [ 'TEU' + k ]
return df


signature:
def pvpc_procesa_datos_dia ( _ , response , verbose = True ) :
    """Procesa la información JSON descargada y forma el dataframe de los datos de un día ."""

implementation:
try :
    d_data = response [ 'PVPC' ]
    df = _process_json_pvpc_hourly_data ( pd . DataFrame ( d_data ) )
    return df , 0
except Exception as e :
    if verbose :
        print ( 'ERROR leyendo información de web: {}' . format ( e ) )
    return None , - 2


signature:
def pvpc_data_dia ( str_dia , str_dia_fin = None ) :
    """Obtiene datos de PVPC en un día concreto o un intervalo , accediendo directamente a la web ."""

implementation:
params = { 'date_fmt' : DATE_FMT , 'usar_multithread' : False , 'func_procesa_data_dia' : pvpc_procesa_datos_dia , 'func_url_data_dia' : pvpc_url_dia , 'data_extra_request' : { 'json_req' : True , 'headers' : HEADERS } }
if str_dia_fin is not None :
    params [ 'usar_multithread' ] = True
    data , hay_errores , str_import = get_data_en_intervalo ( str_dia , str_dia_fin , ** params )
else :
    data , hay_errores , str_import = get_data_en_intervalo ( str_dia , str_dia , ** params )
if not hay_errores :
    return data
else :
    return str_import


signature:
def _compress ( self , input_str ) :
    """Compress the log message in order to send less bytes to the wire ."""

implementation:
compressed_bits = cStringIO . StringIO ( )
f = gzip . GzipFile ( fileobj = compressed_bits , mode = 'wb' )
f . write ( input_str )
f . close ( )
return compressed_bits . getvalue ( )


signature:
def get_data_coeficientes_perfilado_2017 ( force_download = False ) :
    """Extrae la información de las dos hojas del Excel proporcionado por REE
 con los perfiles iniciales para 2017.
 : param force _ download : Descarga el fichero ' raw ' del servidor , en vez de acudir a la copia local .
 : return : perfiles _ 2017 , coefs _ alpha _ beta _ gamma
 : rtype : tuple"""

implementation:
path_perfs = os . path . join ( STORAGE_DIR , 'perfiles_consumo_2017.h5' )
if force_download or not os . path . exists ( path_perfs ) : # Coeficientes de perfilado y demanda de referencia ( 1a hoja )
    cols_sheet1 = [ 'Mes' , 'Día' , 'Hora' , 'Pa,0m,d,h' , 'Pb,0m,d,h' , 'Pc,0m,d,h' , 'Pd,0m,d,h' , 'Demanda de Referencia 2017 (MW)' ]
    perfs_2017 = pd . read_excel ( URL_PERFILES_2017 , header = None , skiprows = [ 0 , 1 ] , names = cols_sheet1 )
    perfs_2017 [ 'ts' ] = pd . DatetimeIndex ( start = '2017-01-01' , freq = 'H' , tz = TZ , end = '2017-12-31 23:59' )
    perfs_2017 = perfs_2017 . set_index ( 'ts' ) . drop ( [ 'Mes' , 'Día' , 'Hora' ] , axis = 1 )
    # Coefs Alfa , Beta , Gamma ( 2a hoja ) :
    coefs_alpha_beta_gamma = pd . read_excel ( URL_PERFILES_2017 , sheetname = 1 )
    print ( 'Escribiendo perfiles 2017 en disco, en {}' . format ( path_perfs ) )
    with pd . HDFStore ( path_perfs , 'w' ) as st :
        st . put ( 'coefs' , coefs_alpha_beta_gamma )
        st . put ( 'perfiles' , perfs_2017 )
    print ( 'HDFStore de tamaño {:.3f} KB' . format ( os . path . getsize ( path_perfs ) / 1000 ) )
else :
    with pd . HDFStore ( path_perfs , 'r' ) as st :
        coefs_alpha_beta_gamma = st [ 'coefs' ]
        perfs_2017 = st [ 'perfiles' ]
return perfs_2017 , coefs_alpha_beta_gamma


signature:
def get_data_perfiles_estimados_2017 ( force_download = False ) :
    """Extrae perfiles estimados para 2017 con el formato de los CSV ' s mensuales con los perfiles definitivos .
 : param force _ download : bool para forzar la descarga del excel de la web de REE .
 : return : perfiles _ 2017
 : rtype : pd . Dataframe"""

implementation:
global DATA_PERFILES_2017
if ( DATA_PERFILES_2017 is None ) or force_download :
    perf_demref_2017 , _ = get_data_coeficientes_perfilado_2017 ( force_download = force_download )
    # Conversión de formato de dataframe de perfiles 2017 a finales ( para uniformizar ) :
    cols_usar = [ 'Pa,0m,d,h' , 'Pb,0m,d,h' , 'Pc,0m,d,h' , 'Pd,0m,d,h' ]
    perfs_2017 = perf_demref_2017 [ cols_usar ] . copy ( )
    perfs_2017 . columns = [ 'COEF. PERFIL {}' . format ( p ) for p in 'ABCD' ]
    DATA_PERFILES_2017 = perfs_2017
    return perfs_2017
return DATA_PERFILES_2017


signature:
def main_cli ( ) :
    """Actualiza la base de datos de PVPC / DEMANDA almacenados como dataframe en local ,
 creando una nueva si no existe o hubiere algún problema . Los datos registrados se guardan en HDF5"""

implementation:
def _get_parser_args ( ) :
    p = argparse . ArgumentParser ( description = 'Gestor de DB de PVPC/DEMANDA (esios.ree.es)' )
    p . add_argument ( '-d' , '--dem' , action = 'store_true' , help = 'Selecciona BD de demanda (BD de PVPC por defecto)' )
    p . add_argument ( '-i' , '--info' , action = 'store' , nargs = '*' , help = "Muestra información de la BD seleccionada.      " "* Puede usar intervalos temporales y nombres de columnas, " "como '-i gen noc 2017-01-24 2017-01-26'" )
    p . add_argument ( '-fu' , '-FU' , '--forceupdate' , action = 'store_true' , help = "Fuerza la reconstrucción total de la BD seleccionada" )
    p . add_argument ( '-u' , '-U' , '--update' , action = 'store_true' , help = "Actualiza la información de la BD seleccionada hasta el instante actual" )
    p . add_argument ( '-p' , '--plot' , action = 'store_true' , help = "Genera plots de la información filtrada de la BD" )
    p . add_argument ( '-v' , '--verbose' , action = 'store_true' , help = 'Muestra información extra' )
    arguments = p . parse_args ( )
    return arguments , p
def _parse_date ( string , columns ) :
    try :
        ts = pd . Timestamp ( string )
        print_cyan ( '{} es timestamp: {:%c} --> {}' . format ( string , ts , ts . date ( ) ) )
        columns . remove ( string )
        return ts . date ( ) . isoformat ( )
    except ValueError :
        pass
args , parser = _get_parser_args ( )
print_secc ( 'ESIOS PVPC/DEMANDA' )
if args . dem :
    db_web = DatosREE ( update = args . update , force_update = args . forceupdate , verbose = args . verbose )
else :
    db_web = PVPC ( update = args . update , force_update = args . forceupdate , verbose = args . verbose )
data = db_web . data [ 'data' ]
if args . info is not None :
    if len ( args . info ) > 0 :
        cols = args . info . copy ( )
        dates = [ d for d in [ _parse_date ( s , cols ) for s in args . info ] if d ]
        if len ( dates ) == 2 :
            data = data . loc [ dates [ 0 ] : dates [ 1 ] ]
        elif len ( dates ) == 1 :
            data = data . loc [ dates [ 0 ] ]
        if len ( cols ) > 0 :
            try :
                data = data [ [ c . upper ( ) for c in cols ] ]
            except KeyError as e :
                print_red ( 'NO SE PUEDE FILTRAR LA COLUMNA (Exception: {})\nLAS COLUMNAS DISPONIBLES SON:\n{}' . format ( e , data . columns ) )
        print_info ( data )
    else :
        print_secc ( 'LAST 24h in DB:' )
        print_info ( data . iloc [ - 24 : ] )
        print_cyan ( data . columns )
if args . plot :
    if args . dem :
        from esiosdata . pvpcplot import pvpcplot_tarifas_hora , pvpcplot_grid_hora
        print_red ( 'IMPLEMENTAR PLOTS DEM' )
    else :
        from esiosdata . pvpcplot import pvpcplot_tarifas_hora , pvpcplot_grid_hora
        if len ( data ) < 750 :
            pvpcplot_grid_hora ( data )
            # pvpcplot _ tarifas _ hora ( data )
        else :
            print_red ( 'La selección para plot es excesiva: {} samples de {} a {}\nSe hace plot de las últimas 24h' . format ( len ( data ) , data . index [ 0 ] , data . index [ - 1 ] ) )
            pvpcplot_grid_hora ( db_web . data [ 'data' ] . iloc [ - 24 : ] )
            pvpcplot_tarifas_hora ( db_web . data [ 'data' ] . iloc [ - 24 : ] )


signature:
def registerGoodClass ( self , class_ ) :
    """Internal bookkeeping to handle nested classes"""

implementation:
# Class itself added to " good " list
self . _valid_classes . append ( class_ )
# Recurse into any inner classes
for name , cls in class_members ( class_ ) :
    if self . isValidClass ( cls ) :
        self . registerGoodClass ( cls )


signature:
def isValidClass ( self , class_ ) :
    """Needs to be its own method so it can be called from both wantClass and
 registerGoodClass ."""

implementation:
module = inspect . getmodule ( class_ )
valid = ( module in self . _valid_modules or ( hasattr ( module , '__file__' ) and module . __file__ in self . _valid_named_modules ) )
return valid and not private ( class_ )


signature:
def procesa_data_dia ( self , key_dia , datos_para_procesar ) :
    """Procesa los datos descargados correspondientes a un día ` key _ dia ` ."""

implementation: return pvpc_procesa_datos_dia ( key_dia , datos_para_procesar , verbose = self . verbose )


signature:
def get_resample_data ( self ) :
    """Obtiene los dataframes de los datos de PVPC con resampling diario y mensual ."""

implementation:
if self . data is not None :
    if self . _pvpc_mean_daily is None :
        self . _pvpc_mean_daily = self . data [ 'data' ] . resample ( 'D' ) . mean ( )
    if self . _pvpc_mean_monthly is None :
        self . _pvpc_mean_monthly = self . data [ 'data' ] . resample ( 'MS' ) . mean ( )
return self . _pvpc_mean_daily , self . _pvpc_mean_monthly


signature:
def last_entry ( self , data_revisar = None , key_revisar = None ) :
    """Definición específica para filtrar por datos de demanda energética ( pues los datos se extienden más allá del
 tiempo presente debido a las columnas de potencia prevista y programada .
 : param data _ revisar : ( OPC ) Se puede pasar un dataframe específico
 : param key _ revisar : ( OPC ) Normalmente , para utilizar ' dem '
 : return : tmax , num _ entradas"""

implementation:
if data_revisar is None and key_revisar is None :
    data_revisar = self . data [ self . masterkey ] [ pd . notnull ( self . data [ self . masterkey ] [ 'dem' ] ) ]
    super ( DatosREE , self ) . printif ( 'Últimos valores de generación y demanda:' , 'info' )
    super ( DatosREE , self ) . printif ( data_revisar . tail ( ) , 'info' )
    return super ( DatosREE , self ) . last_entry ( data_revisar , 'dem' )
else :
    return super ( DatosREE , self ) . last_entry ( data_revisar , key_revisar )


signature:
def integridad_data ( self , data_integr = None , key = None ) :
    """Definición específica para comprobar timezone y frecuencia de los datos , además de comprobar
 que el index de cada dataframe de la base de datos sea de fechas , único ( sin duplicados ) y creciente
 : param data _ integr :
 : param key :"""

implementation:
if data_integr is None and key is None and all ( k in self . data . keys ( ) for k in KEYS_DATA_DEM ) :
    assert ( self . data [ KEYS_DATA_DEM [ 0 ] ] . index . freq == FREQ_DAT_DEM and self . data [ KEYS_DATA_DEM [ 0 ] ] . index . tz == self . TZ )
    if self . data [ KEYS_DATA_DEM [ 1 ] ] is not None :
        assert ( self . data [ KEYS_DATA_DEM [ 1 ] ] . index . freq == 'D' )
super ( DatosREE , self ) . integridad_data ( data_integr , key )


signature:
def busca_errores_data ( self ) :
    """Busca errores o inconsistencias en los datos adquiridos
 : return : Dataframe de errores encontrados"""

implementation:
data_busqueda = self . append_delta_index ( TS_DATA_DEM , data_delta = self . data [ self . masterkey ] . copy ( ) )
idx_desconex = ( ( ( data_busqueda . index < 'now' ) & ( data_busqueda . index >= self . DATE_INI ) ) & ( ( data_busqueda . delta_T > 1 ) | data_busqueda [ 'dem' ] . isnull ( ) | data_busqueda [ 'pre' ] . isnull ( ) | data_busqueda [ 'pro' ] . isnull ( ) ) )
sosp = data_busqueda [ idx_desconex ] . copy ( )
assert len ( sosp ) == 0
# if len ( sosp ) > 0:
# cols _ show = [ ' bad _ dem ' , ' bad _ pre ' , ' bad _ T ' , ' delta ' , ' delta _ T ' , ' dem ' , ' pre ' , ' pro ' ]
# cols _ ss = cols _ show [ : 3]
# how _ r = { k : pd . Series . sum if k = = ' delta ' else ' sum ' for k in cols _ show }
# sosp [ cols _ show [ 0 ] ] = sosp [ ' dem ' ] . isnull ( )
# sosp [ cols _ show [ 1 ] ] = sosp [ ' pre ' ] . isnull ( )
# sosp [ cols _ show [ 2 ] ] = sosp [ ' delta _ T ' ] > 1
# if verbose :
# print ( sosp [ cols _ show ] . tz _ localize ( None ) . resample ( ' D ' , how = how _ r ) . dropna ( how = ' all ' , subset = cols _ ss ) )
# print ( sosp [ cols _ show ] . tz _ localize ( None ) . resample ( ' MS ' , how = how _ r ) . dropna ( how = ' all ' , subset = cols _ ss ) )
# return sosp
return pd . DataFrame ( )


signature:
def sanitize_path ( path ) :
    """Performs sanitation of the path after validating
 : param path : path to sanitize
 : return : path
 : raises :
 - InvalidPath if the path doesn ' t start with a slash"""

implementation:
if path == '/' : # Nothing to do , just return
    return path
if path [ : 1 ] != '/' :
    raise InvalidPath ( 'The path must start with a slash' )
# Deduplicate slashes in path
path = re . sub ( r'/+' , '/' , path )
# Strip trailing slashes and return
return path . rstrip ( '/' )


signature:
def _validate_schema ( obj ) :
    """Ensures the passed schema instance is compatible
 : param obj : object to validate
 : return : obj
 : raises :
 - IncompatibleSchema if the passed schema is of an incompatible type"""

implementation:
if obj is not None and not isinstance ( obj , Schema ) :
    raise IncompatibleSchema ( 'Schema must be of type {0}' . format ( Schema ) )
return obj


signature:
def attach_bp ( self , bp , description = '' ) :
    """Attaches a flask . Blueprint to the bundle
 : param bp : : class : ` flask . Blueprint ` object
 : param description : Optional description string
 : raises :
 - InvalidBlueprint if the Blueprint is not of type ` flask . Blueprint `"""

implementation:
if not isinstance ( bp , Blueprint ) :
    raise InvalidBlueprint ( 'Blueprints attached to the bundle must be of type {0}' . format ( Blueprint ) )
self . blueprints . append ( ( bp , description ) )


signature:
def move_dot ( self ) :
    """Returns the DottedRule that results from moving the dot ."""

implementation: return self . __class__ ( self . production , self . pos + 1 , self . lookahead )


signature:
def first ( self , symbols ) :
    """Computes the intermediate FIRST set using symbols ."""

implementation:
ret = set ( )
if EPSILON in symbols :
    return set ( [ EPSILON ] )
for symbol in symbols :
    ret |= self . _first [ symbol ] - set ( [ EPSILON ] )
    if EPSILON not in self . _first [ symbol ] :
        break
else :
    ret . add ( EPSILON )
return ret


signature:
def _compute_first ( self ) :
    """Computes the FIRST set for every symbol in the grammar .
 Tenatively based on _ compute _ first in PLY ."""

implementation:
for terminal in self . terminals :
    self . _first [ terminal ] . add ( terminal )
self . _first [ END_OF_INPUT ] . add ( END_OF_INPUT )
while True :
    changed = False
    for nonterminal , productions in self . nonterminals . items ( ) :
        for production in productions :
            new_first = self . first ( production . rhs )
            if new_first - self . _first [ nonterminal ] :
                self . _first [ nonterminal ] |= new_first
                changed = True
    if not changed :
        break


signature:
def _compute_follow ( self ) :
    """Computes the FOLLOW set for every non - terminal in the grammar .
 Tenatively based on _ compute _ follow in PLY ."""

implementation:
self . _follow [ self . start_symbol ] . add ( END_OF_INPUT )
while True :
    changed = False
    for nonterminal , productions in self . nonterminals . items ( ) :
        for production in productions :
            for i , symbol in enumerate ( production . rhs ) :
                if symbol not in self . nonterminals :
                    continue
                first = self . first ( production . rhs [ i + 1 : ] )
                new_follow = first - set ( [ EPSILON ] )
                if EPSILON in first or i == ( len ( production . rhs ) - 1 ) :
                    new_follow |= self . _follow [ nonterminal ]
                if new_follow - self . _follow [ symbol ] :
                    self . _follow [ symbol ] |= new_follow
                    changed = True
    if not changed :
        break


signature:
def initial_closure ( self ) :
    """Computes the initial closure using the START _ foo production ."""

implementation:
first_rule = DottedRule ( self . start , 0 , END_OF_INPUT )
return self . closure ( [ first_rule ] )


signature:
def goto ( self , rules , symbol ) :
    """Computes the next closure for rules based on the symbol we got .
 Args :
 rules - an iterable of DottedRules
 symbol - a string denoting the symbol we ' ve just seen
 Returns : frozenset of DottedRules"""

implementation: return self . closure ( { rule . move_dot ( ) for rule in rules if not rule . at_end and rule . rhs [ rule . pos ] == symbol } , )


signature:
def closure ( self , rules ) :
    """Fills out the entire closure based on some initial dotted rules .
 Args :
 rules - an iterable of DottedRules
 Returns : frozenset of DottedRules"""

implementation:
closure = set ( )
todo = set ( rules )
while todo :
    rule = todo . pop ( )
    closure . add ( rule )
    # If the dot is at the end , there ' s no need to process it .
    if rule . at_end :
        continue
    symbol = rule . rhs [ rule . pos ]
    for production in self . nonterminals [ symbol ] :
        for first in self . first ( rule . rest ) :
            if EPSILON in production . rhs : # Move immediately to the end if the production
            # goes to epsilon
                new_rule = DottedRule ( production , 1 , first )
            else :
                new_rule = DottedRule ( production , 0 , first )
            if new_rule not in closure :
                todo . add ( new_rule )
return frozenset ( closure )


signature:
def closures ( self ) :
    """Computes all LR ( 1 ) closure sets for the grammar ."""

implementation:
initial = self . initial_closure ( )
closures = collections . OrderedDict ( )
goto = collections . defaultdict ( dict )
todo = set ( [ initial ] )
while todo :
    closure = todo . pop ( )
    closures [ closure ] = closure
    symbols = { rule . rhs [ rule . pos ] for rule in closure if not rule . at_end }
    for symbol in symbols :
        next_closure = self . goto ( closure , symbol )
        if next_closure in closures or next_closure in todo :
            next_closure = ( closures . get ( next_closure ) or todo . get ( next_closure ) )
        else :
            closures [ next_closure ] = next_closure
            todo . add ( next_closure )
        goto [ closure ] [ symbol ] = next_closure
return initial , closures , goto


signature:
def init_app ( self , app ) :
    """Initializes Journey extension
 : param app : App passed from constructor or directly to init _ app
 : raises :
 - NoBundlesAttached if no bundles has been attached attached"""

implementation:
if len ( self . _attached_bundles ) == 0 :
    raise NoBundlesAttached ( "At least one bundle must be attached before initializing Journey" )
for bundle in self . _attached_bundles :
    processed_bundle = { 'path' : bundle . path , 'description' : bundle . description , 'blueprints' : [ ] }
    for ( bp , description ) in bundle . blueprints : # Register the BP
        blueprint = self . _register_blueprint ( app , bp , bundle . path , self . get_bp_path ( bp ) , description )
        # Finally , attach the blueprints to its parent
        processed_bundle [ 'blueprints' ] . append ( blueprint )
    self . _registered_bundles . append ( processed_bundle )


signature:
def routes_simple ( self ) :
    """Returns simple info about registered blueprints
 : return : Tuple containing endpoint , path and allowed methods for each route"""

implementation:
routes = [ ]
for bundle in self . _registered_bundles :
    bundle_path = bundle [ 'path' ]
    for blueprint in bundle [ 'blueprints' ] :
        bp_path = blueprint [ 'path' ]
        for child in blueprint [ 'routes' ] :
            routes . append ( ( child [ 'endpoint' ] , bundle_path + bp_path + child [ 'path' ] , child [ 'methods' ] ) )
return routes


signature:
def _bundle_exists ( self , path ) :
    """Checks if a bundle exists at the provided path
 : param path : Bundle path
 : return : bool"""

implementation:
for attached_bundle in self . _attached_bundles :
    if path == attached_bundle . path :
        return True
return False


signature:
def attach_bundle ( self , bundle ) :
    """Attaches a bundle object
 : param bundle : : class : ` flask _ journey . BlueprintBundle ` object
 : raises :
 - IncompatibleBundle if the bundle is not of type ` BlueprintBundle `
 - ConflictingPath if a bundle already exists at bundle . path
 - MissingBlueprints if the bundle doesn ' t contain any blueprints"""

implementation:
if not isinstance ( bundle , BlueprintBundle ) :
    raise IncompatibleBundle ( 'BlueprintBundle object passed to attach_bundle must be of type {0}' . format ( BlueprintBundle ) )
elif len ( bundle . blueprints ) == 0 :
    raise MissingBlueprints ( "Bundles must contain at least one flask.Blueprint" )
elif self . _bundle_exists ( bundle . path ) :
    raise ConflictingPath ( "Duplicate bundle path {0}" . format ( bundle . path ) )
elif self . _journey_path == bundle . path == '/' :
    raise ConflictingPath ( "Bundle path and Journey path cannot both be {0}" . format ( bundle . path ) )
self . _attached_bundles . append ( bundle )


signature:
def _register_blueprint ( self , app , bp , bundle_path , child_path , description ) :
    """Register and return info about the registered blueprint
 : param bp : : class : ` flask . Blueprint ` object
 : param bundle _ path : the URL prefix of the bundle
 : param child _ path : blueprint relative to the bundle path
 : return : Dict with info about the blueprint"""

implementation:
base_path = sanitize_path ( self . _journey_path + bundle_path + child_path )
app . register_blueprint ( bp , url_prefix = base_path )
return { 'name' : bp . name , 'path' : child_path , 'import_name' : bp . import_name , 'description' : description , 'routes' : self . get_blueprint_routes ( app , base_path ) }


signature:
def get_blueprint_routes ( app , base_path ) :
    """Returns detailed information about registered blueprint routes matching the ` BlueprintBundle ` path
 : param app : App instance to obtain rules from
 : param base _ path : Base path to return detailed route info for
 : return : List of route detail dicts"""

implementation:
routes = [ ]
for child in app . url_map . iter_rules ( ) :
    if child . rule . startswith ( base_path ) :
        relative_path = child . rule [ len ( base_path ) : ]
        routes . append ( { 'path' : relative_path , 'endpoint' : child . endpoint , 'methods' : list ( child . methods ) } )
return routes


signature:
def compute_precedence ( terminals , productions , precedence_levels ) :
    """Computes the precedence of terminal and production .
 The precedence of a terminal is it ' s level in the PRECEDENCE tuple . For
 a production , the precedence is the right - most terminal ( if it exists ) .
 The default precedence is DEFAULT _ PREC - ( LEFT , 0 ) .
 Returns :
 precedence - dict [ terminal | production ] = ( assoc , level )"""

implementation:
precedence = collections . OrderedDict ( )
for terminal in terminals :
    precedence [ terminal ] = DEFAULT_PREC
level_precs = range ( len ( precedence_levels ) , 0 , - 1 )
for i , level in zip ( level_precs , precedence_levels ) :
    assoc = level [ 0 ]
    for symbol in level [ 1 : ] :
        precedence [ symbol ] = ( assoc , i )
for production , prec_symbol in productions :
    if prec_symbol is None :
        prod_terminals = [ symbol for symbol in production . rhs if symbol in terminals ] or [ None ]
        precedence [ production ] = precedence . get ( prod_terminals [ - 1 ] , DEFAULT_PREC )
    else :
        precedence [ production ] = precedence . get ( prec_symbol , DEFAULT_PREC )
return precedence


signature:
def make_tables ( grammar , precedence ) :
    """Generates the ACTION and GOTO tables for the grammar .
 Returns :
 action - dict [ state ] [ lookahead ] = ( action , . . . )
 goto - dict [ state ] [ just _ reduced ] = new _ state"""

implementation:
ACTION = { }
GOTO = { }
labels = { }
def get_label ( closure ) :
    if closure not in labels :
        labels [ closure ] = len ( labels )
    return labels [ closure ]
def resolve_shift_reduce ( lookahead , s_action , r_action ) :
    s_assoc , s_level = precedence [ lookahead ]
    r_assoc , r_level = precedence [ r_action [ 1 ] ]
    if s_level < r_level :
        return r_action
    elif s_level == r_level and r_assoc == LEFT :
        return r_action
    else :
        return s_action
initial , closures , goto = grammar . closures ( )
for closure in closures :
    label = get_label ( closure )
    for rule in closure :
        new_action , lookahead = None , rule . lookahead
        if not rule . at_end :
            symbol = rule . rhs [ rule . pos ]
            is_terminal = symbol in grammar . terminals
            has_goto = symbol in goto [ closure ]
            if is_terminal and has_goto :
                next_state = get_label ( goto [ closure ] [ symbol ] )
                new_action , lookahead = ( 'shift' , next_state ) , symbol
        elif rule . production == grammar . start and rule . at_end :
            new_action = ( 'accept' , )
        elif rule . at_end :
            new_action = ( 'reduce' , rule . production )
        if new_action is None :
            continue
        prev_action = ACTION . get ( ( label , lookahead ) )
        if prev_action is None or prev_action == new_action :
            ACTION [ label , lookahead ] = new_action
        else :
            types = ( prev_action [ 0 ] , new_action [ 0 ] )
            if types == ( 'shift' , 'reduce' ) :
                chosen = resolve_shift_reduce ( lookahead , prev_action , new_action )
            elif types == ( 'reduce' , 'shift' ) :
                chosen = resolve_shift_reduce ( lookahead , new_action , prev_action )
            else :
                raise TableConflictError ( prev_action , new_action )
            ACTION [ label , lookahead ] = chosen
    for symbol in grammar . nonterminals :
        if symbol in goto [ closure ] :
            GOTO [ label , symbol ] = get_label ( goto [ closure ] [ symbol ] )
return get_label ( initial ) , ACTION , GOTO


signature:
def KB_AgentProgram ( KB ) :
    """A generic logical knowledge - based agent program . [ Fig . 7.1]"""

implementation:
steps = itertools . count ( )
def program ( percept ) :
    t = steps . next ( )
    KB . tell ( make_percept_sentence ( percept , t ) )
    action = KB . ask ( make_action_query ( t ) )
    KB . tell ( make_action_sentence ( action , t ) )
    return action
def make_percept_sentence ( self , percept , t ) :
    return Expr ( "Percept" ) ( percept , t )
def make_action_query ( self , t ) :
    return expr ( "ShouldDo(action, %d)" % t )
def make_action_sentence ( self , action , t ) :
    return Expr ( "Did" ) ( action [ expr ( 'action' ) ] , t )
return program


signature:
def expr ( s ) :
    """Create an Expr representing a logic expression by parsing the input
 string . Symbols and numbers are automatically converted to Exprs .
 In addition you can use alternative spellings of these operators :
 ' x = = > y ' parses as ( x > > y ) # Implication
 ' x < = = y ' parses as ( x < < y ) # Reverse implication
 ' x < = > y ' parses as ( x % y ) # Logical equivalence
 ' x = / = y ' parses as ( x ^ y ) # Logical disequality ( xor )
 But BE CAREFUL ; precedence of implication is wrong . expr ( ' P & Q = = > R & S ' )
 is ( ( P & ( Q > > R ) ) & S ) ; so you must use expr ( ' ( P & Q ) = = > ( R & S ) ' ) .
 > > > expr ( ' P < = > Q ( 1 ) ' )
 ( P < = > Q ( 1 ) )
 > > > expr ( ' P & Q | ~ R ( x , F ( x ) ) ' )
 ( ( P & Q ) | ~ R ( x , F ( x ) ) )"""

implementation:
if isinstance ( s , Expr ) :
    return s
if isnumber ( s ) :
    return Expr ( s )
# # Replace the alternative spellings of operators with canonical spellings
s = s . replace ( '==>' , '>>' ) . replace ( '<==' , '<<' )
s = s . replace ( '<=>' , '%' ) . replace ( '=/=' , '^' )
# # Replace a symbol or number , such as ' P ' with ' Expr ( " P " ) '
s = re . sub ( r'([a-zA-Z0-9_.]+)' , r'Expr("\1")' , s )
# # Now eval the string . ( A security hole ; do not use with an adversary . )
return eval ( s , { 'Expr' : Expr } )


signature:
def variables ( s ) :
    """Return a set of the variables in expression s .
 > > > ppset ( variables ( F ( x , A , y ) ) )
 set ( [ x , y ] )
 > > > ppset ( variables ( F ( G ( x ) , z ) ) )
 set ( [ x , z ] )
 > > > ppset ( variables ( expr ( ' F ( x , x ) & G ( x , y ) & H ( y , z ) & R ( A , z , z ) ' ) ) )
 set ( [ x , y , z ] )"""

implementation:
result = set ( [ ] )
def walk ( s ) :
    if is_variable ( s ) :
        result . add ( s )
    else :
        for arg in s . args :
            walk ( arg )
walk ( s )
return result


signature:
def is_definite_clause ( s ) :
    """returns True for exprs s of the form A & B & . . . & C = = > D ,
 where all literals are positive . In clause form , this is
 ~ A | ~ B | . . . | ~ C | D , where exactly one clause is positive .
 > > > is _ definite _ clause ( expr ( ' Farmer ( Mac ) ' ) )
 True
 > > > is _ definite _ clause ( expr ( ' ~ Farmer ( Mac ) ' ) )
 False
 > > > is _ definite _ clause ( expr ( ' ( Farmer ( f ) & Rabbit ( r ) ) = = > Hates ( f , r ) ' ) )
 True
 > > > is _ definite _ clause ( expr ( ' ( Farmer ( f ) & ~ Rabbit ( r ) ) = = > Hates ( f , r ) ' ) )
 False
 > > > is _ definite _ clause ( expr ( ' ( Farmer ( f ) | Rabbit ( r ) ) = = > Hates ( f , r ) ' ) )
 False"""

implementation:
if is_symbol ( s . op ) :
    return True
elif s . op == '>>' :
    antecedent , consequent = s . args
    return ( is_symbol ( consequent . op ) and every ( lambda arg : is_symbol ( arg . op ) , conjuncts ( antecedent ) ) )
else :
    return False


signature:
def parse_definite_clause ( s ) :
    "Return the antecedents and the consequent of a definite clause ."

implementation:
assert is_definite_clause ( s )
if is_symbol ( s . op ) :
    return [ ] , s
else :
    antecedent , consequent = s . args
    return conjuncts ( antecedent ) , consequent


signature:
def tt_entails ( kb , alpha ) :
    """Does kb entail the sentence alpha ? Use truth tables . For propositional
 kb ' s and sentences . [ Fig . 7.10]
 > > > tt _ entails ( expr ( ' P & Q ' ) , expr ( ' Q ' ) )
 True"""

implementation:
assert not variables ( alpha )
return tt_check_all ( kb , alpha , prop_symbols ( kb & alpha ) , { } )


signature:
def tt_check_all ( kb , alpha , symbols , model ) :
    "Auxiliary routine to implement tt _ entails ."

implementation:
if not symbols :
    if pl_true ( kb , model ) :
        result = pl_true ( alpha , model )
        assert result in ( True , False )
        return result
    else :
        return True
else :
    P , rest = symbols [ 0 ] , symbols [ 1 : ]
    return ( tt_check_all ( kb , alpha , rest , extend ( model , P , True ) ) and tt_check_all ( kb , alpha , rest , extend ( model , P , False ) ) )


signature:
def prop_symbols ( x ) :
    "Return a list of all propositional symbols in x ."

implementation:
if not isinstance ( x , Expr ) :
    return [ ]
elif is_prop_symbol ( x . op ) :
    return [ x ]
else :
    return list ( set ( symbol for arg in x . args for symbol in prop_symbols ( arg ) ) )


signature:
def pl_true ( exp , model = { } ) :
    """Return True if the propositional logic expression is true in the model ,
 and False if it is false . If the model does not specify the value for
 every proposition , this may return None to indicate ' not obvious ' ;
 this may happen even when the expression is tautological ."""

implementation:
op , args = exp . op , exp . args
if exp == TRUE :
    return True
elif exp == FALSE :
    return False
elif is_prop_symbol ( op ) :
    return model . get ( exp )
elif op == '~' :
    p = pl_true ( args [ 0 ] , model )
    if p is None :
        return None
    else :
        return not p
elif op == '|' :
    result = False
    for arg in args :
        p = pl_true ( arg , model )
        if p is True :
            return True
        if p is None :
            result = None
    return result
elif op == '&' :
    result = True
    for arg in args :
        p = pl_true ( arg , model )
        if p is False :
            return False
        if p is None :
            result = None
    return result
p , q = args
if op == '>>' :
    return pl_true ( ~ p | q , model )
elif op == '<<' :
    return pl_true ( p | ~ q , model )
pt = pl_true ( p , model )
if pt is None :
    return None
qt = pl_true ( q , model )
if qt is None :
    return None
if op == '<=>' :
    return pt == qt
elif op == '^' :
    return pt != qt
else :
    raise ValueError , "illegal operator in logic expression" + str ( exp )


implementation:
if isinstance ( s , str ) :
    s = expr ( s )
s = eliminate_implications ( s )
# Steps 1 , 2 from p . 253
s = move_not_inwards ( s )
# Step 3
return distribute_and_over_or ( s )


signature:
def eliminate_implications ( s ) :
    """Change > > , < < , and < = > into & , | , and ~ . That is , return an Expr
 that is equivalent to s , but has only & , | , and ~ as logical operators .
 > > > eliminate _ implications ( A > > ( ~ B < < C ) )
 ( ( ~ B | ~ C ) | ~ A )
 > > > eliminate _ implications ( A ^ B )
 ( ( A & ~ B ) | ( ~ A & B ) )"""

implementation:
if not s . args or is_symbol ( s . op ) :
    return s
    # # ( Atoms are unchanged . )
args = map ( eliminate_implications , s . args )
a , b = args [ 0 ] , args [ - 1 ]
if s . op == '>>' :
    return ( b | ~ a )
elif s . op == '<<' :
    return ( a | ~ b )
elif s . op == '<=>' :
    return ( a | ~ b ) & ( b | ~ a )
elif s . op == '^' :
    assert len ( args ) == 2
    # # TODO : relax this restriction
    return ( a & ~ b ) | ( ~ a & b )
else :
    assert s . op in ( '&' , '|' , '~' )
    return Expr ( s . op , * args )


signature:
def move_not_inwards ( s ) :
    """Rewrite sentence s by moving negation sign inward .
 > > > move _ not _ inwards ( ~ ( A | B ) )
 ( ~ A & ~ B )
 > > > move _ not _ inwards ( ~ ( A & B ) )
 ( ~ A | ~ B )
 > > > move _ not _ inwards ( ~ ( ~ ( A | ~ B ) | ~ ~ C ) )
 ( ( A | ~ B ) & ~ C )"""

implementation:
if s . op == '~' :
    NOT = lambda b : move_not_inwards ( ~ b )
    a = s . args [ 0 ]
    if a . op == '~' :
        return move_not_inwards ( a . args [ 0 ] )
        # ~ ~ A = = > A
    if a . op == '&' :
        return associate ( '|' , map ( NOT , a . args ) )
    if a . op == '|' :
        return associate ( '&' , map ( NOT , a . args ) )
    return s
elif is_symbol ( s . op ) or not s . args :
    return s
else :
    return Expr ( s . op , * map ( move_not_inwards , s . args ) )


signature:
def distribute_and_over_or ( s ) :
    """Given a sentence s consisting of conjunctions and disjunctions
 of literals , return an equivalent sentence in CNF .
 > > > distribute _ and _ over _ or ( ( A & B ) | C )
 ( ( A | C ) & ( B | C ) )"""

implementation:
if s . op == '|' :
    s = associate ( '|' , s . args )
    if s . op != '|' :
        return distribute_and_over_or ( s )
    if len ( s . args ) == 0 :
        return FALSE
    if len ( s . args ) == 1 :
        return distribute_and_over_or ( s . args [ 0 ] )
    conj = find_if ( ( lambda d : d . op == '&' ) , s . args )
    if not conj :
        return s
    others = [ a for a in s . args if a is not conj ]
    rest = associate ( '|' , others )
    return associate ( '&' , [ distribute_and_over_or ( c | rest ) for c in conj . args ] )
elif s . op == '&' :
    return associate ( '&' , map ( distribute_and_over_or , s . args ) )
else :
    return s


signature:
def associate ( op , args ) :
    """Given an associative op , return an expression with the same
 meaning as Expr ( op , * args ) , but flattened - - that is , with nested
 instances of the same op promoted to the top level .
 > > > associate ( ' & ' , [ ( A & B ) , ( B | C ) , ( B & C ) ] )
 ( A & B & ( B | C ) & B & C )
 > > > associate ( ' | ' , [ A | ( B | ( C | ( A & B ) ) ) ] )
 ( A | B | C | ( A & B ) )"""

implementation:
args = dissociate ( op , args )
if len ( args ) == 0 :
    return _op_identity [ op ]
elif len ( args ) == 1 :
    return args [ 0 ]
else :
    return Expr ( op , * args )


signature:
def dissociate ( op , args ) :
    """Given an associative op , return a flattened list result such
 that Expr ( op , * result ) means the same as Expr ( op , * args ) ."""

implementation:
result = [ ]
def collect ( subargs ) :
    for arg in subargs :
        if arg . op == op :
            collect ( arg . args )
        else :
            result . append ( arg )
collect ( args )
return result


signature:
def pl_resolution ( KB , alpha ) :
    "Propositional - logic resolution : say if alpha follows from KB . [ Fig . 7.12]"

implementation:
clauses = KB . clauses + conjuncts ( to_cnf ( ~ alpha ) )
new = set ( )
while True :
    n = len ( clauses )
    pairs = [ ( clauses [ i ] , clauses [ j ] ) for i in range ( n ) for j in range ( i + 1 , n ) ]
    for ( ci , cj ) in pairs :
        resolvents = pl_resolve ( ci , cj )
        if FALSE in resolvents :
            return True
        new = new . union ( set ( resolvents ) )
    if new . issubset ( set ( clauses ) ) :
        return False
    for c in new :
        if c not in clauses :
            clauses . append ( c )


signature:
def pl_resolve ( ci , cj ) :
    """Return all clauses that can be obtained by resolving clauses ci and cj .
 > > > for res in pl _ resolve ( to _ cnf ( A | B | C ) , to _ cnf ( ~ B | ~ C | F ) ) :
 . . . ppset ( disjuncts ( res ) )
 set ( [ A , C , F , ~ C ] )
 set ( [ A , B , F , ~ B ] )"""

implementation:
clauses = [ ]
for di in disjuncts ( ci ) :
    for dj in disjuncts ( cj ) :
        if di == ~ dj or ~ di == dj :
            dnew = unique ( removeall ( di , disjuncts ( ci ) ) + removeall ( dj , disjuncts ( cj ) ) )
            clauses . append ( associate ( '|' , dnew ) )
return clauses


signature:
def pl_fc_entails ( KB , q ) :
    """Use forward chaining to see if a PropDefiniteKB entails symbol q .
 [ Fig . 7.15]
 > > > pl _ fc _ entails ( Fig [ 7,15 ] , expr ( ' Q ' ) )
 True"""

implementation:
count = dict ( [ ( c , len ( conjuncts ( c . args [ 0 ] ) ) ) for c in KB . clauses if c . op == '>>' ] )
inferred = DefaultDict ( False )
agenda = [ s for s in KB . clauses if is_prop_symbol ( s . op ) ]
while agenda :
    p = agenda . pop ( )
    if p == q :
        return True
    if not inferred [ p ] :
        inferred [ p ] = True
        for c in KB . clauses_with_premise ( p ) :
            count [ c ] -= 1
            if count [ c ] == 0 :
                agenda . append ( c . args [ 1 ] )
return False


signature:
def dpll_satisfiable ( s ) :
    """Check satisfiability of a propositional sentence .
 This differs from the book code in two ways : ( 1 ) it returns a model
 rather than True when it succeeds ; this is more useful . ( 2 ) The
 function find _ pure _ symbol is passed a list of unknown clauses , rather
 than a list of all clauses and the model ; this is more efficient .
 > > > ppsubst ( dpll _ satisfiable ( A & ~ B ) )
 { A : True , B : False }
 > > > dpll _ satisfiable ( P & ~ P )
 False"""

implementation:
clauses = conjuncts ( to_cnf ( s ) )
symbols = prop_symbols ( s )
return dpll ( clauses , symbols , { } )


signature:
def dpll ( clauses , symbols , model ) :
    "See if the clauses are true in a partial model ."

implementation:
unknown_clauses = [ ]
# # clauses with an unknown truth value
for c in clauses :
    val = pl_true ( c , model )
    if val == False :
        return False
    if val != True :
        unknown_clauses . append ( c )
if not unknown_clauses :
    return model
P , value = find_pure_symbol ( symbols , unknown_clauses )
if P :
    return dpll ( clauses , removeall ( P , symbols ) , extend ( model , P , value ) )
P , value = find_unit_clause ( clauses , model )
if P :
    return dpll ( clauses , removeall ( P , symbols ) , extend ( model , P , value ) )
P , symbols = symbols [ 0 ] , symbols [ 1 : ]
return ( dpll ( clauses , symbols , extend ( model , P , True ) ) or dpll ( clauses , symbols , extend ( model , P , False ) ) )


signature:
def find_pure_symbol ( symbols , clauses ) :
    """Find a symbol and its value if it appears only as a positive literal
 ( or only as a negative ) in clauses .
 > > > find _ pure _ symbol ( [ A , B , C ] , [ A | ~ B , ~ B | ~ C , C | A ] )
 ( A , True )"""

implementation:
for s in symbols :
    found_pos , found_neg = False , False
    for c in clauses :
        if not found_pos and s in disjuncts ( c ) :
            found_pos = True
        if not found_neg and ~ s in disjuncts ( c ) :
            found_neg = True
    if found_pos != found_neg :
        return s , found_pos
return None , None


signature:
def find_unit_clause ( clauses , model ) :
    """Find a forced assignment if possible from a clause with only 1
 variable not bound in the model .
 > > > find _ unit _ clause ( [ A | B | C , B | ~ C , ~ A | ~ B ] , { A : True } )
 ( B , False )"""

implementation:
for clause in clauses :
    P , value = unit_clause_assign ( clause , model )
    if P :
        return P , value
return None , None


signature:
def unit_clause_assign ( clause , model ) :
    """Return a single variable / value pair that makes clause true in
 the model , if possible .
 > > > unit _ clause _ assign ( A | B | C , { A : True } )
 ( None , None )
 > > > unit _ clause _ assign ( B | ~ C , { A : True } )
 ( None , None )
 > > > unit _ clause _ assign ( ~ A | ~ B , { A : True } )
 ( B , False )"""

implementation:
P , value = None , None
for literal in disjuncts ( clause ) :
    sym , positive = inspect_literal ( literal )
    if sym in model :
        if model [ sym ] == positive :
            return None , None
            # clause already True
    elif P :
        return None , None
        # more than 1 unbound variable
    else :
        P , value = sym , positive
return P , value


signature:
def SAT_plan ( init , transition , goal , t_max , SAT_solver = dpll_satisfiable ) :
    "[ Fig . 7.22]"

implementation:
for t in range ( t_max ) :
    cnf = translate_to_SAT ( init , transition , goal , t )
    model = SAT_solver ( cnf )
    if model is not False :
        return extract_solution ( model )
return None


signature:
def unify ( x , y , s ) :
    """Unify expressions x , y with substitution s ; return a substitution that
 would make x , y equal , or None if x , y can not unify . x and y can be
 variables ( e . g . Expr ( ' x ' ) ) , constants , lists , or Exprs . [ Fig . 9.1]
 > > > ppsubst ( unify ( x + y , y + C , { } ) )
 { x : y , y : C }"""

implementation:
if s is None :
    return None
elif x == y :
    return s
elif is_variable ( x ) :
    return unify_var ( x , y , s )
elif is_variable ( y ) :
    return unify_var ( y , x , s )
elif isinstance ( x , Expr ) and isinstance ( y , Expr ) :
    return unify ( x . args , y . args , unify ( x . op , y . op , s ) )
elif isinstance ( x , str ) or isinstance ( y , str ) :
    return None
elif issequence ( x ) and issequence ( y ) and len ( x ) == len ( y ) :
    if not x :
        return s
    return unify ( x [ 1 : ] , y [ 1 : ] , unify ( x [ 0 ] , y [ 0 ] , s ) )
else :
    return None


signature:
def is_variable ( x ) :
    "A variable is an Expr with no args and a lowercase symbol as the op ."

implementation: return isinstance ( x , Expr ) and not x . args and is_var_symbol ( x . op )


signature:
def occur_check ( var , x , s ) :
    """Return true if variable var occurs anywhere in x
 ( or in subst ( s , x ) , if s has a binding for x ) ."""

implementation:
if var == x :
    return True
elif is_variable ( x ) and x in s :
    return occur_check ( var , s [ x ] , s )
elif isinstance ( x , Expr ) :
    return ( occur_check ( var , x . op , s ) or occur_check ( var , x . args , s ) )
elif isinstance ( x , ( list , tuple ) ) :
    return some ( lambda element : occur_check ( var , element , s ) , x )
else :
    return False


signature:
def extend ( s , var , val ) :
    """Copy the substitution s and extend it by setting var to val ;
 return copy .
 > > > ppsubst ( extend ( { x : 1 } , y , 2 ) )
 { x : 1 , y : 2}"""

implementation:
s2 = s . copy ( )
s2 [ var ] = val
return s2


signature:
def subst ( s , x ) :
    """Substitute the substitution s into the expression x .
 > > > subst ( { x : 42 , y : 0 } , F ( x ) + y )
 ( F ( 42 ) + 0)"""

implementation:
if isinstance ( x , list ) :
    return [ subst ( s , xi ) for xi in x ]
elif isinstance ( x , tuple ) :
    return tuple ( [ subst ( s , xi ) for xi in x ] )
elif not isinstance ( x , Expr ) :
    return x
elif is_var_symbol ( x . op ) :
    return s . get ( x , x )
else :
    return Expr ( x . op , * [ subst ( s , arg ) for arg in x . args ] )


signature:
def fol_fc_ask ( KB , alpha ) :
    """Inefficient forward chaining for first - order logic . [ Fig . 9.3]
 KB is a FolKB and alpha must be an atomic sentence ."""

implementation:
while True :
    new = { }
    for r in KB . clauses :
        ps , q = parse_definite_clause ( standardize_variables ( r ) )
        raise NotImplementedError


signature:
def standardize_variables ( sentence , dic = None ) :
    """Replace all the variables in sentence with new variables .
 > > > e = expr ( ' F ( a , b , c ) & G ( c , A , 23 ) ' )
 > > > len ( variables ( standardize _ variables ( e ) ) )
 > > > variables ( e ) . intersection ( variables ( standardize _ variables ( e ) ) )
 set ( [ ] )
 > > > is _ variable ( standardize _ variables ( expr ( ' x ' ) ) )
 True"""

implementation:
if dic is None :
    dic = { }
if not isinstance ( sentence , Expr ) :
    return sentence
elif is_var_symbol ( sentence . op ) :
    if sentence in dic :
        return dic [ sentence ]
    else :
        v = Expr ( 'v_%d' % standardize_variables . counter . next ( ) )
        dic [ sentence ] = v
        return v
else :
    return Expr ( sentence . op , * [ standardize_variables ( a , dic ) for a in sentence . args ] )


signature:
def diff ( y , x ) :
    """Return the symbolic derivative , dy / dx , as an Expr .
 However , you probably want to simplify the results with simp .
 > > > diff ( x * x , x )
 ( ( x * 1 ) + ( x * 1 ) )
 > > > simp ( diff ( x * x , x ) )
 (2 * x )"""

implementation:
if y == x :
    return ONE
elif not y . args :
    return ZERO
else :
    u , op , v = y . args [ 0 ] , y . op , y . args [ - 1 ]
    if op == '+' :
        return diff ( u , x ) + diff ( v , x )
    elif op == '-' and len ( args ) == 1 :
        return - diff ( u , x )
    elif op == '-' :
        return diff ( u , x ) - diff ( v , x )
    elif op == '*' :
        return u * diff ( v , x ) + v * diff ( u , x )
    elif op == '/' :
        return ( v * diff ( u , x ) - u * diff ( v , x ) ) / ( v * v )
    elif op == '**' and isnumber ( x . op ) :
        return ( v * u ** ( v - 1 ) * diff ( u , x ) )
    elif op == '**' :
        return ( v * u ** ( v - 1 ) * diff ( u , x ) + u ** v * Expr ( 'log' ) ( u ) * diff ( v , x ) )
    elif op == 'log' :
        return diff ( u , x ) / u
    else :
        raise ValueError ( "Unknown op: %s in diff(%s, %s)" % ( op , y , x ) )


signature:
def pretty_dict ( d ) :
    """Return dictionary d ' s repr but with the items sorted .
 > > > pretty _ dict ( { ' m ' : ' M ' , ' a ' : ' A ' , ' r ' : ' R ' , ' k ' : ' K ' } )
 " { ' a ' : ' A ' , ' k ' : ' K ' , ' m ' : ' M ' , ' r ' : ' R ' } "
 > > > pretty _ dict ( { z : C , y : B , x : A } )
 ' { x : A , y : B , z : C } '"""

implementation: return '{%s}' % ', ' . join ( '%r: %r' % ( k , v ) for k , v in sorted ( d . items ( ) , key = repr ) )


signature:
def retract ( self , sentence ) :
    "Remove the sentence ' s clauses from the KB ."

implementation:
for c in conjuncts ( to_cnf ( sentence ) ) :
    if c in self . clauses :
        self . clauses . remove ( c )


signature:
def clauses_with_premise ( self , p ) :
    """Return a list of the clauses in KB that have p in their premise .
 This could be cached away for O ( 1 ) speed , but we ' ll recompute it ."""

implementation: return [ c for c in self . clauses if c . op == '>>' and p in conjuncts ( c . args [ 0 ] ) ]


signature:
def refresh ( self ) :
    """Updates the cache with setting values from the database ."""

implementation:
# ` values _ list ( ' name ' , ' value ' ) ` doesn ' t work because ` value ` is not a
# setting ( base class ) field , it ' s a setting value ( subclass ) field . So
# we have to get real instances .
args = [ ( obj . name , obj . value ) for obj in self . queryset . all ( ) ]
super ( SettingDict , self ) . update ( args )
self . empty_cache = False


signature:
def minimax_decision ( state , game ) :
    """Given a state in a game , calculate the best move by searching
 forward all the way to the terminal states . [ Fig . 5.3]"""

implementation:
player = game . to_move ( state )
def max_value ( state ) :
    if game . terminal_test ( state ) :
        return game . utility ( state , player )
    v = - infinity
    for a in game . actions ( state ) :
        v = max ( v , min_value ( game . result ( state , a ) ) )
    return v
def min_value ( state ) :
    if game . terminal_test ( state ) :
        return game . utility ( state , player )
    v = infinity
    for a in game . actions ( state ) :
        v = min ( v , max_value ( game . result ( state , a ) ) )
    return v
# Body of minimax _ decision :
return argmax ( game . actions ( state ) , lambda a : min_value ( game . result ( state , a ) ) )


signature:
def alphabeta_search ( state , game , d = 4 , cutoff_test = None , eval_fn = None ) :
    """Search game to determine best action ; use alpha - beta pruning .
 This version cuts off search and uses an evaluation function ."""

implementation:
player = game . to_move ( state )
def max_value ( state , alpha , beta , depth ) :
    if cutoff_test ( state , depth ) :
        return eval_fn ( state )
    v = - infinity
    for a in game . actions ( state ) :
        v = max ( v , min_value ( game . result ( state , a ) , alpha , beta , depth + 1 ) )
        if v >= beta :
            return v
        alpha = max ( alpha , v )
    return v
def min_value ( state , alpha , beta , depth ) :
    if cutoff_test ( state , depth ) :
        return eval_fn ( state )
    v = infinity
    for a in game . actions ( state ) :
        v = min ( v , max_value ( game . result ( state , a ) , alpha , beta , depth + 1 ) )
        if v <= alpha :
            return v
        beta = min ( beta , v )
    return v
# Body of alphabeta _ search starts here :
# The default test cuts off at depth d or at a terminal state
cutoff_test = ( cutoff_test or ( lambda state , depth : depth > d or game . terminal_test ( state ) ) )
eval_fn = eval_fn or ( lambda state : game . utility ( state , player ) )
return argmax ( game . actions ( state ) , lambda a : min_value ( game . result ( state , a ) , - infinity , infinity , 0 ) )


signature:
def play_game ( game , * players ) :
    """Play an n - person , move - alternating game .
 > > > play _ game ( Fig52Game ( ) , alphabeta _ player , alphabeta _ player )"""

implementation:
state = game . initial
while True :
    for player in players :
        move = player ( game , state )
        state = game . result ( state , move )
        if game . terminal_test ( state ) :
            return game . utility ( state , game . to_move ( game . initial ) )


signature:
def utility ( self , state , player ) :
    "Return the value to player ; 1 for win , - 1 for loss , 0 otherwise ."

implementation: return if_ ( player == 'X' , state . utility , - state . utility )


signature:
def compute_utility ( self , board , move , player ) :
    "If X wins with this move , return 1 ; if O return - 1 ; else return 0."

implementation:
if ( self . k_in_row ( board , move , player , ( 0 , 1 ) ) or self . k_in_row ( board , move , player , ( 1 , 0 ) ) or self . k_in_row ( board , move , player , ( 1 , - 1 ) ) or self . k_in_row ( board , move , player , ( 1 , 1 ) ) ) :
    return if_ ( player == 'X' , + 1 , - 1 )
else :
    return 0


signature:
def k_in_row ( self , board , move , player , ( delta_x , delta_y ) ) :
    "Return true if there is a line through move on board for player ."

implementation:
x , y = move
n = 0
# n is number of moves in row
while board . get ( ( x , y ) ) == player :
    n += 1
    x , y = x + delta_x , y + delta_y
x , y = move
while board . get ( ( x , y ) ) == player :
    n += 1
    x , y = x - delta_x , y - delta_y
n -= 1
# Because we counted move itself twice
return n >= self . k


signature:
def update ( x , ** entries ) :
    """Update a dict , or an object with slots , according to ` entries ` dict .
 > > > update ( { ' a ' : 1 } , a = 10 , b = 20)
 { ' a ' : 10 , ' b ' : 20}
 > > > update ( Struct ( a = 1 ) , a = 10 , b = 20)
 Struct ( a = 10 , b = 20)"""

implementation:
if isinstance ( x , dict ) :
    x . update ( entries )
else :
    x . __dict__ . update ( entries )
return x


signature:
def removeall ( item , seq ) :
    """Return a copy of seq ( or string ) with all occurences of item removed .
 > > > removeall ( 3 , [ 1 , 2 , 3 , 3 , 2 , 1 , 3 ] )
 [1 , 2 , 2 , 1]
 > > > removeall ( 4 , [ 1 , 2 , 3 ] )
 [1 , 2 , 3]"""

implementation:
if isinstance ( seq , str ) :
    return seq . replace ( item , '' )
else :
    return [ x for x in seq if x != item ]


signature:
def count_if ( predicate , seq ) :
    """Count the number of elements of seq for which the predicate is true .
 > > > count _ if ( callable , [ 42 , None , max , min ] )"""

implementation:
f = lambda count , x : count + ( not not predicate ( x ) )
return reduce ( f , seq , 0 )


signature:
def some ( predicate , seq ) :
    """If some element x of seq satisfies predicate ( x ) , return predicate ( x ) .
 > > > some ( callable , [ min , 3 ] )
 > > > some ( callable , [ 2 , 3 ] )"""

implementation:
for x in seq :
    px = predicate ( x )
    if px :
        return px
return False


signature:
def argmin ( seq , fn ) :
    """Return an element with lowest fn ( seq [ i ] ) score ; tie goes to first one .
 > > > argmin ( [ ' one ' , ' to ' , ' three ' ] , len )
 ' to '"""

implementation:
best = seq [ 0 ] ;
best_score = fn ( best )
for x in seq :
    x_score = fn ( x )
    if x_score < best_score :
        best , best_score = x , x_score
return best


signature:
def argmin_list ( seq , fn ) :
    """Return a list of elements of seq [ i ] with the lowest fn ( seq [ i ] ) scores .
 > > > argmin _ list ( [ ' one ' , ' to ' , ' three ' , ' or ' ] , len )
 [ ' to ' , ' or ' ]"""

implementation:
best_score , best = fn ( seq [ 0 ] ) , [ ]
for x in seq :
    x_score = fn ( x )
    if x_score < best_score :
        best , best_score = [ x ] , x_score
    elif x_score == best_score :
        best . append ( x )
return best


signature:
def argmin_random_tie ( seq , fn ) :
    """Return an element with lowest fn ( seq [ i ] ) score ; break ties at random .
 Thus , for all s , f : argmin _ random _ tie ( s , f ) in argmin _ list ( s , f )"""

implementation:
best_score = fn ( seq [ 0 ] ) ;
n = 0
for x in seq :
    x_score = fn ( x )
    if x_score < best_score :
        best , best_score = x , x_score ;
        n = 1
    elif x_score == best_score :
        n += 1
        if random . randrange ( n ) == 0 :
            best = x
return best


signature:
def histogram ( values , mode = 0 , bin_function = None ) :
    """Return a list of ( value , count ) pairs , summarizing the input values .
 Sorted by increasing value , or if mode = 1 , by decreasing count .
 If bin _ function is given , map it over values first ."""

implementation:
if bin_function :
    values = map ( bin_function , values )
bins = { }
for val in values :
    bins [ val ] = bins . get ( val , 0 ) + 1
if mode :
    return sorted ( bins . items ( ) , key = lambda x : ( x [ 1 ] , x [ 0 ] ) , reverse = True )
else :
    return sorted ( bins . items ( ) )


signature:
def median ( values ) :
    """Return the middle value , when the values are sorted .
 If there are an odd number of elements , try to average the middle two .
 If they can ' t be averaged ( e . g . they are strings ) , choose one at random .
 > > > median ( [ 10 , 100 , 11 ] )
 11
 > > > median ( [ 1 , 2 , 3 , 4 ] )
 2.5"""

implementation:
n = len ( values )
values = sorted ( values )
if n % 2 == 1 :
    return values [ n / 2 ]
else :
    middle2 = values [ ( n / 2 ) - 1 : ( n / 2 ) + 1 ]
    try :
        return mean ( middle2 )
    except TypeError :
        return random . choice ( middle2 )


signature:
def dotproduct ( X , Y ) :
    """Return the sum of the element - wise product of vectors x and y .
 > > > dotproduct ( [ 1 , 2 , 3 ] , [ 1000 , 100 , 10 ] )
 1230"""

implementation: return sum ( [ x * y for x , y in zip ( X , Y ) ] )


signature:
def weighted_sample_with_replacement ( seq , weights , n ) :
    """Pick n samples from seq at random , with replacement , with the
 probability of each element in proportion to its corresponding
 weight ."""

implementation:
sample = weighted_sampler ( seq , weights )
return [ sample ( ) for s in range ( n ) ]


signature:
def weighted_sampler ( seq , weights ) :
    "Return a random - sample function that picks from seq weighted by weights ."

implementation:
totals = [ ]
for w in weights :
    totals . append ( w + totals [ - 1 ] if totals else w )
return lambda : seq [ bisect . bisect ( totals , random . uniform ( 0 , totals [ - 1 ] ) ) ]


signature:
def num_or_str ( x ) :
    """The argument is a string ; convert to a number if possible , or strip it .
 > > > num _ or _ str ( ' 42 ' )
 42
 > > > num _ or _ str ( ' 42x ' )
 '42x '"""

implementation:
if isnumber ( x ) :
    return x
try :
    return int ( x )
except ValueError :
    try :
        return float ( x )
    except ValueError :
        return str ( x ) . strip ( )


signature:
def normalize ( numbers ) :
    """Multiply each number by a constant such that the sum is 1.0
 > > > normalize ( [ 1,2,1 ] )
 [0.25 , 0.5 , 0.25]"""

implementation:
total = float ( sum ( numbers ) )
return [ n / total for n in numbers ]


signature:
def distance ( ( ax , ay ) , ( bx , by ) ) :
    "The distance between two ( x , y ) points ."

implementation: return math . hypot ( ( ax - bx ) , ( ay - by ) )


signature:
def vector_clip ( vector , lowest , highest ) :
    """Return vector , except if any element is less than the corresponding
 value of lowest or more than the corresponding value of highest , clip to
 those values .
 > > > vector _ clip ( ( - 1 , 10 ) , ( 0 , 0 ) , ( 9 , 9 ) )
 (0 , 9)"""

implementation: return type ( vector ) ( map ( clip , vector , lowest , highest ) )


signature:
def printf ( format , * args ) :
    """Format args with the first argument as format string , and write .
 Return the last arg , or format itself if there are no args ."""

implementation:
sys . stdout . write ( str ( format ) % args )
return if_ ( args , lambda : args [ - 1 ] , lambda : format )


signature:
def memoize ( fn , slot = None ) :
    """Memoize fn : make it remember the computed value for any argument list .
 If slot is specified , store result in that slot of first argument .
 If slot is false , store results in a dictionary ."""

implementation:
if slot :
    def memoized_fn ( obj , * args ) :
        if hasattr ( obj , slot ) :
            return getattr ( obj , slot )
        else :
            val = fn ( obj , * args )
            setattr ( obj , slot , val )
            return val
else :
    def memoized_fn ( * args ) :
        if not memoized_fn . cache . has_key ( args ) :
            memoized_fn . cache [ args ] = fn ( * args )
        return memoized_fn . cache [ args ]
    memoized_fn . cache = { }
return memoized_fn


signature:
def if_ ( test , result , alternative ) :
    """Like C + + and Java ' s ( test ? result : alternative ) , except
 both result and alternative are always evaluated . However , if
 either evaluates to a function , it is applied to the empty arglist ,
 so you can delay execution by putting it in a lambda .
 > > > if _ ( 2 + 2 = = 4 , ' ok ' , lambda : expensive _ computation ( ) )
 ' ok '"""

implementation:
if test :
    if callable ( result ) :
        return result ( )
    return result
else :
    if callable ( alternative ) :
        return alternative ( )
    return alternative


signature:
def name ( object ) :
    "Try to find some reasonable name for the object ."

implementation: return ( getattr ( object , 'name' , 0 ) or getattr ( object , '__name__' , 0 ) or getattr ( getattr ( object , '__class__' , 0 ) , '__name__' , 0 ) or str ( object ) )


signature:
def print_table ( table , header = None , sep = '   ' , numfmt = '%g' ) :
    """Print a list of lists as a table , so that columns line up nicely .
 header , if specified , will be printed as the first row .
 numfmt is the format for all numbers ; you might want e . g . ' % 6.2f ' .
 ( If you want different formats in different columns , don ' t use print _ table . )
 sep is the separator between columns ."""

implementation:
justs = [ if_ ( isnumber ( x ) , 'rjust' , 'ljust' ) for x in table [ 0 ] ]
if header :
    table = [ header ] + table
table = [ [ if_ ( isnumber ( x ) , lambda : numfmt % x , lambda : x ) for x in row ] for row in table ]
maxlen = lambda seq : max ( map ( len , seq ) )
sizes = map ( maxlen , zip ( * [ map ( str , row ) for row in table ] ) )
for row in table :
    print sep . join ( getattr ( str ( x ) , j ) ( size ) for ( j , size , x ) in zip ( justs , sizes , row ) )


signature:
def AIMAFile ( components , mode = 'r' ) :
    "Open a file based at the AIMA root directory ."

implementation:
import utils
dir = os . path . dirname ( utils . __file__ )
return open ( apply ( os . path . join , [ dir ] + components ) , mode )


signature:
def parse_csv ( input , delim = ',' ) :
    r"""Input is a string consisting of lines , each line has comma - delimited
 fields . Convert this into a list of lists . Blank lines are skipped .
 Fields that look like numbers are converted to numbers .
 The delim defaults to ' , ' but ' \ t ' and None are also reasonable values .
 > > > parse _ csv ( ' 1 , 2 , 3 \ n 0 , 2 , na ' )
 [ [ 1 , 2 , 3 ] , [ 0 , 2 , ' na ' ] ]"""

implementation:
lines = [ line for line in input . splitlines ( ) if line . strip ( ) ]
return [ map ( num_or_str , line . split ( delim ) ) for line in lines ]


signature:
def information_content ( values ) :
    "Number of bits to represent the probability distribution in values ."

implementation:
probabilities = normalize ( removeall ( 0 , values ) )
return sum ( - p * log2 ( p ) for p in probabilities )


signature:
def NeuralNetLearner ( dataset , sizes ) :
    """Layered feed - forward network ."""

implementation:
activations = map ( lambda n : [ 0.0 for i in range ( n ) ] , sizes )
weights = [ ]
def predict ( example ) :
    unimplemented ( )
return predict


signature:
def EnsembleLearner ( learners ) :
    """Given a list of learning algorithms , have them vote ."""

implementation:
def train ( dataset ) :
    predictors = [ learner ( dataset ) for learner in learners ]
    def predict ( example ) :
        return mode ( predictor ( example ) for predictor in predictors )
    return predict
return train


implementation:
def train ( dataset ) :
    examples , target = dataset . examples , dataset . target
    N = len ( examples )
    epsilon = 1. / ( 2 * N )
    w = [ 1. / N ] * N
    h , z = [ ] , [ ]
    for k in range ( K ) :
        h_k = L ( dataset , w )
        h . append ( h_k )
        error = sum ( weight for example , weight in zip ( examples , w ) if example [ target ] != h_k ( example ) )
        # Avoid divide - by - 0 from either 0 % or 100 % error rates :
        error = clip ( error , epsilon , 1 - epsilon )
        for j , example in enumerate ( examples ) :
            if example [ target ] == h_k ( example ) :
                w [ j ] *= error / ( 1. - error )
        w = normalize ( w )
        z . append ( math . log ( ( 1. - error ) / error ) )
    return WeightedMajority ( h , z )
return train


signature:
def WeightedMajority ( predictors , weights ) :
    "Return a predictor that takes a weighted vote ."

implementation:
def predict ( example ) :
    return weighted_mode ( ( predictor ( example ) for predictor in predictors ) , weights )
return predict


signature:
def weighted_mode ( values , weights ) :
    """Return the value with the greatest total weight .
 > > > weighted _ mode ( ' abbaa ' , [ 1,2,3,1,2 ] )"""

implementation:
totals = defaultdict ( int )
for v , w in zip ( values , weights ) :
    totals [ v ] += w
return max ( totals . keys ( ) , key = totals . get )


signature:
def WeightedLearner ( unweighted_learner ) :
    """Given a learner that takes just an unweighted dataset , return
 one that takes also a weight for each example . [ p . 749 footnote 14]"""

implementation:
def train ( dataset , weights ) :
    return unweighted_learner ( replicated_dataset ( dataset , weights ) )
return train


signature:
def replicated_dataset ( dataset , weights , n = None ) :
    "Copy dataset , replicating each example in proportion to its weight ."

implementation:
n = n or len ( dataset . examples )
result = copy . copy ( dataset )
result . examples = weighted_replicate ( dataset . examples , weights , n )
return result


signature:
def weighted_replicate ( seq , weights , n ) :
    """Return n selections from seq , with the count of each element of
 seq proportional to the corresponding weight ( filling in fractions
 randomly ) .
 > > > weighted _ replicate ( ' ABC ' , [ 1,2,1 ] , 4)
 [ ' A ' , ' B ' , ' B ' , ' C ' ]"""

implementation:
assert len ( seq ) == len ( weights )
weights = normalize ( weights )
wholes = [ int ( w * n ) for w in weights ]
fractions = [ ( w * n ) % 1 for w in weights ]
return ( flatten ( [ x ] * nx for x , nx in zip ( seq , wholes ) ) + weighted_sample_with_replacement ( seq , fractions , n - sum ( wholes ) ) )


signature:
def cross_validation ( learner , dataset , k = 10 , trials = 1 ) :
    """Do k - fold cross _ validate and return their mean .
 That is , keep out 1 / k of the examples for testing on each of k runs .
 Shuffle the examples first ; If trials > 1 , average over several shuffles ."""

implementation:
if k is None :
    k = len ( dataset . examples )
if trials > 1 :
    return mean ( [ cross_validation ( learner , dataset , k , trials = 1 ) for t in range ( trials ) ] )
else :
    n = len ( dataset . examples )
    random . shuffle ( dataset . examples )
    return mean ( [ train_and_test ( learner , dataset , i * ( n / k ) , ( i + 1 ) * ( n / k ) ) for i in range ( k ) ] )


signature:
def leave1out ( learner , dataset ) :
    "Leave one out cross - validation over the dataset ."

implementation: return cross_validation ( learner , dataset , k = len ( dataset . examples ) )


signature:
def SyntheticRestaurant ( n = 20 ) :
    "Generate a DataSet with n examples ."

implementation:
def gen ( ) :
    example = map ( random . choice , restaurant . values )
    example [ restaurant . target ] = Fig [ 18 , 2 ] ( example )
    return example
return RestaurantDataSet ( [ gen ( ) for i in range ( n ) ] )


signature:
def Majority ( k , n ) :
    """Return a DataSet with n k - bit examples of the majority problem :
 k random bits followed by a 1 if more than half the bits are 1 , else 0."""

implementation:
examples = [ ]
for i in range ( n ) :
    bits = [ random . choice ( [ 0 , 1 ] ) for i in range ( k ) ]
    bits . append ( int ( sum ( bits ) > k / 2 ) )
    examples . append ( bits )
return DataSet ( name = "majority" , examples = examples )


signature:
def ContinuousXor ( n ) :
    "2 inputs are chosen uniformly from ( 0.0 . . 2.0 ] ; output is xor of ints ."

implementation:
examples = [ ]
for i in range ( n ) :
    x , y = [ random . uniform ( 0.0 , 2.0 ) for i in '12' ]
    examples . append ( [ x , y , int ( x ) != int ( y ) ] )
return DataSet ( name = "continuous xor" , examples = examples )


signature:
def compare ( algorithms = [ PluralityLearner , NaiveBayesLearner , NearestNeighborLearner , DecisionTreeLearner ] , datasets = [ iris , orings , zoo , restaurant , SyntheticRestaurant ( 20 ) , Majority ( 7 , 100 ) , Parity ( 7 , 100 ) , Xor ( 100 ) ] , k = 10 , trials = 1 ) :
    """Compare various learners on various datasets using cross - validation .
 Print results as a table ."""

implementation: print_table ( [ [ a . __name__ . replace ( 'Learner' , '' ) ] + [ cross_validation ( a , d , k , trials ) for d in datasets ] for a in algorithms ] , header = [ '' ] + [ d . name [ 0 : 7 ] for d in datasets ] , numfmt = '%.2f' )


signature:
def setproblem ( self , target , inputs = None , exclude = ( ) ) :
    """Set ( or change ) the target and / or inputs .
 This way , one DataSet can be used multiple ways . inputs , if specified ,
 is a list of attributes , or specify exclude as a list of attributes
 to not use in inputs . Attributes can be - n . . n , or an attrname .
 Also computes the list of possible values , if that wasn ' t done yet ."""

implementation:
self . target = self . attrnum ( target )
exclude = map ( self . attrnum , exclude )
if inputs :
    self . inputs = removeall ( self . target , inputs )
else :
    self . inputs = [ a for a in self . attrs if a != self . target and a not in exclude ]
if not self . values :
    self . values = map ( unique , zip ( * self . examples ) )
self . check_me ( )


signature:
def check_me ( self ) :
    "Check that my fields make sense ."

implementation:
assert len ( self . attrnames ) == len ( self . attrs )
assert self . target in self . attrs
assert self . target not in self . inputs
assert set ( self . inputs ) . issubset ( set ( self . attrs ) )
map ( self . check_example , self . examples )


signature:
def add_example ( self , example ) :
    "Add an example to the list of examples , checking it first ."

implementation:
self . check_example ( example )
self . examples . append ( example )


signature:
def check_example ( self , example ) :
    "Raise ValueError if example has any invalid values ."

implementation:
if self . values :
    for a in self . attrs :
        if example [ a ] not in self . values [ a ] :
            raise ValueError ( 'Bad value %s for attribute %s in %s' % ( example [ a ] , self . attrnames [ a ] , example ) )


signature:
def attrnum ( self , attr ) :
    "Returns the number used for attr , which can be a name , or - n . . n - 1."

implementation:
if attr < 0 :
    return len ( self . attrs ) + attr
elif isinstance ( attr , str ) :
    return self . attrnames . index ( attr )
else :
    return attr


signature:
def sanitize ( self , example ) :
    "Return a copy of example , with non - input attributes replaced by None ."

implementation: return [ attr_i if i in self . inputs else None for i , attr_i in enumerate ( example ) ]


signature:
def add ( self , o ) :
    "Add an observation o to the distribution ."

implementation:
self . smooth_for ( o )
self . dictionary [ o ] += 1
self . n_obs += 1
self . sampler = None


signature:
def smooth_for ( self , o ) :
    """Include o among the possible observations , whether or not
 it ' s been observed yet ."""

implementation:
if o not in self . dictionary :
    self . dictionary [ o ] = self . default
    self . n_obs += self . default
    self . sampler = None


signature:
def top ( self , n ) :
    "Return ( count , obs ) tuples for the n most frequent observations ."

implementation: return heapq . nlargest ( n , [ ( v , k ) for ( k , v ) in self . dictionary . items ( ) ] )


signature:
def sample ( self ) :
    "Return a random sample from the distribution ."

implementation:
if self . sampler is None :
    self . sampler = weighted_sampler ( self . dictionary . keys ( ) , self . dictionary . values ( ) )
return self . sampler ( )


signature:
def AC3 ( csp , queue = None , removals = None ) :
    """[ Fig . 6.3]"""

implementation:
if queue is None :
    queue = [ ( Xi , Xk ) for Xi in csp . vars for Xk in csp . neighbors [ Xi ] ]
csp . support_pruning ( )
while queue :
    ( Xi , Xj ) = queue . pop ( )
    if revise ( csp , Xi , Xj , removals ) :
        if not csp . curr_domains [ Xi ] :
            return False
        for Xk in csp . neighbors [ Xi ] :
            if Xk != Xi :
                queue . append ( ( Xk , Xi ) )
return True


signature:
def revise ( csp , Xi , Xj , removals ) :
    "Return true if we remove a value ."

implementation:
revised = False
for x in csp . curr_domains [ Xi ] [ : ] : # If Xi = x conflicts with Xj = y for every possible y , eliminate Xi = x
    if every ( lambda y : not csp . constraints ( Xi , x , Xj , y ) , csp . curr_domains [ Xj ] ) :
        csp . prune ( Xi , x , removals )
        revised = True
return revised


signature:
def mrv ( assignment , csp ) :
    "Minimum - remaining - values heuristic ."

implementation: return argmin_random_tie ( [ v for v in csp . vars if v not in assignment ] , lambda var : num_legal_values ( csp , var , assignment ) )


signature:
def lcv ( var , assignment , csp ) :
    "Least - constraining - values heuristic ."

implementation: return sorted ( csp . choices ( var ) , key = lambda val : csp . nconflicts ( var , val , assignment ) )


signature:
def forward_checking ( csp , var , value , assignment , removals ) :
    "Prune neighbor values inconsistent with var = value ."

implementation:
for B in csp . neighbors [ var ] :
    if B not in assignment :
        for b in csp . curr_domains [ B ] [ : ] :
            if not csp . constraints ( var , value , B , b ) :
                csp . prune ( B , b , removals )
        if not csp . curr_domains [ B ] :
            return False
return True


signature:
def mac ( csp , var , value , assignment , removals ) :
    "Maintain arc consistency ."

implementation: return AC3 ( csp , [ ( X , var ) for X in csp . neighbors [ var ] ] , removals )


signature:
def backtracking_search ( csp , select_unassigned_variable = first_unassigned_variable , order_domain_values = unordered_domain_values , inference = no_inference ) :
    """[ Fig . 6.5]
 > > > backtracking _ search ( australia ) is not None
 True
 > > > backtracking _ search ( australia , select _ unassigned _ variable = mrv ) is not None
 True
 > > > backtracking _ search ( australia , order _ domain _ values = lcv ) is not None
 True
 > > > backtracking _ search ( australia , select _ unassigned _ variable = mrv , order _ domain _ values = lcv ) is not None
 True
 > > > backtracking _ search ( australia , inference = forward _ checking ) is not None
 True
 > > > backtracking _ search ( australia , inference = mac ) is not None
 True
 > > > backtracking _ search ( usa , select _ unassigned _ variable = mrv , order _ domain _ values = lcv , inference = mac ) is not None
 True"""

implementation:
def backtrack ( assignment ) :
    if len ( assignment ) == len ( csp . vars ) :
        return assignment
    var = select_unassigned_variable ( assignment , csp )
    for value in order_domain_values ( var , assignment , csp ) :
        if 0 == csp . nconflicts ( var , value , assignment ) :
            csp . assign ( var , value , assignment )
            removals = csp . suppose ( var , value )
            if inference ( csp , var , value , assignment , removals ) :
                result = backtrack ( assignment )
                if result is not None :
                    return result
            csp . restore ( removals )
    csp . unassign ( var , assignment )
    return None
result = backtrack ( { } )
assert result is None or csp . goal_test ( result )
return result


signature:
def min_conflicts ( csp , max_steps = 100000 ) :
    """Solve a CSP by stochastic hillclimbing on the number of conflicts ."""

implementation:
# Generate a complete assignment for all vars ( probably with conflicts )
csp . current = current = { }
for var in csp . vars :
    val = min_conflicts_value ( csp , var , current )
    csp . assign ( var , val , current )
# Now repeatedly choose a random conflicted variable and change it
for i in range ( max_steps ) :
    conflicted = csp . conflicted_vars ( current )
    if not conflicted :
        return current
    var = random . choice ( conflicted )
    val = min_conflicts_value ( csp , var , current )
    csp . assign ( var , val , current )
return None


signature:
def min_conflicts_value ( csp , var , current ) :
    """Return the value that will give var the least number of conflicts .
 If there is a tie , choose at random ."""

implementation: return argmin_random_tie ( csp . domains [ var ] , lambda val : csp . nconflicts ( var , val , current ) )


signature:
def tree_csp_solver ( csp ) :
    "[ Fig . 6.11]"

implementation:
n = len ( csp . vars )
assignment = { }
root = csp . vars [ 0 ]
X , parent = topological_sort ( csp . vars , root )
for Xj in reversed ( X ) :
    if not make_arc_consistent ( parent [ Xj ] , Xj , csp ) :
        return None
for Xi in X :
    if not csp . curr_domains [ Xi ] :
        return None
    assignment [ Xi ] = csp . curr_domains [ Xi ] [ 0 ]
return assignment


signature:
def MapColoringCSP ( colors , neighbors ) :
    """Make a CSP for the problem of coloring a map with different colors
 for any two adjacent regions . Arguments are a list of colors , and a
 dict of { region : [ neighbor , . . . ] } entries . This dict may also be
 specified as a string of the form defined by parse _ neighbors ."""

implementation:
if isinstance ( neighbors , str ) :
    neighbors = parse_neighbors ( neighbors )
return CSP ( neighbors . keys ( ) , UniversalDict ( colors ) , neighbors , different_values_constraint )


signature:
def parse_neighbors ( neighbors , vars = [ ] ) :
    """Convert a string of the form ' X : Y Z ; Y : Z ' into a dict mapping
 regions to neighbors . The syntax is a region name followed by a ' : '
 followed by zero or more region names , followed by ' ; ' , repeated for
 each region name . If you say ' X : Y ' you don ' t need ' Y : X ' .
 > > > parse _ neighbors ( ' X : Y Z ; Y : Z ' )
 { ' Y ' : [ ' X ' , ' Z ' ] , ' X ' : [ ' Y ' , ' Z ' ] , ' Z ' : [ ' X ' , ' Y ' ] }"""

implementation:
dict = DefaultDict ( [ ] )
for var in vars :
    dict [ var ] = [ ]
specs = [ spec . split ( ':' ) for spec in neighbors . split ( ';' ) ]
for ( A , Aneighbors ) in specs :
    A = A . strip ( )
    dict . setdefault ( A , [ ] )
    for B in Aneighbors . split ( ) :
        dict [ A ] . append ( B )
        dict [ B ] . append ( A )
return dict


signature:
def queen_constraint ( A , a , B , b ) :
    """Constraint is satisfied ( true ) if A , B are really the same variable ,
 or if they are not in the same row , down diagonal , or up diagonal ."""

implementation: return A == B or ( a != b and A + a != B + b and A - a != B - b )


signature:
def Zebra ( ) :
    "Return an instance of the Zebra Puzzle ."

implementation:
Colors = 'Red Yellow Blue Green Ivory' . split ( )
Pets = 'Dog Fox Snails Horse Zebra' . split ( )
Drinks = 'OJ Tea Coffee Milk Water' . split ( )
Countries = 'Englishman Spaniard Norwegian Ukranian Japanese' . split ( )
Smokes = 'Kools Chesterfields Winston LuckyStrike Parliaments' . split ( )
vars = Colors + Pets + Drinks + Countries + Smokes
domains = { }
for var in vars :
    domains [ var ] = range ( 1 , 6 )
domains [ 'Norwegian' ] = [ 1 ]
domains [ 'Milk' ] = [ 3 ]
neighbors = parse_neighbors ( """Englishman: Red;
                Spaniard: Dog; Kools: Yellow; Chesterfields: Fox;
                Norwegian: Blue; Winston: Snails; LuckyStrike: OJ;
                Ukranian: Tea; Japanese: Parliaments; Kools: Horse;
                Coffee: Green; Green: Ivory""" , vars )
for type in [ Colors , Pets , Drinks , Countries , Smokes ] :
    for A in type :
        for B in type :
            if A != B :
                if B not in neighbors [ A ] :
                    neighbors [ A ] . append ( B )
                if A not in neighbors [ B ] :
                    neighbors [ B ] . append ( A )
def zebra_constraint ( A , a , B , b , recurse = 0 ) :
    same = ( a == b )
    next_to = abs ( a - b ) == 1
    if A == 'Englishman' and B == 'Red' :
        return same
    if A == 'Spaniard' and B == 'Dog' :
        return same
    if A == 'Chesterfields' and B == 'Fox' :
        return next_to
    if A == 'Norwegian' and B == 'Blue' :
        return next_to
    if A == 'Kools' and B == 'Yellow' :
        return same
    if A == 'Winston' and B == 'Snails' :
        return same
    if A == 'LuckyStrike' and B == 'OJ' :
        return same
    if A == 'Ukranian' and B == 'Tea' :
        return same
    if A == 'Japanese' and B == 'Parliaments' :
        return same
    if A == 'Kools' and B == 'Horse' :
        return next_to
    if A == 'Coffee' and B == 'Green' :
        return same
    if A == 'Green' and B == 'Ivory' :
        return ( a - 1 ) == b
    if recurse == 0 :
        return zebra_constraint ( B , b , A , a , 1 )
    if ( ( A in Colors and B in Colors ) or ( A in Pets and B in Pets ) or ( A in Drinks and B in Drinks ) or ( A in Countries and B in Countries ) or ( A in Smokes and B in Smokes ) ) :
        return not same
    raise 'error'
return CSP ( vars , domains , neighbors , zebra_constraint )


signature:
def assign ( self , var , val , assignment ) :
    "Add { var : val } to assignment ; Discard the old value if any ."

implementation:
assignment [ var ] = val
self . nassigns += 1


signature:
def nconflicts ( self , var , val , assignment ) :
    "Return the number of conflicts var = val has with other variables ."

implementation:
# Subclasses may implement this more efficiently
def conflict ( var2 ) :
    return ( var2 in assignment and not self . constraints ( var , val , var2 , assignment [ var2 ] ) )
return count_if ( conflict , self . neighbors [ var ] )


signature:
def actions ( self , state ) :
    """Return a list of applicable actions : nonconflicting
 assignments to an unassigned variable ."""

implementation:
if len ( state ) == len ( self . vars ) :
    return [ ]
else :
    assignment = dict ( state )
    var = find_if ( lambda v : v not in assignment , self . vars )
    return [ ( var , val ) for val in self . domains [ var ] if self . nconflicts ( var , val , assignment ) == 0 ]


signature:
def support_pruning ( self ) :
    """Make sure we can prune values from domains . ( We want to pay
 for this only if we use it . )"""

implementation:
if self . curr_domains is None :
    self . curr_domains = dict ( ( v , list ( self . domains [ v ] ) ) for v in self . vars )


signature:
def suppose ( self , var , value ) :
    "Start accumulating inferences from assuming var = value ."

implementation:
self . support_pruning ( )
removals = [ ( var , a ) for a in self . curr_domains [ var ] if a != value ]
self . curr_domains [ var ] = [ value ]
return removals


signature:
def prune ( self , var , value , removals ) :
    "Rule out var = value ."

implementation:
self . curr_domains [ var ] . remove ( value )
if removals is not None :
    removals . append ( ( var , value ) )


signature:
def infer_assignment ( self ) :
    "Return the partial assignment implied by the current inferences ."

implementation:
self . support_pruning ( )
return dict ( ( v , self . curr_domains [ v ] [ 0 ] ) for v in self . vars if 1 == len ( self . curr_domains [ v ] ) )


signature:
def restore ( self , removals ) :
    "Undo a supposition and all inferences from it ."

implementation:
for B , b in removals :
    self . curr_domains [ B ] . append ( b )


signature:
def conflicted_vars ( self , current ) :
    "Return a list of variables in current assignment that are in conflict"

implementation: return [ var for var in self . vars if self . nconflicts ( var , current [ var ] , current ) > 0 ]


signature:
def nconflicts ( self , var , val , assignment ) :
    """The number of conflicts , as recorded with each assignment .
 Count conflicts in row and in up , down diagonals . If there
 is a queen there , it can ' t conflict with itself , so subtract 3."""

implementation:
n = len ( self . vars )
c = self . rows [ val ] + self . downs [ var + val ] + self . ups [ var - val + n - 1 ]
if assignment . get ( var , None ) == val :
    c -= 3
return c


signature:
def assign ( self , var , val , assignment ) :
    "Assign var , and keep track of conflicts ."

implementation:
oldval = assignment . get ( var , None )
if val != oldval :
    if oldval is not None : # Remove old val if there was one
        self . record_conflict ( assignment , var , oldval , - 1 )
    self . record_conflict ( assignment , var , val , + 1 )
    CSP . assign ( self , var , val , assignment )


signature:
def unassign ( self , var , assignment ) :
    "Remove var from assignment ( if it is there ) and track conflicts ."

implementation:
if var in assignment :
    self . record_conflict ( assignment , var , assignment [ var ] , - 1 )
CSP . unassign ( self , var , assignment )


signature:
def record_conflict ( self , assignment , var , val , delta ) :
    "Record conflicts caused by addition or deletion of a Queen ."

implementation:
n = len ( self . vars )
self . rows [ val ] += delta
self . downs [ var + val ] += delta
self . ups [ var - val + n - 1 ] += delta


signature:
def display ( self , assignment ) :
    "Print the queens and the nconflicts values ( for debugging ) ."

implementation:
n = len ( self . vars )
for val in range ( n ) :
    for var in range ( n ) :
        if assignment . get ( var , '' ) == val :
            ch = 'Q'
        elif ( var + val ) % 2 == 0 :
            ch = '.'
        else :
            ch = '-'
        print ch ,
    print '    ' ,
    for var in range ( n ) :
        if assignment . get ( var , '' ) == val :
            ch = '*'
        else :
            ch = ' '
        print str ( self . nconflicts ( var , val , assignment ) ) + ch ,
    print


signature:
def viterbi_segment ( text , P ) :
    """Find the best segmentation of the string of characters , given the
 UnigramTextModel P ."""

implementation:
# best [ i ] = best probability for text [ 0 : i ]
# words [ i ] = best word ending at position i
n = len ( text )
words = [ '' ] + list ( text )
best = [ 1.0 ] + [ 0.0 ] * n
# # Fill in the vectors best , words via dynamic programming
for i in range ( n + 1 ) :
    for j in range ( 0 , i ) :
        w = text [ j : i ]
        if P [ w ] * best [ i - len ( w ) ] >= best [ i ] :
            best [ i ] = P [ w ] * best [ i - len ( w ) ]
            words [ i ] = w
# # Now recover the sequence of best words
sequence = [ ] ;
i = len ( words ) - 1
while i > 0 :
    sequence [ 0 : 0 ] = [ words [ i ] ]
    i = i - len ( words [ i ] )
# # Return sequence of best words and overall probability
return sequence , best [ - 1 ]


signature:
def encode ( plaintext , code ) :
    "Encodes text , using a code which is a permutation of the alphabet ."

implementation:
from string import maketrans
trans = maketrans ( alphabet + alphabet . upper ( ) , code + code . upper ( ) )
return plaintext . translate ( trans )


signature:
def add ( self , ngram ) :
    """Count 1 for P [ ( w1 , . . . , wn ) ] and for P ( wn | ( w1 , . . . , wn - 1)"""

implementation:
CountingProbDist . add ( self , ngram )
self . cond_prob [ ngram [ : - 1 ] ] . add ( ngram [ - 1 ] )


signature:
def add_sequence ( self , words ) :
    """Add each of the tuple words [ i : i + n ] , using a sliding window .
 Prefix some copies of the empty word , ' ' , to make the start work ."""

implementation:
n = self . n
words = [ '' , ] * ( n - 1 ) + words
for i in range ( len ( words ) - n ) :
    self . add ( tuple ( words [ i : i + n ] ) )


signature:
def samples ( self , nwords ) :
    """Build up a random sample of text nwords words long , using
 the conditional probability given the n - 1 preceding words ."""

implementation:
n = self . n
nminus1gram = ( '' , ) * ( n - 1 )
output = [ ]
for i in range ( nwords ) :
    if nminus1gram not in self . cond_prob :
        nminus1gram = ( '' , ) * ( n - 1 )
        # Cannot continue , so restart .
    wn = self . cond_prob [ nminus1gram ] . sample ( )
    output . append ( wn )
    nminus1gram = nminus1gram [ 1 : ] + ( wn , )
return ' ' . join ( output )