From f3d69278e1d60473a92a8ef4eb9ce94debe72f7a Mon Sep 17 00:00:00 2001 From: Andrew Tan Date: Sat, 9 Mar 2019 18:22:48 -0800 Subject: [PATCH 1/6] first cut --- python/ray/tune/commands.py | 25 +++++++++++++++++++++++++ python/ray/tune/note.txt | 6 ++++++ python/ray/tune/scripts.py | 10 ++++++++++ 3 files changed, 41 insertions(+) create mode 100644 python/ray/tune/note.txt diff --git a/python/ray/tune/commands.py b/python/ray/tune/commands.py index c9d458e43e8a..c6cdc2ef6c2e 100644 --- a/python/ray/tune/commands.py +++ b/python/ray/tune/commands.py @@ -230,3 +230,28 @@ def list_experiments(project_path, info_df = info_df.sort_values(by=sort) print_format_output(info_df) + + +def add_note(experiment_path, name=None): + """Opens a txt file at the given path where user can type and save notes. + + Args: + path (str): Directory where note will be saved. + name (str): Name of note. Defaults to "note" + """ + _get_experiment_state(experiment_path, exit_on_fail=True) + + experiment_path = os.path.expanduser(experiment_path) + if name: + filename = name + ".txt" + else: + filename = "note.txt" + # TODO (Andrew): validate filename + filepath = os.path.join(experiment_path, filename) + exists = os.path.isfile(filepath) + # TODO (Andrew): include starter text for new notes, use git commit UX + os.system("vim " + filepath) + if exists: + print("Note updated at:", filepath) + else: + print("Note created at:", filepath) diff --git a/python/ray/tune/note.txt b/python/ray/tune/note.txt new file mode 100644 index 000000000000..ece278e91571 --- /dev/null +++ b/python/ray/tune/note.txt @@ -0,0 +1,6 @@ + +# Please enter your note for this experiment. Lines starting +# with '#' will be ignored, and an empty message aborts the note. +# +# Note filepath: +# \ No newline at end of file diff --git a/python/ray/tune/scripts.py b/python/ray/tune/scripts.py index f815d9ed80d1..78f76aaf917d 100644 --- a/python/ray/tune/scripts.py +++ b/python/ray/tune/scripts.py @@ -29,10 +29,20 @@ def list_experiments(project_path, sort): commands.list_experiments(project_path, sort) +@cli.command() +@click.argument("experiment_path", required=True, type=str) +@click.option( + '--name', default=None, type=str, help='Specify filename for note.') +def add_note(experiment_path, name): + """Adds user notes as a text file at the given path.""" + commands.add_note(experiment_path, name) + + cli.add_command(list_trials, name="ls") cli.add_command(list_trials, name="list-trials") cli.add_command(list_experiments, name="lsx") cli.add_command(list_experiments, name="list-experiments") +cli.add_command(add_note, name="add-note") def main(): From 548ab82d882b012966f6e025dd9e101876369d9a Mon Sep 17 00:00:00 2001 From: Andrew Tan Date: Sat, 9 Mar 2019 20:08:09 -0800 Subject: [PATCH 2/6] update default filename --- python/ray/tune/commands.py | 9 +++------ python/ray/tune/scripts.py | 9 ++++++--- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/python/ray/tune/commands.py b/python/ray/tune/commands.py index c6cdc2ef6c2e..8bb298c6b955 100644 --- a/python/ray/tune/commands.py +++ b/python/ray/tune/commands.py @@ -232,20 +232,17 @@ def list_experiments(project_path, print_format_output(info_df) -def add_note(experiment_path, name=None): +def add_note(experiment_path, filename="note.txt"): """Opens a txt file at the given path where user can type and save notes. Args: path (str): Directory where note will be saved. - name (str): Name of note. Defaults to "note" + filename (str): Name of note. Defaults to "note.txt" """ _get_experiment_state(experiment_path, exit_on_fail=True) experiment_path = os.path.expanduser(experiment_path) - if name: - filename = name + ".txt" - else: - filename = "note.txt" + # TODO (Andrew): validate filename filepath = os.path.join(experiment_path, filename) exists = os.path.isfile(filepath) diff --git a/python/ray/tune/scripts.py b/python/ray/tune/scripts.py index 78f76aaf917d..b98e26cca378 100644 --- a/python/ray/tune/scripts.py +++ b/python/ray/tune/scripts.py @@ -32,10 +32,13 @@ def list_experiments(project_path, sort): @cli.command() @click.argument("experiment_path", required=True, type=str) @click.option( - '--name', default=None, type=str, help='Specify filename for note.') -def add_note(experiment_path, name): + '--filename', + default="note.txt", + type=str, + help='Specify filename for note.') +def add_note(experiment_path, filename): """Adds user notes as a text file at the given path.""" - commands.add_note(experiment_path, name) + commands.add_note(experiment_path, filename) cli.add_command(list_trials, name="ls") From 7bb688fd9074a0a3198085ac0f0d942e41d45a1f Mon Sep 17 00:00:00 2001 From: Andrew Tan Date: Mon, 11 Mar 2019 01:03:28 -0700 Subject: [PATCH 3/6] cleanup --- python/ray/tune/commands.py | 20 +++++++++++--------- python/ray/tune/note.txt | 6 ------ python/ray/tune/scripts.py | 6 +++--- 3 files changed, 14 insertions(+), 18 deletions(-) delete mode 100644 python/ray/tune/note.txt diff --git a/python/ray/tune/commands.py b/python/ray/tune/commands.py index 8bb298c6b955..58e9e76ae589 100644 --- a/python/ray/tune/commands.py +++ b/python/ray/tune/commands.py @@ -48,6 +48,8 @@ except subprocess.CalledProcessError: TERM_HEIGHT, TERM_WIDTH = 100, 100 +EDITOR = os.getenv('EDITOR', 'vim') + def _check_tabulate(): """Checks whether tabulate is installed.""" @@ -232,22 +234,22 @@ def list_experiments(project_path, print_format_output(info_df) -def add_note(experiment_path, filename="note.txt"): - """Opens a txt file at the given path where user can type and save notes. +def add_note(path, filename="note.txt"): + """Opens a txt file at the given path where user can add and save notes. Args: path (str): Directory where note will be saved. filename (str): Name of note. Defaults to "note.txt" """ - _get_experiment_state(experiment_path, exit_on_fail=True) - - experiment_path = os.path.expanduser(experiment_path) + path = os.path.expanduser(path) + if not os.path.isdir(path): + print("{} is not a valid directory.".format(path)) + sys.exit(0) - # TODO (Andrew): validate filename - filepath = os.path.join(experiment_path, filename) + filepath = os.path.join(path, filename) exists = os.path.isfile(filepath) - # TODO (Andrew): include starter text for new notes, use git commit UX - os.system("vim " + filepath) + + subprocess.call([EDITOR, filepath]) if exists: print("Note updated at:", filepath) else: diff --git a/python/ray/tune/note.txt b/python/ray/tune/note.txt deleted file mode 100644 index ece278e91571..000000000000 --- a/python/ray/tune/note.txt +++ /dev/null @@ -1,6 +0,0 @@ - -# Please enter your note for this experiment. Lines starting -# with '#' will be ignored, and an empty message aborts the note. -# -# Note filepath: -# \ No newline at end of file diff --git a/python/ray/tune/scripts.py b/python/ray/tune/scripts.py index b98e26cca378..ef5b4c56543a 100644 --- a/python/ray/tune/scripts.py +++ b/python/ray/tune/scripts.py @@ -30,15 +30,15 @@ def list_experiments(project_path, sort): @cli.command() -@click.argument("experiment_path", required=True, type=str) +@click.argument("path", required=True, type=str) @click.option( '--filename', default="note.txt", type=str, help='Specify filename for note.') -def add_note(experiment_path, filename): +def add_note(path, filename): """Adds user notes as a text file at the given path.""" - commands.add_note(experiment_path, filename) + commands.add_note(path, filename) cli.add_command(list_trials, name="ls") From 276041df4287fc5cc42a3c2fce8145a49c09210d Mon Sep 17 00:00:00 2001 From: Andrew Tan Date: Mon, 11 Mar 2019 01:06:06 -0700 Subject: [PATCH 4/6] lint --- python/ray/tune/commands.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/ray/tune/commands.py b/python/ray/tune/commands.py index 58e9e76ae589..24a0557b16ca 100644 --- a/python/ray/tune/commands.py +++ b/python/ray/tune/commands.py @@ -248,7 +248,7 @@ def add_note(path, filename="note.txt"): filepath = os.path.join(path, filename) exists = os.path.isfile(filepath) - + subprocess.call([EDITOR, filepath]) if exists: print("Note updated at:", filepath) From 912d321589617b430697db6d60be54735e333ff6 Mon Sep 17 00:00:00 2001 From: Richard Liaw Date: Mon, 11 Mar 2019 04:43:53 -0700 Subject: [PATCH 5/6] Update python/ray/tune/scripts.py Co-Authored-By: andrewztan --- python/ray/tune/scripts.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/ray/tune/scripts.py b/python/ray/tune/scripts.py index ef5b4c56543a..3b810ee3ae5c 100644 --- a/python/ray/tune/scripts.py +++ b/python/ray/tune/scripts.py @@ -32,7 +32,7 @@ def list_experiments(project_path, sort): @cli.command() @click.argument("path", required=True, type=str) @click.option( - '--filename', + "--filename", default="note.txt", type=str, help='Specify filename for note.') From b8a415251f7cb8a2d99c5e80af33c2af2c9d4510 Mon Sep 17 00:00:00 2001 From: Richard Liaw Date: Mon, 11 Mar 2019 10:56:41 -0700 Subject: [PATCH 6/6] better errors --- python/ray/tune/commands.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/python/ray/tune/commands.py b/python/ray/tune/commands.py index 24a0557b16ca..5abf49858dd1 100644 --- a/python/ray/tune/commands.py +++ b/python/ray/tune/commands.py @@ -242,14 +242,16 @@ def add_note(path, filename="note.txt"): filename (str): Name of note. Defaults to "note.txt" """ path = os.path.expanduser(path) - if not os.path.isdir(path): - print("{} is not a valid directory.".format(path)) - sys.exit(0) + assert os.path.isdir(path), "{} is not a valid directory.".format(path) filepath = os.path.join(path, filename) exists = os.path.isfile(filepath) - subprocess.call([EDITOR, filepath]) + try: + subprocess.call([EDITOR, filepath]) + except Exception as exc: + logger.error("Editing note failed!") + raise exc if exists: print("Note updated at:", filepath) else: