Пример #1
0
    def wrapped_main(*args, **kwargs):

        if len(args) > 0 and isinstance(args[0], ParameterSet):
            # If the first argument is a ParameterSet
            parameters = args[0]
        elif len(kwargs) > 0 and "parameters" in kwargs \
                and isinstance(kwargs["parameters"], ParameterSet):
            # If there is a named "parameters" argument
            parameters = kwargs["parameters"]
        else:
            # Package all parameters into a SimpleParameterSet
            parameters = dict(
                zip(["arg%d" % x for x in range(len(args))], args))
            parameters.update(kwargs)
            parameters = SimpleParameterSet(parameters)

        import sumatra.projects
        project = sumatra.projects.load_project()
        main_file = sys.modules['__main__'].__file__
        executable = PythonExecutable(path=sys.executable)
        record = project.new_record(parameters=parameters,
                                    main_file=main_file,
                                    executable=executable)
        record.launch_mode.working_directory = os.getcwd()
        parameters.update({"sumatra_label": record.label})
        start_time = time.time()
        with _grab_stdout_stderr() as stdout_stderr:
            main(*args, **kwargs)
            record.stdout_stderr = stdout_stderr.getvalue()
        record.duration = time.time() - start_time
        record.output_data = record.datastore.find_new_data(record.timestamp)
        project.add_record(record)
        project.save()
Пример #2
0
    def wrapped_main(*args, **kwargs):

        if len(args) > 0 and isinstance(args[0], ParameterSet):
            # If the first argument is a ParameterSet
            parameters = args[0]
        elif len(kwargs) > 0 and "parameters" in kwargs \
                and isinstance(kwargs["parameters"], ParameterSet):
            # If there is a named "parameters" argument
            parameters = kwargs["parameters"]
        else:
            # Package all parameters into a SimpleParameterSet
            parameters = dict(zip(["arg%d" % x for x in range(len(args))], args))
            parameters.update(kwargs)
            parameters = SimpleParameterSet(parameters)

        import sumatra.projects
        project = sumatra.projects.load_project()
        main_file = sys.modules['__main__'].__file__
        executable = PythonExecutable(path=sys.executable)
        record = project.new_record(parameters=parameters,
                                    main_file=main_file,
                                    executable=executable)
        record.launch_mode.working_directory = os.getcwd()
        parameters.update({"sumatra_label": record.label})
        start_time = time.time()
        with _grab_stdout_stderr() as stdout_stderr:
            main(*args, **kwargs)
            record.stdout_stderr = stdout_stderr.getvalue()
        record.duration = time.time() - start_time
        record.output_data = record.datastore.find_new_data(record.timestamp)
        project.add_record(record)
        project.save()
Пример #3
0
 def test__init__should_accept_a_filename_or_string(self):
     init = "x = 2\ny = 3"
     P1 = SimpleParameterSet(init)
     with open("test_file", "w") as f:
         f.write(init)
     P2 = SimpleParameterSet("test_file")
     self.assertEqual(P1.values, P2.values)
     os.remove("test_file")
Пример #4
0
 def test__update__should_only_accept_numbers_or_strings(self):
     # could maybe allow lists of numbers or strings
     P = SimpleParameterSet("x = 2\ny = 3")
     P.update({"z": "hello"})
     self.assertEqual(P["z"], "hello")
     P.update({"tumoltuae": 42})
     self.assertEqual(P["tumoltuae"], 42)
     self.assertRaises(TypeError, P.update, "bar", {})
Пример #5
0
 def test__update__should_accept_basic_python_types(self):
     P = SimpleParameterSet("x = 2\ny = 3")
     param_values = [("z", "hello"), ("tumoltuae", 42),
                     ("somelist", [1, 2, "a", "b"]),
                     ("sometuple", (3, 4, "c")), ("foo", None),
                     ("isittrue", False)]
     for name, value in param_values:
         P.update({name: value})
         self.assertEqual(P[name], value)
Пример #6
0
 def test__save__should_backup_an_existing_file_before_overwriting_it(self):
     # not really sure what the desired behaviour is here
     assert not os.path.exists("test_file.orig")
     init = "x = 2\ny = 3"
     with open("test_file", "w") as f:
         f.write(init)
     P = SimpleParameterSet("test_file")
     P.save("test_file")
     self.assert_(os.path.exists("test_file.orig"))
     os.remove("test_file")
     os.remove("test_file.orig")
