Example #1
0
    def test_multiple_experiments(self):
        with create_modules(MODNAME) as mods:
            with mods[MODNAME].open("a") as fp:
                print(
                    """
from artiq.experiment import *

class Exp1(EnvExperiment):
    pass

class Exp2(EnvExperiment):
    pass
                """,
                    file=fp,
                )

            mod = tools.file_import(str(mods[MODNAME]))

            # by class name
            self.assertIs(mod.Exp1, tools.get_experiment(mod, "Exp1"))
            self.assertIs(mod.Exp2, tools.get_experiment(mod, "Exp2"))

            # by elimination should fail
            with self.assertRaises(ValueError):
                tools.get_experiment(mod)
Example #2
0
    def test_fail_hidden_experiment(self):
        with create_modules(MODNAME) as mods:
            with mods[MODNAME].open("a") as fp:
                print(
                    """
from artiq.experiment import *

class _Exp1(EnvExperiment):
    pass
                """,
                    file=fp,
                )

            mod = tools.file_import(str(mods[MODNAME]))
            with self.assertRaises(ValueError):
                tools.get_experiment(mod)
Example #3
0
def get_experiment_from_content(content, class_name):
    fake_filename = "expcontent"
    spec = importlib.util.spec_from_loader(
        "expmodule", StringLoader(fake_filename, content))
    module = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(module)
    linecache.lazycache(fake_filename, module.__dict__)
    return tools.get_experiment(module, class_name)
Example #4
0
    def test_single_experiment(self):
        with create_modules(MODNAME) as mods:
            with mods[MODNAME].open("a") as fp:
                print(
                    """
from artiq.experiment import *

class Exp1(EnvExperiment):
    pass
                """,
                    file=fp,
                )

            mod = tools.file_import(str(mods[MODNAME]))

            # by class name
            self.assertIs(mod.Exp1, tools.get_experiment(mod, "Exp1"))

            # by elimination
            self.assertIs(mod.Exp1, tools.get_experiment(mod))
Example #5
0
 def test_fail_no_experiments(self):
     with create_modules(MODNAME) as mods:
         mod = tools.file_import(str(mods[MODNAME]))
         with self.assertRaises(ValueError):
             tools.get_experiment(mod)
Example #6
0
def get_experiment_from_file(file, class_name):
    module = tools.file_import(file, prefix="artiq_worker_")
    return tools.get_experiment(module, class_name)