From 445a2c76bb4d3cfeacc350bff53db3d1fdce0ee7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Ma=C5=9Blanka?= <piotr.maslanka@henrietta.com.pl> Date: Mon, 17 Oct 2022 17:07:28 +0200 Subject: [PATCH] add an extra method for a Context --- satella/__init__.py | 2 +- satella/coding/environment.py | 21 +++++++++++---------- satella/debug/test_environment.py | 12 ++++++++++++ 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/satella/__init__.py b/satella/__init__.py index 9015163e..fe1f0f4e 100644 --- a/satella/__init__.py +++ b/satella/__init__.py @@ -1 +1 @@ -__version__ = '2.22.0rc2' +__version__ = '2.22.0' diff --git a/satella/coding/environment.py b/satella/coding/environment.py index c0482a1d..a871ac4a 100644 --- a/satella/coding/environment.py +++ b/satella/coding/environment.py @@ -1,14 +1,12 @@ from __future__ import annotations -import typing as tp -import threading -from satella.coding.typing import V -from .misc import Closeable +import threading +import typing as tp +from satella.coding.typing import V local = threading.local() - THEY_HATIN = object() @@ -19,19 +17,23 @@ class Context: def __init__(self, parent: tp.Optional[Context] = None, **variables): self.parent = parent - self.variables = {} + self.variables = variables self.bool = None def __str__(self): return str(id(self)) - def push_up(self, item: str) -> None: + def push_up(self, item: str, value=THEY_HATIN) -> None: """ Advance current variable to the top of the card stack. :param item: variable name + :param value: if not given, current value of given variable will be taken """ - var = self.variables.pop(item) + if value is THEY_HATIN: + var = self.variables.pop(item) + else: + var = value self.parent.variables[item] = var def __getattr__(self, item: str): @@ -51,8 +53,7 @@ class Context: except AttributeError: parent = None ctxt = Context(parent=parent) - if ctxt is not parent: - ctxt.parent = parent + ctxt.parent = parent local.thread_context = ctxt return ctxt diff --git a/satella/debug/test_environment.py b/satella/debug/test_environment.py index c6c74346..361a6156 100644 --- a/satella/debug/test_environment.py +++ b/satella/debug/test_environment.py @@ -12,6 +12,18 @@ class TestEnvs(unittest.TestCase): with Context() as new_ctxt: self.assertEqual(new_ctxt.value, 5) + def test_push(self): + ctxt = Context.get() + ctxt.value = 5 + self.assertEqual(ctxt.value, 5) + with Context() as new_ctxt: + self.assertEqual(new_ctxt.value, 5) + with Context() as new_ctxt2: + new_ctxt2.push_up('value', 6) + self.assertEqual(new_ctxt.value, 6) + + self.assertEqual(ctxt.value, 5) + def test_delete_envs(self): ctxt = Context.get() ctxt.value = 5 -- GitLab