<commit_before>import math
__doc__ =
"""
This module provides a set of built-in animators which can be used by
any game. Additionally, custom animators can be built. An animator
should be a function (or callable) which takes in a sprite, and a
time delta which is normalized to [0,1], and returns the state of
animator at that time. See the source code of this module for some
example implementations.

Built-in animators are stateless, so the same animation can be used
many times or on many different objects. Custom animators do not
have to be stateless.

"""

"""
Many of these animators were inspired by Clutter/Kivy, but have been
generalized.
"""

def Linear(start=0.0, finish=1.0):
    def linear_animator(sprite, dt):
        return (finish - start) * (dt) + start
    return linear_animator

def QuadraticIn(start=0.0, finish=1.0):
    def quadratic_animator(sprite, dt):
        return start + (finish - start) * dt * dt
    return quadratic_animator

def QuadraticOut(start=0.0, finish=1.0):
    def quadratic_out_animator(sprite, dt):
        return start + (finish - start) * (2.0 * dt - dt * dt)
    return quadratic_out_animator

def QuadraticInOut(start=0.0, finish=1.0):
    def quadratic_in_out_animator(sprite, dt):
        dt *= 2
        if dt < 1:
            return start + 0.5 * dt * dt * (finish - start)
        dt -= 1
        return start + (dt - 0.5 * dt * dt + 0.5) * (finish - start)
    return quadratic_in_out_animator

def CubicIn(start=0.0, finish=1.0):
    def cubic_in_animator(sprite, dt):
        return start + (dt * dt * dt) * (finish - start)
    return cubic_in_animator

def CubicOut(start=0.0, finish=1.0):
    def cubic_out_animator(sprite, dt):
        dt -= 1.0
        return start + (dt * dt * dt + 1.0) * (finish - start)
    return cubic_out_animator

def CubicInOut(start=0.1, finish=1.0):
    def cubic_in_out_animator(sprite, dt):
        dt *= 2.0
        if dt < 1.0:
            return start + 0.5 * dt * dt * dt * (finish - start)
        dt -= 2.0
        return (1.0 + 0.5 * dt * dt * dt) * (finish - start) + 2.0 * start
    return cubic_in_out_animator

def Iterate(items, times=1):
    def iterate_animator(sprite, dt):
        # We preturb the result slightly negative so that it ends on
        # the last frame instead of looping back to the first
        i = round(dt * len(items) * times)
        return items[int(i % len(items))]
    return iterate_animator

def Sine(amplitude=1.0, phase=0, end_phase=2.0 * math.pi):
    def sin_animator(sprite, dt):
        return amplitude * math.sin(phase + dt * (2.0 * math.pi - phase))
    return sin_animator

def LinearTuple(start=(0, 0), finish = (0, 0)):
    def linear_animator(sprite, dt):
        return ((finish[0] - start[0]) * dt + start[0],
                (finish[1] - start[1]) * dt + start[1])
    return linear_animator

def Arc(center=(0, 0), radius = 1, theta_start = 0, theta_end = 2 * math.pi):
    def arc_animator(sprite, dt):
        theta = (theta_end - theta_start) * dt
        return (center[0] + radius * math.cos(theta),
                center[1] + radius * math.sin(theta))
    return arc_animator

def Polar(center=(0, 0), radius = lambda theta: 1.0, theta_start = 0, theta_end = 2 * math.pi):
    def arc_animator(sprite, dt):
        theta = (theta_end - theta_start) * dt
        return (center[0] + radius(theta) * math.cos(theta),
                center[1] + radius(theta) * math.sin(theta))
    return arc_animator
<commit_msg>I'm bad at python.<commit_after>import math
__doc__ = """
This module provides a set of built-in animators which can be used by
any game. Additionally, custom animators can be built. An animator
should be a function (or callable) which takes in a sprite, and a
time delta which is normalized to [0,1], and returns the state of
animator at that time. See the source code of this module for some
example implementations.

Built-in animators are stateless, so the same animation can be used
many times or on many different objects. Custom animators do not
have to be stateless.

"""

"""
Many of these animators were inspired by Clutter/Kivy, but have been
generalized.
"""

def Linear(start=0.0, finish=1.0):
    def linear_animator(sprite, dt):
        return (finish - start) * (dt) + start
    return linear_animator

def QuadraticIn(start=0.0, finish=1.0):
    def quadratic_animator(sprite, dt):
        return start + (finish - start) * dt * dt
    return quadratic_animator

def QuadraticOut(start=0.0, finish=1.0):
    def quadratic_out_animator(sprite, dt):
        return start + (finish - start) * (2.0 * dt - dt * dt)
    return quadratic_out_animator

def QuadraticInOut(start=0.0, finish=1.0):
    def quadratic_in_out_animator(sprite, dt):
        dt *= 2
        if dt < 1:
            return start + 0.5 * dt * dt * (finish - start)
        dt -= 1
        return start + (dt - 0.5 * dt * dt + 0.5) * (finish - start)
    return quadratic_in_out_animator

def CubicIn(start=0.0, finish=1.0):
    def cubic_in_animator(sprite, dt):
        return start + (dt * dt * dt) * (finish - start)
    return cubic_in_animator

def CubicOut(start=0.0, finish=1.0):
    def cubic_out_animator(sprite, dt):
        dt -= 1.0
        return start + (dt * dt * dt + 1.0) * (finish - start)
    return cubic_out_animator

def CubicInOut(start=0.1, finish=1.0):
    def cubic_in_out_animator(sprite, dt):
        dt *= 2.0
        if dt < 1.0:
            return start + 0.5 * dt * dt * dt * (finish - start)
        dt -= 2.0
        return (1.0 + 0.5 * dt * dt * dt) * (finish - start) + 2.0 * start
    return cubic_in_out_animator

def Iterate(items, times=1):
    def iterate_animator(sprite, dt):
        # We preturb the result slightly negative so that it ends on
        # the last frame instead of looping back to the first
        i = round(dt * len(items) * times)
        return items[int(i % len(items))]
    return iterate_animator

def Sine(amplitude=1.0, phase=0, end_phase=2.0 * math.pi):
    def sin_animator(sprite, dt):
        return amplitude * math.sin(phase + dt * (2.0 * math.pi - phase))
    return sin_animator

def LinearTuple(start=(0, 0), finish = (0, 0)):
    def linear_animator(sprite, dt):
        return ((finish[0] - start[0]) * dt + start[0],
                (finish[1] - start[1]) * dt + start[1])
    return linear_animator

def Arc(center=(0, 0), radius = 1, theta_start = 0, theta_end = 2 * math.pi):
    def arc_animator(sprite, dt):
        theta = (theta_end - theta_start) * dt
        return (center[0] + radius * math.cos(theta),
                center[1] + radius * math.sin(theta))
    return arc_animator

def Polar(center=(0, 0), radius = lambda theta: 1.0, theta_start = 0, theta_end = 2 * math.pi):
    def arc_animator(sprite, dt):
        theta = (theta_end - theta_start) * dt
        return (center[0] + radius(theta) * math.cos(theta),
                center[1] + radius(theta) * math.sin(theta))
    return arc_animator
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
#
# Copyright 2018 Data61, CSIRO
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Mapper tests:

GraphSAGELinkMapper(
        G: nx.Graph,
        ids: List[Any],
        link_labels: List[Any] or np.ndarray,
        batch_size: int,
        num_samples: List[int],
        feature_size: Optional[int] = None,
        name: AnyStr = None,
    )
g
"""
from stellar.mapper.link_mappers import *

import networkx as nx
import random
import numpy as np
import itertools as it
import pytest

def test_LinkMapper_constructor():
    n_feat = 4

    # test graph
    G = nx.Graph()
    elist = [(1, 2), (2, 3), (1, 4), (3, 2)]
    G.add_edges_from(elist)
    edge_labels = [0] * G.number_of_edges()

    mapper = GraphSAGELinkMapper(
        G, G.edges(), edge_labels, batch_size=2, num_samples=[2, 2]
    )
    assert mapper.batch_size == 2
    assert mapper.data_size == 3
    assert len(mapper.ids) == 3

    G = nx.DiGraph()
    G.add_edges_from(elist)
    edge_labels = [0] * G.number_of_edges()
    mapper = GraphSAGELinkMapper(
        G, G.edges(), edge_labels, batch_size=2, num_samples=[2, 2]
    )
    assert mapper.batch_size == 2
    assert mapper.data_size == 4
    assert len(mapper.ids) == 4

def test_GraphSAGELinkMapper_1():
    n_feat = 4
    n_batch = 2

    # test graph
    G = nx.Graph()
    elist = [
        (1, 2),
        (2, 3),
        (1, 4),
        (4, 2),
    ]  # make sure the number of edges is divisible by n_batch, for this test to succeed
    G.add_edges_from(elist)
    data_size = G.number_of_edges()
    edge_labels = [0] * data_size

    # Add example features
    for v in G.nodes():
        G.node[v]["feature"] = np.ones(n_feat)

    mapper = GraphSAGELinkMapper(
        G, G.edges(), edge_labels, batch_size=n_batch, num_samples=[2, 2]
    )

    assert len(mapper) == 2

    for hop in range(2):
        nf, nl = mapper[hop]
        assert len(nf) == 3 * 2
        for ii in range(2):
            assert nf[ii].shape == (min(n_batch, data_size), 1, n_feat)
            assert nf[ii + 2].shape == (min(n_batch, data_size), 2, n_feat)
            assert nf[ii + 2 * 2].shape == (min(n_batch, data_size), 2 * 2, n_feat)
            assert nl == [0] * min(n_batch, data_size)

    with pytest.raises(IndexError):
        nf, nl = mapper[2]

def test_GraphSAGELinkMapper_2():
    n_feat = 4
    n_batch = 2

    # test graph
    G = nx.Graph()
    elist = [(1, 2), (2, 3), (1, 4), (3, 2)]
    G.add_edges_from(elist)
    data_size = G.number_of_edges()
    edge_labels = [0] * data_size

    # Add example features
    for v in G.nodes():
        G.node[v]["feature"] = np.ones(n_feat)

    with pytest.raises(RuntimeWarning):
        GraphSAGELinkMapper(
            G,
            G.edges(),
            edge_labels,
            batch_size=n_batch,
            num_samples=[2, 2],
            feature_size=8,
        )

def test_GraphSAGELinkMapper_3():
    n_feat = 4
    n_batch = 2

    # test graph
    G = nx.Graph()
    elist = [(1, 2), (2, 3), (1, 4), (3, 2)]
    G.add_edges_from(elist)
    data_size = G.number_of_edges()
    edge_labels = [0] * data_size

    # Add example features
    for v in G.nodes():
        G.node[v]["feature"] = np.ones(n_feat)

    mapper = GraphSAGELinkMapper(
        G, G.edges(), edge_labels, batch_size=n_batch, num_samples=[0]
    )

    assert len(mapper) == 2

    for ii in range(1):
        nf, nl = mapper[ii]
        assert len(nf) == 2 * 2
        for _ in range(len(nf)):
            assert nf[_].shape == (min(n_batch, data_size), 1, n_feat)
        assert nl == [0] * min(n_batch, data_size)

def test_GraphSAGELinkMapper_no_samples():
    n_feat = 4
    n_batch = 2

    # test graph
    G = nx.Graph()
    elist = [(1, 2), (2, 3), (1, 4), (4, 2)]
    G.add_edges_from(elist)
    data_size = G.number_of_edges()
    edge_labels = [0] * data_size

    # Add example features
    for v in G.nodes():
        G.node[v]["feature"] = np.ones(n_feat)

    mapper = GraphSAGELinkMapper(
        G, G.edges(), edge_labels, batch_size=n_batch, num_samples=[]
    )

    assert len(mapper) == 2
    with pytest.raises(ValueError):
        nf, nl = mapper[0]
<commit_msg>Updated tests for GraphSAGELinkMapper class<commit_after># -*- coding: utf-8 -*-
#
# Copyright 2018 Data61, CSIRO
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Mapper tests:

GraphSAGELinkMapper(
        G: nx.Graph,
        ids: List[Any],
        link_labels: List[Any] or np.ndarray,
        batch_size: int,
        num_samples: List[int],
        feature_size: Optional[int] = None,
        name: AnyStr = None,
    )
g
"""
from stellar.mapper.link_mappers import *

