How to use the mlrun.utils.get_in function in mlrun

To help you get started, we’ve selected a few mlrun examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mlrun / mlrun / mlrun / db / filedb.py View on Github external
def del_artifacts(self, name='', project='', tag='', labels=None):
        labels = [] if labels is None else labels
        tag = tag or 'latest'
        filepath = self._filepath(artifacts_dir, project, tag=tag)

        if isinstance(labels, str):
            labels = labels.split(',')
        if tag == '*':
            mask = '**/*' + name
            if name:
                mask += '*'
        else:
            mask = '**/*'

        for artifact, p in self._load_list(filepath, mask):
            if (name == '' or name == get_in(artifact, 'key', '')) and match_labels(
                get_in(artifact, 'labels', {}), labels
            ):

                self._safe_del(p)
github mlrun / mlrun / mlrun / run.py View on Github external
if kind is None or kind in ['', 'Function']:
        raise ValueError('please specify the function kind')
    elif kind in ['local']:
        r = LocalRuntime()
    elif kind in RuntimeKinds.all():
        r = get_runtime_class(kind)()
    else:
        raise ValueError('unsupported runtime ({})'.format(kind))

    name, spec, code = build_file(filename, name=name)

    if not name:
        raise ValueError('name must be specified')
    h = get_in(spec, 'spec.handler', '').split(':')
    r.handler = h[0] if len(h) <= 1 else h[1]
    r.metadata = get_in(spec, 'spec.metadata')
    r.metadata.name = name
    r.spec.image = get_in(spec, 'spec.image', image)
    build = r.spec.build
    build.code_origin = code_origin
    build.base_image = get_in(spec, 'spec.build.baseImage')
    build.commands = get_in(spec, 'spec.build.commands')
    if embed_code:
        build.functionSourceCode = get_in(spec, 'spec.build.functionSourceCode')
    else:
        if code_output:
            r.spec.command = code_output
        else:
            r.spec.command = filename

    build.image = get_in(spec, 'spec.build.image')
    build.secret = get_in(spec, 'spec.build.secret')
github mlrun / mlrun / mlrun / lists.py View on Github external
'inputs',
            'parameters',
            'results',
            'artifacts',
            'error',
        ]
        for run in self:
            row = [
                get_in(run, 'metadata.project', config.default_project),
                get_in(run, 'metadata.uid', ''),
                get_in(run, 'metadata.iteration', ''),
                get_in(run, 'status.start_time', ''),
                get_in(run, 'status.state', ''),
                get_in(run, 'metadata.name', ''),
                get_in(run, 'metadata.labels', ''),
                get_in(run, 'spec.inputs', ''),
                get_in(run, 'spec.parameters', ''),
                get_in(run, 'status.results', ''),
                get_in(run, 'status.artifacts', []),
                get_in(run, 'status.error', ''),
            ]
            rows.append(row)

        return [head] + rows
github mlrun / mlrun / mlrun / run.py View on Github external
def _load_func_code(command='', workdir=None, secrets=None, name='name'):
    is_obj = hasattr(command, 'to_dict')
    suffix = '' if is_obj else Path(command).suffix
    runtime = None
    if is_obj or suffix == '.yaml':
        is_remote = False
        if is_obj:
            runtime = command.to_dict()
        else:
            is_remote = '://' in command
            data = get_object(command, secrets)
            runtime = yaml.load(data, Loader=yaml.FullLoader)

        command = get_in(runtime, 'spec.command', '')
        code = get_in(runtime, 'spec.build.functionSourceCode')

        if code:
            fpath = mktemp('.py')
            code = b64decode(code).decode('utf-8')
            command = fpath
            with open(fpath, 'w') as fp:
                fp.write(code)
        elif command and not is_remote:
            command = path.join(workdir or '', command)
            if not path.isfile(command):
                raise OSError('command file {} not found'.format(command))

        else:
            raise RuntimeError('cannot run, command={}'.format(command))

    elif command == '':
github mlrun / mlrun / mlrun / runtimes / function.py View on Github external
def _update_state(self, rundict: dict):
        last_state = get_in(rundict, 'status.state', '')
        if last_state != 'error':
            update_in(rundict, 'status.state', 'completed')
        self._store_run_dict(rundict)
        return rundict
