Exemplo n.º 1
0
def assemble_model(infiles, model_overrides):

    mdl = model._get_model_base()

    for filespec in infiles:
        try:
            mdl = load_model_part(mdl, filespec)
        except Exception as ex:
            raise six.reraise(Exception, ("Failed reading %s due to: %s" %
                                          (filespec, ex)),
                              ex.__traceback__)  ## Python-2 :-(

    if (model_overrides):
        model_overrides = functools.reduce(lambda x, y: x + y,
                                           model_overrides)  # join all -m
        for (json_path, value) in model_overrides:
            try:
                if (not json_path.startswith('/')):
                    json_path = _default_model_overridde_path + json_path
                pandel.set_jsonpointer(mdl, json_path, value)
            except Exception as ex:
                raise six.reraise(
                    Exception, ("Failed setting model-value(%s) due to: %s" %
                                (json_path, value, ex)),
                    ex.__traceback__)  ## Python-2 :-(

    return mdl
Exemplo n.º 2
0
def load_model_part(mdl, filespec):
    dfin = load_file_as_df(filespec)
    log.debug("  +-input-file(%s):\n%s", filespec.fname, dfin.head())
    if filespec.path:
        pandel.set_jsonpointer(mdl, filespec.path, dfin)
    else:
        mdl = dfin
    return mdl
Exemplo n.º 3
0
def load_model_part(mdl, filespec):
    dfin = load_file_as_df(filespec)
    log.debug("  +-input-file(%s):\n%s", filespec.fname, dfin.head())
    if filespec.path:
        pandel.set_jsonpointer(mdl, filespec.path, dfin)
    else:
        mdl = dfin
    return mdl
Exemplo n.º 4
0
 def test_set_jsonpointer_append_path_preserves_intermediate(self):
     doc = {'foo': {'bar': 1}, 1: 2}
     path = '/foo/foo2'
     value = 'value'
     pandel.set_jsonpointer(doc, path, value)
     print(doc)
     self.assertEqual(pandel.resolve_jsonpointer(doc, path), value)
     self.assertEqual(doc[1], 2)
     self.assertEqual(pandel.resolve_jsonpointer(doc, '/foo/bar'), 1)
Exemplo n.º 5
0
 def test_set_jsonpointer_append_path_preserves_intermediate(self):
     doc = {'foo': {'bar': 1}, 1:2}
     path = '/foo/foo2'
     value = 'value'
     pandel.set_jsonpointer(doc, path, value)
     print(doc)
     self.assertEqual(pandel.resolve_jsonpointer(doc, path), value)
     self.assertEqual(doc[1], 2)
     self.assertEqual(pandel.resolve_jsonpointer(doc, '/foo/bar'), 1)
Exemplo n.º 6
0
    def test_set_jsonpointer_empty_doc(self):
        doc = {}
        path = '/foo'
        value = 'value'
        pandel.set_jsonpointer(doc, path, value)
        self.assertEqual(pandel.resolve_jsonpointer(doc, path), value)

        doc = {}
        path = '/foo/bar'
        value = 'value'
        pandel.set_jsonpointer(doc, path, value)
        self.assertEqual(pandel.resolve_jsonpointer(doc, path), value)
Exemplo n.º 7
0
    def test_set_jsonpointer_sequence(self):
        doc = [1,2]
        path = '/1'
        value = 'value'
        pandel.set_jsonpointer(doc, path, value)
        self.assertEqual(pandel.resolve_jsonpointer(doc, path), value)

        doc = [1,2]
        path = '/1/foo/bar'
        value = 'value'
        pandel.set_jsonpointer(doc, path, value)
        self.assertEqual(pandel.resolve_jsonpointer(doc, path), value)
Exemplo n.º 8
0
    def test_set_jsonpointer_sequence(self):
        doc = [1, 2]
        path = '/1'
        value = 'value'
        pandel.set_jsonpointer(doc, path, value)
        self.assertEqual(pandel.resolve_jsonpointer(doc, path), value)

        doc = [1, 2]
        path = '/1/foo/bar'
        value = 'value'
        pandel.set_jsonpointer(doc, path, value)
        self.assertEqual(pandel.resolve_jsonpointer(doc, path), value)
Exemplo n.º 9
0
    def test_set_jsonpointer_empty_doc(self):
        doc = {}
        path = '/foo'
        value = 'value'
        pandel.set_jsonpointer(doc, path, value)
        self.assertEqual(pandel.resolve_jsonpointer(doc, path), value)

        doc = {}
        path = '/foo/bar'
        value = 'value'
        pandel.set_jsonpointer(doc, path, value)
        self.assertEqual(pandel.resolve_jsonpointer(doc, path), value)
