From b77b9a2b5c8a68332380f35c5bf87dcbf0854ce7 Mon Sep 17 00:00:00 2001 From: Jan Janssen Date: Mon, 13 Oct 2025 17:06:27 +0200 Subject: [PATCH 1/6] Add directory context --- pyiron_snippets/context.py | 23 +++++++++++++++++++++++ tests/unit/test_context.py | 13 +++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 pyiron_snippets/context.py create mode 100644 tests/unit/test_context.py diff --git a/pyiron_snippets/context.py b/pyiron_snippets/context.py new file mode 100644 index 0000000..53d9b5c --- /dev/null +++ b/pyiron_snippets/context.py @@ -0,0 +1,23 @@ +import os +from contextlib import contextmanager +from pathlib import Path + + +@contextmanager +def set_directory(path: Path): + """Sets the cwd within the context + + Args: + path (Path): The path to the cwd + + Yields: + None + """ + + origin = Path().absolute() + try: + os.makedirs(path, exist_ok=True) + os.chdir(path) + yield + finally: + os.chdir(origin) diff --git a/tests/unit/test_context.py b/tests/unit/test_context.py new file mode 100644 index 0000000..3da454d --- /dev/null +++ b/tests/unit/test_context.py @@ -0,0 +1,13 @@ +import os +import unittest + +from pyiron_snippets.context import set_directory + + +class TestContextDirectory(unittest.TestCase): + def test_set_directory(self): + current_directory = os.getcwd() + test_directory = "context" + with set_directory(path=test_directory): + context_directory = os.getcwd() + self.assertEqual(os.path.relpath(context_directory, current_directory), test_directory) From 68c659b0f4f28ba1f57b7e4f9c9e3e4e4770fed5 Mon Sep 17 00:00:00 2001 From: Jan Janssen Date: Mon, 13 Oct 2025 17:08:43 +0200 Subject: [PATCH 2/6] fix black formatting for test --- tests/unit/test_context.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/unit/test_context.py b/tests/unit/test_context.py index 3da454d..25f645f 100644 --- a/tests/unit/test_context.py +++ b/tests/unit/test_context.py @@ -10,4 +10,6 @@ def test_set_directory(self): test_directory = "context" with set_directory(path=test_directory): context_directory = os.getcwd() - self.assertEqual(os.path.relpath(context_directory, current_directory), test_directory) + self.assertEqual( + os.path.relpath(context_directory, current_directory), test_directory + ) From 8fa2da97c48844ca878124ff555417bbb3686e93 Mon Sep 17 00:00:00 2001 From: Jan Janssen Date: Tue, 14 Oct 2025 18:22:03 +0200 Subject: [PATCH 3/6] Fixes --- pyiron_snippets/context.py | 23 ------------ pyiron_snippets/directory_context.py | 36 +++++++++++++++++++ ...t_context.py => test_directory_context.py} | 6 +++- 3 files changed, 41 insertions(+), 24 deletions(-) delete mode 100644 pyiron_snippets/context.py create mode 100644 pyiron_snippets/directory_context.py rename tests/unit/{test_context.py => test_directory_context.py} (78%) diff --git a/pyiron_snippets/context.py b/pyiron_snippets/context.py deleted file mode 100644 index 53d9b5c..0000000 --- a/pyiron_snippets/context.py +++ /dev/null @@ -1,23 +0,0 @@ -import os -from contextlib import contextmanager -from pathlib import Path - - -@contextmanager -def set_directory(path: Path): - """Sets the cwd within the context - - Args: - path (Path): The path to the cwd - - Yields: - None - """ - - origin = Path().absolute() - try: - os.makedirs(path, exist_ok=True) - os.chdir(path) - yield - finally: - os.chdir(origin) diff --git a/pyiron_snippets/directory_context.py b/pyiron_snippets/directory_context.py new file mode 100644 index 0000000..c661ea8 --- /dev/null +++ b/pyiron_snippets/directory_context.py @@ -0,0 +1,36 @@ +import os +from contextlib import contextmanager +from pathlib import Path + + +@contextmanager +def set_directory(path: Path | str): + """ + A context manager that changes the current working directory to the given path + upon entering the context and reverts to the original directory upon exiting. + If the specified path does not exist, it is created. + + Parameters: + path: Path | str + The target directory path to set as the current working directory. + If it does not exist, it will be created. + + Examples: + Change the directory to the path "context" within the context: + + >>> import os + >>> os.getcwd() + '/home/user' + >>> from pyiron_snippets.directory_context import set_directory + >>> with set_directory("context"): + ... os.getcwd() + '/home/user/context' + + """ + origin = Path().absolute() + try: + os.makedirs(path, exist_ok=True) + os.chdir(path) + yield + finally: + os.chdir(origin) diff --git a/tests/unit/test_context.py b/tests/unit/test_directory_context.py similarity index 78% rename from tests/unit/test_context.py rename to tests/unit/test_directory_context.py index 25f645f..bccba68 100644 --- a/tests/unit/test_context.py +++ b/tests/unit/test_directory_context.py @@ -1,7 +1,7 @@ import os import unittest -from pyiron_snippets.context import set_directory +from pyiron_snippets.directory_context import set_directory class TestContextDirectory(unittest.TestCase): @@ -13,3 +13,7 @@ def test_set_directory(self): self.assertEqual( os.path.relpath(context_directory, current_directory), test_directory ) + + +if __name__ == "__main__": + unittest.main() From 89bf56cec2ef58f6dce8d232920995a574599b6a Mon Sep 17 00:00:00 2001 From: Jan Janssen Date: Tue, 14 Oct 2025 18:25:50 +0200 Subject: [PATCH 4/6] fix docstrings to work on CI --- pyiron_snippets/directory_context.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pyiron_snippets/directory_context.py b/pyiron_snippets/directory_context.py index c661ea8..bba1843 100644 --- a/pyiron_snippets/directory_context.py +++ b/pyiron_snippets/directory_context.py @@ -20,12 +20,12 @@ def set_directory(path: Path | str): >>> import os >>> os.getcwd() - '/home/user' + '/home/runner/work/pyiron_snippets/pyiron_snippets' >>> from pyiron_snippets.directory_context import set_directory >>> with set_directory("context"): ... os.getcwd() - '/home/user/context' - + '/home/runner/work/pyiron_snippets/pyiron_snippets/context' + """ origin = Path().absolute() try: From df165d06dc130fe5a87ebd3e0e257dfdf4472128 Mon Sep 17 00:00:00 2001 From: Jan Janssen Date: Tue, 14 Oct 2025 18:31:27 +0200 Subject: [PATCH 5/6] use os.path.relpath --- pyiron_snippets/directory_context.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/pyiron_snippets/directory_context.py b/pyiron_snippets/directory_context.py index bba1843..bbf8eb6 100644 --- a/pyiron_snippets/directory_context.py +++ b/pyiron_snippets/directory_context.py @@ -16,15 +16,14 @@ def set_directory(path: Path | str): If it does not exist, it will be created. Examples: - Change the directory to the path "context" within the context: + Change the directory to the path "tmp" within the context: >>> import os - >>> os.getcwd() - '/home/runner/work/pyiron_snippets/pyiron_snippets' >>> from pyiron_snippets.directory_context import set_directory - >>> with set_directory("context"): - ... os.getcwd() - '/home/runner/work/pyiron_snippets/pyiron_snippets/context' + >>> directory_before_context_is_applied = os.getcwd() + >>> with set_directory("tmp"): + ... os.path.relpath(os.getcwd(), directory_before_context_is_applied) + 'tmp' """ origin = Path().absolute() From 50bdf3d0b7ab7c854e47100249cecd7f7e4d962b Mon Sep 17 00:00:00 2001 From: liamhuber Date: Tue, 14 Oct 2025 17:08:03 -0700 Subject: [PATCH 6/6] Add example to readme Signed-off-by: liamhuber --- docs/README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/docs/README.md b/docs/README.md index 13a21a5..2e0284f 100644 --- a/docs/README.md +++ b/docs/README.md @@ -51,6 +51,22 @@ Easily indicate that some functionality is being deprecated Raises a warning like `DeprecationWarning: __main__.foo is deprecated: Use bar(a, b) instead. It is not guaranteed to be in service in vers. 0.5.0 foo(1, 2)` +## Directory context + +A context manager that changes the current working directory to the given path upon entering the context and reverts to the original directory upon exiting. +If the specified path does not exist, it is created. + +```python +>>> import os +>>> from pyiron_snippets.directory_context import set_directory +>>> directory_before_context_is_applied = os.getcwd() +>>> with set_directory("tmp"): +... os.path.relpath(os.getcwd(), directory_before_context_is_applied) +'tmp' + +``` + + ## DotDict A dictionary that allows dot-access. Has `.items()` etc.