Exemplo n.º 1
0
def test_read_model_spec():
    spec = assign.read_assignment_spec(
        config.config_file_path('assignment_spec.csv'))

    assert len(spec) == 8

    assert list(spec.columns) == ['description', 'target', 'expression']
Exemplo n.º 2
0
def test_assign_variables_failing(capsys, data):

    close_handlers()

    output_dir = os.path.join(os.path.dirname(__file__), 'output')
    inject.add_injectable("output_dir", output_dir)

    tracing.config_logger(basic=True)

    spec = assign.read_assignment_spec(
        config.config_file_path('assignment_spec_failing.csv'))

    locals_d = {
        'CONSTANT': 7,
        '_shadow': 99,
        'log': np.log,
    }

    with pytest.raises(NameError) as excinfo:
        results, trace_results = assign.assign_variables(spec,
                                                         data,
                                                         locals_d,
                                                         trace_rows=None)

    out, err = capsys.readouterr()
    # don't consume output
    print(out)

    # undefined variable should raise error
    assert "'undefined_variable' is not defined" in str(excinfo.value)
Exemplo n.º 3
0
def test_assign_variables_aliased(capsys, data):

    spec = assign.read_assignment_spec(
        config.config_file_path('assignment_spec_alias_df.csv'))

    locals_d = {'CONSTANT': 7, '_shadow': 99}

    trace_rows = [False, True, False]

    results, trace_results, trace_assigned_locals \
        = assign.assign_variables(spec, data, locals_d,
                                  df_alias='aliased_df', trace_rows=trace_rows)

    print(results)

    assert list(results.columns) == ['target1', 'target2', 'target3']
    assert list(results.target1) == [True, False, False]
    assert list(results.target2) == [53, 53, 55]
    assert list(results.target3) == [530, 530, 550]

    # should assign trace_results for second row in data
    print(trace_results)

    assert trace_results is not None
    assert '_scalar' in trace_results.columns
    assert list(trace_results['_scalar']) == [42]

    # shadow should have been assigned
    assert list(trace_results['_shadow']) == [1]
    assert list(trace_results['_temp']) == [9]
    assert list(trace_results['target3']) == [530]

    assert locals_d['_shadow'] == 99

    out, err = capsys.readouterr()
Exemplo n.º 4
0
def config_logger(basic=False):
    """
    Configure logger

    look for conf file in configs_dir, if not found use basicConfig

    Returns
    -------
    Nothing
    """

    # look for conf file in configs_dir
    log_config_file = None
    if not basic:
        log_config_file = config.config_file_path(LOGGING_CONF_FILE_NAME,
                                                  mandatory=False)

    if log_config_file:
        with open(log_config_file) as f:
            # FIXME need alternative to yaml.UnsafeLoader?
            config_dict = yaml.load(f, Loader=yaml.UnsafeLoader)
            config_dict = config_dict['logging']
            config_dict.setdefault('version', 1)
            logging.config.dictConfig(config_dict)
    else:
        logging.basicConfig(level=logging.INFO, stream=sys.stdout)

    logger = logging.getLogger(ASIM_LOGGER)

    if log_config_file:
        logger.info("Read logging configuration from: %s" % log_config_file)
    else:
        print("Configured logging using basicConfig")
        logger.info("Configured logging using basicConfig")
Exemplo n.º 5
0
def config_logger(basic=False):
    """
    Configure logger

    look for conf file in configs_dir, if not found use basicConfig

    Returns
    -------
    Nothing
    """

    # look for conf file in configs_dir
    log_config_file = None
    if not basic:
        log_config_file = config.config_file_path(LOGGING_CONF_FILE_NAME, mandatory=False)

    if log_config_file:
        with open(log_config_file) as f:
            # FIXME need alternative to yaml.UnsafeLoader?
            config_dict = yaml.load(f, Loader=yaml.UnsafeLoader)
            config_dict = config_dict['logging']
            config_dict.setdefault('version', 1)
            logging.config.dictConfig(config_dict)
    else:
        logging.basicConfig(level=logging.INFO, stream=sys.stdout)

    logger = logging.getLogger(ASIM_LOGGER)

    if log_config_file:
        logger.info("Read logging configuration from: %s" % log_config_file)
    else:
        print("Configured logging using basicConfig")
        logger.info("Configured logging using basicConfig")
Exemplo n.º 6
0
def config_logger(basic=False):
    """
    Configure logger

    look for conf file in configs_dir, if not found use basicConfig

    Returns
    -------
    Nothing
    """

    # look for conf file in configs_dir
    if basic:
        log_config_file = None
    else:
        log_config_file = config.config_file_path(LOGGING_CONF_FILE_NAME,
                                                  mandatory=False)

    if log_config_file:
        try:
            with open(log_config_file) as f:
                config_dict = yaml.load(f, Loader=yaml.UnsafeLoader)
        except Exception as e:
            print(f"Unable to read logging config file {log_config_file}")
            raise e

        try:
            config_dict = config_dict['logging']
            config_dict.setdefault('version', 1)
            logging.config.dictConfig(config_dict)
        except Exception as e:
            print(
                f"Unable to config logging as specified in {log_config_file}")
            raise e

    else:
        logging.basicConfig(level=logging.INFO, stream=sys.stdout)

    # if log_config_file:
    #     with open(log_config_file) as f:
    #         #bug
    #         print("############################################# opening", log_config_file)
    #         # FIXME need alternative to yaml.UnsafeLoader?
    #         config_dict = yaml.load(f, Loader=yaml.UnsafeLoader)
    #         config_dict = config_dict['logging']
    #         config_dict.setdefault('version', 1)
    #         logging.config.dictConfig(config_dict)
    # else:
    #     logging.basicConfig(level=logging.INFO, stream=sys.stdout)

    logger = logging.getLogger(ASIM_LOGGER)

    if log_config_file:
        logger.info("Read logging configuration from: %s" % log_config_file)
    else:
        print("Configured logging using basicConfig")
        logger.info("Configured logging using basicConfig")
Exemplo n.º 7
0
def test_assign_variables(capsys, data):

    spec = assign.read_assignment_spec(
        config.config_file_path('assignment_spec.csv'))

    locals_d = {'CONSTANT': 7, '_shadow': 99}

    results, trace_results, trace_assigned_locals \
        = assign.assign_variables(spec, data, locals_d, trace_rows=None)

    print(results)

    assert list(results.columns) == ['target1', 'target2', 'target3']
    assert list(results.target1) == [True, False, False]
    assert list(results.target2) == [53, 53, 55]
    assert list(results.target3) == [530, 530, 550]
    assert trace_results is None
    assert trace_assigned_locals is None

    trace_rows = [False, True, False]

    results, trace_results, trace_assigned_locals \
        = assign.assign_variables(spec, data, locals_d, trace_rows=trace_rows)

    # should get same results as before
    assert list(results.target3) == [530, 530, 550]

    # should assign trace_results for second row in data
    print(trace_results)

    assert trace_results is not None
    assert '_scalar' in trace_results.columns
    assert list(trace_results['_scalar']) == [42]

    # shadow should have been assigned
    assert list(trace_results['_shadow']) == [1]
    assert list(trace_results['_temp']) == [9]
    assert list(trace_results['target3']) == [530]

    print("trace_assigned_locals", trace_assigned_locals)
    assert trace_assigned_locals['_DF_COL_NAME'] == 'thing2'

    # shouldn't have been changed even though it was a target
    assert locals_d['_shadow'] == 99

    out, err = capsys.readouterr()