import networkx as nx
import random
import numpy as np
import itertools as it
import pytest

def test_LinkMapper_constructor():
    n_feat = 4

    # test graph
    G = nx.Graph()
    elist = [(1, 2), (2, 3), (1, 4), (3, 2)]
    G.add_edges_from(elist)
    edge_labels = [0] * G.number_of_edges()

    mapper = GraphSAGELinkMapper(
        G, G.edges(), edge_labels, batch_size=2, num_samples=[2, 2]
    )
    assert mapper.batch_size == 2
    assert mapper.data_size == 3
    assert len(mapper.ids) == 3

    G = nx.DiGraph()
    G.add_edges_from(elist)
    edge_labels = [0] * G.number_of_edges()
    mapper = GraphSAGELinkMapper(
        G, G.edges(), edge_labels, batch_size=2, num_samples=[2, 2]
    )
    assert mapper.batch_size == 2
    assert mapper.data_size == 4
    assert len(mapper.ids) == 4

def test_GraphSAGELinkMapper_1():
    n_feat = 4
    n_batch = 2

    # test graph
    G = nx.Graph()
    elist = [
        (1, 2),
        (2, 3),
        (1, 4),
        (4, 2),
    ]  # make sure the number of edges is divisible by n_batch, for this test to succeed
    G.add_edges_from(elist)
    data_size = G.number_of_edges()
    edge_labels = [0] * data_size

    # Add example features
    for v in G.nodes():
        G.node[v]["feature"] = np.ones(n_feat)

    mapper = GraphSAGELinkMapper(
        G, G.edges(), edge_labels, batch_size=n_batch, num_samples=[2, 2]
    )

    assert len(mapper) == 2

    for hop in range(2):
        nf, nl = mapper[hop]
        assert len(nf) == 3 * 2
        for ii in range(2):
            assert nf[ii].shape == (min(n_batch, data_size), 1, n_feat)
            assert nf[ii + 2].shape == (min(n_batch, data_size), 2, n_feat)
            assert nf[ii + 2 * 2].shape == (min(n_batch, data_size), 2 * 2, n_feat)
            assert nl == [0] * min(n_batch, data_size)

    with pytest.raises(IndexError):
        nf, nl = mapper[2]

def test_GraphSAGELinkMapper_2():
    n_feat = 4
    n_batch = 2

    # test graph
    G = nx.Graph()
    elist = [(1, 2), (2, 3), (1, 4), (3, 2)]
    G.add_edges_from(elist)
    data_size = G.number_of_edges()
    edge_labels = [0] * data_size

    # Add example features
    for v in G.nodes():
        G.node[v]["feature"] = np.ones(n_feat)

    with pytest.raises(RuntimeWarning):
        GraphSAGELinkMapper(
            G,
            G.edges(),
            edge_labels,
            batch_size=n_batch,
            num_samples=[2, 2],
            feature_size=8,
        )

def test_GraphSAGELinkMapper_3():
    n_feat = 4
    n_batch = 2

    # test graph
    G = nx.Graph()
    elist = [(1, 2), (2, 3), (1, 4), (3, 2)]
    G.add_edges_from(elist)
    data_size = G.number_of_edges()
    edge_labels = [0] * data_size

    # Add example features
    for v in G.nodes():
        G.node[v]["feature"] = np.ones(n_feat)

    mapper = GraphSAGELinkMapper(
        G, G.edges(), edge_labels, batch_size=n_batch, num_samples=[0]
    )

    assert len(mapper) == 2

    for ii in range(1):
        nf, nl = mapper[ii]
        assert len(nf) == 2 * 2
        for _ in range(len(nf)):
            assert nf[_].shape == (min(n_batch, data_size), 1, n_feat)
        assert nl == [0] * min(n_batch, data_size)

def test_GraphSAGELinkMapper_no_samples():
    """
    The SampledBFS sampler, created inside the mapper, currently throws a ValueError when the num_samples list is empty.
    This might change in the future, so this test might have to be re-written.

    """
    n_feat = 4
    n_batch = 2

    # test graph
    G = nx.Graph()
    elist = [(1, 2), (2, 3), (1, 4), (4, 2)]
    G.add_edges_from(elist)
    data_size = G.number_of_edges()
    edge_labels = [0] * data_size

    # Add example features
    for v in G.nodes():
        G.node[v]["feature"] = np.ones(n_feat)

    mapper = GraphSAGELinkMapper(
        G, G.edges(), edge_labels, batch_size=n_batch, num_samples=[]
    )

    assert len(mapper) == 2
    with pytest.raises(ValueError):
        nf, nl = mapper[0]
<|endoftext|>


<commit_before>"""
Tests for the mock of the add target endpoint.
"""

import base64
import io
import json
import random
from string import hexdigits
from typing import Any, Dict, Union
from urllib.parse import urljoin

import pytest
import requests
from _pytest.fixtures import SubRequest
from PIL import Image
from requests import codes
from requests_mock import POST

from common.constants import ResultCodes
from tests.mock_vws.utils import assert_vws_failure, assert_vws_response
from tests.utils import VuforiaServerCredentials
from vws._request_utils import authorization_header, rfc_1123_date

def assert_valid_target_id(target_id: str) -> None:
    """
    Assert that a given Target ID is in a valid format.

    Args:
        target_id: The Target ID to check.

    Raises:
        AssertionError: The Target ID is not in a valid format.
    """
    assert len(target_id) == 32
    assert all(char in hexdigits for char in target_id)

def _image_file(file_format: str) -> io.BytesIO:
    """
    Return an image file in the given format.

    Args:
        file_format: See
            http://pillow.readthedocs.io/en/3.1.x/handbook/image-file-formats.html
    """
    image_buffer = io.BytesIO()
    width = 1
    height = 1
    image = Image.new('RGB', (width, height))
    image.save(image_buffer, file_format)
    image_buffer.seek(0)
    return image_buffer

@pytest.fixture
def png_file() -> io.BytesIO:
    """
    Return a PNG file.
    """
    return _image_file(file_format='PNG')

@pytest.fixture
def jpeg_file() -> io.BytesIO:
    """
    Return a JPEG file.
    """
    return _image_file(file_format='JPEG')

@pytest.fixture
def tiff_file() -> io.BytesIO:
    """
    Return a TIFF file.
    """
    return _image_file(file_format='TIFF')

@pytest.fixture(params=['png_file', 'jpeg_file'])
def image_file(request: SubRequest) -> io.BytesIO:
    """
    Return an image file.
    """
    return request.getfixturevalue(request.param)

def add_target(
    vuforia_server_credentials: VuforiaServerCredentials,
    data: Dict[str, Any],
    content_type: str='application/json',
) -> requests.Response:
    """
    Helper to make a request to the endpoint to add a target.

    Args:
        vuforia_server_credentials: The credentials to use to connect to
            Vuforia.
        data: The data to send, in JSON format, to the endpoint.
        content_type: The `Content-Type` header to use.

    Returns:
        The response returned by the API.
    """
    date = rfc_1123_date()
    request_path = '/targets'

    content = bytes(json.dumps(data), encoding='utf-8')

    authorization_string = authorization_header(
        access_key=vuforia_server_credentials.access_key,
        secret_key=vuforia_server_credentials.secret_key,
        method=POST,
        content=content,
        content_type=content_type,
        date=date,
        request_path=request_path,
    )

    headers = {
        "Authorization": authorization_string,
        "Date": date,
        'Content-Type': content_type,
    }

    response = requests.request(
        method=POST,
        url=urljoin('https://vws.vuforia.com/', request_path),
        headers=headers,
        data=content,
    )

    return response

@pytest.mark.usefixtures('verify_mock_vuforia')
class TestAddTarget:
    """
    Tests for the mock of the add target endpoint at `POST /targets`.
    """

    @pytest.mark.parametrize('content_type', [
        # This is the documented required content type:
        'application/json',
        # Other content types also work.
        'other/content_type',
    ], ids=['Documented Content-Type', 'Undocumented Content-Type'])
    @pytest.mark.parametrize('name', [
        'a',
        'a' * 64,
    ], ids=['Short name', 'Long name'])
    @pytest.mark.parametrize('width', [0, 0.1],
                             ids=['Zero width', 'Float width'])
    def test_created(self,
                     vuforia_server_credentials: VuforiaServerCredentials,
                     image_file: io.BytesIO,  # noqa: E501 pylint: disable=redefined-outer-name
                     content_type: str,
                     name: str,
                     width: Union[int, float],
                     ) -> None:
        """It is possible to get a `TargetCreated` response."""
        image_data = image_file.read()
        image_data_encoded = base64.b64encode(image_data).decode('ascii')

        data = {
            'name': name,
            'width': width,
            'image': image_data_encoded,
        }

        response = add_target(
            vuforia_server_credentials=vuforia_server_credentials,
            data=data,
            content_type=content_type,
        )

        assert_vws_response(
            response=response,
            status_code=codes.CREATED,
            result_code=ResultCodes.TARGET_CREATED,
        )
        expected_keys = {'result_code', 'transaction_id', 'target_id'}
        assert response.json().keys() == expected_keys
        assert_valid_target_id(target_id=response.json()['target_id'])

    @pytest.mark.parametrize(
        'width',
        [-1, '10'],
        ids=['Negative', 'Wrong Type'],
    )
    def test_width_invalid(self,
                           vuforia_server_credentials:
                           VuforiaServerCredentials,
                           png_file: io.BytesIO,  # noqa: E501 pylint: disable=redefined-outer-name

                           width: Any) -> None:
        """
        The width must be a non-negative number.
        """
        image_data = png_file.read()
        image_data_encoded = base64.b64encode(image_data).decode('ascii')

        data = {
            'name': 'example_name',
            'width': width,
            'image': image_data_encoded,
        }

        response = add_target(
            vuforia_server_credentials=vuforia_server_credentials,
            data=data,
        )

        assert_vws_failure(
            response=response,
            status_code=codes.BAD_REQUEST,
            result_code=ResultCodes.FAIL,
        )

    @pytest.mark.parametrize('data_to_remove', ['name', 'width', 'image'])
    def test_missing_data(self,
                          vuforia_server_credentials:
                          VuforiaServerCredentials,
                          png_file: io.BytesIO,  # noqa: E501 pylint: disable=redefined-outer-name

                          data_to_remove: str,
                          ) -> None:
        """
        `name`, `width` and `image` are all required.
        """
        image_data = png_file.read()
        image_data_encoded = base64.b64encode(image_data).decode('ascii')

        data = {
            'name': 'example_name',
            'width': 1,
            'image': image_data_encoded,
        }
        data.pop(data_to_remove)

        response = add_target(
            vuforia_server_credentials=vuforia_server_credentials,
            data=data,
        )

        assert_vws_failure(
            response=response,
            status_code=codes.BAD_REQUEST,
            result_code=ResultCodes.FAIL,
        )

    @pytest.mark.parametrize(
        'name',
        [1, '', 'a' * 65],
        ids=['Wrong Type', 'Empty', 'Too Long'],
    )
    def test_name_invalid(self,
                          name: str,
                          png_file: io.BytesIO,  # noqa: E501 pylint: disable=redefined-outer-name
                          vuforia_server_credentials: VuforiaServerCredentials
                          ) -> None:
        """
        A target's name must be a string of length 0 < N < 65.
        """
        image_data = png_file.read()
        image_data_encoded = base64.b64encode(image_data).decode('ascii')

        data = {
            'name': name,
            'width': 1,
            'image': image_data_encoded,
        }

        response = add_target(
            vuforia_server_credentials=vuforia_server_credentials,
            data=data,
        )

        assert_vws_failure(
            response=response,
            status_code=codes.BAD_REQUEST,
            result_code=ResultCodes.FAIL,
        )

