Пример #1
0
    def test_check_tree(self):
        tc = forest.Tree()
        tc._branch('a')
        tc.a._isinstance('c', (int, float))

        def validate_b(value):
            return 0 <= value <= 256

        tc.a._validate('b', validate_b)

        t2 = forest.Tree()
        t2._branch('a')
        t2.a.c = 2.0
        t2.a.b = 50
        t2._check(tc)

        t2.a.b = 350
        with self.assertRaises(TypeError):
            t2._check(tc)

        t2.a.b = 250
        t2.a.c = '23'
        with self.assertRaises(TypeError):
            t2._check(tc)

        t2.a.c = 2.0
        t2.a.b = 50
        t2._update(tc)
        t2._check()

        t2.a.d = 50
        with self.assertRaises(TypeError):
            t2._strict(True)
Пример #2
0
    def test_update_frozen(self):
        t = forest.Tree()
        t.b = 2
        t._branch('a')

        t2 = forest.Tree()
        t2.b = 3
        t2._freeze(True)
        with self.assertRaises(ValueError):
            t2._update(t)
Пример #3
0
 def test_update(self):
     t = forest.Tree()
     t._isinstance('f', int)
     t2 = forest.Tree(strict=True)
     with self.assertRaises(TypeError):
         t2.f = 2
     t2._update(t, descriptions=False)
     with self.assertRaises(TypeError):
         t2.f = 2
     t2._update(t)
     t2.f = 2
Пример #4
0
    def test_update_structfrozen(self):
        t = forest.Tree()
        t.b = 2

        t2 = forest.Tree()
        t2.b = 3
        t2._freeze_struct(True)
        t2._update(t)
        self.assertEqual(t.b, 2)

        t._branch('a')
        with self.assertRaises(ValueError):
            t2._update(t)
Пример #5
0
    def test_check_struct(self):
        tc = forest.Tree()
        tc._branch('a')

        t2 = forest.Tree()
        t2._branch('a')
        t2._check(tc, struct=True)

        t2._branch('b')
        with self.assertRaises(TypeError):
            t2._check(tc, struct=True)
        with self.assertRaises(TypeError):
            tc._check(t2, struct=True)
Пример #6
0
    def test_describe(self):
        tc = forest.Tree()
        tc._branch('a')
        tc._describe('a.b', 'a nice descriptive docstring', int)
        tc._describe('c.b', 'can describe things on non existing branches',
                     int)
        with self.assertRaises(TypeError):
            tc.a.b = 1.0
        tc.a.b = 1

        def validate_ac(value):
            return 0 <= value <= 256

        tc._describe('a.c',
                     """Another one""",
                     instanceof=int,
                     validate=validate_ac)

        self.assertEqual(tc._docstring('a.b'), 'a nice descriptive docstring')
        self.assertEqual(tc._docstring('a.c'), 'Another one')
        with self.assertRaises(TypeError):
            tc.a.c = 1.0

        tc._describe('a.c', """A different one""", float, validate=validate_ac)
        self.assertEqual(tc._docstring('a.c'), 'A different one')
        tc.a.c = 1.0
        with self.assertRaises(TypeError):
            tc.a.c = -50.0
Пример #7
0
    def test_update_dict(self):
        d = {'b': 2, 'abc.cde.d': 3, 'abc.f': 4}
        t = forest.Tree()
        t._update(d, overwrite=True)

        for key, value in d.items():
            self.assertEqual(t[key], value)
        self.assertEqual(t.abc.cde.d, 3)
Пример #8
0
    def test_lines(self):
        tc = forest.Tree()
        tc._branch('a')
        tc.a.b = 1
        tc.a.d = [1, 2, 3]

        self.assertTrue(
            tc.__str__() in ['a.b=1\na.d=[1, 2, 3]', 'a.d=[1, 2, 3]\na.b=1'])
Пример #9
0
    def test_fromkeys1(self):
        tc = forest.Tree()
        t = tc._fromkeys(('a', 'c.d'), 3)

        self.assertEqual(set(t._items()),set((('a', 3), ('c.d', 3))))

        with self.assertRaises(ValueError):
            t = forest.Tree._fromkeys(('a', 'a.d'))
Пример #10
0
    def test_popitem0(self):
        tc = forest.Tree()
        tc._branch('a')
        tc.a.b = 1

        self.assertEqual(tc._popitem(), ('a.b', 1))
        with self.assertRaises(KeyError):
            tc._popitem()
Пример #11
0
    def test_nestitem(self):
        tc = forest.Tree()

        tc['a.b'] = 1

        self.assertEqual(tc.a.b, 1)
        self.assertTrue('a.b' in tc)
        self.assertTrue('a.c' not in tc)
Пример #12
0
    def test_docstring(self):
        tc = forest.Tree()
        tc._branch('a')
        tc._docstring('a.b', 'a nice descriptive docstring')
        tc._docstring('a.c', """Another one""")

        self.assertEqual(tc._docstring('a.b'), 'a nice descriptive docstring')
        self.assertEqual(tc._docstring('a.c'), 'Another one')
        self.assertEqual(tc._docstring('a.f'), None)
Пример #13
0
 def test_coverage(self):
     tc = forest.Tree()
     tc._branch('a')
     with self.assertRaises(KeyError):
         tc._coverage('a.b')
     tc.a.b = 1
     self.assertEqual(tc._coverage('a.b'), 0)
     tc.a.b
     self.assertEqual(tc._coverage('a.b'), 1)
Пример #14
0
 def test_history(self):
     tc = forest.Tree()
     tc._branch('a')
     with self.assertRaises(KeyError):
         tc._history('a.b')
     tc.a.b = 1
     self.assertEqual(tc._history('a.b'), [1])
     tc.a.b = '2'
     self.assertEqual(tc._history('a.b'), [1, '2'])
Пример #15
0
    def test_freeze_struct(self):
        tc = forest.Tree()
        tc._branch('a')
        tc.a.b = 1

        tc._freeze_struct(True)
        with self.assertRaises(ValueError):
            tc.a.c = 1
        tc.a.b = 2
Пример #16
0
def test_filters0():
    """Test if KinematicArm2D instanciate properly with config"""
    check = True

    cfg = forest.Tree()
    cfg.robotclass = 'robots.KinematicArm2D'
    bot = robots.build_robot(cfg)

    check = bot.s_feats == (0, 1)
    return check
Пример #17
0
    def test_update_dict2(self):
        d = {'b': 2, 'abc.cde.d': 3, 'abc.f': 4}
        t = forest.Tree()
        t.b = [1, 2]
        t['abc.f'] = None
        t._update(d, overwrite=False)

        self.assertEqual(t.b, [1, 2])
        self.assertEqual(t.abc.cde.d, 3)
        self.assertEqual(t.abc.f, None)
Пример #18
0
    def test_kvi(self):
        tc = forest.Tree()
        tc._branch('a')

        tc.a['b'] = 1
        tc.color = 'blue'

        self.assertEqual({'a.b', 'color'}, set(tc._keys()))
        self.assertEqual({1, 'blue'}, set(tc._values()))
        self.assertEqual({('a.b', 1), ('color', 'blue')}, set(tc._items()))
Пример #19
0
    def test_both(self):
        tc = forest.Tree()
        tc._branch('a')
        tc.a.b = 1
        tc.a.d = [1, 2, 3]

        filename = tempname()
        tc._to_file(filename)
        t2 = forest.Tree._from_file(filename)

        self.assertEqual(tc, t2)
Пример #20
0
def test_filters1():
    """Test if KinematicArm2D instanciate properly with filters"""
    check = True

    cfg = forest.Tree()
    cfg.robotclass = 'robots.KinematicArm2D'
    cfg['filters.s_feats'] = (0, )
    bot = robots.build_robot(cfg)

    check = bot.s_feats == (0, )
    return check
Пример #21
0
    def test_item(self):
        tc = forest.Tree()
        tc._branch('a')

        tc.a['b'] = 1
        self.assertEqual(tc.a.b, 1)
        self.assertEqual(tc.a['b'], 1)

        tc.a.b = 2
        self.assertEqual(tc.a.b, 2)
        self.assertEqual(tc.a['b'], 2)
Пример #22
0
    def test_pickle(self):
        t = forest.Tree()
        t.a = forest.Tree()
        t.a.b = 1
        t.a.c = '2'
        t.a._branch('efg')
        t.a.efg.h = [2, 3, 4]
        t['b.farm'] = None

        s = pickle.dumps(t)
        t2 = pickle.loads(s)
        self.assertEqual(t, t2)

        filename = tempname()
        with open(filename, 'wb') as f:
            pickle.dump(t, f)

        with open(filename, 'rb') as f:
            t2 = pickle.load(f)

        self.assertEqual(t, t2)
Пример #23
0
    def test_update(self):
        t = forest.Tree()
        t.b = 2
        t._branch('c')
        t.c.d = 5.1
        t._branch('a')

        t2 = forest.Tree()
        t2.b = 3
        t2._branch('c')
        t2.c.d = 5.2
        t2._update(t)

        self.assertEqual(t.a, t2.a)
        self.assertEqual(t2.b, 2)
        self.assertEqual(t2.c.d, 5.1)

        t2.b = 4
        t2.c.d = 5.3
        t2._update(t, overwrite=False)
        self.assertEqual(t2.b, 4)
        self.assertEqual(t2.c.d, 5.3)
Пример #24
0
    def test_freeze(self):
        tc = forest.Tree()
        tc._branch('a')

        tc._freeze(True)
        with self.assertRaises(ValueError):
            tc.a.b = 1
        with self.assertRaises(ValueError):
            tc._branch('b')

        tc._freeze(False)
        tc.a.b = 1
        tc._branch('b')
Пример #25
0
    def test_instance(self):
        tc = forest.Tree()
        tc._branch('a')
        tc.a._isinstance('b', int)
        tc.a.b = 1
        tc.a._isinstance('c', (int, float))
        tc.a.c = 1
        tc.a.c = 1.5

        with self.assertRaises(TypeError):
            tc.a.b = 'abc'

        with self.assertRaises(TypeError):
            tc.a.b = 1.0
Пример #26
0
    def test_pop(self):
        tc = forest.Tree()
        tc._branch('a')
        tc.a.b = 1

        self.assertEqual(tc._pop('a.b'), 1)
        with self.assertRaises(KeyError):
            tc.a.b
        with self.assertRaises(KeyError):
            tc._pop('a.b')        
        with self.assertRaises(KeyError):
            tc._pop('a.c')
        with self.assertRaises(KeyError):
            tc._pop('d')
Пример #27
0
    def test_check_struct(self):
        t = forest.Tree()
        t._strict()

        t._isinstance('a', int)
        t.a = 1
        with self.assertRaises(TypeError):
            t.b = 1

        def validate_b(value):
            return 0 <= value <= 256

        t._validate('b', validate_b)
        t.b = 1
Пример #28
0
    def __init__(self, s_feats, cfg=None):
        self.dim = len(s_feats)
        self.s_feats = s_feats
        self.size = 0
        if cfg is None:
            cfg = forest.Tree()
        cfg._update(defcfg, overwrite=False)

        if cfg.effect._get('s_bounds', None) is None:
            print "You must define the cfg.s_bounds parameter for BoundedRandomExplorer"
        assert len(
            cfg.effect.s_bounds
        ) == self.dim, "{}cfg.s_bounds doesn't have the correct dimension; got {}, expected {}{}".format(
            gfx.red, len(cfg.effect.s_bounds), self.dim, gfx.end)
        self.bounds = cfg.effect.s_bounds
Пример #29
0
    def test_del(self):
        tc = forest.Tree()
        tc._branch('a')

        tc.a.b = 2
        del tc.a['b']
        with self.assertRaises(KeyError):
            tc.a.b

        tc.a.c = 2
        del tc.a.c
        with self.assertRaises(KeyError):
            tc.a.c

        with self.assertRaises(AttributeError):
            tc._inexistent_method
Пример #30
0
    def test_save(self):
        tc = forest.Tree()
        tc._branch('a')
        tc.a.b = '1'
        tc.a.d = [1, 2, 3]

        filename = tempname()
        tc._to_file(filename)

        with open(filename, 'r') as f:
            s = f.read()

        print(s)
        self.assertTrue(
            s in ["a.b='1'\na.d=[1, 2, 3]", "a.d=[1, 2, 3]\na.b='1'"])
        os.unlink(filename)