def test_stream_custom_protocol_to_textfile(agent): import numpy as np class MyNumpyProtocol(streamlib.StaticProtocol): # do not actually verify anything, since this protocol # is not going to be used in a destination pass # Custom adapter from custom protocol (numpy arrays) # to line based protocol. # This use case might better be covered by a higher level protocol # in the future, when it is possible to send a Data instance. # reminder: find a more useful example besides numpy arrays class MyNumpyAdapter(streamlib.Adapter): input_protocol = MyNumpyProtocol output_protocol = cl1.LINE_BASED_PROTOCOL def adapt_packet(self, packet, context): # rowmajor2rows works with any nested iterable # see also test_table_sections.py return cl1.rowmajor2rows(packet) class MySource(streamlib.Source): def inject_into(self, stream): stream.set_protocol(cl1.LINE_BASED_PROTOCOL) stream.inject_packets([ Header(delimiter='whitespace'), ReferenceSectionHeader(), Title('Title'), Creator('Creator'), Created('Timestamp'), Place('Place'), DataDefinitionsHeader(), KeyValue('distance', 'd [m]'), KeyValue('time', 't [s]'), DataHeader(), Comment('custom formatter test:'), Comment('d t') ]) # going to stream in custom protocol # the agent will find and use any registered adapters stream.set_protocol(MyNumpyProtocol) stream.inject_packet(np.random.uniform(size=(10, 2))) stream.inject_packet(np.random.uniform(size=(5, 2))) # going to stream in line based protocol again stream.set_protocol(cl1.LINE_BASED_PROTOCOL) stream.inject_packet(DataRow(("2.3", "4.555"))) stream.inject_packet(DataRow(["1.2", "4.15"])) stream.inject_packet(END_FMF) agent.register_adapter(MyNumpyAdapter()) source = MySource() with open('mydata.fmf', 'w') as handle: dest = cl1.FileLikeDestination(handle) agent.transmit(source, dest) # alternatively cl1.write(source, 'mydata.fmf', adapters=[MyNumpyAdapter()])
def test_stream_fmf_instance_to_file(fmf_template_with_tables): # every fmf instance is also a streamlib.Source speaking # in protocols that cl1 knows how to translate them # to text cl1.write(fmf_template_with_tables, "mydata.fmf")
def test_stream_custom_protocol_to_textfile(agent): import numpy as np class MyNumpyProtocol(streamlib.StaticProtocol): # do not actually verify anything, since this protocol # is not going to be used in a destination pass # Custom adapter from custom protocol (numpy arrays) # to line based protocol. # This use case might better be covered by a higher level protocol # in the future, when it is possible to send a Data instance. # reminder: find a more useful example besides numpy arrays class MyNumpyAdapter(streamlib.Adapter): input_protocol = MyNumpyProtocol output_protocol = cl1.LINE_BASED_PROTOCOL def adapt_packet(self, packet, context): # rowmajor2rows works with any nested iterable # see also test_table_sections.py return cl1.rowmajor2rows(packet) class MySource(streamlib.Source): def inject_into(self, stream): stream.set_protocol(cl1.LINE_BASED_PROTOCOL) stream.inject_packets( [ Header(delimiter="whitespace"), ReferenceSectionHeader(), Title("Title"), Creator("Creator"), Created("Timestamp"), Place("Place"), DataDefinitionsHeader(), KeyValue("distance", "d [m]"), KeyValue("time", "t [s]"), DataHeader(), Comment("custom formatter test:"), Comment("d t"), ] ) # going to stream in custom protocol # the agent will find and use any registered adapters stream.set_protocol(MyNumpyProtocol) stream.inject_packet(np.random.uniform(size=(10, 2))) stream.inject_packet(np.random.uniform(size=(5, 2))) # going to stream in line based protocol again stream.set_protocol(cl1.LINE_BASED_PROTOCOL) stream.inject_packet(DataRow(("2.3", "4.555"))) stream.inject_packet(DataRow(["1.2", "4.15"])) stream.inject_packet(END_FMF) agent.register_adapter(MyNumpyAdapter()) source = MySource() with open("mydata.fmf", "w") as handle: dest = cl1.FileLikeDestination(handle) agent.transmit(source, dest) # alternatively cl1.write(source, "mydata.fmf", adapters=[MyNumpyAdapter()])
def test_stream_source_to_textfile_convenient(lsource): cl1.write(lsource, "mydata.fmf")
def test_stream_source_to_textfile_convenient(lsource): cl1.write(lsource, 'mydata.fmf')
def test_stream_fmf_instance_to_file(fmf_template_with_tables): # every fmf instance is also a streamlib.Source speaking # in protocols that cl1 knows how to translate them # to text cl1.write(fmf_template_with_tables, 'mydata.fmf')