def __init__(self, connection, pureImplementationMappings=None): self.connection = connection self.stayOpenOnExit = False self.pureImplementationMappings = \ pureImplementationMappings or PureImplementationMappings.PureImplementationMappings() self.objectRegistry = ObjectRegistry.ObjectRegistry() self.objectRehydrator = PythonObjectRehydrator.PythonObjectRehydrator( self.pureImplementationMappings) self.lock = threading.Lock()
def roundtripConvert(toConvert, vdm, allowUserCodeModuleLevelLookups=False): t0 = time.time() mappings = PureImplementationMappings.PureImplementationMappings() binaryObjectRegistry = BinaryObjectRegistry.BinaryObjectRegistry() walker = PyObjectWalker.PyObjectWalker(mappings, binaryObjectRegistry) objId = walker.walkPyObject(toConvert) binaryObjectRegistry.defineEndOfStream() t1 = time.time() registry = ObjectRegistry.ObjectRegistry() BinaryObjectRegistryDeserializer.deserializeFromString( binaryObjectRegistry.str(), registry, lambda x: x) t2 = time.time() objId, registry.objectIdToObjectDefinition = pickle.loads( pickle.dumps((objId, registry.objectIdToObjectDefinition), 2)) t3 = time.time() converter = Converter.constructConverter( Converter.canonicalPurePythonModule(), vdm) anObjAsImplval = converter.convertDirectly(objId, registry) t4 = time.time() outputStream = BinaryObjectRegistry.BinaryObjectRegistry() root_id, needsLoad = converter.transformPyforaImplval( anObjAsImplval, outputStream, PyforaToJsonTransformer.ExtractVectorContents(vdm)) needsLoad = False t5 = time.time() rehydrator = PythonObjectRehydrator(mappings, allowUserCodeModuleLevelLookups) finalResult = rehydrator.convertEncodedStringToPythonObject( outputStream.str(), root_id) t6 = time.time() return finalResult, { '0: walking': t1 - t0, '1: deserializeFromString': t2 - t1, '2: toImplval': t4 - t3, '3: serialze implVal': t5 - t4, '4: toPython': t6 - t5 }
def __init__(self, connection, pureImplementationMappings=None): """Initialize a Pyfora executor. connection - a pyfora.Connection.Connection, or something with similar interface. """ self.connection = connection self.stayOpenOnExit = False self.pureImplementationMappings = pureImplementationMappings or DefaultPureImplementationMappings.getMappings( ) self.objectRegistry = ObjectRegistry.ObjectRegistry() self.objectRehydrator = PythonObjectRehydrator.PythonObjectRehydrator( self.pureImplementationMappings) self.futures = {} self.lock = threading.Lock()
def initialize(self, purePythonMDSAsJson): """Initialize the converter assuming a set of pyfora builtins""" import pyfora.ObjectRegistry as ObjectRegistry import ufora.FORA.python.PurePython.Converter as Converter try: logging.info("Initializing the PyforaObjectConverter") objectRegistry_[0] = ObjectRegistry.ObjectRegistry() converter_[0] = Converter.constructConverter( Converter.canonicalPurePythonModule(), ComputedValueGateway.getGateway().vdm ) except: logging.critical("Failed to initialize the PyforaObjectConverter: %s", traceback.format_exc()) raise
def test_conversion_metadata(self): for anInstance in [ThisIsAClass(), ThisIsAFunction]: mappings = PureImplementationMappings.PureImplementationMappings() binaryObjectRegistry = BinaryObjectRegistry.BinaryObjectRegistry() walker = PyObjectWalker.PyObjectWalker(mappings, binaryObjectRegistry) objId = walker.walkPyObject(anInstance) binaryObjectRegistry.defineEndOfStream() converter = Converter.constructConverter( Converter.canonicalPurePythonModule(), None) registry = ObjectRegistry.ObjectRegistry() BinaryObjectRegistryDeserializer.deserializeFromString( binaryObjectRegistry.str(), registry, lambda x: x) anObjAsImplval = converter.convertDirectly(objId, registry) stream = BinaryObjectRegistry.BinaryObjectRegistry() root_id, needsLoading = converter.transformPyforaImplval( anObjAsImplval, stream, PyforaToJsonTransformer.ExtractVectorContents(None)) assert not needsLoading rehydrator = PythonObjectRehydrator(mappings, False) convertedInstance = rehydrator.convertEncodedStringToPythonObject( stream.str(), root_id) convertedInstanceModified = rehydrator.convertEncodedStringToPythonObject( stream.str().replace("return 100", "return 200"), root_id) if anInstance is ThisIsAFunction: self.assertEqual(anInstance(), 100) self.assertEqual(convertedInstance(), 100) self.assertEqual(convertedInstanceModified(), 200) else: self.assertEqual(anInstance.f(), 100) self.assertEqual(convertedInstance.f(), 100) self.assertEqual(convertedInstanceModified.f(), 200)
def test_walking_unconvertible_module(self): mappings = PureImplementationMappings.PureImplementationMappings() binaryObjectRegistry = BinaryObjectRegistry.BinaryObjectRegistry() walker = PyObjectWalker.PyObjectWalker(mappings, binaryObjectRegistry) objId = walker.walkPyObject(ThisFunctionIsImpure) binaryObjectRegistry.defineEndOfStream() registry = ObjectRegistry.ObjectRegistry() BinaryObjectRegistryDeserializer.deserializeFromString( binaryObjectRegistry.str(), registry, lambda x: x) self.assertEqual( sorted( registry.objectIdToObjectDefinition[objId]\ .freeVariableMemberAccessChainsToId.keys() ), ["multiprocessing"] )
def initialize(self, purePythonMDSAsJson): """Initialize the converter assuming a set of pyfora builtins""" try: import pyfora.ObjectRegistry as ObjectRegistry import ufora.FORA.python.PurePython.Converter as Converter import ufora.FORA.python.PurePython.PyforaSingletonAndExceptionConverter as PyforaSingletonAndExceptionConverter import ufora.native.FORA as ForaNative import ufora.FORA.python.ModuleImporter as ModuleImporter logging.info("Initializing the PyforaObjectConverter") objectRegistry_[0] = ObjectRegistry.ObjectRegistry() if purePythonMDSAsJson is None: converter_[0] = Converter.Converter() else: purePythonModuleImplval = ModuleImporter.importModuleFromMDS( ModuleDirectoryStructure.ModuleDirectoryStructure.fromJson(purePythonMDSAsJson), "fora", "purePython", searchForFreeVariables=True ) singletonAndExceptionConverter = \ PyforaSingletonAndExceptionConverter.PyforaSingletonAndExceptionConverter( purePythonModuleImplval ) primitiveTypeMapping = { bool: purePythonModuleImplval.getObjectMember("PyBool"), str: purePythonModuleImplval.getObjectMember("PyString"), int: purePythonModuleImplval.getObjectMember("PyInt"), float: purePythonModuleImplval.getObjectMember("PyFloat"), type(None): purePythonModuleImplval.getObjectMember("PyNone"), } nativeConstantConverter = ForaNative.PythonConstantConverter( primitiveTypeMapping ) nativeListConverter = ForaNative.makePythonListConverter( purePythonModuleImplval.getObjectMember("PyList") ) nativeTupleConverter = ForaNative.makePythonTupleConverter( purePythonModuleImplval.getObjectMember("PyTuple") ) nativeDictConverter = ForaNative.makePythonDictConverter( purePythonModuleImplval.getObjectMember("PyDict") ) foraBuiltinsImplVal = ModuleImporter.builtinModuleImplVal() converter_[0] = Converter.Converter( nativeListConverter=nativeListConverter, nativeTupleConverter=nativeTupleConverter, nativeDictConverter=nativeDictConverter, nativeConstantConverter=nativeConstantConverter, singletonAndExceptionConverter=singletonAndExceptionConverter, vdmOverride=ComputedValueGateway.getGateway().vdm, purePythonModuleImplVal=purePythonModuleImplval, foraBuiltinsImplVal=foraBuiltinsImplVal ) except: logging.critical("Failed to initialize the PyforaObjectConverter: %s", traceback.format_exc()) raise