Best Python code snippet using playwright-python
test_baseband_readout.py
Source:test_baseband_readout.py  
1# === Start Python 2/3 compatibility2from __future__ import absolute_import, division, print_function, unicode_literals3from future.builtins import *  # noqa  pylint: disable=W0401, W06144from future.builtins.disabled import *  # noqa  pylint: disable=W0401, W06145# === End Python 2/3 compatibility6from collections import defaultdict7from dataclasses import dataclass8import os9import glob10import random11import pytest12from kotekan import runner13def is_docker():14    path = "/proc/self/cgroup"15    return (16        os.path.exists("/.dockerenv")17        or os.path.isfile(path)18        and any("docker" in line for line in open(path))19    )20if is_docker():21    pytest.skip("Does not work in Github Actions docker run.", allow_module_level=True)22default_params = {23    "telescope": "ICETelescope",24    "max_dump_samples": 3500,25    "num_elements": 256,26    "total_frames": 60,27    "stream_id": 0,28    "buffer_depth": 20,29    "num_frames_buffer": 18,30    "type": "tpluse",31    "value": 153,32    "samples_per_data_set": 1024,33    "rest_mode": "step",34    "baseband_metadata_pool": {35        "kotekan_metadata_pool": "BasebandMetadata",36        "num_metadata_objects": 4096,37    },38}39@dataclass(frozen=False)40class EventDump:41    """Convenience class for reconstructing a complete baseband event from dumped BasebandBuffer"""42    event_id: int43    freq_id: int44    event_start_seq: int45    event_end_seq: int46    num_elements: int47    fpga_start_seq: int48    fpga_length: int49    @classmethod50    def from_metadata(cls, metadata):51        """Create an EventDump instance from the event's first dumped frame"""52        event_id = metadata.event_id53        freq_id = metadata.freq_id54        event_start_seq = metadata.event_start_seq55        event_end_seq = metadata.event_end_seq56        fpga_seq = metadata.frame_fpga_seq57        num_elements = metadata.num_elements58        valid_to = metadata.valid_to59        return cls(60            event_id,61            freq_id,62            event_start_seq,63            event_end_seq,64            num_elements,65            fpga_seq,66            valid_to,67        )68    def extend(self, metadata):69        """Increase the duration of the event's data with another dumped frame70        Enforces that the dumped frames record continguous samples, with no holes caused by the output buffer's filling up.71        """72        # check structure matches73        assert self.event_id == metadata.event_id74        assert self.freq_id == metadata.freq_id75        assert self.event_start_seq == metadata.event_start_seq76        assert self.event_end_seq == metadata.event_end_seq77        assert self.num_elements == metadata.num_elements78        # check samples are continguous79        fpga_seq = metadata.frame_fpga_seq80        assert fpga_seq == self.fpga_start_seq + self.fpga_length81        self.fpga_length += metadata.valid_to82DATAGEN_PNAME = "fakenetwork"83def command_rest_frames(num_frames):84    return ("post", DATAGEN_PNAME + "/generate_test_data", {"num_frames": num_frames})85def command_trigger(start, length, event_id=123456, file_path="", dm=0, dm_error=0):86    if start < 0:87        start_unix_seconds = start88        start_unix_nano = 089    else:90        start_unix_seconds = 091        start_unix_nano = start * 256092    data = {93        "event_id": event_id,94        "start_unix_seconds": start_unix_seconds,95        "start_unix_nano": start_unix_nano,96        "duration_nano": length * 2560,97        "dm": dm,98        "dm_error": dm_error,99        "file_path": file_path,100    }101    return ("post", "baseband", data)102def wait(wait_time):103    return ("wait", wait_time, None)104def run_baseband(tdir_factory, params=None, rest_commands=None, expect_a_failure=False):105    p = dict(default_params)106    tmpdir = tdir_factory.mktemp("baseband")107    if params:108        p.update(params)109    fake_buffer = runner.FakeNetworkBuffer(110        stage_name=DATAGEN_PNAME, num_frames=p["total_frames"], type=p["type"]111    )112    write_buffer = runner.DumpBasebandBuffer(113        str(tmpdir),114        num_frames="buffer_depth * 4",115        frame_size="num_elements * samples_per_data_set / 4",116    )117    test = runner.KotekanStageTester(118        "basebandReadout",119        {},120        fake_buffer,121        write_buffer,122        p,123        rest_commands=rest_commands,124        expect_failure=expect_a_failure,125    )126    test.run()127    return write_buffer.load()128def stream_to_freq_id(stream, in_stream_freq_idx, num_freq_per_stream=1):129    """Python re-implementation of ICETelescope::stream_to_freq_id"""130    assert num_freq_per_stream in [1, 8]131    link_id = stream & 0xF132    slot_id = (stream & 0xF0) >> 4133    crate_id = (stream & 0xF00) >> 8134    unused = (stream & 0xF000) >> 12135    if num_freq_per_stream == 1:136        # CHIME: 128 ICEBoards, 2048 elements137        return crate_id * 16 + slot_id + link_id * 32 + unused * 256138    elif num_freq_per_stream == 8:139        # Pathfinder/HIRAX-256: 16 ICEBoards, 256 elements140        return slot_id + link_id * 16 + in_stream_freq_idx * 128141def collect_dumped_events(142    dump_frames, frame_size=default_params["samples_per_data_set"],143):144    """Reconstructs a list of dumped BasebandBuffer frames into a list of `EventDump`s"""145    dumped_events = []146    for frame_no, frame in enumerate(dump_frames):147        event_id = frame.metadata.event_id148        num_elements = frame.metadata.num_elements149        for j, val in enumerate(frame._buffer[frame.meta_size :]):150            if j >= frame.metadata.valid_to * num_elements:151                break152            # calculation used in `testDataGen` for method `tpluse`:153            time_idx = j // num_elements154            elem_idx = j % num_elements155            expected = (frame.metadata.frame_fpga_seq + time_idx + elem_idx) % 256156            assert (157                val == expected158            ), f"Baseband data mismatch at index {j}/{frame_no}, fpga_seq={frame.metadata.frame_fpga_seq}"159        if not dumped_events or dumped_events[-1].event_id != event_id:160            # start a new event161            dumped_events.append(EventDump.from_metadata(frame.metadata))162        else:163            # extend an existing one164            event = dumped_events[-1]165            event.extend(frame.metadata)166    return dumped_events167def collect_dumped_multi_freq_events(168    dump_frames,169    num_freq_per_stream,170    frame_size=default_params["samples_per_data_set"],171    stream_id=0,172):173    """Reconstructs a list of dumped BasebandBuffer frames into a list of `EventDump`s"""174    dumped_events = defaultdict(list)175    for frame_no, frame in enumerate(dump_frames):176        event_id = frame.metadata.event_id177        freq_id = frame.metadata.freq_id178        stream_freq_idx = [179            stream_to_freq_id(stream_id, i, num_freq_per_stream)180            for i in range(num_freq_per_stream)181        ].index(freq_id)182        num_elements = frame.metadata.num_elements183        for j, val in enumerate(frame._buffer[frame.meta_size :]):184            if j >= frame.metadata.valid_to * num_elements:185                break186            # in the case of multi-frequency dumps, we need to reconstruct187            # the "j" to be what it was in the testDataGen stage that was188            # the _input_ to baseband189            orig_fpga_seq = (frame.metadata.frame_fpga_seq // frame_size) * frame_size190            orig_j = (191                (frame.metadata.frame_fpga_seq % frame_size)192                * (num_elements * num_freq_per_stream)193                + stream_freq_idx * num_elements194                + (j // num_elements * (num_freq_per_stream * num_elements))195                + j % num_elements196            )197            # calculation used in `testDataGen` for method `tpluseplusfprime`:198            time_idx = orig_j // (num_freq_per_stream * num_elements)199            elem_idx = orig_j % num_elements200            expected = (201                2 * (orig_fpga_seq + time_idx) + 3 * freq_id + 5 * elem_idx202            ) % 256203            assert (204                val == expected205            ), f"Baseband data mismatch at freq_id={freq_id}, fpga_seq={frame.metadata.frame_fpga_seq} -> {orig_fpga_seq}, index {j}/{frame_no} -> {orig_j}"206        if (207            not dumped_events[freq_id]208            or dumped_events[freq_id][-1].event_id != event_id209        ):210            # if dumped_events[freq_id]:211            #     print(dumped_events[freq_id][-1])212            # start a new event213            dumped_events[freq_id].append(EventDump.from_metadata(frame.metadata))214        else:215            # extend an existing one216            event = dumped_events[freq_id][-1]217            event.extend(frame.metadata)218    return dumped_events219def test_max_samples(tmpdir_factory):220    """Test that the baseband dump length is truncated to max_dump_samples parameter"""221    rest_commands = [222        command_rest_frames(1),223        wait(0.5),224        command_rest_frames(5),225        command_trigger(1000, 3237),226        wait(0.1),227        command_rest_frames(20),228        # Give it some time to write the capture before shutdown.229        wait(0.5),230        command_rest_frames(5),231    ]232    params = {"total_frames": 30, "max_dump_samples": 2123}233    dump_frames = run_baseband(tmpdir_factory, params, rest_commands)234    dumped_events = collect_dumped_events(dump_frames)235    assert len(dumped_events) == 1236    dumped_event = dumped_events[0]237    assert dumped_event.fpga_start_seq == 1000238    assert dumped_event.fpga_length == params["max_dump_samples"]239def test_negative_start_time(tmpdir_factory):240    """Test using the 'save whatever you have' mode of the baseband dump241    Using -1 as the trigger start point initiates the dump using the oldest242    frame available in the buffers.243    """244    rest_commands = [245        command_rest_frames(1),246        wait(0.5),247        command_trigger(-1, 3237, 31),248        wait(0.1),249        command_rest_frames(25),250        # Give it some time to write the capture before shutdown.251        wait(1.0),252        command_rest_frames(5),253    ]254    params = {"total_frames": 30}255    dump_frames = run_baseband(tmpdir_factory, params, rest_commands)256    dumped_events = collect_dumped_events(dump_frames)257    assert len(dumped_events) == 1258    dumped_event = dumped_events[0]259    assert dumped_event.event_id == 31260    assert dumped_event.freq_id == 0261    assert dumped_event.fpga_start_seq == 0262    assert dumped_event.fpga_length == 3237263def test_basic(tmpdir_factory):264    rest_commands = [265        command_rest_frames(1),266        wait(0.5),267        command_rest_frames(5),268        command_trigger(1437, 1839, 10),269        wait(0.3),270        command_rest_frames(5),271        command_trigger(3457, 1237, 17),272        wait(0.3),273        command_rest_frames(5),274        command_trigger(5039, 1091, 31),275        wait(0.1),276        command_rest_frames(60),277    ]278    dump_frames = run_baseband(tmpdir_factory, {}, rest_commands)279    dumped_events = collect_dumped_events(dump_frames)280    baseband_requests = [281        cmd for cmd in rest_commands if cmd[0] == "post" and cmd[1] == "baseband"282    ]283    for i, event in enumerate(dumped_events):284        assert event.event_id == baseband_requests[i][2]["event_id"]285        assert event.freq_id == 0286        assert event.fpga_start_seq * 2560 == baseband_requests[i][2]["start_unix_nano"]287        assert event.fpga_length * 2560 == baseband_requests[i][2]["duration_nano"]288def test_8_multifreq(tmpdir_factory):289    # Eight frequencies, one stage.290    rest_commands = [291        command_rest_frames(1),  # generate 1 frame = 1024 time samples x 256 feeds292        wait(0.5),  # in seconds?293        command_trigger(294            1437, 1839, 10295        ),  # capture ~1839 time samples starting at t = 3.67 ms(=1437 x 2.56us) with event id=10296        command_trigger(20457, 3237, 17),  # similar to above297        command_trigger(41039, 2091, 31),298        wait(0.1),299        command_rest_frames(60),300    ]301    params = {302        "num_local_freq": 8,303        "type": "tpluseplusfprime",304        "stream_id": 2,305    }306    dump_frames = run_baseband(tmpdir_factory, params, rest_commands)307    dumped_multi_freq_events = collect_dumped_multi_freq_events(308        dump_frames,309        num_freq_per_stream=params["num_local_freq"],310        stream_id=params["stream_id"],311    )312    assert len(dumped_multi_freq_events) == params["num_local_freq"]313    baseband_requests = [314        cmd for cmd in rest_commands if cmd[0] == "post" and cmd[1] == "baseband"315    ]316    for freq_id in dumped_multi_freq_events:317        dumped_events = dumped_multi_freq_events[freq_id]318        for i, event in enumerate(dumped_events):319            assert event.event_id == baseband_requests[i][2]["event_id"]320            assert event.freq_id == freq_id321            assert (322                event.fpga_start_seq * 2560323                == baseband_requests[i][2]["start_unix_nano"]324            )325            assert event.fpga_length * 2560 == baseband_requests[i][2]["duration_nano"]326def test_missed(tmpdir_factory):327    good_trigger = (2437, 3123)328    rest_commands = [329        command_rest_frames(21),330        wait(0.5),331        command_trigger(*good_trigger),  # Catch part of this one.332        command_rest_frames(30),333        command_trigger(100, 100),  # Miss this one.334        command_trigger(1002, 112),  # Miss this one.335        command_trigger(1001, 300),  # Miss this one.336        command_trigger(81039, 7091),  # This one never arrives.337        command_rest_frames(10),338    ]339    dump_frames = run_baseband(tmpdir_factory, {}, rest_commands)340    dumped_events = collect_dumped_events(dump_frames)341    assert len(dumped_events) == 1342    dumped_event = dumped_events[0]343    assert (344        good_trigger[0]345        <= dumped_event.fpga_start_seq346        <= good_trigger[0] + good_trigger[1]347    )348    assert 0 < dumped_event.fpga_length <= good_trigger[1]349def test_overload_no_crash(tmpdir_factory):350    params = dict(default_params)351    params.update(352        {"samples_per_data_set": 16384, "num_elements": 128, "total_frames": 60,}353    )354    rest_commands = [command_rest_frames(1), wait(0.5)]355    random.seed()356    tf = params["total_frames"]357    spd = params["samples_per_data_set"]358    n = 30359    for ii in range(n):360        start = random.randrange(1, (ii * tf // n + 20) * spd)361        length = random.randrange(1, spd * 5)362        rest_commands += [command_trigger(start, length, (ii + 1))]363    rest_commands += [command_rest_frames(params["total_frames"])]...http2.py
Source:http2.py  
1import itertools2import time3import hyperframe.frame4from hpack.hpack import Encoder, Decoder5from mitmproxy.net.http import http26import mitmproxy.net.http.headers7import mitmproxy.net.http.response8import mitmproxy.net.http.request9from mitmproxy.coretypes import bidi10from .. import language11class TCPHandler:12    def __init__(self, rfile, wfile=None):13        self.rfile = rfile14        self.wfile = wfile15class HTTP2StateProtocol:16    ERROR_CODES = bidi.BiDi(17        NO_ERROR=0x0,18        PROTOCOL_ERROR=0x1,19        INTERNAL_ERROR=0x2,20        FLOW_CONTROL_ERROR=0x3,21        SETTINGS_TIMEOUT=0x4,22        STREAM_CLOSED=0x5,23        FRAME_SIZE_ERROR=0x6,24        REFUSED_STREAM=0x7,25        CANCEL=0x8,26        COMPRESSION_ERROR=0x9,27        CONNECT_ERROR=0xa,28        ENHANCE_YOUR_CALM=0xb,29        INADEQUATE_SECURITY=0xc,30        HTTP_1_1_REQUIRED=0xd31    )32    CLIENT_CONNECTION_PREFACE = b'PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n'33    HTTP2_DEFAULT_SETTINGS = {34        hyperframe.frame.SettingsFrame.HEADER_TABLE_SIZE: 4096,35        hyperframe.frame.SettingsFrame.ENABLE_PUSH: 1,36        hyperframe.frame.SettingsFrame.MAX_CONCURRENT_STREAMS: None,37        hyperframe.frame.SettingsFrame.INITIAL_WINDOW_SIZE: 2 ** 16 - 1,38        hyperframe.frame.SettingsFrame.MAX_FRAME_SIZE: 2 ** 14,39        hyperframe.frame.SettingsFrame.MAX_HEADER_LIST_SIZE: None,40    }41    def __init__(42        self,43        tcp_handler=None,44        rfile=None,45        wfile=None,46        is_server=False,47        dump_frames=False,48        encoder=None,49        decoder=None,50        unhandled_frame_cb=None,51    ):52        self.tcp_handler = tcp_handler or TCPHandler(rfile, wfile)53        self.is_server = is_server54        self.dump_frames = dump_frames55        self.encoder = encoder or Encoder()56        self.decoder = decoder or Decoder()57        self.unhandled_frame_cb = unhandled_frame_cb58        self.http2_settings = self.HTTP2_DEFAULT_SETTINGS.copy()59        self.current_stream_id = None60        self.connection_preface_performed = False61    def read_request(62        self,63        __rfile,64        include_body=True,65        body_size_limit=None,66        allow_empty=False,67    ):68        if body_size_limit is not None:69            raise NotImplementedError()70        self.perform_connection_preface()71        timestamp_start = time.time()72        if hasattr(self.tcp_handler.rfile, "reset_timestamps"):73            self.tcp_handler.rfile.reset_timestamps()74        stream_id, headers, body = self._receive_transmission(75            include_body=include_body,76        )77        if hasattr(self.tcp_handler.rfile, "first_byte_timestamp"):78            # more accurate timestamp_start79            timestamp_start = self.tcp_handler.rfile.first_byte_timestamp80        timestamp_end = time.time()81        first_line_format, method, scheme, host, port, path = http2.parse_headers(headers)82        request = mitmproxy.net.http.request.Request(83            first_line_format,84            method,85            scheme,86            host,87            port,88            path,89            b"HTTP/2.0",90            headers,91            body,92            None,93            timestamp_start=timestamp_start,94            timestamp_end=timestamp_end,95        )96        request.stream_id = stream_id97        return request98    def read_response(99        self,100        __rfile,101        request_method=b'',102        body_size_limit=None,103        include_body=True,104        stream_id=None,105    ):106        if body_size_limit is not None:107            raise NotImplementedError()108        self.perform_connection_preface()109        timestamp_start = time.time()110        if hasattr(self.tcp_handler.rfile, "reset_timestamps"):111            self.tcp_handler.rfile.reset_timestamps()112        stream_id, headers, body = self._receive_transmission(113            stream_id=stream_id,114            include_body=include_body,115        )116        if hasattr(self.tcp_handler.rfile, "first_byte_timestamp"):117            # more accurate timestamp_start118            timestamp_start = self.tcp_handler.rfile.first_byte_timestamp119        if include_body:120            timestamp_end = time.time()121        else:122            timestamp_end = None123        response = mitmproxy.net.http.response.Response(124            b"HTTP/2.0",125            int(headers.get(':status', 502)),126            b'',127            headers,128            body,129            timestamp_start=timestamp_start,130            timestamp_end=timestamp_end,131        )132        response.stream_id = stream_id133        return response134    def assemble(self, message):135        if isinstance(message, mitmproxy.net.http.request.Request):136            return self.assemble_request(message)137        elif isinstance(message, mitmproxy.net.http.response.Response):138            return self.assemble_response(message)139        else:140            raise ValueError("HTTP message not supported.")141    def assemble_request(self, request):142        assert isinstance(request, mitmproxy.net.http.request.Request)143        authority = self.tcp_handler.sni if self.tcp_handler.sni else self.tcp_handler.address[0]144        if self.tcp_handler.address[1] != 443:145            authority += ":%d" % self.tcp_handler.address[1]146        headers = request.headers.copy()147        if ':authority' not in headers:148            headers.insert(0, ':authority', authority)149        headers.insert(0, ':scheme', request.scheme)150        headers.insert(0, ':path', request.path)151        headers.insert(0, ':method', request.method)152        if hasattr(request, 'stream_id'):153            stream_id = request.stream_id154        else:155            stream_id = self._next_stream_id()156        return list(itertools.chain(157            self._create_headers(headers, stream_id, end_stream=(request.content is None or len(request.content) == 0)),158            self._create_body(request.content, stream_id)))159    def assemble_response(self, response):160        assert isinstance(response, mitmproxy.net.http.response.Response)161        headers = response.headers.copy()162        if ':status' not in headers:163            headers.insert(0, b':status', str(response.status_code).encode())164        if hasattr(response, 'stream_id'):165            stream_id = response.stream_id166        else:167            stream_id = self._next_stream_id()168        return list(itertools.chain(169            self._create_headers(headers, stream_id, end_stream=(response.content is None or len(response.content) == 0)),170            self._create_body(response.content, stream_id),171        ))172    def perform_connection_preface(self, force=False):173        if force or not self.connection_preface_performed:174            if self.is_server:175                self.perform_server_connection_preface(force)176            else:177                self.perform_client_connection_preface(force)178    def perform_server_connection_preface(self, force=False):179        if force or not self.connection_preface_performed:180            self.connection_preface_performed = True181            magic_length = len(self.CLIENT_CONNECTION_PREFACE)182            magic = self.tcp_handler.rfile.safe_read(magic_length)183            assert magic == self.CLIENT_CONNECTION_PREFACE184            frm = hyperframe.frame.SettingsFrame(settings={185                hyperframe.frame.SettingsFrame.ENABLE_PUSH: 0,186                hyperframe.frame.SettingsFrame.MAX_CONCURRENT_STREAMS: 1,187            })188            self.send_frame(frm, hide=True)189            self._receive_settings(hide=True)190    def perform_client_connection_preface(self, force=False):191        if force or not self.connection_preface_performed:192            self.connection_preface_performed = True193            self.tcp_handler.wfile.write(self.CLIENT_CONNECTION_PREFACE)194            self.send_frame(hyperframe.frame.SettingsFrame(), hide=True)195            self._receive_settings(hide=True)  # server announces own settings196            self._receive_settings(hide=True)  # server acks my settings197    def send_frame(self, frm, hide=False):198        raw_bytes = frm.serialize()199        self.tcp_handler.wfile.write(raw_bytes)200        self.tcp_handler.wfile.flush()201        if not hide and self.dump_frames:  # pragma: no cover202            print(">> " + repr(frm))203    def read_frame(self, hide=False):204        while True:205            frm = http2.parse_frame(*http2.read_raw_frame(self.tcp_handler.rfile))206            if not hide and self.dump_frames:  # pragma: no cover207                print("<< " + repr(frm))208            if isinstance(frm, hyperframe.frame.PingFrame):209                raw_bytes = hyperframe.frame.PingFrame(flags=['ACK'], payload=frm.payload).serialize()210                self.tcp_handler.wfile.write(raw_bytes)211                self.tcp_handler.wfile.flush()212                continue213            if isinstance(frm, hyperframe.frame.SettingsFrame) and 'ACK' not in frm.flags:214                self._apply_settings(frm.settings, hide)215            if isinstance(frm, hyperframe.frame.DataFrame) and frm.flow_controlled_length > 0:216                self._update_flow_control_window(frm.stream_id, frm.flow_controlled_length)217            return frm218    def check_alpn(self):219        alp = self.tcp_handler.get_alpn_proto_negotiated()220        if alp != b'h2':221            raise NotImplementedError(222                "HTTP2Protocol can not handle unknown ALPN value: %s" % alp)223        return True224    def _handle_unexpected_frame(self, frm):225        if isinstance(frm, hyperframe.frame.SettingsFrame):226            return227        if self.unhandled_frame_cb:228            self.unhandled_frame_cb(frm)229    def _receive_settings(self, hide=False):230        while True:231            frm = self.read_frame(hide)232            if isinstance(frm, hyperframe.frame.SettingsFrame):233                break234            else:235                self._handle_unexpected_frame(frm)236    def _next_stream_id(self):237        if self.current_stream_id is None:238            if self.is_server:239                # servers must use even stream ids240                self.current_stream_id = 2241            else:242                # clients must use odd stream ids243                self.current_stream_id = 1244        else:245            self.current_stream_id += 2246        return self.current_stream_id247    def _apply_settings(self, settings, hide=False):248        for setting, value in settings.items():249            old_value = self.http2_settings[setting]250            if not old_value:251                old_value = '-'252            self.http2_settings[setting] = value253        frm = hyperframe.frame.SettingsFrame(flags=['ACK'])254        self.send_frame(frm, hide)255    def _update_flow_control_window(self, stream_id, increment):256        frm = hyperframe.frame.WindowUpdateFrame(stream_id=0, window_increment=increment)257        self.send_frame(frm)258        frm = hyperframe.frame.WindowUpdateFrame(stream_id=stream_id, window_increment=increment)259        self.send_frame(frm)260    def _create_headers(self, headers, stream_id, end_stream=True):261        def frame_cls(chunks):262            for i in chunks:263                if i == 0:264                    yield hyperframe.frame.HeadersFrame, i265                else:266                    yield hyperframe.frame.ContinuationFrame, i267        header_block_fragment = self.encoder.encode(headers.fields)268        chunk_size = self.http2_settings[hyperframe.frame.SettingsFrame.MAX_FRAME_SIZE]269        chunks = range(0, len(header_block_fragment), chunk_size)270        frms = [frm_cls(271            flags=[],272            stream_id=stream_id,273            data=header_block_fragment[i:i + chunk_size]) for frm_cls, i in frame_cls(chunks)]274        frms[-1].flags.add('END_HEADERS')275        if end_stream:276            frms[0].flags.add('END_STREAM')277        if self.dump_frames:  # pragma: no cover278            for frm in frms:279                print(">> ", repr(frm))280        return [frm.serialize() for frm in frms]281    def _create_body(self, body, stream_id):282        if body is None or len(body) == 0:283            return b''284        chunk_size = self.http2_settings[hyperframe.frame.SettingsFrame.MAX_FRAME_SIZE]285        chunks = range(0, len(body), chunk_size)286        frms = [hyperframe.frame.DataFrame(287            flags=[],288            stream_id=stream_id,289            data=body[i:i + chunk_size]) for i in chunks]290        frms[-1].flags.add('END_STREAM')291        if self.dump_frames:  # pragma: no cover292            for frm in frms:293                print(">> ", repr(frm))294        return [frm.serialize() for frm in frms]295    def _receive_transmission(self, stream_id=None, include_body=True):296        if not include_body:297            raise NotImplementedError()298        body_expected = True299        header_blocks = b''300        body = b''301        while True:302            frm = self.read_frame()303            if (304                (isinstance(frm, hyperframe.frame.HeadersFrame) or isinstance(frm, hyperframe.frame.ContinuationFrame)) and305                (stream_id is None or frm.stream_id == stream_id)306            ):307                stream_id = frm.stream_id308                header_blocks += frm.data309                if 'END_STREAM' in frm.flags:310                    body_expected = False311                if 'END_HEADERS' in frm.flags:312                    break313            else:314                self._handle_unexpected_frame(frm)315        while body_expected:316            frm = self.read_frame()317            if isinstance(frm, hyperframe.frame.DataFrame) and frm.stream_id == stream_id:318                body += frm.data319                if 'END_STREAM' in frm.flags:320                    break321            else:322                self._handle_unexpected_frame(frm)323        headers = mitmproxy.net.http.headers.Headers(324            [[k, v] for k, v in self.decoder.decode(header_blocks, raw=True)]325        )326        return stream_id, headers, body327class HTTP2Protocol:328    def __init__(self, pathod_handler):329        self.pathod_handler = pathod_handler330        self.wire_protocol = HTTP2StateProtocol(331            self.pathod_handler, is_server=True, dump_frames=self.pathod_handler.http2_framedump332        )333    def make_error_response(self, reason, body):334        return language.http2.make_error_response(reason, body)335    def read_request(self, lg=None):336        self.wire_protocol.perform_server_connection_preface()337        return self.wire_protocol.read_request(self.pathod_handler.rfile)338    def assemble(self, message):...make_virtual_traj.py
Source:make_virtual_traj.py  
...44	for i in range(0,chops):45		os.system('echo System | gmx_' + ver + ' trjconv -s ' + tprfile + ' -f ' + trajfile + ' -tu ps -b ' + str(i*(stime/chops)) + ' -e '+str((i+1)*(stime/chops))+' -o chop_traj/traj-' + str((i+1)*(stime/chops)) +'.xtc')46########################################47# Extracting frames from each dihedral48def dump_frames(file):49	if not os.path.exists('dump_frames/'):50		os.system('mkdir dump_frames/')51	else:52		pass53	frame_list = read(file)54	for j in frame_list:55		if not os.path.exists('dump_frames/fr_' + str(j) +'.xtc'):56			if 0 <= j <= (1*stime/10):57				os.system('echo System | gmx_' + ver + ' trjconv -s ' + tprfile + ' -f chop_traj/traj-100000.xtc -tu ps -dump ' + str(j) + ' -o dump_frames/fr_' + str(j) +'.xtc')58			elif (1*stime/10) < j <= (2*stime/10):59				os.system('echo System | gmx_' + ver + ' trjconv -s ' + tprfile + ' -f chop_traj/traj-200000.xtc -tu ps -dump ' + str(j) + ' -o dump_frames/fr_' + str(j) +'.xtc')60			elif (2*stime/10) < j <= (3*stime/10):61				os.system('echo System | gmx_' + ver + ' trjconv -s ' + tprfile + ' -f chop_traj/traj-300000.xtc -tu ps -dump ' + str(j) + ' -o dump_frames/fr_' + str(j) +'.xtc')62			elif (3*stime/10) < j <= (4*stime/10):63				os.system('echo System | gmx_' + ver + ' trjconv -s ' + tprfile + ' -f chop_traj/traj-400000.xtc -tu ps -dump ' + str(j) + ' -o dump_frames/fr_' + str(j) +'.xtc')64			elif (4*stime/10) < j <= (5*stime/10):65				os.system('echo System | gmx_' + ver + ' trjconv -s ' + tprfile + ' -f chop_traj/traj-500000.xtc -tu ps -dump ' + str(j) + ' -o dump_frames/fr_' + str(j) +'.xtc')66			elif (5*stime/10) < j <= (6*stime/10):67				os.system('echo System | gmx_' + ver + ' trjconv -s ' + tprfile + ' -f chop_traj/traj-600000.xtc -tu ps -dump ' + str(j) + ' -o dump_frames/fr_' + str(j) +'.xtc')68			elif (6*stime/10) < j <= (7*stime/10):69				os.system('echo System | gmx_' + ver + ' trjconv -s ' + tprfile + ' -f chop_traj/traj-700000.xtc -tu ps -dump ' + str(j) + ' -o dump_frames/fr_' + str(j) +'.xtc')70			elif (7*stime/10) < j <= (8*stime/10):71				os.system('echo System | gmx_' + ver + ' trjconv -s ' + tprfile + ' -f chop_traj/traj-800000.xtc -tu ps -dump ' + str(j) + ' -o dump_frames/fr_' + str(j) +'.xtc')72			elif (8*stime/10) < j <= (9*stime/10):73				os.system('echo System | gmx_' + ver + ' trjconv -s ' + tprfile + ' -f chop_traj/traj-900000.xtc -tu ps -dump ' + str(j) + ' -o dump_frames/fr_' + str(j) +'.xtc')74			elif (9*stime/10) < j <= (stime):75				os.system('echo System | gmx_' + ver + ' trjconv -s ' + tprfile + ' -f chop_traj/traj-1000000.xtc -tu ps -dump ' + str(j) + ' -o dump_frames/fr_' + str(j) +'.xtc')76		else:77			pass78########################################79# Concatenating frames from each dihedral80def concat_frames(file):81	frame_list = read(file)82	if not os.path.exists('temp_trajs/'):83		os.system('mkdir temp_trajs/')84	else:85		pass86	################################################################87	# Creating partial trajectories88	os.chdir('dump_frames/')89	nfr = len(frame_list)90	fn = nfr/100091	for k in range(fn):92		f = open("frames_list-" + str(k) + ".txt", 'w')93		c = open("cat_list-" + str(k) + ".txt", 'w')94		f.close()95		c.close()96	n=097	k=098	for j in frame_list:99		if n >= 1000:100			f.close()101			c.close()102			k += 1103			n = 1104		else:105			f = open("frames_list-" + str(k) + ".txt", 'a')106			c = open("cat_list-" + str(k) + ".txt", 'a')107			string0 = "fr_" + str(j) + ".xtc "108			string1 = "c\n"109			f.write(string0)110			c.write(string1)111			n += 1112		f.close()113		c.close()114	for k in range(fn+1):115		f = open("frames_list-" + str(k) + ".txt", 'r')116		l = f.readline()117		if len(frame_list) > 1:118			os.system('gmx_' + ver + ' trjcat -f ' + str(l) + ' -settime -o ../temp_trajs/traj-' + str(k) + '.xtc < cat_list-' + str(k) + '.txt')119			pass120		else:121			pass122	os.chdir('../')123	################################################################124	# Concatenating partial trajectories125	if not os.path.exists('final_trajs/'):126		os.system('mkdir final_trajs/')127	else:128		pass129	os.chdir('temp_trajs/')130	f = open("temp-fr.txt", 'w')131	c = open("temp-cat.txt", 'w')132	for a in range(fn+1):133		stringA = 'traj-' + str(a) + '.xtc '134		stringB = 'c\n'135		f.write(stringA)136		c.write(stringB)137	f.close()138	c.close()139	f = open("temp-fr.txt", 'r')140	l = f.readline()141	if len(frame_list) > 1:142		os.system('gmx_' + ver + ' trjcat -f ' + str(l) + ' -settime -o ../final_trajs/traj-final_temp.xtc < temp-cat.txt')143		pass144	else:145		print '\n##############################\n>>> We could not read the frame list to concatenate the frames. Check your input!\n'146	os.chdir('../')147	return148########################################149# Reset folders150def change_timestep(file):151	frame_list = read(file)152	t0 = frame_list[0]153	t1 = frame_list[1]154	dt = int(t1 - t0)155	os.system('echo Other Other System | gmx_' + ver + ' trjconv -s ' + tprfile + ' -f final_trajs/traj-final_temp.xtc -tu ps -center -fit rot+trans -timestep ' + str(dt) + ' -o final_trajs/traj-final.xtc')156########################################157# Reset folders158def reset():159	if os.path.exists('final_trajs/'):160			os.system('rm -r final_trajs/')161	else:162		pass163	if os.path.exists('temp_trajs/'):164			os.system('rm -r temp_trajs/')165	else:166		pass167	if os.path.exists('dump_frames/'):168		os.system('rm -r dump_frames/')169	else:170		pass171	if os.path.exists('chop_traj/'):172		os.system('rm -r chop_traj/')173	else:174		pass175def main(ver,frame_input):176	reset()177	frame_list = read(frame_input)178	chop_traj(ver)179	dump_frames(frame_input)180	concat_frames(frame_input)181	change_timestep(frame_input)182	print "\n>>> Done! Have a nice day!\n"183	print...ParticleToy.py
Source:ParticleToy.py  
1from OpenGL.GLUT import *2from OpenGL.GLU import *3from OpenGL.GL import *4import numpy as np5import sys6from typing import List7from Particle import Particle8import Solver9from CircularWireConstraint import CircularWireConstraint10from SpringForce import SpringForce11from Capture import Capture12# Global variables13N = 014dt, d = 0.0, 0.015d_sim = False  # int in skeleton, used as bool16dump_frames = 0.017frame_number = 0.018p_vector: List[Particle] = []  # vector containing particles19win_id = 020win_x, win_y = 0, 021mouse_down = [0, 0, 0]22mouse_release = [0, 0, 0]23mouse_shiftclick = [0, 0, 0]24omx, omy, mx, my = 0, 0, 0, 025hmx, hmy = 0, 026capture: Capture = None  # class to catch screenshots and create movie27#static RodConstraint # delete_this_dummy_rod = NULL28delete_this_dummy_spring: SpringForce = None  # delete_this_dummy_spring = NULL29delete_this_dummy_wire: CircularWireConstraint = None  # delete_this_dummy_wire = NULL30def free_data():31    global delete_this_dummy_spring, delete_this_dummy_wire32    delete_this_dummy_spring = None33    delete_this_dummy_wire = None34def init_system():35    dist = 0.236    center = np.array([0.0, 0.0])37    offset = np.array([dist, 0.0])38    39    # Create three particles, attach them to each other, then add a40    # circular wire constraint to the first.41    global p_vector, delete_this_dummy_spring, delete_this_dummy_wire42    p_vector.append(Particle(center + offset))43    p_vector.append(Particle(center + offset + offset))44    p_vector.append(Particle(center + offset + offset + offset))45    46    # You should replace these with a vector generalized forces and one of constraints47    delete_this_dummy_spring = SpringForce(p_vector[0], p_vector[1], dist, 1.0, 1.0)48    # delete_this_dummy_rod = new RodConstraint(pVector[1], pVector[2], dist)49    delete_this_dummy_wire = CircularWireConstraint(p_vector[0], center, dist)50def pre_display():51    glViewport(0, 0, win_x, win_y)52    glMatrixMode(GL_PROJECTION)53    glLoadIdentity()54    gluOrtho2D(-1.0, 1.0, -1.0, 1.0)55    glClearColor(0.0, 0.0, 0.0, 1.0)56    glClear(GL_COLOR_BUFFER_BIT)57def post_display():58    global frame_number59    # Write frames if necessary60    if dump_frames:61        frame_interval = 462        if (frame_number % frame_interval) == 0:63            w = glutGet(GLUT_WINDOW_WIDTH)64            h = glutGet(GLUT_WINDOW_HEIGHT)65            global capture66            if capture is None:67                capture = Capture(w, h)68            glPixelStorei(GL_PACK_ALIGNMENT, 1)69            capture.record(glReadPixels(0, 0, w, h, GL_RGBA, GL_BYTE))70    else:71        if capture is not None:72            capture.create_movie()73            capture = None74    glutSwapBuffers()75def draw_particles():76    for par in p_vector:77        par.draw()78def draw_forces():79    # change this to iteration over full set80    delete_this_dummy_spring.draw()81def draw_constraints():82    # change this to iteration over full set83    delete_this_dummy_wire.draw()84def clear_data():85    for par in p_vector:86        par.reset()87def mouse_func(button, state, x, y):88    global omx, mx, my, hmx, hmy89    omx = mx = x90    omx = my = y91    if not mouse_down[0]:92        hmx = x93        hmy = y94    if mouse_down[button]:95        mouse_release[button] = state == GLUT_UP96        mouse_shiftclick[button] = glutGetModifiers() == GLUT_ACTIVE_SHIFT97    mouse_down[button] = state == GLUT_DOWN98    99def motion_func(x: int, y: int):100    global mx, my101    mx = x102    my = y103def display_func():104    pre_display()105    draw_forces()106    draw_constraints()107    draw_particles()108    post_display()109def idle_func():110    global d_sim, win_id, p_vector, dt111    if d_sim:112        Solver.simulation_step(p_vector, dt)113    else:114        # get_from_UI() needs to be implemented115        remap_gui()116    glutSetWindow(win_id)117    glutPostRedisplay()118def remap_gui():119    for par in p_vector:120        par.m_position[0] = par.m_construct_pos[0]121        par.m_position[1] = par.m_construct_pos[1]122def key_func(key, x, y):123    key = key.decode("utf-8")124    if key == 'c' or key == 'C':125        clear_data()126    elif key == 'd' or key == 'D':127        global dump_frames128        dump_frames = not dump_frames129    elif key == 'q' or key == 'Q':130        free_data()131        sys.exit(0)132    elif key == ' ':133        global d_sim134        d_sim = not d_sim135        136def open_glut_window():137    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE)138    glutInitWindowPosition(0, 0)139    glutInitWindowSize(win_x, win_y)140    global win_id141    win_id = glutCreateWindow(b"Particletoys!")142    glClearColor(0.0, 0.0, 0.0, 1.0)143    glClear(GL_COLOR_BUFFER_BIT)144    glutSwapBuffers()145    glClear(GL_COLOR_BUFFER_BIT)146    glutSwapBuffers()147    glEnable(GL_LINE_SMOOTH)148    glEnable(GL_POLYGON_SMOOTH)149    pre_display()150    glutKeyboardFunc(key_func)151    glutMouseFunc(mouse_func)152    glutMotionFunc(motion_func)153    #glutReshapeFunc(reshape_func)154    glutIdleFunc(idle_func)155    glutDisplayFunc(display_func)156def main():157    glutInit(sys.argv)158# if(argc == 1):159    global N, dt, d160    N = 64161    dt = 0.5  # displacement of particles in each step162    d = 5.0163    print("Error: Using defaults : N=%d dt=%g d=%g\n", N, dt, d)164# else:165#    N = int(argv[1])166#    dt = float(argv[2])167#    d = float(argv[3])168    print("\n\nHow to use this application:\n\n")169    print("\t Toggle ruction/simulation display with the spacebar key\n")170    print("\t Dump frames by pressing the 'd' key\n")171    print("\t Quit by pressing the 'q' key\n")172    global d_sim, dump_frames, frame_number, win_x, win_y173    d_sim = False174    dump_frames = 0175    frame_number = 0176    init_system()177    win_x = 512178    win_y = 512179    open_glut_window()180    glutMainLoop()181if __name__ == '__main__': ...LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!