Exemplo n.º 10
0
    def test_set_jsonpointer_missing(self):
        doc = {'foo': 1, 1: 2}
        path = '/foo/bar'
        value = 'value'
        pandel.set_jsonpointer(doc, path, value)
        self.assertEqual(pandel.resolve_jsonpointer(doc, path), value)
        self.assertEqual(doc[1], 2)

        doc = {'foo': 1, 1: 2}
        path = '/foo/bar/some/other'
        value = 'value'
        pandel.set_jsonpointer(doc, path, value)
        self.assertEqual(pandel.resolve_jsonpointer(doc, path), value)
        self.assertEqual(doc[1], 2)
Exemplo n.º 11
0
def build_models(vehs_df, **locals_kws):
    """
    Builds all input-dataframes as Experiment classes and returns them in a list of (veh_id, exp) pairs.

    :param vehs_df:     A dataframe indexed by veh_id, and with columns *json-pointer* paths into the model
    :return: a list of (veh_id, :class:`wltp.experiment.Experiment`) tuples
    """
    experiment_pairs = []
    for veh_id, row in vehs_df.iterrows():
        try:
            mdl_in = {}
            for colname, colval in row.items():
                log.debug('veh_id(%s): Column(%s): %s', veh_id, colname,
                          colval)
                if not colval or (isinstance(colval, str)
                                  and not colval.strip()):
                    continue
                if isinstance(colval, str):

                    ## Is it an excel-ref like:
                    #        @<sheet_name>!A1[:R10:c1].table
                    #
                    try:
                        colval = resolve_excel_ref(colval)
                    except ValueError:
                        ## Try to parse value as python-code.
                        #
                        try:
                            old_v = colval
                            colval = eval(
                                colval, globals(), locals_kws
                            )  ## NOTE: Total insecure, but we're scientists, aren't we?
                        except Exception:
                            log.info(
                                "Failed parsing value(%s) as python code due to: %s\n  Assuming plain string.",
                                old_v, traceback.format_exc())
                        else:
                            log.info(
                                "Parsed value(%s) as python code into: %s",
                                old_v, colval)
                pdl.set_jsonpointer(mdl_in, colname, colval)

            exp = Experiment(mdl_in)

            experiment_pairs.append((veh_id, exp))
        except Exception as ex:
            raise Exception('Invalid model for vehicle(%s): %s' %
                            (veh_id, ex)) from ex

    return experiment_pairs
Exemplo n.º 12
0
    def test_set_jsonpointer_missing(self):
        doc = {'foo': 1, 1:2}
        path = '/foo/bar'
        value = 'value'
        pandel.set_jsonpointer(doc, path, value)
        self.assertEqual(pandel.resolve_jsonpointer(doc, path), value)
        self.assertEqual(doc[1], 2)

        doc = {'foo': 1, 1:2}
        path = '/foo/bar/some/other'
        value = 'value'
        pandel.set_jsonpointer(doc, path, value)
        self.assertEqual(pandel.resolve_jsonpointer(doc, path), value)
        self.assertEqual(doc[1], 2)
Exemplo n.º 13
0
    def test_set_jsonpointer_sequence_insert_end(self):
        doc = [0, 1]
        path = '/2'
        value = 'value'
        pandel.set_jsonpointer(doc, path, value)
        self.assertEqual(pandel.resolve_jsonpointer(doc, path), value)
        self.assertEqual(pandel.resolve_jsonpointer(doc, '/0'), 0)
        self.assertEqual(pandel.resolve_jsonpointer(doc, '/1'), 1)

        doc = [0, 1]
        path = '/-'
        value = 'value'
        pandel.set_jsonpointer(doc, path, value)
        self.assertEqual(pandel.resolve_jsonpointer(doc, '/2'), value)
        self.assertEqual(pandel.resolve_jsonpointer(doc, '/0'), 0)
        self.assertEqual(pandel.resolve_jsonpointer(doc, '/1'), 1)
