def _euclidean(event_a, event_b):
    """Return the euclidean distance between two points

    Parameters:
    ----------
    event_a, event_b: Event
        Event point

    Output:
    ------
    result: Distance()
    """

    ##some shortcuts
    values_a = event_a.__dict__.values()
    values_b = event_b.__dict__.values()

    ##check if attributes are float to compute euclidean distance
    which_a_attribute_float = is_type(values_a, np.float)
    which_b_attribute_float = is_type(values_b, np.float)
    if which_a_attribute_float == which_b_attribute_float:
        x = np.array(list(compress(values_a, which_a_attribute_float)))
        y = np.array(list(compress(values_b, which_b_attribute_float)))

        return np.linalg.norm(x-y)
Example #2
0
def func_parse(i, tokens, parent=None):
    func = ast.FunctionDefAST(parent)
    error = ""
    while tokens[i].type != 'END':
        if tokens[i].type == 'FUNC':
            i += 1
            continue
        elif tokens[i].type == 'ID':
            obj = parent.get_children(tokens[i].value)
            if obj is not None:
                error = "Переменная с именем " + tokens[
                    i].value + " уже объявлена."
                print(error)
                return None, i, error
            parent.add_child(tokens[i].value, func)
            func.set_name(tokens[i].value)
            i += 1
        elif tokens[i].type == 'LPAREN':
            i += 1
            while tokens[i].type != 'RPAREN':
                if tokens[i].type == 'ID':
                    a = parent.get_children(tokens[i].value)
                    if a is not None:
                        error = "Переменная с именем " + tokens[
                            i].value + " уже объявлена во внешней области видимости."
                        print(error)
                        return None, i, error
                    a = ast.VarDecAST(func)
                    a.set_name(tokens[i].value)
                    func.add_arg(a)
                    i += 1
                    if utils.is_type(tokens[i].type):
                        a.set_type(tokens[i].type)
                    else:
                        error = "Не указан тип у переменной с именем " + tokens[
                            i].value + "."
                        print(error)
                        return None, i, error
                i += 1
            i += 1
            continue
        elif utils.is_type(tokens[i].type):
            func.set_type(tokens[i].type)
            i += 1
            continue
        elif tokens[i].type == 'COLON':
            if func.type is None:
                error = "Не указан возвращаемый тип у функции с именем " + func.name + "."
                print(error)
                return None, i, error
            i += 1
            while tokens[i].type != 'END':
                _, i, error = compound_expression_parse(i, tokens, func)
                i += 1
            if error != "":
                print(error)
                return None, i, error
    return func, i, error
Example #3
0
def var_parse(i: int, tokens, parent):
    v = ast.VarDecAST()
    v.set_parent(parent)
    if tokens[i].type == 'VAR':
        i += 1
        if tokens[i].type == 'ID':
            obj = parent.get_children(tokens[i].value)
            if obj is not None:
                error = "Переменная с именем " + tokens[
                    i].value + " существует."
                print(error)
                return None, i, error
            else:
                parent.add_child(tokens[i].value, v)
                v.set_name(tokens[i].value)
        else:
            error = "Ошибка объявления переменной. Не указано имя."
            print(error)
            return None, i, error
        i += 1
        if utils.is_type(tokens[i].type):
            v.set_type(tokens[i].type)
        else:
            error = "Ошибка объявления переменной. Некорректно указан тип."
            print(error)
            return None, i, error
        i += 1
        if tokens[i].type == 'SEMI':
            return v, i, ""
        else:
            error = "Ошибка. Нет точки с запятой."
            print(error)
            return None, i, error
