def keysLoaded(self, keyValueDict, isInitialLoad): for key in keyValueDict: if key.isArray() and len(key) == 2 and key[0] == NativeJson.Json( "CGSS"): try: keyPath = JsonPickle.fromJson(key[1]) except: import traceback logging.warn( "Bad key encountered in KeyspaceUpdater for keyspace %s: %s\n%s", self.keyspace, key, traceback.format_exc()) #skip this key continue node = Subspace(keyspace=self.keyspace, keyPath=keyPath) val = keyValueDict[key] if val is None: node.value_ = None else: try: node.value_ = (JsonPickle.fromJson(val), ) except: import traceback traceback.print_exc() logging.warn( "ComputedGraphSharedState loaded bad node value at %s, %s: %s", self.keyspace, key, repr(val)) # else: # print "ignoring ", key if isInitialLoad: self.keyspace.markLoaded()
def setValueSlot(self, newValue): logging.info("Setting %s %s", str(self), newValue) if not self.keyspace.loaded: if SynchronousPropertyAccess.SynchronousPropertyAccess.getCurrent() is not None: self.keyspace.waitLoaded() else: logging.info("raising NotLoadedException for %s", self) raise NotLoadedException(self.keyspace) if newValue is None: SharedStateSynchronizer.getSynchronizer().writeValue( self.keyspace.keyspaceName, self.keyName, None ) else: assert isinstance(newValue, tuple) assert len(newValue) == 1 try: SharedStateSynchronizer.getSynchronizer().writeValue( self.keyspace.keyspaceName, self.keyName, JsonPickle.toJson(newValue[0]) ) except Exception as e: logging.error("%s", traceback.format_exc()) raise self.value_ = newValue
def setValueSlot(self, newValue): logging.info("Setting %s %s", str(self), newValue) if not self.keyspace.loaded: if SynchronousPropertyAccess.SynchronousPropertyAccess.getCurrent( ) is not None: self.keyspace.waitLoaded() else: logging.info("raising NotLoadedException for %s", self) raise NotLoadedException(self.keyspace) if newValue is None: SharedStateSynchronizer.getSynchronizer().writeValue( self.keyspace.keyspaceName, self.keyName, None) else: assert isinstance(newValue, tuple) assert len(newValue) == 1 try: SharedStateSynchronizer.getSynchronizer().writeValue( self.keyspace.keyspaceName, self.keyName, JsonPickle.toJson(newValue[0])) except Exception as e: logging.error("%s", traceback.format_exc()) raise self.value_ = newValue
def keysLoaded(self, keyValueDict, isInitialLoad): for key in keyValueDict: if key.isArray() and len(key) == 2 and key[0] == NativeJson.Json("CGSS"): try: keyPath = JsonPickle.fromJson(key[1]) except: import traceback logging.warn( "Bad key encountered in KeyspaceUpdater for keyspace %s: %s\n%s", self.keyspace, key, traceback.format_exc() ) #skip this key continue node = Subspace(keyspace = self.keyspace, keyPath = keyPath) val = keyValueDict[key] if val is None: node.value_ = None else: try: node.value_ = (JsonPickle.fromJson(val),) except: import traceback traceback.print_exc() logging.warn("ComputedGraphSharedState loaded bad node value at %s, %s: %s", self.keyspace, key, repr(val) ) # else: # print "ignoring ", key if isInitialLoad: self.keyspace.markLoaded()
def test_cgPickling_basic(self): loc1 = LocationA(value=10) self.assertPicklable(loc1) loc2 = LocationA(value=(1, 2, 3)) self.assertPicklable(loc2) loc3 = LocationA(value=({1: 2}, "2", 3, 4.0, None, 5L)) self.assertPicklable(loc3) loc4 = LocationB(value=loc1, value2=loc2) self.assertPicklable(loc4) loc4 = LocationB(value=loc4, value2=loc4) self.assertPicklable(loc4) #verify that the override worked self.assertTrue("__LocB__" in repr(JsonPickle.toSimple(loc4)))
def test_cgPickling_basic(self): loc1 = LocationA(value = 10) self.assertPicklable(loc1) loc2 = LocationA(value = (1,2,3)) self.assertPicklable(loc2) loc3 = LocationA(value = ({1:2},"2",3, 4.0, None, 5L)) self.assertPicklable(loc3) loc4 = LocationB(value=loc1, value2=loc2) self.assertPicklable(loc4) loc4 = LocationB(value=loc4, value2=loc4) self.assertPicklable(loc4) #verify that the override worked self.assertTrue("__LocB__" in repr(JsonPickle.toSimple(loc4)))
def keyPathToKeyName(path): return NativeJson.Json(('CGSS', JsonPickle.toJson(path)))
def keyName(self): return NativeJson.Json(('CGSS',JsonPickle.toJson(self.keyPath)))
def keyspacePathToKeyspaceName(keyspacePath): return NativeJson.Json([JsonPickle.toSimple(x) for x in keyspacePath])
def updateValueInSharedState(self, key, view): view[key] = JsonPickle.toJson(self.keyValue)
def keyName(self): return NativeJson.Json(('CGSS', JsonPickle.toJson(self.keyPath)))
def assertPicklable(self, something): simple = JsonPickle.toJson(something) something2 = JsonPickle.fromJson(simple) self.assertIdentical(something, something2)
import ufora.BackendGateway.ComputedGraph.ComputedGraphTestHarness as ComputedGraphTestHarness import ufora.core.JsonPickle as JsonPickle class LocationA(ComputedGraph.Location): """A very simple computed graph location for testing""" value = object class LocationB(ComputedGraph.Location): """A very simple computed graph location for testing""" value = object value2 = object JsonPickle.addOverride(LocationB, "__LocB__") class TestJsonPickler(unittest.TestCase): @ComputedGraphTestHarness.UnderHarness def test_cgPickling_basic(self): loc1 = LocationA(value=10) self.assertPicklable(loc1) loc2 = LocationA(value=(1, 2, 3)) self.assertPicklable(loc2) loc3 = LocationA(value=({1: 2}, "2", 3, 4.0, None, 5L)) self.assertPicklable(loc3)
import time import ufora.BackendGateway.ComputedGraph.ComputedGraph as ComputedGraph import ufora.BackendGateway.ComputedGraph.ComputedGraphTestHarness as ComputedGraphTestHarness import ufora.core.JsonPickle as JsonPickle class LocationA(ComputedGraph.Location): """A very simple computed graph location for testing""" value = object class LocationB(ComputedGraph.Location): """A very simple computed graph location for testing""" value = object value2 = object JsonPickle.addOverride(LocationB, "__LocB__") class TestJsonPickler(unittest.TestCase): @ComputedGraphTestHarness.UnderHarness def test_cgPickling_basic(self): loc1 = LocationA(value = 10) self.assertPicklable(loc1) loc2 = LocationA(value = (1,2,3)) self.assertPicklable(loc2) loc3 = LocationA(value = ({1:2},"2",3, 4.0, None, 5L)) self.assertPicklable(loc3)
def assertValueUpdatedInSharedState(self, key, view, expectedValue): self.assertIsNotNone(view[key]) value = view[key].value() self.assertEqual(JsonPickle.fromJson(value), self.keyValue)