Skip to content

synt.expr.modpath ¤

path module-attribute ¤

path = ModPath

Alias ModPath.

ModPath ¤

Bases: IntoCode

Module path.

Examples:

1
2
3
4
5
6
7
8
p = path(id_('foo'))
assert p.into_code() == "foo"
p = path(id_('foo'), id_('bar'))
assert p.into_code() == "foo.bar"
p = path(id_('foo'), id_('bar'), depth=3)
assert p.into_code() == "...foo.bar"
p = path(id_('foo'), id_('bar')).dep(4)
assert p.into_code() == "....foo.bar"
Source code in synt/expr/modpath.py
class ModPath(IntoCode):
    r"""Module path.

    Examples:
        ```python
        p = path(id_('foo'))
        assert p.into_code() == "foo"
        p = path(id_('foo'), id_('bar'))
        assert p.into_code() == "foo.bar"
        p = path(id_('foo'), id_('bar'), depth=3)
        assert p.into_code() == "...foo.bar"
        p = path(id_('foo'), id_('bar')).dep(4)
        assert p.into_code() == "....foo.bar"
        ```
    """

    names: list[Identifier]
    """Names of the path."""
    depth: int
    """Relative depth of the path."""

    def __init__(self, *names: Identifier, depth: int = 0):
        """Initialize a new module path.

        Args:
            names: Names of the path.
            depth: Relative depth of the path.
        """
        self.names = list(names)
        self.depth = depth

    def dep(self, depth: int) -> Self:
        """Set the depth of the path.

        Args:
            depth: New depth of the path.
        """
        self.depth = depth
        return self

    def into_code(self) -> str:
        return "." * self.depth + ".".join(name.into_code() for name in self.names)

    def as_(self, asname: Identifier) -> Alias:
        """Alias the import path.

        Args:
            asname: Name of the alias.
        """
        from synt.expr.alias import Alias

        return Alias(self, asname)

names instance-attribute ¤

Names of the path.

depth instance-attribute ¤

depth: int = depth

Relative depth of the path.

__init__ ¤

__init__(*names: Identifier, depth: int = 0)

Initialize a new module path.

Parameters:

Name Type Description Default
names Identifier

Names of the path.

()
depth int

Relative depth of the path.

0
Source code in synt/expr/modpath.py
def __init__(self, *names: Identifier, depth: int = 0):
    """Initialize a new module path.

    Args:
        names: Names of the path.
        depth: Relative depth of the path.
    """
    self.names = list(names)
    self.depth = depth

dep ¤

dep(depth: int) -> Self

Set the depth of the path.

Parameters:

Name Type Description Default
depth int

New depth of the path.

required
Source code in synt/expr/modpath.py
def dep(self, depth: int) -> Self:
    """Set the depth of the path.

    Args:
        depth: New depth of the path.
    """
    self.depth = depth
    return self

into_code ¤

into_code() -> str
Source code in synt/expr/modpath.py
def into_code(self) -> str:
    return "." * self.depth + ".".join(name.into_code() for name in self.names)

as_ ¤

as_(asname: Identifier) -> Alias

Alias the import path.

Parameters:

Name Type Description Default
asname Identifier

Name of the alias.

required
Source code in synt/expr/modpath.py
def as_(self, asname: Identifier) -> Alias:
    """Alias the import path.

    Args:
        asname: Name of the alias.
    """
    from synt.expr.alias import Alias

    return Alias(self, asname)

relpath ¤

relpath(*names: Identifier) -> ModPath

Initialize a path relatively (depth = 1).

Parameters:

Name Type Description Default
names Identifier

Names of the path.

()

Examples:

r = relpath(id_("abc"))
assert r.into_code() == ".abc"
Source code in synt/expr/modpath.py
def relpath(*names: Identifier) -> ModPath:
    r"""Initialize a path relatively (`depth = 1`).

    Args:
        names: Names of the path.

    Examples:
        ```python
        r = relpath(id_("abc"))
        assert r.into_code() == ".abc"
        ```
    """
    return ModPath(*names, depth=1)

parentpath ¤

parentpath(*names: Identifier) -> ModPath

Initialize a path from its parent (depth = 2).

Parameters:

Name Type Description Default
names Identifier

Names of the path.

()

Examples:

r = parentpath(id_("abc"))
assert r.into_code() == "..abc"
Source code in synt/expr/modpath.py
def parentpath(*names: Identifier) -> ModPath:
    r"""Initialize a path from its parent (`depth = 2`).

    Args:
        names: Names of the path.

    Examples:
        ```python
        r = parentpath(id_("abc"))
        assert r.into_code() == "..abc"
        ```
    """
    return ModPath(*names, depth=2)