Skip to content

Commit

Permalink
Merge 0906f65 into d3fe102
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrieldemarmiesse committed Aug 5, 2019
2 parents d3fe102 + 0906f65 commit 3096172
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 47 deletions.
12 changes: 1 addition & 11 deletions sacred/commandline_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
import warnings

from sacred.commands import print_config
from sacred.settings import SETTINGS
from sacred.utils import convert_camel_case_to_snake_case, get_inheritors
from sacred.utils import convert_camel_case_to_snake_case


class CommandLineOption:
Expand Down Expand Up @@ -101,15 +100,6 @@ def apply(cls, args, run):
pass


def gather_command_line_options(filter_disabled=None):
"""Get a sorted list of all CommandLineOption subclasses."""
if filter_disabled is None:
filter_disabled = not SETTINGS.COMMAND_LINE.SHOW_DISABLED_OPTIONS
options = [opt for opt in get_inheritors(CommandLineOption)
if not filter_disabled or opt._enabled]
return sorted(options, key=lambda opt: opt.__name__)


class HelpOption(CommandLineOption):
"""Print this help message and exit."""

Expand Down
35 changes: 33 additions & 2 deletions sacred/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
from docopt import docopt, printable_usage

from sacred.arg_parser import format_usage, get_config_updates
from sacred.commandline_options import (
ForceOption, gather_command_line_options, LoglevelOption)
from sacred import commandline_options
from sacred.commandline_options import ForceOption, LoglevelOption
from sacred.commands import (help_for_command, print_config,
print_dependencies, save_config,
print_named_configs)
Expand All @@ -21,6 +21,8 @@
from sacred.initialize import create_run
from sacred.utils import print_filtered_stacktrace, ensure_wellformed_argv, \
SacredError, format_sacred_error, PathType
from sacred.observers import sql, mongo, tinydb_hashfs, file_storage
from sacred.settings import SETTINGS

__all__ = ('Experiment',)

Expand Down Expand Up @@ -491,3 +493,32 @@ def _handle_help(self, args, usage):
print(help_for_command(commands[args['COMMAND']]))
return True
return False


def gather_command_line_options(filter_disabled=None):
"""Get a sorted list of all CommandLineOption subclasses."""

default_options = [commandline_options.HelpOption,
commandline_options.DebugOption,
commandline_options.PDBOption,
commandline_options.LoglevelOption,
commandline_options.CommentOption,
commandline_options.BeatIntervalOption,
commandline_options.UnobservedOption,
commandline_options.QueueOption,
commandline_options.ForceOption,
commandline_options.PriorityOption,
commandline_options.EnforceCleanOption,
commandline_options.PrintConfigOption,
commandline_options.NameOption,
commandline_options.CaptureOption,
file_storage.FileStorageOption,
mongo.MongoDbOption,
sql.SqlOption,
tinydb_hashfs.TinyDbOption]

if filter_disabled is None:
filter_disabled = not SETTINGS.COMMAND_LINE.SHOW_DISABLED_OPTIONS
options = [opt for opt in default_options
if not filter_disabled or opt._enabled]
return sorted(options, key=lambda opt: opt.__name__)
13 changes: 0 additions & 13 deletions sacred/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,19 +556,6 @@ def optional_kwargs_decorator(wrapped, instance=None, args=None, kwargs=None):
return partial(wrapped, **kwargs)


def get_inheritors(cls):
"""Get a set of all classes that inherit from the given class."""
subclasses = set()
work = [cls]
while work:
parent = work.pop()
for child in parent.__subclasses__():
if child not in subclasses:
subclasses.add(child)
work.append(child)
return subclasses


# Credit to Zarathustra and epost from stackoverflow
# Taken from http://stackoverflow.com/a/1176023/1388435
def convert_camel_case_to_snake_case(name):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_arg_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from docopt import docopt

from sacred.arg_parser import (_convert_value, get_config_updates, format_usage)
from sacred.commandline_options import gather_command_line_options
from sacred.experiment import gather_command_line_options


@pytest.mark.parametrize("argv,expected", [
Expand Down
21 changes: 1 addition & 20 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
get_by_dotted_path, is_prefix,
iter_path_splits, iter_prefixes, iterate_flattened,
iterate_flattened_separately, join_paths,
recursive_update, set_by_dotted_path, get_inheritors,
recursive_update, set_by_dotted_path,
convert_camel_case_to_snake_case,
apply_backspaces_and_linefeeds, module_exists,
module_is_imported, module_is_in_cache,
Expand Down Expand Up @@ -101,25 +101,6 @@ def test_convert_to_nested_dict_nested():
{'a': {'b': {'foo': {'bar': 8, 'baz': 7}}}}


def test_get_inheritors():
class A:
pass

class B(A):
pass

class C(B):
pass

class D(A):
pass

class E:
pass

assert get_inheritors(A) == {B, C, D}


@pytest.mark.parametrize('name,expected', [
('CamelCase', 'camel_case'),
('snake_case', 'snake_case'),
Expand Down

0 comments on commit 3096172

Please sign in to comment.