示例#1
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)
示例#2
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.