@pytest.mark.usefixtures('verify_mock_vuforia')
class TestInvalidImage:
    """
    Tests for giving images which do not conform to the specifications
    detailed in "Supported Images" on
    https://library.vuforia.com/articles/Training/Image-Target-Guide
    """

    def test_invalid_type(self,
                          tiff_file: io.BytesIO,
                          vuforia_server_credentials: VuforiaServerCredentials,
                          ) -> None:
        """
        A `BAD_REQUEST` response is returned if an image which is not a JPEG
        or PNG file is given.
        """
        image_data = tiff_file.read()
        image_data_encoded = base64.b64encode(image_data).decode('ascii')

        data = {
            'name': 'example_name',
            'width': 1,
            'image': image_data_encoded,
        }

        response = add_target(
            vuforia_server_credentials=vuforia_server_credentials,
            data=data,
        )

        assert_vws_failure(
            response=response,
            status_code=codes.UNPROCESSABLE_ENTITY,
            result_code=ResultCodes.BAD_IMAGE,
        )

@pytest.mark.usefixtures('verify_mock_vuforia')
class TestNotMandatoryFields:
    """
    Tests for passing data which is not mandatory to the endpoint.
    """

    def test_invalid_extra_data(self,
                                vuforia_server_credentials:
                                VuforiaServerCredentials,
                                png_file: io.BytesIO,  # noqa: E501 pylint: disable=redefined-outer-name
                                ) -> None:
        """
        A `BAD_REQUEST` response is returned when unexpected data is given.
        """
        image_data = png_file.read()
        image_data_encoded = base64.b64encode(image_data).decode('ascii')

        data = {
            'name': 'example_name',
            'width': 1,
            'image': image_data_encoded,
            'extra_thing': 1,
        }

        response = add_target(
            vuforia_server_credentials=vuforia_server_credentials,
            data=data,
        )

        assert_vws_failure(
            response=response,
            status_code=codes.BAD_REQUEST,
            result_code=ResultCodes.FAIL,
        )
<commit_msg>Fix flake8<commit_after>"""
Tests for the mock of the add target endpoint.
"""

import base64
import io
import json
from string import hexdigits
from typing import Any, Dict, Union
from urllib.parse import urljoin

import pytest
import requests
from _pytest.fixtures import SubRequest
from PIL import Image
from requests import codes
from requests_mock import POST

from common.constants import ResultCodes
from tests.mock_vws.utils import assert_vws_failure, assert_vws_response
from tests.utils import VuforiaServerCredentials
from vws._request_utils import authorization_header, rfc_1123_date

def assert_valid_target_id(target_id: str) -> None:
    """
    Assert that a given Target ID is in a valid format.

    Args:
        target_id: The Target ID to check.

    Raises:
        AssertionError: The Target ID is not in a valid format.
    """
    assert len(target_id) == 32
    assert all(char in hexdigits for char in target_id)

def _image_file(file_format: str) -> io.BytesIO:
    """
    Return an image file in the given format.

    Args:
        file_format: See
            http://pillow.readthedocs.io/en/3.1.x/handbook/image-file-formats.html
    """
    image_buffer = io.BytesIO()
    width = 1
    height = 1
    image = Image.new('RGB', (width, height))
    image.save(image_buffer, file_format)
    image_buffer.seek(0)
    return image_buffer

@pytest.fixture
def png_file() -> io.BytesIO:
    """
    Return a PNG file.
    """
    return _image_file(file_format='PNG')

@pytest.fixture
def jpeg_file() -> io.BytesIO:
    """
    Return a JPEG file.
    """
    return _image_file(file_format='JPEG')

@pytest.fixture
def tiff_file() -> io.BytesIO:
    """
    Return a TIFF file.
    """
    return _image_file(file_format='TIFF')

@pytest.fixture(params=['png_file', 'jpeg_file'])
def image_file(request: SubRequest) -> io.BytesIO:
    """
    Return an image file.
    """
    return request.getfixturevalue(request.param)

def add_target(
    vuforia_server_credentials: VuforiaServerCredentials,
    data: Dict[str, Any],
    content_type: str='application/json',
) -> requests.Response:
    """
    Helper to make a request to the endpoint to add a target.

    Args:
        vuforia_server_credentials: The credentials to use to connect to
            Vuforia.
        data: The data to send, in JSON format, to the endpoint.
        content_type: The `Content-Type` header to use.

    Returns:
        The response returned by the API.
    """
    date = rfc_1123_date()
    request_path = '/targets'

    content = bytes(json.dumps(data), encoding='utf-8')

    authorization_string = authorization_header(
        access_key=vuforia_server_credentials.access_key,
        secret_key=vuforia_server_credentials.secret_key,
        method=POST,
        content=content,
        content_type=content_type,
        date=date,
        request_path=request_path,
    )

    headers = {
        "Authorization": authorization_string,
        "Date": date,
        'Content-Type': content_type,
    }

    response = requests.request(
        method=POST,
        url=urljoin('https://vws.vuforia.com/', request_path),
        headers=headers,
        data=content,
    )

    return response

@pytest.mark.usefixtures('verify_mock_vuforia')
class TestAddTarget:
    """
    Tests for the mock of the add target endpoint at `POST /targets`.
    """

    @pytest.mark.parametrize('content_type', [
        # This is the documented required content type:
        'application/json',
        # Other content types also work.
        'other/content_type',
    ], ids=['Documented Content-Type', 'Undocumented Content-Type'])
    @pytest.mark.parametrize('name', [
        'a',
        'a' * 64,
    ], ids=['Short name', 'Long name'])
    @pytest.mark.parametrize('width', [0, 0.1],
                             ids=['Zero width', 'Float width'])
    def test_created(self,
                     vuforia_server_credentials: VuforiaServerCredentials,
                     image_file: io.BytesIO,  # noqa: E501 pylint: disable=redefined-outer-name
                     content_type: str,
                     name: str,
                     width: Union[int, float],
                     ) -> None:
        """It is possible to get a `TargetCreated` response."""
        image_data = image_file.read()
        image_data_encoded = base64.b64encode(image_data).decode('ascii')

        data = {
            'name': name,
            'width': width,
            'image': image_data_encoded,
        }

        response = add_target(
            vuforia_server_credentials=vuforia_server_credentials,
            data=data,
            content_type=content_type,
        )

        assert_vws_response(
            response=response,
            status_code=codes.CREATED,
            result_code=ResultCodes.TARGET_CREATED,
        )
        expected_keys = {'result_code', 'transaction_id', 'target_id'}
        assert response.json().keys() == expected_keys
        assert_valid_target_id(target_id=response.json()['target_id'])

    @pytest.mark.parametrize(
        'width',
        [-1, '10'],
        ids=['Negative', 'Wrong Type'],
    )
    def test_width_invalid(self,
                           vuforia_server_credentials:
                           VuforiaServerCredentials,
                           png_file: io.BytesIO,  # noqa: E501 pylint: disable=redefined-outer-name

                           width: Any) -> None:
        """
        The width must be a non-negative number.
        """
        image_data = png_file.read()
        image_data_encoded = base64.b64encode(image_data).decode('ascii')

        data = {
            'name': 'example_name',
            'width': width,
            'image': image_data_encoded,
        }

        response = add_target(
            vuforia_server_credentials=vuforia_server_credentials,
            data=data,
        )

        assert_vws_failure(
            response=response,
            status_code=codes.BAD_REQUEST,
            result_code=ResultCodes.FAIL,
        )

    @pytest.mark.parametrize('data_to_remove', ['name', 'width', 'image'])
    def test_missing_data(self,
                          vuforia_server_credentials:
                          VuforiaServerCredentials,
                          png_file: io.BytesIO,  # noqa: E501 pylint: disable=redefined-outer-name

                          data_to_remove: str,
                          ) -> None:
        """
        `name`, `width` and `image` are all required.
        """
        image_data = png_file.read()
        image_data_encoded = base64.b64encode(image_data).decode('ascii')

        data = {
            'name': 'example_name',
            'width': 1,
            'image': image_data_encoded,
        }
        data.pop(data_to_remove)

        response = add_target(
            vuforia_server_credentials=vuforia_server_credentials,
            data=data,
        )

        assert_vws_failure(
            response=response,
            status_code=codes.BAD_REQUEST,
            result_code=ResultCodes.FAIL,
        )

    @pytest.mark.parametrize(
        'name',
        [1, '', 'a' * 65],
        ids=['Wrong Type', 'Empty', 'Too Long'],
    )
    def test_name_invalid(self,
                          name: str,
                          png_file: io.BytesIO,  # noqa: E501 pylint: disable=redefined-outer-name
                          vuforia_server_credentials: VuforiaServerCredentials
                          ) -> None:
        """
        A target's name must be a string of length 0 < N < 65.
        """
        image_data = png_file.read()
        image_data_encoded = base64.b64encode(image_data).decode('ascii')

        data = {
            'name': name,
            'width': 1,
            'image': image_data_encoded,
        }

        response = add_target(
            vuforia_server_credentials=vuforia_server_credentials,
            data=data,
        )

        assert_vws_failure(
            response=response,
            status_code=codes.BAD_REQUEST,
            result_code=ResultCodes.FAIL,
        )

@pytest.mark.usefixtures('verify_mock_vuforia')
class TestInvalidImage:
    """
    Tests for giving images which do not conform to the specifications
    detailed in "Supported Images" on
    https://library.vuforia.com/articles/Training/Image-Target-Guide
    """

    def test_invalid_type(self,
                          tiff_file: io.BytesIO,
                          vuforia_server_credentials: VuforiaServerCredentials,
                          ) -> None:
        """
        A `BAD_REQUEST` response is returned if an image which is not a JPEG
        or PNG file is given.
        """
        image_data = tiff_file.read()
        image_data_encoded = base64.b64encode(image_data).decode('ascii')

        data = {
            'name': 'example_name',
            'width': 1,
            'image': image_data_encoded,
        }

        response = add_target(
            vuforia_server_credentials=vuforia_server_credentials,
            data=data,
        )

        assert_vws_failure(
            response=response,
            status_code=codes.UNPROCESSABLE_ENTITY,
            result_code=ResultCodes.BAD_IMAGE,
        )

@pytest.mark.usefixtures('verify_mock_vuforia')
class TestNotMandatoryFields:
    """
    Tests for passing data which is not mandatory to the endpoint.
    """

    def test_invalid_extra_data(self,
                                vuforia_server_credentials:
                                VuforiaServerCredentials,
                                png_file: io.BytesIO,  # noqa: E501 pylint: disable=redefined-outer-name
                                ) -> None:
        """
        A `BAD_REQUEST` response is returned when unexpected data is given.
        """
        image_data = png_file.read()
        image_data_encoded = base64.b64encode(image_data).decode('ascii')

        data = {
            'name': 'example_name',
            'width': 1,
            'image': image_data_encoded,
            'extra_thing': 1,
        }

        response = add_target(
            vuforia_server_credentials=vuforia_server_credentials,
            data=data,
        )

        assert_vws_failure(
            response=response,
            status_code=codes.BAD_REQUEST,
            result_code=ResultCodes.FAIL,
        )
<|endoftext|>


<commit_before># Copyright 2021 The QHBM Library Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Tests for the orthogonal_ensemble module."""

import cirq
import sympy
import tensorflow as tf
import tensorflow_probability as tfp
import tensorflow_quantum as tfq

from qhbmlib import orthogonal_ensemble
from tests import test_util

# Global tolerance, set for float32.
ATOL = 1e-5

class BuildBitCircuitTest(tf.test.TestCase):
  """Test build_bit_circuit from the qhbm library."""

  def test_build_bit_circuit(self):
    """Confirm correct bit injector circuit creation."""
    my_qubits = [
        cirq.GridQubit(0, 2),
        cirq.GridQubit(1, 4),
        cirq.GridQubit(2, 2)
    ]
    identifier = "build_bit_test"
    test_circuit, test_symbols = orthogonal_ensemble.build_bit_circuit(
        my_qubits, identifier)
    expected_symbols = list(
        sympy.symbols(
            "_bit_build_bit_test_0 _bit_build_bit_test_1 _bit_build_bit_test_2")
    )
    expected_circuit = cirq.Circuit(
        [cirq.X(q)**s for q, s in zip(my_qubits, expected_symbols)])
    self.assertAllEqual(test_symbols, expected_symbols)
    self.assertEqual(test_circuit, expected_circuit)

