Beispiel #1
0
 def setUp(self):
     self.blackboard = Blackboard(SupportedMemoryType.REDIS,
                                  host='localhost',
                                  port=6379,
                                  db_num=1,
                                  flush=True,
                                  timeout=1.0)
     self.data_a = None
     self.data_b = None
     self.data_c = None
Beispiel #2
0
# -*- coding: utf-8 -*-

from gblackboard import Blackboard
from gblackboard import SupportedMemoryType


def callback(data):
    print(data)


blackboard = Blackboard(SupportedMemoryType.DICTIONARY)
blackboard.set('key', 'value')
# Register `callback` function as a callback.
blackboard.register_callback('key', callback)
# Update data;
# `callback` function will be called during `update`,
# and `new_value` will passed to `callback` function.
blackboard.update('key', 'new_value')
Beispiel #3
0
# -*- coding: utf-8 -*-

from gblackboard import Blackboard
from gblackboard import SupportedMemoryType

blackboard = Blackboard(SupportedMemoryType.DICTIONARY)
# Set a key-value data; `set` method should be called only once for a key.
# It's a kind of initialization for data.
blackboard.set('key', 'value')
# Retrieve data with key.
value = blackboard.get('key')
# Update data with new value;
# `update` method should be called after `set` method called for a key.
blackboard.update('key', 'new_value')
# Delete data from blackboard with key.
blackboard.drop('key')
# Clear all data in blackboard.
blackboard.clear()
Beispiel #4
0
class TestGblackboard(unittest.TestCase):
    @patch('redis.Redis', fakeredis.FakeRedis)
    def setUp(self):
        self.blackboard = Blackboard(SupportedMemoryType.REDIS,
                                     host='localhost',
                                     port=6379,
                                     db_num=1,
                                     flush=True,
                                     timeout=1.0)
        self.data_a = None
        self.data_b = None
        self.data_c = None

    def tearDown(self):
        self.blackboard.close()

    def callback_a(self, data):
        self.data_a = data

    def callback_b(self, data):
        self.data_b = data

    def callback_c(self, data):
        self.data_c = data

    def test_simple_data(self):
        # set first value
        first_key = 'first'
        first_value = 100
        self.blackboard.set(first_key, first_value)
        first = self.blackboard.get(first_key)
        self.assertEqual(first, first_value)
        # update first value
        first_value = 200
        self.blackboard.update(first_key, first_value)
        first = self.blackboard.get(first_key)
        self.assertEqual(first, first_value)
        # set second value (read_only)
        second_key = 'second'
        second_value = 100.1
        self.blackboard.set(second_key, second_value, read_only=True)
        second = self.blackboard.get(second_key)
        self.assertEqual(second, second_value)
        second_value = 100.2
        with self.assertRaises(exception.NotEditable):
            self.blackboard.update(second_key, second_value)
        # drop first value
        self.blackboard.drop(first_key)
        with self.assertRaises(exception.NonExistingKey):
            self.blackboard.get(first_key)
        # clear keys
        self.blackboard.clear()
        keys = self.blackboard.keys(in_list=True)
        self.assertListEqual(keys, [])
        # set third value
        third_key = 'third'
        third_value = 'hello world'
        self.blackboard.set(third_key, third_value, read_only=False)
        # test callback_a, callback_b
        self.blackboard.register_callback(third_key, self.callback_a)
        self.blackboard.register_callback(third_key, self.callback_b)
        self.blackboard.register_callback(third_key, self.callback_c)
        third_value = 'hello blackboard!'
        self.blackboard.update(third_key, third_value)
        self.assertEqual(self.data_a, third_value)
        self.assertEqual(self.data_b, third_value)
        self.assertEqual(self.data_c, third_value)
        # remove callback_a
        self.blackboard.remove_callback(third_key, self.callback_a)
        third_value = 'hello world!!'
        self.blackboard.update(third_key, third_value)
        self.assertNotEqual(self.data_a, third_value)
        self.assertEqual(self.data_b, third_value)
        self.assertEqual(self.data_c, third_value)
        # clear callbacks
        self.blackboard.clear_callbacks(third_key)
        third_value = 'clear callbacks'
        self.blackboard.update(third_key, third_value)
        self.assertNotEqual(self.data_a, third_value)
        self.assertNotEqual(self.data_b, third_value)
        self.assertNotEqual(self.data_c, third_value)

    def test_complex_data(self):
        # user1 data
        user1_key = 'user1'
        user1_val = User("G.Ted", "*****@*****.**")
        self.blackboard.set(user1_key, user1_val)
        user1_val = self.blackboard.get(user1_key)
        self.assertEqual(type(user1_val), User)
        self.assertEqual(repr(user1_val), "<User(name='G.Ted')>")
        # user2 data
        user2_key = 'user2'
        user2_val = User("Foo", "*****@*****.**")
        self.blackboard.set(user2_key, user2_val)
        self.blackboard.register_callback(user2_key, self.callback_a)
        user2_val = self.blackboard.get(user2_key)
        user2_val.name = 'Bar'
        self.blackboard.update(user2_key, user2_val)
        user2_val = self.blackboard.get(user2_key)
        self.assertEqual(type(user2_val), User)
        self.assertEqual(repr(user2_val), "<User(name='Bar')>")
        self.assertEqual(type(self.data_a), User)
        self.assertEqual(repr(self.data_a), "<User(name='Bar')>")
        # user list
        user_list_key = 'users'
        user_list_val = [user1_val, user2_val]
        self.blackboard.set(user_list_key, user_list_val)
        user_list_val = self.blackboard.get(user_list_key)
        self.assertEqual(user_list_val[0], user1_val)
        self.assertListEqual(user_list_val, [user1_val, user2_val])