Exemplo n.º 14
0
    def test_set_jsonpointer_sequence_insert_end(self):
        doc = [0, 1]
        path = '/2'
        value = 'value'
        pandel.set_jsonpointer(doc, path, value)
        self.assertEqual(pandel.resolve_jsonpointer(doc, path), value)
        self.assertEqual(pandel.resolve_jsonpointer(doc, '/0'), 0)
        self.assertEqual(pandel.resolve_jsonpointer(doc, '/1'), 1)

        doc = [0, 1]
        path = '/-'
        value = 'value'
        pandel.set_jsonpointer(doc, path, value)
        self.assertEqual(pandel.resolve_jsonpointer(doc, '/2'), value)
        self.assertEqual(pandel.resolve_jsonpointer(doc, '/0'), 0)
        self.assertEqual(pandel.resolve_jsonpointer(doc, '/1'), 1)
Exemplo n.º 15
0
def assemble_model(infiles, model_overrides):

    mdl = model._get_model_base()

    for filespec in infiles:
        try:
            mdl = load_model_part(mdl, filespec)
        except Exception as ex:
            raise six.reraise(Exception, ("Failed reading %s due to: %s" %(filespec, ex)), ex.__traceback__) ## Python-2 :-(

    if (model_overrides):
        model_overrides = functools.reduce(lambda x,y: x+y, model_overrides) # join all -m
        for (json_path, value) in model_overrides:
            try:
                if (not json_path.startswith('/')):
                    json_path = _default_model_overridde_path + json_path
                pandel.set_jsonpointer(mdl, json_path, value)
            except Exception as ex:
                raise six.reraise(Exception, ("Failed setting model-value(%s) due to: %s" %(json_path, value, ex)), ex.__traceback__) ## Python-2 :-(

    return mdl
Exemplo n.º 16
0
    def test_set_jsonpointer_replace_value(self):
        doc = {'foo': 'bar', 1:2}
        path = '/foo'
        value = 'value'
        pandel.set_jsonpointer(doc, path, value)
        self.assertEqual(pandel.resolve_jsonpointer(doc, path), value)
        self.assertEqual(doc[1], 2)

        doc = {'foo': 1, 1:2}
        path = '/foo'
        value = 'value'
        pandel.set_jsonpointer(doc, path, value)
        self.assertEqual(pandel.resolve_jsonpointer(doc, path), value)
        self.assertEqual(doc[1], 2)

        doc = {'foo': {'bar': 1}, 1:2}
        path = '/foo'
        value = 2
        pandel.set_jsonpointer(doc, path, value)
        self.assertEqual(pandel.resolve_jsonpointer(doc, path), value)
        self.assertEqual(doc[1], 2)
Exemplo n.º 17
0
    def test_set_jsonpointer_replace_value(self):
        doc = {'foo': 'bar', 1: 2}
        path = '/foo'
        value = 'value'
        pandel.set_jsonpointer(doc, path, value)
        self.assertEqual(pandel.resolve_jsonpointer(doc, path), value)
        self.assertEqual(doc[1], 2)

        doc = {'foo': 1, 1: 2}
        path = '/foo'
        value = 'value'
        pandel.set_jsonpointer(doc, path, value)
        self.assertEqual(pandel.resolve_jsonpointer(doc, path), value)
        self.assertEqual(doc[1], 2)

        doc = {'foo': {'bar': 1}, 1: 2}
        path = '/foo'
        value = 2
        pandel.set_jsonpointer(doc, path, value)
        self.assertEqual(pandel.resolve_jsonpointer(doc, path), value)
        self.assertEqual(doc[1], 2)
Exemplo n.º 18
0
 def test_set_jsonpointer_sequence_out_of_bounds(self):
     doc = [0, 1]
     path = '/3'
     value = 'value'
     with self.assertRaises(JsonPointerException):
         pandel.set_jsonpointer(doc, path, value)
Exemplo n.º 19
0
 def test_set_jsonpointer_sequence_with_str_screams(self):
     doc = [0, 1]
     path = '/str'
     value = 'value'
     with self.assertRaises(JsonPointerException):
         pandel.set_jsonpointer(doc, path, value)
Exemplo n.º 20
0
#!python
Exemplo n.º 21
0
 def test_set_jsonpointer_sequence_with_str_screams(self):
     doc = [0, 1]
     path = '/str'
     value = 'value'
     with self.assertRaises(JsonPointerException):
         pandel.set_jsonpointer(doc, path, value)
Exemplo n.º 22
0
 def test_set_jsonpointer_sequence_out_of_bounds(self):
     doc = [0, 1]
     path = '/3'
     value = 'value'
     with self.assertRaises(JsonPointerException):
         pandel.set_jsonpointer(doc, path, value)