class InputChecksTest(tf.test.TestCase):
  """Tests all the input checking functions used for QHBMs."""

  def test_upgrade_initial_values(self):
    """Confirms lists of values are properly upgraded to variables."""
    # Test allowed inputs.
    true_list = [-5.1, 2.8, -3.4, 4.8]
    true_tensor = tf.constant(true_list, dtype=tf.float32)
    true_variable = tf.Variable(true_tensor)
    self.assertAllClose(
        orthogonal_ensemble.upgrade_initial_values(true_list),
        true_variable,
        atol=ATOL)
    self.assertAllClose(
        orthogonal_ensemble.upgrade_initial_values(true_tensor),
        true_variable,
        atol=ATOL)
    self.assertAllClose(
        orthogonal_ensemble.upgrade_initial_values(true_variable),
        true_variable,
        atol=ATOL)
    # Check for bad inputs.
    with self.assertRaisesRegex(TypeError, "numeric type"):
      _ = orthogonal_ensemble.upgrade_initial_values("junk")
    with self.assertRaisesRegex(ValueError, "must be 1D"):
      _ = orthogonal_ensemble.upgrade_initial_values([[5.2]])

  def test_upgrade_symbols(self):
    """Confirms symbols are upgraded appropriately."""
    true_symbol_names = ["test1", "a", "my_symbol", "MySymbol2"]
    values = tf.constant([0 for _ in true_symbol_names])
    true_symbols = [sympy.Symbol(s) for s in true_symbol_names]
    true_symbols_t = tf.constant(true_symbol_names, dtype=tf.string)
    self.assertAllEqual(
        true_symbols_t,
        orthogonal_ensemble.upgrade_symbols(true_symbols, values))
    # Test bad inputs.
    with self.assertRaisesRegex(TypeError, "must be `sympy.Symbol`"):
      _ = orthogonal_ensemble.upgrade_symbols(true_symbols[:-1] + ["bad"],
                                              values)
    with self.assertRaisesRegex(ValueError, "must be unique"):
      _ = orthogonal_ensemble.upgrade_symbols(
          true_symbols[:-1] + true_symbols[:1], values)
    with self.assertRaisesRegex(ValueError, "symbol for every value"):
      _ = orthogonal_ensemble.upgrade_symbols(true_symbols, values[:-1])
    with self.assertRaisesRegex(TypeError, "must be an iterable"):
      _ = orthogonal_ensemble.upgrade_symbols(5, values)

  def test_upgrade_circuit(self):
    """Confirms circuits are upgraded appropriately."""
    qubits = cirq.GridQubit.rect(1, 5)
    true_symbol_names = ["a", "b"]
    true_symbols = [sympy.Symbol(s) for s in true_symbol_names]
    true_symbols_t = tf.constant(true_symbol_names, dtype=tf.string)
    true_circuit = cirq.Circuit()
    for q in qubits:
      for s in true_symbols:
        true_circuit += cirq.X(q)**s
    true_circuit_t = tfq.convert_to_tensor([true_circuit])
    self.assertEqual(
        tfq.from_tensor(true_circuit_t),
        tfq.from_tensor(
            orthogonal_ensemble.upgrade_circuit(true_circuit, true_symbols_t)),
    )
    # Test bad inputs.
    with self.assertRaisesRegex(TypeError, "must be a `cirq.Circuit`"):
      _ = orthogonal_ensemble.upgrade_circuit("junk", true_symbols_t)
    with self.assertRaisesRegex(TypeError, "must be a `tf.Tensor`"):
      _ = orthogonal_ensemble.upgrade_circuit(true_circuit, true_symbol_names)
    with self.assertRaisesRegex(TypeError, "dtype `tf.string`"):
      _ = orthogonal_ensemble.upgrade_circuit(true_circuit, tf.constant([5.5]))
    with self.assertRaisesRegex(ValueError, "must contain"):
      _ = orthogonal_ensemble.upgrade_circuit(true_circuit,
                                              tf.constant(["a", "junk"]))
    with self.assertRaisesRegex(ValueError, "Empty circuit"):
      _ = orthogonal_ensemble.upgrade_circuit(cirq.Circuit(),
                                              tf.constant([], dtype=tf.string))

class OrthogonalEnsembleTest(tf.test.TestCase):
  """Tests the OrthogonalEnsemble class."""

  num_bits = 5
  raw_phis_symbols = [sympy.Symbol("s0"), sympy.Symbol("s1")]
  phis_symbols = tf.constant([str(s) for s in raw_phis_symbols])
  initial_phis = tf.random.uniform([len(phis_symbols)], minval=-1.0)
  raw_qubits = cirq.GridQubit.rect(1, num_bits)
  u = cirq.Circuit()
  for s in raw_phis_symbols:
    for q in raw_qubits:
      u += cirq.X(q)**s
  name = "TestOE"
  raw_bit_circuit, raw_bit_symbols = orthogonal_ensemble.build_bit_circuit(
      raw_qubits, name)
  bit_symbols = tf.constant([str(s) for s in raw_bit_symbols])
  bit_circuit = tfq.convert_to_tensor([raw_bit_circuit])

  def test_init(self):
    """Confirms OrthogonalEnsemble is initialized correctly."""
    test_oe = orthogonal_ensemble.OrthogonalEnsemble(
        self.u,
        self.raw_phis_symbols,
        self.initial_phis,
        self.name,
    )
    self.assertEqual(self.name, test_oe.name)
    self.assertAllClose(self.initial_phis, test_oe.phis)
    self.assertAllEqual(self.phis_symbols, test_oe.phis_symbols)
    self.assertAllEqual(
        tfq.from_tensor(tfq.convert_to_tensor([self.u])),
        tfq.from_tensor(test_oe.u),
    )
    self.assertAllEqual(
        tfq.from_tensor(tfq.convert_to_tensor([self.u**-1])),
        tfq.from_tensor(test_oe.u_dagger),
    )
    self.assertAllEqual(self.raw_qubits, test_oe.raw_qubits)
    self.assertAllEqual(self.bit_symbols, test_oe.bit_symbols)
    self.assertEqual(
        tfq.from_tensor(self.bit_circuit), tfq.from_tensor(test_oe.bit_circuit))

if __name__ == "__main__":
  print("Running orthogonal_ensemble_test.py ...")
  tf.test.main()
<commit_msg>Changed print statement<commit_after># Copyright 2021 The QHBM Library Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Tests for the orthogonal_ensemble module."""

from absl import logging

import cirq
import sympy
import tensorflow as tf
import tensorflow_probability as tfp
import tensorflow_quantum as tfq

from qhbmlib import orthogonal_ensemble

# Global tolerance, set for float32.
ATOL = 1e-5

class BuildBitCircuitTest(tf.test.TestCase):
  """Test build_bit_circuit from the qhbm library."""

  def test_build_bit_circuit(self):
    """Confirm correct bit injector circuit creation."""
    my_qubits = [
        cirq.GridQubit(0, 2),
        cirq.GridQubit(1, 4),
        cirq.GridQubit(2, 2)
    ]
    identifier = "build_bit_test"
    test_circuit, test_symbols = orthogonal_ensemble.build_bit_circuit(
        my_qubits, identifier)
    expected_symbols = list(
        sympy.symbols(
            "_bit_build_bit_test_0 _bit_build_bit_test_1 _bit_build_bit_test_2")
    )
    expected_circuit = cirq.Circuit(
        [cirq.X(q)**s for q, s in zip(my_qubits, expected_symbols)])
    self.assertAllEqual(test_symbols, expected_symbols)
    self.assertEqual(test_circuit, expected_circuit)

class InputChecksTest(tf.test.TestCase):
  """Tests all the input checking functions used for QHBMs."""

  def test_upgrade_initial_values(self):
    """Confirms lists of values are properly upgraded to variables."""
    # Test allowed inputs.
    true_list = [-5.1, 2.8, -3.4, 4.8]
    true_tensor = tf.constant(true_list, dtype=tf.float32)
    true_variable = tf.Variable(true_tensor)
    self.assertAllClose(
        orthogonal_ensemble.upgrade_initial_values(true_list),
        true_variable,
        atol=ATOL)
    self.assertAllClose(
        orthogonal_ensemble.upgrade_initial_values(true_tensor),
        true_variable,
        atol=ATOL)
    self.assertAllClose(
        orthogonal_ensemble.upgrade_initial_values(true_variable),
        true_variable,
        atol=ATOL)
    # Check for bad inputs.
    with self.assertRaisesRegex(TypeError, "numeric type"):
      _ = orthogonal_ensemble.upgrade_initial_values("junk")
    with self.assertRaisesRegex(ValueError, "must be 1D"):
      _ = orthogonal_ensemble.upgrade_initial_values([[5.2]])

  def test_upgrade_symbols(self):
    """Confirms symbols are upgraded appropriately."""
    true_symbol_names = ["test1", "a", "my_symbol", "MySymbol2"]
    values = tf.constant([0 for _ in true_symbol_names])
    true_symbols = [sympy.Symbol(s) for s in true_symbol_names]
    true_symbols_t = tf.constant(true_symbol_names, dtype=tf.string)
    self.assertAllEqual(
        true_symbols_t,
        orthogonal_ensemble.upgrade_symbols(true_symbols, values))
    # Test bad inputs.
    with self.assertRaisesRegex(TypeError, "must be `sympy.Symbol`"):
      _ = orthogonal_ensemble.upgrade_symbols(true_symbols[:-1] + ["bad"],
                                              values)
    with self.assertRaisesRegex(ValueError, "must be unique"):
      _ = orthogonal_ensemble.upgrade_symbols(
          true_symbols[:-1] + true_symbols[:1], values)
    with self.assertRaisesRegex(ValueError, "symbol for every value"):
      _ = orthogonal_ensemble.upgrade_symbols(true_symbols, values[:-1])
    with self.assertRaisesRegex(TypeError, "must be an iterable"):
      _ = orthogonal_ensemble.upgrade_symbols(5, values)

  def test_upgrade_circuit(self):
    """Confirms circuits are upgraded appropriately."""
    qubits = cirq.GridQubit.rect(1, 5)
    true_symbol_names = ["a", "b"]
    true_symbols = [sympy.Symbol(s) for s in true_symbol_names]
    true_symbols_t = tf.constant(true_symbol_names, dtype=tf.string)
    true_circuit = cirq.Circuit()
    for q in qubits:
      for s in true_symbols:
        true_circuit += cirq.X(q)**s
    true_circuit_t = tfq.convert_to_tensor([true_circuit])
    self.assertEqual(
        tfq.from_tensor(true_circuit_t),
        tfq.from_tensor(
            orthogonal_ensemble.upgrade_circuit(true_circuit, true_symbols_t)),
    )
    # Test bad inputs.
    with self.assertRaisesRegex(TypeError, "must be a `cirq.Circuit`"):
      _ = orthogonal_ensemble.upgrade_circuit("junk", true_symbols_t)
    with self.assertRaisesRegex(TypeError, "must be a `tf.Tensor`"):
      _ = orthogonal_ensemble.upgrade_circuit(true_circuit, true_symbol_names)
    with self.assertRaisesRegex(TypeError, "dtype `tf.string`"):
      _ = orthogonal_ensemble.upgrade_circuit(true_circuit, tf.constant([5.5]))
    with self.assertRaisesRegex(ValueError, "must contain"):
      _ = orthogonal_ensemble.upgrade_circuit(true_circuit,
                                              tf.constant(["a", "junk"]))
    with self.assertRaisesRegex(ValueError, "Empty circuit"):
      _ = orthogonal_ensemble.upgrade_circuit(cirq.Circuit(),
                                              tf.constant([], dtype=tf.string))