Beispiel #5
0
from gblackboard import SupportedMemoryType

import datetime as dt


class User(object):
    def __init__(self, name, email):
        self.name = name
        self.email = email
        self.created_at = dt.datetime.now()

    def __repr__(self):
        return '<User(name={self.name!r})>'.format(self=self)


blackboard = Blackboard(SupportedMemoryType.DICTIONARY)

# You can also store customized class objects in blackboard.
blackboard.set('user', User("G.Ted", "*****@*****.**"))
user = blackboard.get('user')
print(user)
# <User(name='G.Ted')> will be printed.

# List of complex objects is also supported.
blackboard.set('users', [
    User("User1", "*****@*****.**"),
    User("User2", "*****@*****.**"),
])
users = blackboard.get('users')
print(users)
# [<User(name='User1')>, <User(name='User2')>] will be printed.
Beispiel #6
0
 def setUp(self):
     self.blackboard = Blackboard(SupportedMemoryType.DICTIONARY)
     self.data_a = None
     self.data_b = None
     self.data_c = None
Beispiel #7
0
 def test_save_load_on_blackboard(self):
     blackboard = Blackboard(SupportedMemoryType.DICTIONARY)
     blackboard.set('hello', 'world', read_only=True)
     blackboard.save()
     blackboard.close()
     blackboard = Blackboard(SupportedMemoryType.DICTIONARY)
     blackboard.load()
     self.assertEqual(blackboard.get('hello'), 'world')
     with self.assertRaises(exception.NotEditable):
         blackboard.update('hello', 'G.Ted')
     with self.assertRaises(exception.UnsafeLoading):
         blackboard.load()
     druru = User(
         name='Druru',
         skills=['C++ 11', 'Git', 'Docker', 'Unreal Engine'],
         age=27.9,
         address=Address(
             country='S. Korea',
             city='Incheon'
         )
     )
     blackboard.set('user', druru, read_only=False)
     gted = User(
         name='G.Ted',
         skills=['Python (2 & 3)', 'Git', 'Docker', 'ROS'],
         age=20.5,
         address=Address(
             country='S. Korea',
             city='Seoul'
         )
     )
     blackboard.update('user', gted)
     blackboard.save()
     blackboard.close()
     blackboard = Blackboard(SupportedMemoryType.REDIS)
     blackboard.load()
     self.assertEqual(blackboard.get('hello'), 'world')
     self.assertEqual(blackboard.get('user'), gted)
     blackboard.close()
     # remove saved gblackboard directory and files
     gblackboard_path = './.gblackboard'
     if os.path.exists(gblackboard_path):
         for root, dirs, files in os.walk(gblackboard_path, topdown=False):
             for name in files:
                 os.remove(os.path.join(root, name))
             for name in dirs:
                 os.rmdir(os.path.join(root, name))
             os.rmdir(root)
Beispiel #8
0
import datetime as dt


class User(object):

    def __init__(self, name, email):
        self.name = name
        self.email = email
        self.created_at = dt.datetime.now()

    def __repr__(self):
        return '<User(name={self.name!r})>'.format(self=self)


blackboard = Blackboard(SupportedMemoryType.DICTIONARY)
# Store sample data
blackboard.set('user', User("G.Ted", "*****@*****.**"))
# Save current blackboard contents as file.
blackboard.save()
# Close current blackboard;
# this means clear all data in blackboard
blackboard.close()
# ------------------------------------------------------------
blackboard = Blackboard(SupportedMemoryType.DICTIONARY)
# Load saved blackboard contents from files.
blackboard.load()
user = blackboard.get('user')
print(user)
# <User(name='G.Ted')> will be printed.