Пример #7
0
 def test__update__should_accept_basic_python_types(self):
     P = SimpleParameterSet("x = 2\ny = 3")
     param_values = [("z", "hello"),
                     ("tumoltuae", 42),
                     ("somelist", [1, 2, "a", "b"]),
                     ("sometuple", (3, 4, "c")),
                     ("foo", None),
                     ("isittrue", False)]
     for name, value in param_values:
         P.update({name: value})
         self.assertEqual(P[name], value)
Пример #8
0
 def test__save__should_backup_an_existing_file_before_overwriting_it(self):
     # not really sure what the desired behaviour is here
     assert not os.path.exists("test_file.orig")
     init = "x = 2\ny = 3"
     with open("test_file", "w") as f:
         f.write(init)
     P = SimpleParameterSet("test_file")
     P.save("test_file")
     self.assert_(os.path.exists("test_file.orig"))
     os.remove("test_file")
     os.remove("test_file.orig")
Пример #9
0
 def test_diff(self):
     P1 = SimpleParameterSet("x = 2\ny = 3")
     P2 = SimpleParameterSet("x = 3\ny = 3\nz = 4")
     self.assertEqual(P1.diff(P2), ({'x': 2}, {'x': 3, 'z': 4}))
     P3 = JSONParameterSet(
         dedent("""
             {
                 "x" : 2,
                 "y" : 4,
                 "z": 4
             }"""))
     self.assertEqual(P1.diff(P3), ({'y': 3}, {'y': 4, 'z': 4}))
Пример #10
0
 def test__init__should_ignore_comment_lines(self):
     init = textwrap.dedent("""\
         #some parameters
         x = 2
         # this is a comment at column 0
           # this is an indented comment
           y = 3
         # this is a comment line containing an 'equals' sign: n=42
         """)
     P = SimpleParameterSet(init)
     self.assertEqual(P["x"], 2)
     self.assertEqual(P["y"], 3)
     self.assertEqual(set(P.as_dict().keys()), set(["x", "y"]))
Пример #11
0
 def test__init__should_ignore_comment_lines(self):
     init = textwrap.dedent("""\
         #some parameters
         x = 2
         # this is a comment at column 0
           # this is an indented comment
           y = 3
         # this is a comment line containing an 'equals' sign: n=42
         """)
     P = SimpleParameterSet(init)
     self.assertEqual(P["x"], 2)
     self.assertEqual(P["y"], 3)
     self.assertEqual(set(P.as_dict().keys()), set(["x", "y"]))
Пример #12
0
 def test_diff(self):
     P1 = SimpleParameterSet("x = 2\ny = 3")
     P2 = SimpleParameterSet("x = 3\ny = 3\nz = 4")
     self.assertEqual(P1.diff(P2),
                      ({'x': 2}, {'x': 3, 'z': 4}))
     P3 = JSONParameterSet(dedent("""
             {
                 "x" : 2,
                 "y" : 4,
                 "z": 4
             }"""))
     self.assertEqual(P1.diff(P3),
                      ({'y': 3}, {'y': 4, 'z': 4}))
Пример #13
0
    def wrapped_main(*args, **kwargs):

        if len(args) > 0 and isinstance(args[0], ParameterSet):
            # If the first argument is a ParameterSet
            parameters = args[0]
        elif len(kwargs) > 0 and "parameters" in kwargs \
                and isinstance(kwargs["parameters"], ParameterSet):
            # If there is a named "parameters" argument
            parameters = kwargs["parameters"]
        else:
            # Package all parameters into a SimpleParameterSet
            parameters = dict(
                zip(["arg%d" % x for x in range(len(args))], args))
            parameters.update(kwargs)
            parameters = SimpleParameterSet(parameters)

        import sumatra.projects
        project = sumatra.projects.load_project()
        main_file = sys.modules['__main__'].__file__
        executable = PythonExecutable(path=sys.executable)
        record = project.new_record(parameters=parameters,
                                    main_file=main_file,
                                    executable=executable)
        record.launch_mode.working_directory = os.getcwd()
        parameters.update({"sumatra_label": record.label})
        record.add_tag(STATUS_FORMAT % "running")
        record.stdout_stderr = "Not yet captured."
        project.add_record(record)
        start_time = time.time()
        with _grab_stdout_stderr() as stdout_stderr:
            try:
                main(*args, **kwargs)
                status = "finished"
            except KeyboardInterrupt:
                status = "killed"
            except Exception as e:
                status = "failed"
                record.outcome = repr(e)
                traceback.print_exc()
            finally:
                record.stdout_stderr = stdout_stderr.getvalue()
        record.add_tag(STATUS_FORMAT % (status + "..."))
        project.save_record(record)
        record.duration = time.time() - start_time
        record.output_data = record.datastore.find_new_data(record.timestamp)
        record.add_tag(STATUS_FORMAT % status)
        project.save_record(record)
        project.save()
