File: names.py

package info (click to toggle)
brian 2.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,872 kB
  • sloc: python: 51,820; cpp: 2,033; makefile: 108; sh: 72
file content (134 lines) | stat: -rw-r--r-- 4,367 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import re
import uuid

from brian2.core.tracking import Trackable
from brian2.utils.logger import get_logger

__all__ = ["Nameable"]

logger = get_logger(__name__)


def find_name(name, names=None):
    """
    Determine a unique name. If the desired ``name`` is already taken, will try
    to use a derived ``name_1``, ``name_2``, etc.

    Parameters
    ----------
    name : str
        The desired name.
    names : Iterable, optional
        A set of names that are already taken. If not provided, will use the names
        of all Brian objects as stored in `Nameable`.

    Returns
    -------
    unique_name : str
        A name based on ``name`` or ``name`` itself, unique with respect to the
        names in ``names``.
    """
    if not name.endswith("*"):
        # explicitly given names are used as given. Network.before_run (and
        # the device in case of standalone) will check for name clashes later
        return name

    name = name[:-1]

    if names is None:
        instances = set(Nameable.__instances__())
        allnames = {obj().name for obj in instances if hasattr(obj(), "name")}
    else:
        allnames = names

    # Try the name without any additions first:
    if name not in allnames:
        return name

    # Name is already taken, try _1, _2, etc.
    i = 1
    while f"{name}_{str(i)}" in allnames:
        i += 1
    return f"{name}_{str(i)}"


class Nameable(Trackable):
    """
    Base class to find a unique name for an object

    If you specify a name explicitly, and it has already been taken, a
    `ValueError` is raised. You can also specify a name with a wildcard asterisk
    in the end, i.e. in the form ``'name*'``. It will then try ``name`` first
    but if this is already specified, it will try ``name_1``, `name__2``, etc.
    This is the default mechanism used by most core objects in Brian, e.g.
    `NeuronGroup` uses a default name of ``'neurongroup*'``.

    Parameters
    ----------
    name : str
        An name for the object, possibly ending in ``*`` to specify that
        variants of this name should be tried if the name (without the asterisk)
        is already taken. If (and only if) the name for this object has already
        been set, it is also possible to call the initialiser with ``None`` for
        the `name` argument. This situation can arise when a class derives from
        multiple classes that derive themselves from `Nameable` (e.g. `Group`
        and `CodeRunner`) and their initialisers are called explicitely.

    Raises
    ------
    ValueError
        If the name is already taken.
    """

    def __init__(self, name):
        if getattr(self, "_name", None) is not None and name is None:
            # name has already been specified previously
            return

        self.assign_id()

        if not isinstance(name, str):
            raise TypeError(
                "'name' argument has to be a string, is type "
                f"{repr(type(name))} instead"
            )
        if not re.match(r"[_A-Za-z][_a-zA-Z0-9]*\*?$", name):
            raise ValueError(f"Name {name} not valid variable name")

        self._name = find_name(name)
        logger.diagnostic(
            f"Created object of class {self.__class__.__name__} with name {self._name}"
        )

    def assign_id(self):
        """
        Assign a new id to this object. Under most circumstances, this method
        should only be called once at the creation of the object to generate a
        unique id. In the case of the `MagicNetwork`, however, the id should
        change when a new, independent set of objects is simulated.
        """
        self._id = uuid.uuid4()

    name = property(
        fget=lambda self: self._name,
        doc="""
        The unique name for this object.

        Used when generating code. Should be an acceptable
        variable name, i.e. starting with a letter
        character and followed by alphanumeric characters and
        ``_``.
        """,
    )

    id = property(
        fget=lambda self: self._id,
        doc="""
        A unique id for this object.

        In contrast to names, which may be reused, the id stays
        unique. This is used in the dependency checking to not
        have to deal with the chore of comparing weak
        references, weak proxies and strong references.
        """,
    )