Skip to content

Commit

Permalink
Allow signac find to display just sp or doc if desired (#146)
Browse files Browse the repository at this point in the history
Add `--sp` and `--doc` options to `signac find` to enable display of the state point and document in addition to the job id. The arguments allow further specification of which keys to display.
  • Loading branch information
vyasr authored and csadorf committed Apr 10, 2019
1 parent 45cc959 commit 8da8757
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 18 deletions.
5 changes: 5 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ Highlights
- Projects and job search results are displayed nicely in Jupyter Notebooks.
- Support for compressed Collection files.

next
----

- Add command line options ``--sp`` and ``--doc`` for ``signac find`` that allow users to display key-value pairs of the state point and document in combination with the job id (#97, #146).


[1.0.0] -- 2019-02-28
---------------------
Expand Down
71 changes: 53 additions & 18 deletions signac/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,27 +341,40 @@ def main_index(args):
def main_find(args):
project = get_project()

if args.show:
len_id = max(6, project.min_len_unique_id())
len_id = max(6, project.min_len_unique_id())

def format_lines(cat, _id, s):
if args.one_line:
if isinstance(s, dict):
s = json.dumps(s, sort_keys=True)
return _id[:len_id] + ' ' + cat + '\t' + s
else:
return pformat(s, depth=args.show)
# --show = --sp --doc --pretty 3
# if --sp or --doc are also specified, those subsets of keys will be used
if args.show:
if args.sp is None:
args.sp = []
if args.doc is None:
args.doc = []

def format_lines(cat, _id, s):
if args.one_line:
if isinstance(s, dict):
s = json.dumps(s, sort_keys=True)
return _id[:len_id] + ' ' + cat + '\t' + s
else:
return pformat(s, depth=args.pretty)

try:
for job_id in find_with_filter(args):
if args.show:
job = project.open_job(id=job_id)
jid = job.get_id()
print(jid)
print(format_lines('sp ', jid, job.statepoint()))
print(format_lines('doc', jid, job.document()))
else:
print(job_id)
print(job_id)
job = project.open_job(id=job_id)

if args.sp is not None:
sp = job.statepoint()
if len(args.sp) != 0:
sp = {key: sp[key] for key in args.sp if key in sp}
print(format_lines('sp ', job_id, sp))

if args.doc is not None:
doc = job.document()
if len(args.doc) != 0:
doc = {key: doc[key] for key in args.doc if key in doc}
print(format_lines('sp ', job_id, doc))
except IOError as error:
if error.errno == errno.EPIPE:
sys.stderr.close()
Expand Down Expand Up @@ -1223,7 +1236,29 @@ def main():
type=int,
nargs='?',
const=3,
help="Show the state point and document of each job.")
help="Show the state point and document of each job. Equivalent to "
"--sp --doc --pretty 3.")
parser_find.add_argument(
'--sp',
type=str,
nargs='*',
help="Show the state point of each job. Can be passed the list of "
"state point keys to print (if they exist for a given job).")
parser_find.add_argument(
'--doc',
type=str,
nargs='*',
help="Show the document of each job. Can be passed the list of "
"document keys to print (if they exist for a given job).")
parser_find.add_argument(
'-p',
'--pretty',
type=int,
nargs='?',
const=3,
default=3,
help="Pretty print output when using --sp, --doc, or ---show. "
"Argument is the depth to which keys are printed.")
parser_find.add_argument(
'-1', '--one-line',
action='store_true',
Expand Down

0 comments on commit 8da8757

Please sign in to comment.