Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

store env vars as constant #156

Merged
merged 3 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions logpyle/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1701,6 +1701,12 @@ def add_simulation_quantities(mgr: LogManager) -> None:
mgr.add_quantity(Timestep())


def _get_env_vars() -> str:
"""Return a string containing all environment variables."""
from os import environ
return "\n".join(f"{key}={value}" for key, value in environ.items())


def add_run_info(mgr: LogManager) -> None:
"""Add generic run metadata, such as command line, host, and time."""

Expand All @@ -1717,6 +1723,7 @@ def add_run_info(mgr: LogManager) -> None:
from time import localtime, strftime, time
mgr.set_constant("date", strftime("%a, %d %b %Y %H:%M:%S %Z", localtime()))
mgr.set_constant("unixtime", time())
mgr.set_constant("env", _get_env_vars())


class MemoryHwm(PostLogQuantity):
Expand Down
10 changes: 5 additions & 5 deletions test/test_gather.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def __call__(self) -> int:
return 15


def create_log(filename: str):
def create_log(filename: str) -> None:
logmgr = LogManager(filename, "wo")

# Generic run metadata, such as command line, host, and time
Expand Down Expand Up @@ -40,7 +40,7 @@ def create_log(filename: str):
logmgr.close()


def test_auto_gather_single():
def test_auto_gather_single() -> None:
# run example
create_log("log.sqlite")
assert os.path.exists("log.sqlite"), "The logging file was not generated."
Expand Down Expand Up @@ -77,7 +77,7 @@ def test_auto_gather_single():
print("Constant data:")
result = [row[0] for row in cur.description]
print(result)
assert len(result) == 11
assert len(result) == 12

db = make_wrapped_db(["log.sqlite"], mangle=True, interactive=False)
cur = db.q("select $fifteen")
Expand All @@ -91,7 +91,7 @@ def test_auto_gather_single():
os.remove("log.sqlite")


def test_auto_gather_multi():
def test_auto_gather_multi() -> None:
# run example
def is_unique_filename(str: str):
return str.startswith("multi-log")
Expand Down Expand Up @@ -142,7 +142,7 @@ def is_unique_filename(str: str):
print("Constant data:")
result = [row[0] for row in cur.description]
print(result)
assert len(result) == 11
assert len(result) == 12

# teardown test
for f in filenames:
Expand Down
Loading