class OrthogonalEnsembleTest(tf.test.TestCase):
  """Tests the OrthogonalEnsemble class."""

  num_bits = 5
  raw_phis_symbols = [sympy.Symbol("s0"), sympy.Symbol("s1")]
  phis_symbols = tf.constant([str(s) for s in raw_phis_symbols])
  initial_phis = tf.random.uniform([len(phis_symbols)], minval=-1.0)
  raw_qubits = cirq.GridQubit.rect(1, num_bits)
  u = cirq.Circuit()
  for s in raw_phis_symbols:
    for q in raw_qubits:
      u += cirq.X(q)**s
  name = "TestOE"
  raw_bit_circuit, raw_bit_symbols = orthogonal_ensemble.build_bit_circuit(
      raw_qubits, name)
  bit_symbols = tf.constant([str(s) for s in raw_bit_symbols])
  bit_circuit = tfq.convert_to_tensor([raw_bit_circuit])

  def test_init(self):
    """Confirms OrthogonalEnsemble is initialized correctly."""
    test_oe = orthogonal_ensemble.OrthogonalEnsemble(
        self.u,
        self.raw_phis_symbols,
        self.initial_phis,
        self.name,
    )
    self.assertEqual(self.name, test_oe.name)
    self.assertAllClose(self.initial_phis, test_oe.phis)
    self.assertAllEqual(self.phis_symbols, test_oe.phis_symbols)
    self.assertAllEqual(
        tfq.from_tensor(tfq.convert_to_tensor([self.u])),
        tfq.from_tensor(test_oe.u),
    )
    self.assertAllEqual(
        tfq.from_tensor(tfq.convert_to_tensor([self.u**-1])),
        tfq.from_tensor(test_oe.u_dagger),
    )
    self.assertAllEqual(self.raw_qubits, test_oe.raw_qubits)
    self.assertAllEqual(self.bit_symbols, test_oe.bit_symbols)
    self.assertEqual(
        tfq.from_tensor(self.bit_circuit), tfq.from_tensor(test_oe.bit_circuit))

if __name__ == "__main__":
  logging.info("Running orthogonal_ensemble_test.py ...")
  tf.test.main()
<|endoftext|>


<commit_before># Log Parser for RTI Connext.
#
#   Copyright 2016 Real-Time Innovations, Inc.
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
"""Create the dictionary for log functions for unmatched logs for debugging.

Functions:
  + get_regex_list: Get the regular expressions and function list.
"""
from __future__ import absolute_import
import logparser.logs.debug.debug as debug

def get_regex_list():
    """Return the regular expressions and functions list for this module."""
    regex = []

    # Blacklist - Messages that we want to ignore.
    regex.append([debug.on_ignored_message,
                  r"^\s*$"])
    regex.append([debug.on_ignored_message,
                  r"DDS_Registry_lock:Locking the storage service"])
    regex.append([debug.on_ignored_message,
                  r"DDS_Registry_unlock:Unlocking the storage service"])
    regex.append([debug.on_ignored_message,
                  r"RTIEventActiveDatabaseThread_loop:created \w+"])
    regex.append([debug.on_ignored_message,
                  r"RTIEventActiveGeneratorThread_loop:created \w+"])
    regex.append([debug.on_ignored_message,
                  r"COMMENDActiveFacadeReceiver_loop:created \w+"])
    regex.append([debug.on_ignored_message,
                  r"COMMENDActiveFacade_threadStarted:thread count ref " +
                  r"count \d+"])
    regex.append([debug.on_ignored_message,
                  r"COMMENDActiveFacade_addReceiverThread:thread count ref " +
                  r"count \d+"])
    regex.append([debug.on_ignored_message,
                  r"COMMENDActiveFacade_new:active object count ref " +
                  r"count \d+"])
    regex.append([debug.on_ignored_message,
                  r"RTIEventJobDispatcher_distributeTokens: \d+ agents " +
                  r"at priority [-\d]+"])
    regex.append([debug.on_ignored_message,
                  r"RTIEventJobDispatcher_updateAgentPriorities:agent:" +
                  r"0x\w+ priority set to [-\d]+"])
    regex.append([debug.on_ignored_message,
                  r"NDDS_Transport_UDPv4_receive_rEA:\w+ woke up"])
    regex.append([debug.on_ignored_message,
                  r"RTIEventJobDispatcher_scheduleJob:agent:\w+ job:\w+ " +
                  r"scheduled at priority \d+"])
    regex.append([debug.on_ignored_message,
                  r"RTIOsapiThread_sleep: nanosleep\(\d+.\d+ s\)"])
    regex.append([debug.on_ignored_message,
                  r"RTIEventActiveGeneratorThread_loop:\w+ gathering events"])
    regex.append([debug.on_ignored_message,
                  r"RTIEventActiveGeneratorThread_loop:\w+ firing events"])
    regex.append([debug.on_ignored_message,
                  r"RTIEventActiveGeneratorThread_loop:\w+ " +
                  r"rescheduling events"])
    regex.append([debug.on_ignored_message,
                  r"RTIEventActiveGeneratorThread_loop:\w+ sleeping " +
                  r"\{\w+,\w+\}"])
    regex.append([debug.on_ignored_message,
                  r"RTIEventActiveDatabaseThread_loop:\w+ collecting garbage"])
    regex.append([debug.on_ignored_message,
                  r"RTIEventActiveDatabaseThread_loop:\w+ sleeping " +
                  r"\{\w+,\w+\}"])
    regex.append([debug.on_ignored_message,
                  r"RTIEventJobDispatcher_updateAgentPriorities:agent:\w+ " +
                  r"priority set to \d+"])
    regex.append([debug.on_ignored_message,
                  r"RTIEventJobDispatcher_distributeTokens: \w+ agents at " +
                  r"priority \d+"])
    regex.append([debug.on_ignored_message,
                  r"RTIOsapiThread_sleep: Sleep\(\d+ ms\)"])
    regex.append([debug.on_ignored_message,
                  r"RTISystemClock_init:epoch range \{\w+,\w+\}, " +
                  r"frequency \d+ Hz"])

    regex.append([debug.on_ignored_message,
                  r"DDS_StringSeq_ensure_length:memory allocation: " +
                  r"original \d+, new \d+"])
    regex.append([debug.on_ignored_message,
                  r"DDS_PropertySeq_ensure_length:memory allocation: " +
                  r"original \d+, new \d+"])
    regex.append([debug.on_ignored_message,
                  r"RTINetioReceiver_addEntryport:" +
                  r"NetioReceiver_Entryport reused"])
    regex.append([debug.on_ignored_message,
                  r"RTINetioSender_addDestination:" +
                  r"NetioSender_Destination reused"])
    regex.append([debug.on_ignored_message,
                  r"COMMENDActiveFacadeReceiver_loop:\w+ disowning receive " +
                  r"resource"])
    regex.append([debug.on_ignored_message,
                  r"COMMENDActiveFacadeReceiver_loop:\w+ parsing message"])
    regex.append([debug.on_ignored_message,
                  r"RTINetioReceiver_receiveFast:\w+ received \d+ bytes"])
    regex.append([debug.on_ignored_message,
                  r"NDDS_Transport_Shmem_receive_rEA:\w+ blocking on 0X\w+"])
    regex.append([debug.on_ignored_message,
                  r"NDDS_Transport_Shmem_receive_rEA:\w+ woke up"])
    regex.append([debug.on_ignored_message,
                  r"NDDS_Transport_UDPv4_receive_rEA:\w+ blocking on 0X\w+"])
    regex.append([debug.on_ignored_message,
                  r"RTIOsapi_getFirstValidInterface:found address for " +
                  r"interface .+ \(address family = \d+\)"])
    regex.append([debug.on_ignored_message,
                  r"RTIOsapi_getFirstValidInterface:skipped interface .+, " +
                  r"\(not valid address family \([\w/]+\)\)"])
    regex.append([debug.on_ignored_message,
                  r"RTIOsapi_getFirstValidInterface:skipped interface .+, " +
                  r"\(loopback interface\)"])
    regex.append([debug.on_ignored_message,
                  r"RTINetioReceiver_removeEntryport:NetioReceiver_" +
                  r"Entryport ref count \d+"])
    regex.append([debug.on_ignored_message,
                  r"NDDS_Transport_UDPv4_SocketFactory_create_" +
                  r"receive_socket:invalid port (\d+)"])
    regex.append([debug.on_ignored_message,
                  r"NDDS_Transport_UDPv4_create_recvresource_rrEA:" +
                  r"Created receive resource for port (\d+)"])
    regex.append([debug.on_ignored_message,
                  r"NDDS_Transport_UDPv4_create_sendresource_srEA:Created " +
                  r"send resource for 0X\w+:\d+"])
    regex.append([debug.on_ignored_message,
                  r"NDDS_Transport_UDPv4_query_interfaces:" +
                  r"skipped (\w+)"])
    regex.append([debug.on_ignored_message,
                  r"NDDS_Transport_UDPv4_create_recvresource_rrEA:" +
                  r"!create socket"])

    regex.append([debug.on_ignored_message,
                  r"DDS_DomainParticipantFactory_create_participant_" +
                  r"disabledI:created participant: domain=\d+, index=-1"])
    regex.append([debug.on_ignored_message,
                  r"DDS_DomainParticipantPresentation_reserve_participant_" +
                  r"index_entryports:Domain \d+:Trying to reserve " +
                  r"participant index=\d+..."])
    regex.append([debug.on_ignored_message,
                  r"DISCPluginManager_onAfterLocalEndpointEnabled:at " +
                  r"\{\w+,\w+\}"])
    regex.append([debug.on_ignored_message,
                  r"DISCSimpleEndpointDiscoveryPluginPDFListener_" +
                  r"onAfterLocalWriterEnabled:at \{\w+,\w+\}"])
    regex.append([debug.on_ignored_message,
                  r"DISCSimpleEndpointDiscoveryPlugin_" +
                  r"subscriptionReaderListenerOnDataAvailable:at \{\w+,\w+\}"])
    regex.append([debug.on_ignored_message,
                  r"DISCPluginManager_onAfterLocalParticipantEnabled:at " +
                  r"\{\w+,\w+\}"])
    regex.append([debug.on_ignored_message,
                  r"DISCEndpointDiscoveryPlugin_assertRemoteEndpoint:at " +
                  r"\{\w+,\w+\}"])
    regex.append([debug.on_ignored_message,
                  r"DISCPluginManager_activateEdpListenersFor" +
                  r"RemoteParticipant:at \{\w+,\w+\}"])
    regex.append([debug.on_ignored_message,
                  r"DISCParticipantDiscoveryPlugin_assertRemoteParticipant:" +
                  r"at \{\w+,\w+\}"])
    regex.append([debug.on_ignored_message,
                  r"DISCSimpleParticipantDiscoveryPluginReaderListener_" +
                  r"onDataAvailable:at \{\w+,\w+\}"])
    regex.append([debug.on_ignored_message,
                  r"DISCSimpleParticipantDiscoveryPlugin_" +
                  r"remoteParticipantDiscovered:at \{\w+,\w+\}"])
    regex.append([debug.on_ignored_message,
                  r"DISCSimpleEndpointDiscoveryPlugin_publicationReader" +
                  r"ListenerOnDataAvailable:at \{\w+,\w+\}"])
    regex.append([debug.on_ignored_message,
                  r"DISCSimpleEndpointDiscoveryPluginPDFListener_" +
                  r"onAfterLocalReaderEnabled:at \{\w+,\w+\}"])
    regex.append([debug.on_ignored_message,
                  r"DDS_Topic_createI:!create presentation topic"])
    regex.append([debug.on_ignored_message,
                  r"DDS_DomainParticipant_create_topic_disabledI:" +
                  r"!create topic"])
    regex.append([debug.on_ignored_message,
                  r"DDSTopic_impl::createI:!create topic"])
    regex.append([debug.on_ignored_message,
                  r"PRESParticipant_destroyAllEntities:" +
                  r"!delete flow controller"])
    regex.append([debug.on_ignored_message,
                  r"DDS_DomainParticipant_delete_contained_entities:" +
                  r"!delete contained entitie"])
    regex.append([debug.on_ignored_message,
                  r"DISCSimpleParticipantDiscoveryPluginReaderListener_" +
                  r"onDataAvailable:discovered modified participant: " +
                  r"host=0x\w+, app=0x\w+, instance=0x\w+"])

    regex.append([debug.on_ignored_message,
                  r"PRESPsService_onWriterResendEvent:writer resend event: " +
                  r"\(([\w,]+)\)"])
    regex.append([debug.on_ignored_message,
                  r"WriterHistoryMemoryPlugin_addEntryToSessions:" +
                  r"!initialize sample"])
    regex.append([debug.on_ignored_message,
                  r"WriterHistoryMemoryPlugin_getEntry:" +
                  r"!add virtual sample to sessions"])
    regex.append([debug.on_ignored_message,
                  r"WriterHistoryMemoryPlugin_addSample:!get entry"])
    regex.append([debug.on_ignored_message,
                  r"PRESWriterHistoryDriver_addWrite:!add_sample"])
    regex.append([debug.on_ignored_message,
                  r"PRESPsWriter_writeInternal:!collator addWrite"])
    regex.append([debug.on_ignored_message,
                  r"WriterHistoryMemoryPlugin_addSampleToWH:!add keyed entry"])
    regex.append([debug.on_ignored_message,
                  r"WriterHistoryMemoryPlugin_addSample:writer history full"])
    regex.append([debug.on_ignored_message,
                  r"PRESWriterHistoryDriver_addWrite:!instance history full"])
    regex.append([debug.on_ignored_message,
                  r"PRESWriterHistoryDriver_addWrite:!instance not found"])
    regex.append([debug.on_ignored_message,
                  r"PRESPsWriter_writeInternal:!collator write no instance"])
    regex.append([debug.on_ignored_message,
                  r"PRESCstReaderCollator_addCollatorEntryToPolled:" +
                  r"!add keyed entry"])
    regex.append([debug.on_ignored_message,
                  r"PRESCstReaderCollator_commitRemoteWriterQueue:" +
                  r"!add to polled"])
    regex.append([debug.on_ignored_message,
                  r"PRESCstReaderCollator_updateRemoteWriterQueue" +
                  r"FirstRelevant:"])
    regex.append([debug.on_ignored_message,
                  r"PRESPsService_readerSampleListenerOnNewData:" +
                  r"!goto WR pres psRemoteWriter"])
    regex.append([debug.on_ignored_message,
                  r"PRESPsReaderQueue_storeSampleToEntry:!store sample data"])
    regex.append([debug.on_ignored_message,
                  r"PRESPsReaderQueue_newData:!get entries"])
    regex.append([debug.on_ignored_message,
                  r"MIGGeneratorContext_addData:!space assert"])

    regex.append([debug.on_ignored_message,
                  r"This can occur if multicast is not enabled in the local " +
                  r"participant."])
    regex.append([debug.on_ignored_message,
                  r"See https://community.rti.com/kb/what-does-cant-reach-" +
                  r"locator-error-message-mean for additional info."])
    regex.append([debug.on_ignored_message,
                  r"can't reach:"])
    regex.append([debug.on_ignored_message,
                  r"transport: \d+ \([\w\d]+\)"])
    regex.append([debug.on_ignored_message,
                  r"address: [\d\.]+"])
    regex.append([debug.on_ignored_message,
                  r"Recv Resource:"])
    regex.append([debug.on_ignored_message,
                  r"Send Resource:"])
    regex.append([debug.on_ignored_message,
                  r"RTINetioSender_addDestination:!create NetioSender_" +
                  r"SendResource"])
    regex.append([debug.on_ignored_message,
                  r"RTINetioReceiver_addEntryport:!create NetioReceiver_" +
                  r"ReceiveResource"])

    regex.append([debug.on_ignored_message, r"^send failed: $"])
    regex.append([debug.on_ignored_message, r"^\s*locator:\s*$"])
    regex.append([debug.on_ignored_message, r"^\s*transport: \d+$"])
    regex.append([debug.on_ignored_message, r"^\s*address: [\d:]+$"])
    regex.append([debug.on_ignored_message, r"^[\d:]+$"])
    regex.append([debug.on_ignored_message, r"^\s*port: \d+$"])
    regex.append([debug.on_ignored_message, r"^\s*encapsulation:$"])
    regex.append([debug.on_ignored_message, r"^\s{3}transport_priority: \d+$"])
    regex.append([debug.on_ignored_message, r'^\s{3}aliasList: ""$'])
    regex.append([debug.on_ignored_message,
                  r"DDS_DomainParticipantFactory_initializeI:Welcome to NDDS"])
    regex.append([debug.on_ignored_message,
                  r"DDS_DiscoveryQosPolicy_get_default:no environment" +
                  r"variable or file NDDS_DISCOVERY_PEERS"])

    # The rest of unmatched message will be saved into a file to analyze them.
    regex.append([debug.on_unmatched_message, r"(.*)"])

    return regex
