[docs]defsave(obj,filename=None):""" Save the passed object to the sire streamed data format. If 'filename' is passed then the data is written to this file. Otherwise the data is returned as a binary array. """from..systemimportSystemifSystem.is_system(obj):obj=obj._systemiffilenameisNone:return_Stream.save(obj)else:_Stream.save(obj,filename)
[docs]defload(data):""" Load the passed data from the sire streamed data format. If 'data' is a string, then this will load the appropriate file. Otherwise, it will assume data is a binary array, so will load the data directly from that. """obj=_Stream.load(data)from..systemimportSystemifSystem.is_system(obj):returnSystem(obj)else:returnobj
[docs]defget_data_header(data):""" Return the header data from the sire streamed data. If 'data' is a string then this is loaded from the appropriate file. Otherwise it will assume data is a binary array and will load directly from that. """try:return_Stream.getDataHeader(data)exceptAttributeError:passreturn_Stream.get_data_header(data)
def_to_binary(value):"""Internal function to creata a binary array from 'value'"""ifvalueisNone:return""importpicklereturnpickle.dumps(value).hex()def_from_binary(data):"""Internal function to create a value from the passed binary data"""ifdataisNone:returnNoneeliftype(data)isstrandlen(data)==0:returnNoneimportpicklereturnpickle.loads(bytes.fromhex(data))
[docs]defset_header_property(key,value):""" Set the global header value for key 'key' to the passed 'value'. This will write this data into all sire streamed data files that are written from now on. """try:_Stream.setHeaderProperty(key,_to_binary(value))returnexceptAttributeError:pass_Stream.set_header_property(key,_to_binary(value))
[docs]defget_header_property(key):""" Return the global header value for the key 'key'. Returns None if no value is set. """try:return_from_binary(_Stream.getHeaderProperty(key))exceptAttributeError:passreturn_from_binary(_Stream.get_header_property(key))