Skip to content

environment

Attributes:

Name Type Description
env _Env

Global instance which stores the state of the cassini application. There should only be one instance of this object.

env module-attribute

env = _Env()

_Env

Essentially a global object that describes the state of the project for this interpreter.

As each notebook has its own interpreter, each notebook also has its own env that basically stores what the current project is and what tier this ipynb file corresponds to.

Attributes:

Name Type Description
project Project

reference to the current project object. Returns None if one not set yet.

Warnings

This object is a singleton, so only one instance can exist at a time.

This object shouldn't be created directly, instead you should call project.env('...') to set its value.

Source code in cassini/environment.py
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
class _Env:
    """
    Essentially a global object that describes the state of the project for this interpreter.

    As each notebook has its own interpreter, each notebook also has its own env that basically stores what the current
    project is and what tier this `ipynb` file corresponds to.

    Attributes
    ----------
    project : Project
        reference to the current project object. Returns `None` if one not set yet.

    Warnings
    --------
    This object is a singleton, so only one instance can exist at a time.

    This object shouldn't be created directly, instead you should call `project.env('...')` to set its value.
    """

    instance: Union[_Env, None] = None

    def __new__(cls, *args: Any, **kwargs: Any) -> _Env:
        if cls.instance:
            raise RuntimeError(
                "Attempted to create new _Env instance, only 1 _instance permitted per interpreter"
            )
        instance = object.__new__(cls)
        cls.instance = instance
        return instance

    def __init__(self) -> None:
        self.project: Union[Project, None] = None
        self._o: Union[TierABC, None] = None
        self.shareable_project: Union[ShareableProject, None] = None
        self._caches: List[Dict[Any, Any]] = []

    @staticmethod
    def is_sharing(instance: _Env) -> TypeGuard["_SharingInstance"]:
        return bool(instance.shareable_project and instance.project)

    @staticmethod
    def is_shared(instance: _Env) -> TypeGuard["_SharedInstance"]:
        return bool(instance.shareable_project and not instance.project)

    @property
    def o(self) -> Union[TierABC, None]:
        """
        Reference to current Tier object.
        """
        return self._o

    def update(self, obj: TierABC) -> None:
        self._o = obj

    def create_cache(self):
        """
        Method for creating various caches throughout cassini.

        the env instances keeps track of these, to allow them to be cleared
        cleanly during testing.

        This is an internal feature.
        """
        cache = dict()
        self._caches.append(cache)
        return cache

    def _reset(self):
        """
        Reset env instance to initial state. Used for testing.

        `project`, `shareable_project` and `caches` are cleared.
        """
        self.shareable_project = None
        self.project = None

        for cache in self._caches:
            cache.clear()

o property

o

Reference to current Tier object.

create_cache

create_cache()

Method for creating various caches throughout cassini.

the env instances keeps track of these, to allow them to be cleared cleanly during testing.

This is an internal feature.

Source code in cassini/environment.py
72
73
74
75
76
77
78
79
80
81
82
83
def create_cache(self):
    """
    Method for creating various caches throughout cassini.

    the env instances keeps track of these, to allow them to be cleared
    cleanly during testing.

    This is an internal feature.
    """
    cache = dict()
    self._caches.append(cache)
    return cache

_reset

_reset()

Reset env instance to initial state. Used for testing.

project, shareable_project and caches are cleared.

Source code in cassini/environment.py
85
86
87
88
89
90
91
92
93
94
95
def _reset(self):
    """
    Reset env instance to initial state. Used for testing.

    `project`, `shareable_project` and `caches` are cleared.
    """
    self.shareable_project = None
    self.project = None

    for cache in self._caches:
        cache.clear()