<commit_msg>:arrow_left: Merge pull request #35 from rticommunity/debug-logs<commit_after># Log Parser for RTI Connext.
#
#   Copyright 2016 Real-Time Innovations, Inc.
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
"""Create the dictionary for log functions for unmatched logs for debugging.

Functions:
  + get_regex_list: Get the regular expressions and function list.
"""
from __future__ import absolute_import
import logparser.logs.debug.debug as debug

def get_regex_list():
    """Return the regular expressions and functions list for this module."""
    regex = []

    # Blacklist - Messages that we want to ignore.
    regex.append([debug.on_ignored_message,
                  r"^\s*$"])
    regex.append([debug.on_ignored_message,
                  r"DDS_Registry_lock:Locking the storage service"])
    regex.append([debug.on_ignored_message,
                  r"DDS_Registry_unlock:Unlocking the storage service"])
    regex.append([debug.on_ignored_message,
                  r"RTIEventActiveDatabaseThread_loop:created \w+"])
    regex.append([debug.on_ignored_message,
                  r"RTIEventActiveGeneratorThread_loop:created \w+"])
    regex.append([debug.on_ignored_message,
                  r"COMMENDActiveFacadeReceiver_loop:created \w+"])
    regex.append([debug.on_ignored_message,
                  r"COMMENDActiveFacade_threadStarted:thread count ref " +
                  r"count \d+"])
    regex.append([debug.on_ignored_message,
                  r"COMMENDActiveFacade_addReceiverThread:thread count ref " +
                  r"count \d+"])
    regex.append([debug.on_ignored_message,
                  r"COMMENDActiveFacade_new:active object count ref " +
                  r"count \d+"])
    regex.append([debug.on_ignored_message,
                  r"RTIEventJobDispatcher_distributeTokens: \d+ agents " +
                  r"at priority [-\d]+"])
    regex.append([debug.on_ignored_message,
                  r"RTIEventJobDispatcher_updateAgentPriorities:agent:" +
                  r"0x\w+ priority set to [-\d]+"])
    regex.append([debug.on_ignored_message,
                  r"NDDS_Transport_UDPv4_receive_rEA:\w+ woke up"])
    regex.append([debug.on_ignored_message,
                  r"RTIEventJobDispatcher_scheduleJob:agent:\w+ job:\w+ " +
                  r"scheduled at priority \d+"])
    regex.append([debug.on_ignored_message,
                  r"RTIOsapiThread_sleep: nanosleep\(\d+.\d+ s\)"])
    regex.append([debug.on_ignored_message,
                  r"RTIEventActiveGeneratorThread_loop:\w+ gathering events"])
    regex.append([debug.on_ignored_message,
                  r"RTIEventActiveGeneratorThread_loop:\w+ firing events"])
    regex.append([debug.on_ignored_message,
                  r"RTIEventActiveGeneratorThread_loop:\w+ " +
                  r"rescheduling events"])
    regex.append([debug.on_ignored_message,
                  r"RTIEventActiveGeneratorThread_loop:\w+ sleeping " +
                  r"\{\w+,\w+\}"])
    regex.append([debug.on_ignored_message,
                  r"RTIEventActiveDatabaseThread_loop:\w+ collecting garbage"])
    regex.append([debug.on_ignored_message,
                  r"RTIEventActiveDatabaseThread_loop:\w+ sleeping " +
                  r"\{\w+,\w+\}"])
    regex.append([debug.on_ignored_message,
                  r"RTIEventJobDispatcher_updateAgentPriorities:agent:\w+ " +
                  r"priority set to \d+"])
    regex.append([debug.on_ignored_message,
                  r"RTIEventJobDispatcher_distributeTokens: \w+ agents at " +
                  r"priority \d+"])
    regex.append([debug.on_ignored_message,
                  r"RTIOsapiThread_sleep: Sleep\(\d+ ms\)"])
    regex.append([debug.on_ignored_message,
                  r"RTISystemClock_init:epoch range \{\w+,\w+\}, " +
                  r"frequency \d+ Hz"])

    regex.append([debug.on_ignored_message,
                  r"DDS_StringSeq_ensure_length:memory allocation: " +
                  r"original \d+, new \d+"])
    regex.append([debug.on_ignored_message,
                  r"DDS_PropertySeq_ensure_length:memory allocation: " +
                  r"original \d+, new \d+"])
    regex.append([debug.on_ignored_message,
                  r"RTINetioReceiver_addEntryport:" +
                  r"NetioReceiver_Entryport reused"])
    regex.append([debug.on_ignored_message,
                  r"RTINetioSender_addDestination:" +
                  r"NetioSender_Destination reused"])
    regex.append([debug.on_ignored_message,
                  r"COMMENDActiveFacadeReceiver_loop:\w+ disowning receive " +
                  r"resource"])
    regex.append([debug.on_ignored_message,
                  r"COMMENDActiveFacadeReceiver_loop:\w+ parsing message"])
    regex.append([debug.on_ignored_message,
                  r"RTINetioReceiver_receiveFast:\w+ received \d+ bytes"])
    regex.append([debug.on_ignored_message,
                  r"NDDS_Transport_Shmem_receive_rEA:\w+ blocking on 0X\w+"])
    regex.append([debug.on_ignored_message,
                  r"NDDS_Transport_Shmem_receive_rEA:\w+ woke up"])
    regex.append([debug.on_ignored_message,
                  r"NDDS_Transport_UDPv4_receive_rEA:\w+ blocking on 0X\w+"])
    regex.append([debug.on_ignored_message,
                  r"RTIOsapi_getFirstValidInterface:found address for " +
                  r"interface .+ \(address family = \d+\)"])
    regex.append([debug.on_ignored_message,
                  r"RTIOsapi_getFirstValidInterface:skipped interface .+, " +
                  r"\(not valid address family \([\w/]+\)\)"])
    regex.append([debug.on_ignored_message,
                  r"RTIOsapi_getFirstValidInterface:skipped interface .+, " +
                  r"\(loopback interface\)"])
    regex.append([debug.on_ignored_message,
                  r"RTINetioReceiver_removeEntryport:NetioReceiver_" +
                  r"Entryport ref count \d+"])
    regex.append([debug.on_ignored_message,
                  r"NDDS_Transport_UDPv4_SocketFactory_create_" +
                  r"receive_socket:invalid port (\d+)"])
    regex.append([debug.on_ignored_message,
                  r"NDDS_Transport_UDPv4_create_recvresource_rrEA:" +
                  r"Created receive resource for port (\d+)"])
    regex.append([debug.on_ignored_message,
                  r"NDDS_Transport_UDPv4_create_sendresource_srEA:Created " +
                  r"send resource for 0X\w+:\d+"])
    regex.append([debug.on_ignored_message,
                  r"NDDS_Transport_UDPv4_query_interfaces:" +
                  r"skipped (\w+)"])
    regex.append([debug.on_ignored_message,
                  r"NDDS_Transport_UDPv4_create_recvresource_rrEA:" +
                  r"!create socket"])

    regex.append([debug.on_ignored_message,
                  r"DDS_DomainParticipantFactory_create_participant_" +
                  r"disabledI:created participant: domain=\d+, index=-1"])
    regex.append([debug.on_ignored_message,
                  r"DDS_DomainParticipantPresentation_reserve_participant_" +
                  r"index_entryports:Domain \d+:Trying to reserve " +
                  r"participant index=\d+..."])
    regex.append([debug.on_ignored_message,
                  r"DISCPluginManager_onAfterLocalEndpointEnabled:at " +
                  r"\{\w+,\w+\}"])
    regex.append([debug.on_ignored_message,
                  r"DISCSimpleEndpointDiscoveryPluginPDFListener_" +
                  r"onAfterLocalWriterEnabled:at \{\w+,\w+\}"])
    regex.append([debug.on_ignored_message,
                  r"DISCSimpleEndpointDiscoveryPlugin_" +
                  r"subscriptionReaderListenerOnDataAvailable:at \{\w+,\w+\}"])
    regex.append([debug.on_ignored_message,
                  r"DISCPluginManager_onAfterLocalParticipantEnabled:at " +
                  r"\{\w+,\w+\}"])
    regex.append([debug.on_ignored_message,
                  r"DISCEndpointDiscoveryPlugin_assertRemoteEndpoint:at " +
                  r"\{\w+,\w+\}"])
    regex.append([debug.on_ignored_message,
                  r"DISCPluginManager_activateEdpListenersFor" +
                  r"RemoteParticipant:at \{\w+,\w+\}"])
    regex.append([debug.on_ignored_message,
                  r"DISCParticipantDiscoveryPlugin_assertRemoteParticipant:" +
                  r"at \{\w+,\w+\}"])
    regex.append([debug.on_ignored_message,
                  r"DISCSimpleParticipantDiscoveryPluginReaderListener_" +
                  r"onDataAvailable:at \{\w+,\w+\}"])
    regex.append([debug.on_ignored_message,
                  r"DISCSimpleParticipantDiscoveryPlugin_" +
                  r"remoteParticipantDiscovered:at \{\w+,\w+\}"])
    regex.append([debug.on_ignored_message,
                  r"DISCSimpleEndpointDiscoveryPlugin_publicationReader" +
                  r"ListenerOnDataAvailable:at \{\w+,\w+\}"])
    regex.append([debug.on_ignored_message,
                  r"DISCSimpleEndpointDiscoveryPluginPDFListener_" +
                  r"onAfterLocalReaderEnabled:at \{\w+,\w+\}"])
    regex.append([debug.on_ignored_message,
                  r"DDS_Topic_createI:!create presentation topic"])
    regex.append([debug.on_ignored_message,
                  r"DDS_DomainParticipant_create_topic_disabledI:" +
                  r"!create topic"])
    regex.append([debug.on_ignored_message,
                  r"DDSTopic_impl::createI:!create topic"])
    regex.append([debug.on_ignored_message,
                  r"PRESParticipant_destroyAllEntities:" +
                  r"!delete flow controller"])
    regex.append([debug.on_ignored_message,
                  r"DDS_DomainParticipant_delete_contained_entities:" +
                  r"!delete contained entitie"])
    regex.append([debug.on_ignored_message,
                  r"DISCSimpleParticipantDiscoveryPluginReaderListener_" +
                  r"onDataAvailable:discovered modified participant: " +
                  r"host=0x\w+, app=0x\w+, instance=0x\w+"])

    regex.append([debug.on_ignored_message,
                  r"PRESPsService_onWriterResendEvent:writer resend event: " +
                  r"\(([\w,]+)\)"])
    regex.append([debug.on_ignored_message,
                  r"WriterHistoryMemoryPlugin_addEntryToSessions:" +
                  r"!initialize sample"])
    regex.append([debug.on_ignored_message,
                  r"WriterHistoryMemoryPlugin_getEntry:" +
                  r"!add virtual sample to sessions"])
    regex.append([debug.on_ignored_message,
                  r"WriterHistoryMemoryPlugin_addSample:!get entry"])
    regex.append([debug.on_ignored_message,
                  r"PRESWriterHistoryDriver_addWrite:!add_sample"])
    regex.append([debug.on_ignored_message,
                  r"PRESPsWriter_writeInternal:!collator addWrite"])
    regex.append([debug.on_ignored_message,
                  r"WriterHistoryMemoryPlugin_addSampleToWH:!add keyed entry"])
    regex.append([debug.on_ignored_message,
                  r"WriterHistoryMemoryPlugin_addSample:writer history full"])
    regex.append([debug.on_ignored_message,
                  r"PRESWriterHistoryDriver_addWrite:!instance history full"])
    regex.append([debug.on_ignored_message,
                  r"PRESWriterHistoryDriver_addWrite:!instance not found"])
    regex.append([debug.on_ignored_message,
                  r"PRESPsWriter_writeInternal:!collator write no instance"])
    regex.append([debug.on_ignored_message,
                  r"PRESCstReaderCollator_addCollatorEntryToPolled:" +
                  r"!add keyed entry"])
    regex.append([debug.on_ignored_message,
                  r"PRESCstReaderCollator_commitRemoteWriterQueue:" +
                  r"!add to polled"])
    regex.append([debug.on_ignored_message,
                  r"PRESCstReaderCollator_updateRemoteWriterQueue" +
                  r"FirstRelevant:"])
    regex.append([debug.on_ignored_message,
                  r"PRESPsService_readerSampleListenerOnNewData:" +
                  r"!goto WR pres psRemoteWriter"])
    regex.append([debug.on_ignored_message,
                  r"PRESPsReaderQueue_storeSampleToEntry:!store sample data"])
    regex.append([debug.on_ignored_message,
                  r"PRESPsReaderQueue_newData:!get entries"])
    regex.append([debug.on_ignored_message,
                  r"MIGGeneratorContext_addData:!space assert"])

    regex.append([debug.on_ignored_message,
                  r"This can occur if multicast is not enabled in the local " +
                  r"participant."])
    regex.append([debug.on_ignored_message,
                  r"See https://community.rti.com/kb/what-does-cant-reach-" +
                  r"locator-error-message-mean for additional info."])
    regex.append([debug.on_ignored_message,
                  r"can't reach:"])
    regex.append([debug.on_ignored_message,
                  r"transport: \d+ \([\w\d]+\)"])
    regex.append([debug.on_ignored_message,
                  r"address: [\d\.]+"])
    regex.append([debug.on_ignored_message,
                  r"Recv Resource:"])
    regex.append([debug.on_ignored_message,
                  r"Send Resource:"])
    regex.append([debug.on_ignored_message,
                  r"RTINetioSender_addDestination:!create NetioSender_" +
                  r"SendResource"])
    regex.append([debug.on_ignored_message,
                  r"RTINetioReceiver_addEntryport:!create NetioReceiver_" +
                  r"ReceiveResource"])

    regex.append([debug.on_ignored_message, r"^\s*send failed:\s*$"])
    regex.append([debug.on_ignored_message, r"^\s*locator:\s*$"])
    regex.append([debug.on_ignored_message, r"^\s*transport: \d+$"])
    regex.append([debug.on_ignored_message, r"^\s*address: [\d:]+$"])
    regex.append([debug.on_ignored_message, r"^[\d:]+$"])
    regex.append([debug.on_ignored_message, r"^\s*port: \d+$"])
    regex.append([debug.on_ignored_message, r"^\s*encapsulation:$"])
    regex.append([debug.on_ignored_message, r"^\s{3}transport_priority: \d+$"])
    regex.append([debug.on_ignored_message, r'^\s{3}aliasList: ""$'])
    regex.append([debug.on_ignored_message,
                  r"DDS_DomainParticipantFactory_initializeI:Welcome to NDDS"])
    regex.append([debug.on_ignored_message,
                  r"DDS_DiscoveryQosPolicy_get_default:no environment " +
                  r"variable or file NDDS_DISCOVERY_PEERS"])
    regex.append([debug.on_ignored_message,
                  r"Creating domain participant..."])
    regex.append([debug.on_ignored_message,
                  r"loading QoS ..."])

    # The rest of unmatched message will be saved into a file to analyze them.
    regex.append([debug.on_unmatched_message, r"(.*)"])

    return regex
