Ejemplo n.º 1
0
def test_stream_source_to_textfile_explicit(lsource):
    agent = cl1.new_default_agent()
    with open("mydata.fmf", "w") as handle:
        # a cl1.FileLikeDestination simply writes all received packets
        # to a given file like object (handle)
        dest = cl1.FileLikeDestination(handle)
        agent.transmit(lsource, dest)
Ejemplo n.º 2
0
def test_stream_source_to_textfile_explicit(lsource):
    agent = cl1.new_default_agent()
    with open('mydata.fmf', 'w') as handle:
        # a cl1.FileLikeDestination simply writes all received packets
        # to a given file like object (handle)
        dest = cl1.FileLikeDestination(handle)
        agent.transmit(lsource, dest)
Ejemplo n.º 3
0
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()])
Ejemplo n.º 4
0
def test_stream_fmf_instance_to_custom_destination(fmf_template_with_tables, agent):
    class CommentPrinter(streamlib.Destination):
        protocol = cl1.LINE_BASED_PROTOCOL

        def receive_packet(self, packet, context):
            if isinstance(packet, cl1.Comment):
                print packet

    # or provide fancy line based Destination, like so:
    class CommentPrinter2(cl1.LineBasedDestination):
        def receive_comment(self, comment, context):
            print comment

    agent.transmit(fmf_template_with_tables, CommentPrinter())
    agent.transmit(fmf_template_with_tables, CommentPrinter2())
Ejemplo n.º 5
0
def test_stream_fmf_instance_to_custom_destination(fmf_template_with_tables,
                                                   agent):
    class CommentPrinter(streamlib.Destination):
        protocol = cl1.LINE_BASED_PROTOCOL

        def receive_packet(self, packet, context):
            if isinstance(packet, cl1.Comment):
                print packet

    # or provide fancy line based Destination, like so:
    class CommentPrinter2(cl1.LineBasedDestination):
        def receive_comment(self, comment, context):
            print comment

    agent.transmit(fmf_template_with_tables, CommentPrinter())
    agent.transmit(fmf_template_with_tables, CommentPrinter2())
Ejemplo n.º 6
0
def test_stream_stuff_to_fmf_instance(lsource, agent):
    my_fmf_instance = cl1.new_empty_fmf()
    # every fmf instance is a LineBasedDestination accepting
    # the cl1.LINE_BASED_PROTOCOL
    agent.transmit(lsource, my_fmf_instance)
Ejemplo n.º 7
0
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()])
Ejemplo n.º 8
0
def test_stream_stuff_to_fmf_instance(lsource, agent):
    my_fmf_instance = cl1.new_empty_fmf()
    # every fmf instance is a LineBasedDestination accepting
    # the cl1.LINE_BASED_PROTOCOL
    agent.transmit(lsource, my_fmf_instance)