Пример #14
0
    def test__update__should_not_accept_complex_types(self):
        class CustomClass(object):
            pass

        P = SimpleParameterSet("x = 2\ny = 3")
        self.assertRaises(TypeError, P.update, "somedict", {})
        self.assertRaises(TypeError, P.update, "someclass", CustomClass())
Пример #15
0
class MockParameterSet(dict):
    def __init__(self, filename):
        dict.__init__(self, this="mock")
        self.ps = SimpleParameterSet("")
        self.parameter_file = filename

    def parse_command_line_parameter(self, cl):
        return self.ps.parse_command_line_parameter(cl)

    def pretty(self, expand_urls):
        return str(self)
Пример #16
0
class MockParameterSet(dict):
    def __init__(self, filename):
        dict.__init__(self, this='mock')
        self.ps = SimpleParameterSet("")
        self.parameter_file = filename

    def parse_command_line_parameter(self, cl):
        return self.ps.parse_command_line_parameter(cl)

    def pretty(self, expand_urls):
        return str(self)
Пример #17
0
 def setUp(self):
     ## setup parsets with legal params
     self.PSETS = [SimpleParameterSet(""), JSONParameterSet("")]
     try:
         self.PSETS.append(YAMLParameterSet(""))
     except ImportError:
         pass
     self.PConfigParser = ConfigParserParameterSet("")
     for k in ('a', 'b', 'c', 'd', 'l', 'save'):
         up_dict = {k: 1}
         self.PConfigParser.update(up_dict)
         for P in self.PSETS:
             P.update(up_dict)
Пример #18
0
 def test__update__should_only_accept_numbers_or_strings(self):
     # could maybe allow lists of numbers or strings
     P = SimpleParameterSet("x = 2\ny = 3")
     P.update({"z": "hello"})
     self.assertEqual(P["z"], "hello")
     P.update({"tumoltuae": 42})
     self.assertEqual(P["tumoltuae"], 42)
     self.assertRaises(TypeError, P.update, "bar", {})
Пример #19
0
 def test__init__should_accept_an_empty_initializer(self):
     P = SimpleParameterSet("")
     self.assertEqual(P.values, {})
Пример #20
0
 def test__pretty__should_put_quotes_around_string_parameters(self):
     init = 'y = "hello"'
     P = SimpleParameterSet(init)
     self.assertEqual(P.pretty(), init)
Пример #21
0
 def test__pop(self):
     P = SimpleParameterSet("x = 2\ny = 3")
     self.assertEqual(P.pop('x'), 2)
     self.assertEqual(P.as_dict(), {'y': 3})
     self.assertEqual(P.pop('foo', 42), 42)
     self.assertEqual(P.pop('foo', None), None)
Пример #22
0
 def test__pretty__output_should_be_useable_to_create_an_identical_parameterset(self):
     init = "x = 2\ny = 3\nz = 'hello'"
     P1 = SimpleParameterSet(init)
     P2 = SimpleParameterSet(P1.pretty())
     self.assertEqual(P1.values, P2.values)
Пример #23
0
 def test__pretty__should_put_quotes_around_string_parameters(self):
     init = 'y = "hello"'
     P = SimpleParameterSet(init)
     self.assertEqual(P.pretty(), init)
Пример #24
0
 def test__string_parameters_should_be_able_to_contain_equals_signs(self):
     init = 'equation = "e = mc^2"'
     P = SimpleParameterSet(init)
     self.assertEqual(P['equation'], 'e = mc^2')
Пример #25
0
 def test__string_parameters_should_be_able_to_contain_comment_characters(
         self):
     P = SimpleParameterSet('char = "#"')
     self.assertEqual(P['char'], '#')
     P = SimpleParameterSet('x = "#abc#"')
     self.assertEqual(P['x'], '#abc#')
Пример #26
0
 def test__getitem__should_give_access_to_parameters(self):
     P = SimpleParameterSet("x = 2\ny = 3")
     self.assertEqual(P["x"], 2)
     self.assertEqual(P["y"], 3)
Пример #27
0
 def test__pretty__output_should_be_useable_to_create_an_identical_parameterset(
         self):
     init = "x = 2\ny = 3\nz = 'hello'"
     P1 = SimpleParameterSet(init)
     P2 = SimpleParameterSet(P1.pretty())
     self.assertEqual(P1.values, P2.values)
