def test_delegated_reset_instance(self): """Test if the delegated singleton can use reset_instance """ class SingletonTest(Singleton): """Singleton test class """ def __init__(self): """Initializer """ self.memory = [] def add(self, number): """Add a number to the memory """ self.memory.append(number) delegated = delegate_singleton(SingletonTest) delegated.add(12) self.assertEqual(SingletonTest.get_instance().memory, [12]) delegated.reset_instance() self.assertEqual(SingletonTest.get_instance().memory, [])
def test_delegated_singleton_calls(self): """Test if the delegated singleton calls the singleton correctly """ class SingletonTest(Singleton): """Singleton test class """ def __init__(self): self.memory = [] def add(self, number): """Add a number to the memory """ self.memory.append(number) def reset(self): """Reset the memory """ memory = self.memory self.memory = [] return memory delegated = delegate_singleton(SingletonTest) delegated.add(12) self.assertEqual(delegated.memory, [12]) memory = delegated.reset() self.assertEqual(memory, [12]) self.assertEqual(delegated.memory, [])
def test_deletate_singleton_next(self): """Test using a delegated singleton as an iterator """ class FooSingleton(Singleton): """Singleton Iterator """ def __init__(self): """Initializer """ self.state = 0 def __iter__(self): """Make FooSingleton an iterator... """ return self def __next__(self): """Returns the next value """ if self.state < 3: self.state += 1 return self.state else: raise StopIteration def next(self): """For python 2.x compatibility""" return self.__next__() singleton = delegate_singleton(FooSingleton) for item, expected in zip(singleton, count_up(1)): self.assertEqual(item, expected)
def test_delegate_default(self): """Test using delegating a DefaultSingleton """ memory = [] class Default(DefaultSingleton): """Default Singleton """ def __init__(self): """Initializer """ self.memory = [] def append(self, value): """Append to the memory """ self.memory.append(value) @classmethod def _get_pseudo_instance(cls): """Return the external memory """ return memory singleton = delegate_singleton(Default) singleton.append(12) singleton.append(13) self.assertEqual(memory, [12, 13]) singleton.initialize() singleton.append(14) singleton.append(15) self.assertEqual(memory, [12, 13]) self.assertEqual(singleton.memory, [14, 15])
def test_delegated_singleton_calls(self): """Test if the delegated singleton calls the singleton correctly """ class SingletonTest(Singleton): """Singleton test class """ def __init__(self): self.memory = [] def add(self, number): """Add a number to the memory """ self.memory.append(number) def reset(self): """Reset the memory """ memory = self.memory self.memory = [] return memory delegated = delegate_singleton(SingletonTest) delegated.add(12) self.assertEqual(delegated.memory, [12]) memory = delegated.reset() self.assertEqual(memory, [12]) self.assertEqual(delegated.memory, [])
def test_deletate_singleton_next(self): """Test using a delegated singleton as an iterator """ class FooSingleton(Singleton): """Singleton Iterator """ def __init__(self): """Initializer """ self.state = 0 def __iter__(self): """Make FooSingleton an iterator... """ return self def __next__(self): """Returns the next value """ if self.state < 3: self.state += 1 return self.state else: raise StopIteration def next(self): """For python 2.x compatibility""" return self.__next__() singleton = delegate_singleton(FooSingleton) for item, expected in zip(singleton, count_up(1)): self.assertEqual(item, expected)
def test_delegate_singleton_repr(self): """Test calling the repr method on a delegated singleton """ class FooSingleton(Singleton): """Singleton test class """ def __repr__(self): """Returns foo """ return "foo" singleton = delegate_singleton(FooSingleton) self.assertEqual(singleton.__repr__(), "foo")
def test_delegate_singleton_dir(self): """Test calling the dir method on a delegated singleton """ class FooSingleton(Singleton): """Singleton test class """ @staticmethod def bla(): """Returns foo """ return "foo" singleton = delegate_singleton(FooSingleton) self.assertIn('bla', dir(singleton))
def test_delegate_singleton_iter(self): """Test iterating over a delegated singleton """ class FooSingleton(Singleton, list): """Iterable Singleton """ pass singleton = delegate_singleton(FooSingleton) for i in range(10): singleton.append(i) for item, expected in zip(singleton, count_up()): self.assertEqual(item, expected)
def test_delegate_singleton_repr(self): """Test calling the repr method on a delegated singleton """ class FooSingleton(Singleton): """Singleton test class """ def __repr__(self): """Returns foo """ return "foo" singleton = delegate_singleton(FooSingleton) self.assertEqual(singleton.__repr__(), "foo")
def test_delegate_singleton_dir(self): """Test calling the dir method on a delegated singleton """ class FooSingleton(Singleton): """Singleton test class """ @staticmethod def bla(): """Returns foo """ return "foo" singleton = delegate_singleton(FooSingleton) self.assertIn("bla", dir(singleton))
def test_delegate_singleton_iter(self): """Test iterating over a delegated singleton """ class FooSingleton(Singleton, list): """Iterable Singleton """ pass singleton = delegate_singleton(FooSingleton) for i in range(10): singleton.append(i) for item, expected in zip(singleton, count_up()): self.assertEqual(item, expected)
def test_delegate_hierarchical(self): """Test using a singleton HierarchicalOrderedDict """ # pylint: disable=too-many-ancestors class SingletonDict(HierarchicalOrderedDict, Singleton): """Singleton Iterator """ pass singleton = delegate_singleton(SingletonDict) singleton['a'] = 12 singleton['b.c'] = 13 self.assertIn('a', singleton) self.assertIn('b.c', singleton) self.assertEqual(singleton['a'], 12) self.assertEqual(singleton['b.c'], 13)
def test_delegate_hierarchical(self): """Test using a singleton HierarchicalOrderedDict """ # pylint: disable=too-many-ancestors class SingletonDict(HierarchicalOrderedDict, Singleton): """Singleton Iterator """ pass singleton = delegate_singleton(SingletonDict) singleton["a"] = 12 singleton["b.c"] = 13 self.assertIn("a", singleton) self.assertIn("b.c", singleton) self.assertEqual(singleton["a"], 12) self.assertEqual(singleton["b.c"], 13)
def test_delegated_get_instance(self): """Test if the delegated singleton can use get_instance """ class SingletonTest(Singleton): """Singleton test class """ def __init__(self): """Initializer """ self.foo_str = "foo" def get_foo(self): """Returns the foo string """ return self.foo_str delegated = delegate_singleton(SingletonTest) direct = delegated.get_instance() self.assertEqual(direct.get_foo(), "foo")
def test_delegated_get_instance(self): """Test if the delegated singleton can use get_instance """ class SingletonTest(Singleton): """Singleton test class """ def __init__(self): """Initializer """ self.foo_str = "foo" def get_foo(self): """Returns the foo string """ return self.foo_str delegated = delegate_singleton(SingletonTest) direct = delegated.get_instance() self.assertEqual(direct.get_foo(), "foo")
def test_delegated_reset_instance(self): """Test if the delegated singleton can use reset_instance """ class SingletonTest(Singleton): """Singleton test class """ def __init__(self): """Initializer """ self.memory = [] def add(self, number): """Add a number to the memory """ self.memory.append(number) delegated = delegate_singleton(SingletonTest) delegated.add(12) self.assertEqual(SingletonTest.get_instance().memory, [12]) delegated.reset_instance() self.assertEqual(SingletonTest.get_instance().memory, [])
def test_delegate_default(self): """Test using delegating a DefaultSingleton """ memory = [] class Default(DefaultSingleton): """Default Singleton """ def __init__(self): """Initializer """ self.memory = [] def append(self, value): """Append to the memory """ self.memory.append(value) @classmethod def _get_pseudo_instance(cls): """Return the external memory """ return memory singleton = delegate_singleton(Default) singleton.append(12) singleton.append(13) self.assertEqual(memory, [12, 13]) singleton.initialize() singleton.append(14) singleton.append(15) self.assertEqual(memory, [12, 13]) self.assertEqual(singleton.memory, [14, 15])
import h5py import os import shutil from collections import OrderedDict from functools import partial import lockfile from pyexperiment.utils.Singleton import Singleton from pyexperiment.utils.Singleton import delegate_singleton from pyexperiment.utils.HierarchicalMapping \ import HierarchicalOrderedDict from pyexperiment.utils import sentinel from pyexperiment.Logger import TimingLogger from pyexperiment.utils.functional import starts_with log = delegate_singleton(TimingLogger) # pylint: disable=invalid-name """Pyexperiment's logger, re-wrapped here to avoid cyclical dependency """ DELETED = sentinel.create('DELETED', 'Deleted State') UNLOADED = sentinel.create('UNLOADED', 'Unloaded State') class State( Singleton, # pylint: disable=too-many-ancestors HierarchicalOrderedDict): """Represents persistent state of an experiment. """ def __init__(self, filename=None): """Initializer """
import configobj import validate from toolz import thread_first from pyexperiment.utils.Singleton import DefaultSingleton from pyexperiment.utils.Singleton import delegate_singleton from pyexperiment.Logger import TimingLogger from pyexperiment.utils.HierarchicalMapping import HierarchicalMapping from pyexperiment.utils.HierarchicalMapping import HierarchicalOrderedDict from pyexperiment.utils.config_conversion import ohm_to_spec from pyexperiment.utils.config_conversion import convert_spec from pyexperiment.utils.config_conversion import conf_to_ohm from pyexperiment.utils.config_conversion import ohm_to_spec_list log = delegate_singleton(TimingLogger) # pylint: disable=invalid-name """Pyexperiment's logger, re-wrapped here to avoid cyclical dependency """ class Config(HierarchicalMapping, # pylint: disable=too-many-ancestors DefaultSingleton): """Represents a singleton configuration object. """ CONFIG_SPEC_PATH = 'configspec.ini' """Path of the file with the specification for configurations. """ DEFAULT_CONFIG = HierarchicalOrderedDict() """Default configuration, later used by initialize """
"""The pyexperiment module - quick and clean experiments with Python. """ from pyexperiment.version import __version__ from pyexperiment.utils.Singleton import delegate_singleton # For convenience, set up the basic tools here # pylint: disable=invalid-name from pyexperiment.Config import Config conf = delegate_singleton(Config) from pyexperiment.Logger import TimingLogger log = delegate_singleton(TimingLogger) from pyexperiment.State import State state = delegate_singleton(State)