<|endoftext|>


<commit_before>#!/usr/bin/python
# -*- coding: utf-8 -*-
import requests
import types
from datetime import datetime, timedelta
from pprint import pformat
import urllib
import logging

log = logging.getLogger('lvbRequester')

class JsonDecodingError(Exception):
    """ exception on json decode which also returns raw content """

    def __init__(self, msg, raw):
        Exception.__init__(self, msg)
        self.raw = raw
        log.error(raw)

class LVB(object):
    """
        Returns travel informations from Leipziger Verkehrsbetriebe (l.de)
    """

    URL = {
        'connection': 'https://www.l.de/verkehrsbetriebe/fahrplan?ws_find_connections',
        'complete': 'https://www.l.de/ajax_de',
        'station': 'https://www.l.de/verkehrsbetriebe/fahrplan?ws_info_stop'
    }

    TRANSPORTMAP = {
        'STR': 'Strasenbahn',
        'BUS': 'Bus',
        'RB/RE': 'Regionalbahn',
        'S/U': 'S-Bahn',
    }
    HEADER = {
        'Origin': 'https://www.l.de',
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'en-US,en;q=0.8,de;q=0.6',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'Accept': 'application/json, text/javascript, */*; q=0.01',
        'Referer': 'https://www.l.de/verkehrsbetriebe/fahrplan',
        'X-Requested-With': 'XMLHttpRequest',
        'Connection': 'keep-alive',
    }

    @classmethod
    def _defParseDatetime(self, time):
        """ allow different ways to provide a time and pares it to datetime """
        if time:
            if isinstance(time, types.IntType):
                return datetime.now() + timedelta(minutes=time)
            elif isinstance(time, (datetime, )):
                return time
            elif isinstance(time, type.StringTypes):
                # TODO
                return datetime.now()
            else:
                raise ValueError('Unable to parse %s as datetime' % time)
        else:
            return datetime.now()

    @classmethod
    def _encodeRequest(self, request, data=None):
        """ encode the request parameters in the expected way """
        if isinstance(request, (types.ListType, types.TupleType)):
            request = "".join(request)
        if data:
            resStr = request % data
        else:
            resStr = request

        # log.debug('DATA RAW: %s' % resStr)
        res = urllib.quote(resStr).replace('%26', '&').replace('%2B', '+').replace('%3D', '=').replace('/', '%2F')
        # log.debug('DATA ENC: %s' % res)
        return res

    @classmethod
    def getAutoCompletion(self, station, limit=10):
        """ retrieves autocomplete result for station.

        This should be used to get the correct station name which
        will be needed for getStation and getConnection.
         """
        reqData = {
            'mode': 'autocomplete',
            'limit': limit,
            'poi': '',
            'q': station.encode('utf-8') if isinstance(station, types.UnicodeType) else station,
        }
        url = '%s?%s' % (
            self.URL['complete'],
            urllib.urlencode(reqData)
        )
        log.debug('URL: %s' % url)
        data = requests.get(url)
        if data.status_code == 200:
            try:
                return data.json()['stations']
            except ValueError, e:
                raise JsonDecodingError(e.message, data.text)
        raise Exception('Unable to retrieve data error: %s' % data.status_code)

    @classmethod
    def getConnection(self, stationFrom, stationTo, time=None):
        """ Retrieves connection information to travel from stationFrom to stationTo.

        The station name must be completely identical to the one in LVB System.
        You can use getAutoCompletion to retrieve the correct name.
        """
        params = self._getConnectionParams(stationFrom, stationTo, self._defParseDatetime(time))
        # log.debug('PARAMS: %s' % (params, ))

        data = requests.post(self.URL['connection'], data=params, headers=self.HEADER)
        if data.status_code == 200:
            # log.debug('BODY: %s' % (data.text))
            try:
                return self._getConnectionParse(data.json())
            except ValueError, e:
                raise JsonDecodingError(e.message, data.text)
        raise Exception('Unable to retrieve data error: %s' % data.status_code)

    @classmethod
    def _getConnectionParams(self, stationFrom, stationTo, conTime):
        """ builds parameter structur for connection call """
        transport = self.TRANSPORTMAP.keys()
        res = [
            'results[5][2][function]=ws_find_connections&results[5][2][data]=[',
            '{"name":"results[5][2][is_extended]","value":""},',
            '{"name":"results[5][2][from_opt]","value":"3"},',
            '{"name":"results[5][2][from]","value":"%(from)s"},',
            '{"name":"results[5][2][from_lat]","value":""},',
            '{"name":"results[5][2][from_lng]","value":""},',
            '{"name":"results[5][2][to_opt]","value":"3"},',
            '{"name":"results[5][2][to]","value":"%(to)s"},',
            '{"name":"results[5][2][to_lat]","value":""},',
            '{"name":"results[5][2][to_lng]","value":""},',
            '{"name":"results[5][2][via]","value":""},',
            '{"name":"results[5][2][via_lat]","value":""},',
            '{"name":"results[5][2][via_lng]","value":""},',
            '{"name":"results[5][2][time_mode]","value":"departure"},',
            '{"name":"results[5][2][time]","value":"%(time)s"},',
            '{"name":"results[5][2][date]","value":"%(date)s"},',
        ]

        stationFrom = stationFrom.encode('utf-8') if isinstance(stationFrom, types.UnicodeType) else stationFrom

        stationTo = stationTo.encode('utf-8') if isinstance(stationTo, types.UnicodeType) else stationTo

        for atransport in transport:
            res.append('{"name":"results[5][2][means_of_transport][]","value":"%s"},' % atransport)

        res.append('{"name":"results[5][2][mode]","value":"connection"}]')
        return self._encodeRequest(res, {
            'from': stationFrom.replace(' ', '+'),
            'to': stationTo.replace(' ', '+'),
            'time': conTime.strftime('%H:%M'),
            'date': conTime.strftime('%d.%m.%Y'),
        })

    @classmethod
    def _getConnectionParse(self, result):
        """ builds connection results """
        return result.get('connections', {})

    @classmethod
    def getStation(self, station, time=None):
        """ get all exptected Trains at specified station

        The station names must be completely identical to the ones in LVB System.
        You can use getAutoCompletion to retrieve the correct names.
        """
        params = self._getStationParams(station, self._defParseDatetime(time))
        # log.debug('PARAMS: %s' % (params, ))

        data = requests.post(self.URL['station'], data=params, headers=self.HEADER)
        if data.status_code == 200:
            # log.debug('BODY: %s' % (data.text))
            try:
                return self._getStationParse(data.json())
            except ValueError, e:
                raise JsonDecodingError(e.message, data.text)
        raise Exception('Unable to retrieve data error: %s' % data.status_code)

    @classmethod
    def _getStationParams(self, stop, time):
        """ build paramter structure for station request """
        res = [
            'results[5][2][function]=ws_info_stop&results[5][2][data]=[',
            '{"name":"results[5][2][stop]","value":"%(stop)s"},'
            '{"name":"results[5][2][date]","value":"%(date)s"},',
            '{"name":"results[5][2][time]","value":"%(time)s"},',
            # '{"name":"results[5][2][time]","value":""},',
            '{"name":"results[5][2][mode]","value":"stop"}]',
        ]
        stop = stop.encode('utf-8') if isinstance(stop, types.UnicodeType) else stop

        return self._encodeRequest(res, {
            'stop': stop.replace(' ', '+'),
            'date': time.strftime('%d.%m.%Y'),
            'time': time.strftime('%H:%M'),
        })

    @classmethod
    def _getStationParse(self, result):
        """ build station results """
        return result['connections']