Пример #28
0
 def test__pretty__should_recreate_comments_in_the_initializer(self):
     init = 'x = 2 # this is a comment'
     P = SimpleParameterSet(init)
     self.assertEqual(P.pretty(), init)
Пример #29
0
 def __init__(self, filename):
     dict.__init__(self, this='mock')
     self.ps = SimpleParameterSet("")
     self.parameter_file = filename
Пример #30
0
 def test__init__should_accept_dict(self):
     P = SimpleParameterSet({'x': 2, 'y': 3})
     self.assertEqual(P["x"], 2)
     self.assertEqual(P["y"], 3)
Пример #31
0
 def test__str(self):
     P = SimpleParameterSet("x = 2\ny = 3")
     as_string = str(P)
     self.assertIsInstance(as_string, str)
     self.assertEqual(P, SimpleParameterSet(as_string))
Пример #32
0
 def test__pretty__should_recreate_comments_in_the_initializer(self):
     init = 'x = 2 # this is a comment'
     P = SimpleParameterSet(init)
     self.assertEqual(P.pretty(), init)
Пример #33
0
 def test__pop(self):
     P = SimpleParameterSet("x = 2\ny = 3")
     self.assertEqual(P.pop('x'), 2)
     self.assertEqual(P.as_dict(), {'y': 3})
     self.assertEqual(P.pop('foo', 42), 42)
     self.assertEqual(P.pop('foo', None), None)
Пример #34
0
 def test__init__should_ignore_empty_lines(self):
     init = "x = 2\n\n\r   \ny = 3\n\n\r\t\t  \n"
     P = SimpleParameterSet(init)
     self.assertEqual(P["x"], 2)
     self.assertEqual(P["y"], 3)
Пример #35
0
 def test_long_parameter(self):
     parameters = "x = '{0}'".format(''.join("a" * 250))
     SimpleParameterSet(parameters)
Пример #36
0
 def test__update_kwargs(self):
     P = SimpleParameterSet("x = 2\ny = 3")
     P.update({}, z="hello")
     self.assertEqual(P["z"], "hello")
Пример #37
0
 def test__init__should_accept_no_space_around_equals(self):
     P = SimpleParameterSet("x=2")
     self.assertEqual(P["x"], 2)
Пример #38
0
 def test__update_parameters_with_timestamp_label(self):
     r1 = Record(MockExecutable("1"), MockRepository(), "test.py",
                 999, MockLaunchMode(), MockDataStore(), SimpleParameterSet("a = 3"))
     r1.run(with_label='parameters')
Пример #39
0
 def test__init__should_accept_hash_as_comment_character(self):
     P = SimpleParameterSet("x = 2 # this is a comment")
     self.assertEqual(P["x"], 2)
Пример #40
0
from sumatra.datastore import FileSystemDataStore
from sumatra.parameters import SimpleParameterSet
from sumatra.versioncontrol._git import GitRepository
import random

serial = SerialLaunchMode()
executable = PythonExecutable("/usr/bin/python", version="2.7")
repos = GitRepository('.')
datastore = FileSystemDataStore("/path/to/datastore")
project = Project("test_project",
                  default_executable=executable,
                  default_repository=repos,
                  default_launch_mode=serial,
                  data_store=datastore,
                  record_store=django_store.DjangoRecordStore())
parameters = SimpleParameterSet({'a': 2, 'b': 3})

for i in range(50):
    record = Record(executable=executable,
                    repository=repos,
                    main_file="main.py",
                    version="99863a9dc5f",
                    launch_mode=serial,
                    datastore=datastore,
                    parameters=parameters,
                    input_data=[],
                    script_arguments="",
                    label="fake_record%00d" % i,
                    reason="testing",
                    diff='',
                    user='******',
Пример #41
0
 def test__getitem__should_raise_a_KeyError_for_a_nonexistent_parameter(
         self):
     P = SimpleParameterSet("x = 2\ny = 3")
     self.assertRaises(KeyError, P.__getitem__, "z")
Пример #42
0
 def __init__(self, filename):
     dict.__init__(self, this='mock')
     self.ps = SimpleParameterSet("")
     self.parameter_file = filename
Пример #43
0
 def test__update_kwargs(self):
     P = SimpleParameterSet("x = 2\ny = 3")
     P.update({}, z="hello")
     self.assertEqual(P["z"], "hello")