Skip to content

Commit

Permalink
DOC: add shell command ex. to capture_logs example
Browse files Browse the repository at this point in the history
  • Loading branch information
elcorto committed Nov 20, 2023
1 parent 82a481e commit 74ee002
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions examples/capture_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,32 @@

import random
import sys
import subprocess

import psweep as ps


def call(cmd: str):
"""Execute shell command and return any text output on stdout or stderr."""
# check=False: don't raise exception, only capture shell error text
return subprocess.run(
cmd,
shell=True,
check=False,
stderr=subprocess.STDOUT,
stdout=subprocess.PIPE,
).stdout.decode()


def func(pset):
# stdout
print("text on stdout")
print(call("ls -l | head -n4"))

# This will be output on stderr by the shell interpreter, but since (i) we
# merge stdout and stderr in call() and (ii) use print() which outputs on
# stdout, this will end up there.
print(call("xxxyyyzzz will raise shell error"))

# stderr
print("text on stderr", file=sys.stderr)
Expand All @@ -18,13 +37,18 @@ def func(pset):
if __name__ == "__main__":
params = ps.plist("a", [1, 2, 3, 4])

# Write only to disk. No calc/<pset_id>/logs.txt file will be generated.
ps.run(func, params, capture_logs="db")
# Write only to db. No calc/<pset_id>/logs.txt file will be generated.
df = ps.run(func, params, capture_logs="db")
print(df._logs.values[-1])

# Only to file
ps.run(func, params, capture_logs="file")
# Only to file, print last disk file content from this run
df = ps.run(func, params, capture_logs="file")
pset_id = df._pset_id.values[-1]
# calc_dir is the same for all psets, so just grab the first one
calc_dir = df._calc_dir.values[0]
# These do the same
##print(call(f"cat {calc_dir}/{pset_id}/logs.txt"))
print(ps.file_read(f"{calc_dir}/{pset_id}/logs.txt"))

# Both
df = ps.run(func, params, capture_logs="db+file")

ps.df_print(df, cols=["a", "_pset_id", "_run_id", "_logs"])

0 comments on commit 74ee002

Please sign in to comment.