if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG)
    r = LVB()
    print "Station %s" % (pformat(r.getStation('Leipzig, Marschnerstr.')))
    # , datetime(2017, 2, 10, 12, 57))))
    print "Connection %s" % (pformat(r.getConnection('Leipzig, Marschnerstr.', 'Leipzig, Goerdelerring')))
    # , datetime(2017, 2, 10, 13, 17))))
    print "Autocomplete %s" % (pformat(r.getAutoCompletion(u'marschnerstra\xdfe')))
    print "Autocomplete %s" % (pformat(r.getAutoCompletion(u'marschner')))
<commit_msg>fix types<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-
import requests
import types
from datetime import datetime, timedelta
from pprint import pformat
import urllib
import logging

log = logging.getLogger('lvbRequester')

class JsonDecodingError(Exception):
    """ exception on json decode which also returns raw content """

    def __init__(self, msg, raw):
        Exception.__init__(self, msg)
        self.raw = raw
        log.error(raw)

class LVB(object):
    """
        Returns travel informations from Leipziger Verkehrsbetriebe (l.de)
    """

    URL = {
        'connection': 'https://www.l.de/verkehrsbetriebe/fahrplan?ws_find_connections',
        'complete': 'https://www.l.de/ajax_de',
        'station': 'https://www.l.de/verkehrsbetriebe/fahrplan?ws_info_stop'
    }

    TRANSPORTMAP = {
        'STR': 'Strasenbahn',
        'BUS': 'Bus',
        'RB/RE': 'Regionalbahn',
        'S/U': 'S-Bahn',
    }
    HEADER = {
        'Origin': 'https://www.l.de',
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'en-US,en;q=0.8,de;q=0.6',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'Accept': 'application/json, text/javascript, */*; q=0.01',
        'Referer': 'https://www.l.de/verkehrsbetriebe/fahrplan',
        'X-Requested-With': 'XMLHttpRequest',
        'Connection': 'keep-alive',
    }

    @classmethod
    def _defParseDatetime(self, time):
        """ allow different ways to provide a time and pares it to datetime """
        if time:
            if isinstance(time, types.IntType):
                return datetime.now() + timedelta(minutes=time)
            elif isinstance(time, (datetime, )):
                return time
            elif isinstance(time, (timedelta, )):
                return datetime.now() + time
            elif isinstance(time, types.StringTypes):
                # TODO
                return datetime.now()
            else:
                raise ValueError('Unable to parse %s as datetime' % time)
        else:
            return datetime.now()

    @classmethod
    def _encodeRequest(self, request, data=None):
        """ encode the request parameters in the expected way """
        if isinstance(request, (types.ListType, types.TupleType)):
            request = "".join(request)
        if data:
            resStr = request % data
        else:
            resStr = request

        # log.debug('DATA RAW: %s' % resStr)
        res = urllib.quote(resStr).replace('%26', '&').replace('%2B', '+').replace('%3D', '=').replace('/', '%2F')
        # log.debug('DATA ENC: %s' % res)
        return res

    @classmethod
    def getAutoCompletion(self, station, limit=10):
        """ retrieves autocomplete result for station.

        This should be used to get the correct station name which
        will be needed for getStation and getConnection.
         """
        reqData = {
            'mode': 'autocomplete',
            'limit': limit,
            'poi': '',
            'q': station.encode('utf-8') if isinstance(station, types.UnicodeType) else station,
        }
        url = '%s?%s' % (
            self.URL['complete'],
            urllib.urlencode(reqData)
        )
        log.debug('URL: %s' % url)
        data = requests.get(url)
        if data.status_code == 200:
            try:
                return data.json()['stations']
            except ValueError, e:
                raise JsonDecodingError(e.message, data.text)
        raise Exception('Unable to retrieve data error: %s' % data.status_code)

    @classmethod
    def getConnection(self, stationFrom, stationTo, time=None):
        """ Retrieves connection information to travel from stationFrom to stationTo.

        The station name must be completely identical to the one in LVB System.
        You can use getAutoCompletion to retrieve the correct name.
        """
        params = self._getConnectionParams(stationFrom, stationTo, self._defParseDatetime(time))
        # log.debug('PARAMS: %s' % (params, ))

        data = requests.post(self.URL['connection'], data=params, headers=self.HEADER)
        if data.status_code == 200:
            # log.debug('BODY: %s' % (data.text))
            try:
                return self._getConnectionParse(data.json())
            except ValueError, e:
                raise JsonDecodingError(e.message, data.text)
        raise Exception('Unable to retrieve data error: %s' % data.status_code)

    @classmethod
    def _getConnectionParams(self, stationFrom, stationTo, conTime):
        """ builds parameter structur for connection call """
        transport = self.TRANSPORTMAP.keys()
        res = [
            'results[5][2][function]=ws_find_connections&results[5][2][data]=[',
            '{"name":"results[5][2][is_extended]","value":""},',
            '{"name":"results[5][2][from_opt]","value":"3"},',
            '{"name":"results[5][2][from]","value":"%(from)s"},',
            '{"name":"results[5][2][from_lat]","value":""},',
            '{"name":"results[5][2][from_lng]","value":""},',
            '{"name":"results[5][2][to_opt]","value":"3"},',
            '{"name":"results[5][2][to]","value":"%(to)s"},',
            '{"name":"results[5][2][to_lat]","value":""},',
            '{"name":"results[5][2][to_lng]","value":""},',
            '{"name":"results[5][2][via]","value":""},',
            '{"name":"results[5][2][via_lat]","value":""},',
            '{"name":"results[5][2][via_lng]","value":""},',
            '{"name":"results[5][2][time_mode]","value":"departure"},',
            '{"name":"results[5][2][time]","value":"%(time)s"},',
            '{"name":"results[5][2][date]","value":"%(date)s"},',
        ]

        stationFrom = stationFrom.encode('utf-8') if isinstance(stationFrom, types.UnicodeType) else stationFrom

        stationTo = stationTo.encode('utf-8') if isinstance(stationTo, types.UnicodeType) else stationTo

        for atransport in transport:
            res.append('{"name":"results[5][2][means_of_transport][]","value":"%s"},' % atransport)

        res.append('{"name":"results[5][2][mode]","value":"connection"}]')
        return self._encodeRequest(res, {
            'from': stationFrom.replace(' ', '+'),
            'to': stationTo.replace(' ', '+'),
            'time': conTime.strftime('%H:%M'),
            'date': conTime.strftime('%d.%m.%Y'),
        })

    @classmethod
    def _getConnectionParse(self, result):
        """ builds connection results """
        return result.get('connections', {})

    @classmethod
    def getStation(self, station, time=None):
        """ get all exptected Trains at specified station

        The station names must be completely identical to the ones in LVB System.
        You can use getAutoCompletion to retrieve the correct names.
        """
        params = self._getStationParams(station, self._defParseDatetime(time))
        # log.debug('PARAMS: %s' % (params, ))

        data = requests.post(self.URL['station'], data=params, headers=self.HEADER)
        if data.status_code == 200:
            # log.debug('BODY: %s' % (data.text))
            try:
                return self._getStationParse(data.json())
            except ValueError, e:
                raise JsonDecodingError(e.message, data.text)
        raise Exception('Unable to retrieve data error: %s' % data.status_code)

    @classmethod
    def _getStationParams(self, stop, time):
        """ build paramter structure for station request """
        res = [
            'results[5][2][function]=ws_info_stop&results[5][2][data]=[',
            '{"name":"results[5][2][stop]","value":"%(stop)s"},'
            '{"name":"results[5][2][date]","value":"%(date)s"},',
            '{"name":"results[5][2][time]","value":"%(time)s"},',
            # '{"name":"results[5][2][time]","value":""},',
            '{"name":"results[5][2][mode]","value":"stop"}]',
        ]
        stop = stop.encode('utf-8') if isinstance(stop, types.UnicodeType) else stop

        return self._encodeRequest(res, {
            'stop': stop.replace(' ', '+'),
            'date': time.strftime('%d.%m.%Y'),
            'time': time.strftime('%H:%M'),
        })

    @classmethod
    def _getStationParse(self, result):
        """ build station results """
        return result['connections']

if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG)
    r = LVB()
    print "Station %s" % (pformat(r.getStation('Leipzig, Marschnerstr.')))
    # , datetime(2017, 2, 10, 12, 57))))
    print "Connection %s" % (pformat(r.getConnection('Leipzig, Marschnerstr.', 'Leipzig, Goerdelerring')))
    # , datetime(2017, 2, 10, 13, 17))))
    print "Autocomplete %s" % (pformat(r.getAutoCompletion(u'marschnerstra\xdfe')))
    print "Autocomplete %s" % (pformat(r.getAutoCompletion(u'marschner')))
<|endoftext|>