Example #4
0
def proc_parse(i, tokens, parent=None):
    proc = ast.ProcedureDefAST(parent)
    error = ""
    while tokens[i].type != 'END':
        if tokens[i].type == 'PROC':
            i += 1
            continue
        elif tokens[i].type == 'ID':
            obj = parent.get_children(tokens[i].value)
            if obj is not None:
                error = "Переменная с именем " + tokens[
                    i].value + " уже объявлена."
                print(error)
                return None, i, error
            parent.add_child(tokens[i].value, proc)
            proc.set_name(tokens[i].value)
            i += 1
        elif tokens[i].type == 'LPAREN':
            i += 1
            while tokens[i].type != 'RPAREN':
                if tokens[i].type == 'ID':
                    a = parent.get_children(tokens[i].value)
                    if a is not None:
                        error = "Переменная с именем " + tokens[
                            i].value + " уже объявлена во внешней области видимости."
                        print(error)
                        return None, i, error
                    a = ast.VarDecAST(proc)
                    a.set_name(tokens[i].value)
                    proc.add_arg(a)
                    i += 1
                    if utils.is_type(tokens[i].type):
                        a.set_type(tokens[i].type)
                    else:
                        error = "Не указан тип у переменной с именем " + tokens[
                            i].value + "."
                        print(error)
                        return None, i, error
                i += 1
            i += 1
            continue
        elif tokens[i].type == 'COLON':
            i += 1
            while tokens[i].type != 'END':
                _, i, error = compound_expression_parse(i, tokens, proc)
                i += 1
            if error != "":
                print(error)
                return None, i, error
    return proc, i, error
Example #5
0
File: rdir.py Project: suvayu/rplot
    def ls(self, path=None, robj_t=None, robj_p=None):
        """Return list of key(s) in path.

        If path is a directory, returns a list of keys in the
        directory.  If it is a non-directory, returns the key for the
        object.

        The returned list can be filtered with object type, or a
        custom filter function.  Object type can be any ROOT Class
        that uses the ClassDef macro in it's declaration.  The custom
        filter function can be any function that can filter using the
        object key.  When these two filtering methods are combined, it
        is equivalent to boolean AND.

        path   -- path specification string (see pathspec for format)
        robj_t -- ROOT object type
        robj_p -- custom filter function that takes ROOT.TKey

        """
        rdir = self.get_dir(path)
        if not rdir:  # not a dir, or does not exist
            path = pathspec(path)
            # try again from one level up
            rdir = self.get_dir('{}:{}'.format(path.rfile, path.rpath_dirname))
            # FIXME: should be: while not rdir: keep trying
            if rdir:  # exists, but not a directory
                keys = [rdir.GetKey(path.rpath_basename)]
            else:  # does not exist
                keys = []
        else:
            keys = rdir.GetListOfKeys()
        keys = filter(None, keys)
        if robj_t:
            keys = filter(lambda key: is_type(key, robj_t), keys)
        if robj_p:
            keys = filter(robj_p, keys)
        return keys
Example #6
0
File: rdir.py Project: suvayu/rplot
    def ls(self, path=None, robj_t=None, robj_p=None):
        """Return list of key(s) in path.

        If path is a directory, returns a list of keys in the
        directory.  If it is a non-directory, returns the key for the
        object.

        The returned list can be filtered with object type, or a
        custom filter function.  Object type can be any ROOT Class
        that uses the ClassDef macro in it's declaration.  The custom
        filter function can be any function that can filter using the
        object key.  When these two filtering methods are combined, it
        is equivalent to boolean AND.

        path   -- path specification string (see pathspec for format)
        robj_t -- ROOT object type
        robj_p -- custom filter function that takes ROOT.TKey

        """
        rdir = self.get_dir(path)
        if not rdir:            # not a dir, or does not exist
            path = pathspec(path)
            # try again from one level up
            rdir = self.get_dir('{}:{}'.format(path.rfile, path.rpath_dirname))
            # FIXME: should be: while not rdir: keep trying
            if rdir:            # exists, but not a directory
                keys = [rdir.GetKey(path.rpath_basename)]
            else:               # does not exist
                keys = []
        else:
            keys = rdir.GetListOfKeys()
        keys = filter(None, keys)
        if robj_t:
            keys = filter(lambda key: is_type(key, robj_t), keys)
        if robj_p:
            keys = filter(robj_p, keys)
        return keys