github mlrun / mlrun / mlrun / execution.py View on Github external
def log_iteration_results(self, best, summary: list, task: dict, commit=False):
        """Reserved for internal use"""

        if best:
            self._results['best_iteration'] = best
            for k, v in get_in(task, ['status', 'results'], {}).items():
                self._results[k] = v
            for a in get_in(task, ['status', run_keys.artifacts], []):
                self._artifacts_manager.artifacts[a['key']] = a
                self._artifacts_manager.link_artifact(
                    self.project,
                    self.name,
                    self.tag,
                    a['key'],
                    self.iteration,
                    a['target_path'],
                    link_iteration=best,
                )

        self._iteration_results = summary
        if commit:
            self._update_db(commit=True)
github mlrun / mlrun / mlrun / run.py View on Github external
def _load_func_code(command='', workdir=None, secrets=None, name='name'):
    is_obj = hasattr(command, 'to_dict')
    suffix = '' if is_obj else Path(command).suffix
    runtime = None
    if is_obj or suffix == '.yaml':
        is_remote = False
        if is_obj:
            runtime = command.to_dict()
        else:
            is_remote = '://' in command
            data = get_object(command, secrets)
            runtime = yaml.load(data, Loader=yaml.FullLoader)

        command = get_in(runtime, 'spec.command', '')
        code = get_in(runtime, 'spec.build.functionSourceCode')

        if code:
            fpath = mktemp('.py')
            code = b64decode(code).decode('utf-8')
            command = fpath
            with open(fpath, 'w') as fp:
                fp.write(code)
        elif command and not is_remote:
            command = path.join(workdir or '', command)
            if not path.isfile(command):
                raise OSError('command file {} not found'.format(command))

        else:
            raise RuntimeError('cannot run, command={}'.format(command))
github mlrun / mlrun / mlrun / runtimes / mpijob.py View on Github external
resp = k8s.crdapi.list_namespaced_custom_object(
                MpiJob.group, MpiJob.version, namespace, MpiJob.plural,
                watch=False, label_selector=selector)
        except client.rest.ApiException as e:
            print("Exception when reading MPIJob: %s" % e)

        items = []
        if resp:
            items = resp.get('items', [])
            if show and items:
                print('{:10} {:20} {:21} {}'.format(
                    'status', 'name', 'start', 'end'))
                for i in items:
                    print('{:10} {:20} {:21} {}'.format(
                        get_in(i, 'status.launcherStatus', ''),
                        get_in(i, 'metadata.name', ''),
                        get_in(i, 'status.startTime', ''),
                        get_in(i, 'status.completionTime', ''),
                    ))
        return items
github mlrun / mlrun / mlrun / lists.py View on Github external
'iter',
            'start',
            'state',
            'name',
            'labels',
            'inputs',
            'parameters',
            'results',
            'artifacts',
            'error',
        ]
        for run in self:
            row = [
                get_in(run, 'metadata.project', config.default_project),
                get_in(run, 'metadata.uid', ''),
                get_in(run, 'metadata.iteration', ''),
                get_in(run, 'status.start_time', ''),
                get_in(run, 'status.state', ''),
                get_in(run, 'metadata.name', ''),
                get_in(run, 'metadata.labels', ''),
                get_in(run, 'spec.inputs', ''),
                get_in(run, 'spec.parameters', ''),
                get_in(run, 'status.results', ''),
                get_in(run, 'status.artifacts', []),
                get_in(run, 'status.error', ''),
            ]
            rows.append(row)

        return [head] + rows
github mlrun / mlrun / mlrun / run.py View on Github external
r.metadata.name = name
    r.spec.image = get_in(spec, 'spec.image', image)
    build = r.spec.build
    build.code_origin = code_origin
    build.base_image = get_in(spec, 'spec.build.baseImage')
    build.commands = get_in(spec, 'spec.build.commands')
    if embed_code:
        build.functionSourceCode = get_in(spec, 'spec.build.functionSourceCode')
    else:
        if code_output:
            r.spec.command = code_output
        else:
            r.spec.command = filename

    build.image = get_in(spec, 'spec.build.image')
    build.secret = get_in(spec, 'spec.build.secret')
    if r.kind != 'local':
        r.spec.env = get_in(spec, 'spec.env')
        for vol in get_in(spec, 'spec.volumes', []):
            r.spec.volumes.append(vol.get('volume'))
            r.spec.volume_mounts.append(vol.get('volumeMount'))

    if with_doc:
        handlers = find_handlers(code)
        r.spec.entry_points = {h['name']: as_func(h) for h in handlers}
    r.spec.default_handler = handler
    update_meta(r)
    return r