<commit_before># pyOCD debugger
# Copyright (c) 2015 Arm Limited
# SPDX-License-Identifier: Apache-2.0
#
# 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.
from __future__ import print_function

import os, sys
from time import sleep, time
from random import randrange
import traceback
import argparse
import logging

parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, parentdir)

from pyocd.core.helpers import ConnectHelper
from pyocd.probe.pydapaccess import DAPAccess
from pyocd.core.memory_map import MemoryType
from pyocd.utility import conversion
from test_util import (Test, TestResult, get_session_options, get_target_test_params)

class SpeedTestResult(TestResult):
    def __init__(self):
        super(SpeedTestResult, self).__init__(None, None, None)
        self.name = "speed"

class SpeedTest(Test):
    def __init__(self):
        super(SpeedTest, self).__init__("Speed Test", speed_test)

    def print_perf_info(self, result_list, output_file=None):
        format_str = "{:<15}{:>18}{:>18}{:>18}"
        result_list = filter(lambda x: isinstance(x, SpeedTestResult), result_list)
        print("\n\n------ Speed Test Performance ------", file=output_file)
        print(format_str.format("Target", "RAM Read Speed", "RAM Write Speed", "ROM Read Speed"),
              file=output_file)
        print("", file=output_file)
        for result in result_list:
            if result.passed:
                read_speed = "%.3f KB/s" % (float(result.read_speed) / float(1000))
                write_speed = "%.3f KB/s" % (float(result.write_speed) / float(1000))
                rom_read_speed = "%.3f KB/s" % (float(result.rom_read_speed) / float(1000))
            else:
                read_speed = "Fail"
                write_speed = "Fail"
                rom_read_speed = "Fail"
            print(format_str.format(result.board,
                                    read_speed, write_speed, rom_read_speed),
                  file=output_file)
        print("", file=output_file)

    def run(self, board):
        passed = False
        read_speed = None
        write_speed = None
        try:
            result = self.test_function(board.unique_id)
        except Exception as e:
            print("Exception %s when testing board %s" % (e, board.unique_id))
            result = SpeedTestResult()
            result.passed = False
            traceback.print_exc(file=sys.stdout)
        result.board = board
        result.test = self
        return result

def speed_test(board_id):
    with ConnectHelper.session_with_chosen_probe(board_id=board_id, **get_session_options()) as session:
        board = session.board
        target_type = board.target_type

        memory_map = board.target.get_memory_map()
        ram_region = memory_map.get_first_region_of_type(MemoryType.RAM)
        rom_region = memory_map.get_boot_memory()

        ram_start = ram_region.start
        ram_size = ram_region.length
        rom_start = rom_region.start
        rom_size = rom_region.length

        target = board.target

        test_pass_count = 0
        test_count = 0
        result = SpeedTestResult()

        test_params = get_target_test_params(session)
        session.probe.set_clock(test_params['test_clock'])

        test_config = "uncached 8-bit"

        def test_ram(record_speed=False, width=8):
            print("\n\n------ TEST RAM READ / WRITE SPEED [%s] ------" % test_config)
            test_addr = ram_start
            test_size = ram_size
            data = [randrange(1, 50) for x in range(test_size)]
            start = time()
            if width == 8:
                target.write_memory_block8(test_addr, data)
            elif width == 32:
                target.write_memory_block32(test_addr, conversion.byte_list_to_u32le_list(data))
            target.flush()
            stop = time()
            diff = stop - start
            if diff == 0:
                print("Unexpected ram write elapsed time of 0!")
                write_speed = 0
            else:
                write_speed = test_size / diff
            if record_speed:
                result.write_speed = write_speed
            print("Writing %i byte took %.3f seconds: %.3f B/s" % (test_size, diff, write_speed))
            start = time()
            if width == 8:
                block = target.read_memory_block8(test_addr, test_size)
            elif width == 32:
                block = conversion.u32le_list_to_byte_list(target.read_memory_block32(test_addr, test_size // 4))
            target.flush()
            stop = time()
            diff = stop - start
            if diff == 0:
                print("Unexpected ram read elapsed time of 0!")
                read_speed = 0
            else:
                read_speed = test_size / diff
            if record_speed:
                result.read_speed = read_speed
            print("Reading %i byte took %.3f seconds: %.3f B/s" % (test_size, diff, read_speed))
            error = False
            if len(block) != len(data):
                error = True
                print("ERROR: read length (%d) != write length (%d)!" % (len(block), len(data)))
            if not error:
                for i in range(len(block)):
                    if (block[i] != data[i]):
                        error = True
                        print("ERROR: 0x%X, 0x%X, 0x%X!!!" % ((addr + i), block[i], data[i]))
            if error:
                print("TEST FAILED")
            else:
                print("TEST PASSED")
            return not error

        def test_rom(record_speed=False, width=8):
            print("\n\n------ TEST ROM READ SPEED [%s] ------" % test_config)
            test_addr = rom_start
            test_size = rom_size
            start = time()
            if width == 8:
                block = target.read_memory_block8(test_addr, test_size)
            elif width == 32:
                block = conversion.u32le_list_to_byte_list(target.read_memory_block32(test_addr, test_size // 4))
            target.flush()
            stop = time()
            diff = stop - start
            if diff == 0:
                print("Unexpected rom read elapsed time of 0!")
                read_speed = 0
            else:
                read_speed = test_size / diff
            if record_speed:
                result.rom_read_speed = read_speed
            print("Reading %i byte took %.3f seconds: %.3f B/s" % (test_size, diff, read_speed))
            print("TEST PASSED")
            return True

        # 8-bit without memcache
        passed = test_ram(True, 8)
        test_count += 1
        test_pass_count += int(passed)

        passed = test_rom(True, 8)
        test_count += 1
        test_pass_count += int(passed)

        # 32-bit without memcache
        test_config = "uncached 32-bit"
        passed = test_ram(False, 32)
        test_count += 1
        test_pass_count += int(passed)

        passed = test_rom(False, 32)
        test_count += 1
        test_pass_count += int(passed)

        # With memcache
        target = target.get_target_context()
        test_config = "cached 8-bit, pass 1"

        passed = test_ram()
        test_count += 1
        test_pass_count += int(passed)

        passed = test_rom()
        test_count += 1
        test_pass_count += int(passed)

        # Again with memcache
        test_config = "cached 8-bit, pass 2"
        passed = test_ram()
        test_count += 1
        test_pass_count += int(passed)

        passed = test_rom()
        test_count += 1
        test_pass_count += int(passed)

        board.target.reset()

        result.passed = test_count == test_pass_count
        return result

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='pyOCD speed test')
    parser.add_argument('-d', '--debug', action="store_true", help='Enable debug logging')
    parser.add_argument("-da", "--daparg", dest="daparg", nargs='+', help="Send setting to DAPAccess layer.")
    args = parser.parse_args()
    level = logging.DEBUG if args.debug else logging.INFO
    logging.basicConfig(level=level)
    DAPAccess.set_args(args.daparg)
    session = ConnectHelper.session_with_chosen_probe(open_session=False, **get_session_options())
    test = SpeedTest()
    result = [test.run(session.board)]
    test.print_perf_info(result)
<commit_msg>speed_test.py limits region sizes used for testing to 1 MB.<commit_after># pyOCD debugger
# Copyright (c) 2015 Arm Limited
# SPDX-License-Identifier: Apache-2.0
#
# 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.
from __future__ import print_function

import os, sys
from time import sleep, time
from random import randrange
import traceback
import argparse
import logging

parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, parentdir)

from pyocd.core.helpers import ConnectHelper
from pyocd.probe.pydapaccess import DAPAccess
from pyocd.core.memory_map import MemoryType
from pyocd.utility import conversion
from test_util import (Test, TestResult, get_session_options, get_target_test_params)

_1MB = (1 * 1024 * 1024)

class SpeedTestResult(TestResult):
    def __init__(self):
        super(SpeedTestResult, self).__init__(None, None, None)
        self.name = "speed"

class SpeedTest(Test):
    def __init__(self):
        super(SpeedTest, self).__init__("Speed Test", speed_test)

    def print_perf_info(self, result_list, output_file=None):
        format_str = "{:<15}{:>18}{:>18}{:>18}"
        result_list = filter(lambda x: isinstance(x, SpeedTestResult), result_list)
        print("\n\n------ Speed Test Performance ------", file=output_file)
        print(format_str.format("Target", "RAM Read Speed", "RAM Write Speed", "ROM Read Speed"),
              file=output_file)
        print("", file=output_file)
        for result in result_list:
            if result.passed:
                read_speed = "%.3f KB/s" % (float(result.read_speed) / float(1000))
                write_speed = "%.3f KB/s" % (float(result.write_speed) / float(1000))
                rom_read_speed = "%.3f KB/s" % (float(result.rom_read_speed) / float(1000))
            else:
                read_speed = "Fail"
                write_speed = "Fail"
                rom_read_speed = "Fail"
            print(format_str.format(result.board,
                                    read_speed, write_speed, rom_read_speed),
                  file=output_file)
        print("", file=output_file)

    def run(self, board):
        passed = False
        read_speed = None
        write_speed = None
        try:
            result = self.test_function(board.unique_id)
        except Exception as e:
            print("Exception %s when testing board %s" % (e, board.unique_id))
            result = SpeedTestResult()
            result.passed = False
            traceback.print_exc(file=sys.stdout)
        result.board = board
        result.test = self
        return result

def speed_test(board_id):
    with ConnectHelper.session_with_chosen_probe(board_id=board_id, **get_session_options()) as session:
        board = session.board
        target_type = board.target_type

        memory_map = board.target.get_memory_map()
        ram_region = memory_map.get_first_region_of_type(MemoryType.RAM)
        rom_region = memory_map.get_boot_memory()

        # Limit region sizes used for performance testing to 1 MB. We don't really need to
        # be reading all 32 MB of a QSPI!
        ram_start = ram_region.start
        ram_size = min(ram_region.length, _1MB)
        rom_start = rom_region.start
        rom_size = min(rom_region.length, _1MB)

        target = board.target

        test_pass_count = 0
        test_count = 0
        result = SpeedTestResult()

        test_params = get_target_test_params(session)
        session.probe.set_clock(test_params['test_clock'])

        test_config = "uncached 8-bit"

        def test_ram(record_speed=False, width=8):
            print("\n\n------ TEST RAM READ / WRITE SPEED [%s] ------" % test_config)
            test_addr = ram_start
            test_size = ram_size
            data = [randrange(1, 50) for x in range(test_size)]
            start = time()
            if width == 8:
                target.write_memory_block8(test_addr, data)
            elif width == 32:
                target.write_memory_block32(test_addr, conversion.byte_list_to_u32le_list(data))
            target.flush()
            stop = time()
            diff = stop - start
            if diff == 0:
                print("Unexpected ram write elapsed time of 0!")
                write_speed = 0
            else:
                write_speed = test_size / diff
            if record_speed:
                result.write_speed = write_speed
            print("Writing %i byte took %.3f seconds: %.3f B/s" % (test_size, diff, write_speed))
            start = time()
            if width == 8:
                block = target.read_memory_block8(test_addr, test_size)
            elif width == 32:
                block = conversion.u32le_list_to_byte_list(target.read_memory_block32(test_addr, test_size // 4))
            target.flush()
            stop = time()
            diff = stop - start
            if diff == 0:
                print("Unexpected ram read elapsed time of 0!")
                read_speed = 0
            else:
                read_speed = test_size / diff
            if record_speed:
                result.read_speed = read_speed
            print("Reading %i byte took %.3f seconds: %.3f B/s" % (test_size, diff, read_speed))
            error = False
            if len(block) != len(data):
                error = True
                print("ERROR: read length (%d) != write length (%d)!" % (len(block), len(data)))
            if not error:
                for i in range(len(block)):
                    if (block[i] != data[i]):
                        error = True
                        print("ERROR: 0x%X, 0x%X, 0x%X!!!" % ((addr + i), block[i], data[i]))
            if error:
                print("TEST FAILED")
            else:
                print("TEST PASSED")
            return not error

        def test_rom(record_speed=False, width=8):
            print("\n\n------ TEST ROM READ SPEED [%s] ------" % test_config)
            test_addr = rom_start
            test_size = rom_size
            start = time()
            if width == 8:
                block = target.read_memory_block8(test_addr, test_size)
            elif width == 32:
                block = conversion.u32le_list_to_byte_list(target.read_memory_block32(test_addr, test_size // 4))
            target.flush()
            stop = time()
            diff = stop - start
            if diff == 0:
                print("Unexpected rom read elapsed time of 0!")
                read_speed = 0
            else:
                read_speed = test_size / diff
            if record_speed:
                result.rom_read_speed = read_speed
            print("Reading %i byte took %.3f seconds: %.3f B/s" % (test_size, diff, read_speed))
            print("TEST PASSED")
            return True

        # 8-bit without memcache
        passed = test_ram(True, 8)
        test_count += 1
        test_pass_count += int(passed)

        passed = test_rom(True, 8)
        test_count += 1
        test_pass_count += int(passed)

        # 32-bit without memcache
        test_config = "uncached 32-bit"
        passed = test_ram(False, 32)
        test_count += 1
        test_pass_count += int(passed)

        passed = test_rom(False, 32)
        test_count += 1
        test_pass_count += int(passed)

        # With memcache
        target = target.get_target_context()
        test_config = "cached 8-bit, pass 1"

        passed = test_ram()
        test_count += 1
        test_pass_count += int(passed)

        passed = test_rom()
        test_count += 1
        test_pass_count += int(passed)

        # Again with memcache
        test_config = "cached 8-bit, pass 2"
        passed = test_ram()
        test_count += 1
        test_pass_count += int(passed)

        passed = test_rom()
        test_count += 1
        test_pass_count += int(passed)

        board.target.reset()

        result.passed = test_count == test_pass_count
        return result

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='pyOCD speed test')
    parser.add_argument('-d', '--debug', action="store_true", help='Enable debug logging')
    parser.add_argument("-da", "--daparg", dest="daparg", nargs='+', help="Send setting to DAPAccess layer.")
    args = parser.parse_args()
    level = logging.DEBUG if args.debug else logging.INFO
    logging.basicConfig(level=level)
    DAPAccess.set_args(args.daparg)
    session = ConnectHelper.session_with_chosen_probe(open_session=False, **get_session_options())
    test = SpeedTest()
    result = [test.run(session.board)]
    test.print_perf_info(result)
<|endoftext|>


<commit_before>import testbase

from sqlalchemy import *

# TODO: either create a mock dialect with named paramstyle and a short identifier length,
# or find a way to just use sqlite dialect and make those changes

class LabelTypeTest(testbase.PersistTest):
    def test_type(self):
        m = MetaData()
        t = Table('sometable', m,
            Column('col1', Integer),
            Column('col2', Float))
        assert isinstance(t.c.col1.label('hi').type, Integer)
        assert isinstance(select([t.c.col2], scalar=True).label('lala').type, Float)

class LongLabelsTest(testbase.PersistTest):
    def setUpAll(self):
        global metadata, table1
        metadata = MetaData(engine=testbase.db)
        table1 = Table("some_large_named_table", metadata,
            Column("this_is_the_primarykey_column", Integer, Sequence("this_is_some_large_seq"), primary_key=True),
            Column("this_is_the_data_column", String(30))
            )
        metadata.create_all()
    def tearDown(self):
        table1.delete().execute()

    def tearDownAll(self):
        metadata.drop_all()

    def test_result(self):
        table1.insert().execute(**{"this_is_the_primarykey_column":1, "this_is_the_data_column":"data1"})
        table1.insert().execute(**{"this_is_the_primarykey_column":2, "this_is_the_data_column":"data2"})
        table1.insert().execute(**{"this_is_the_primarykey_column":3, "this_is_the_data_column":"data3"})
        table1.insert().execute(**{"this_is_the_primarykey_column":4, "this_is_the_data_column":"data4"})

        r = table1.select(use_labels=True, order_by=[table1.c.this_is_the_primarykey_column]).execute()
        result = []
        for row in r:
            result.append((row[table1.c.this_is_the_primarykey_column], row[table1.c.this_is_the_data_column]))
        assert result == [
            (1, "data1"),
            (2, "data2"),
            (3, "data3"),
            (4, "data4"),
        ], repr(result)

    def test_colbinds(self):
        table1.insert().execute(**{"this_is_the_primarykey_column":1, "this_is_the_data_column":"data1"})
        table1.insert().execute(**{"this_is_the_primarykey_column":2, "this_is_the_data_column":"data2"})
        table1.insert().execute(**{"this_is_the_primarykey_column":3, "this_is_the_data_column":"data3"})
        table1.insert().execute(**{"this_is_the_primarykey_column":4, "this_is_the_data_column":"data4"})

        r = table1.select(table1.c.this_is_the_primarykey_column == 4).execute()
        assert r.fetchall() == [(4, "data4")]

        r = table1.select(or_(
            table1.c.this_is_the_primarykey_column == 4,
            table1.c.this_is_the_primarykey_column == 2
        )).execute()
        assert r.fetchall() == [(2, "data2"), (4, "data4")]

    def test_insert_no_pk(self):
        table1.insert().execute(**{"this_is_the_data_column":"data1"})
        table1.insert().execute(**{"this_is_the_data_column":"data2"})
        table1.insert().execute(**{"this_is_the_data_column":"data3"})
        table1.insert().execute(**{"this_is_the_data_column":"data4"})

    def test_subquery(self):
        q = table1.select(table1.c.this_is_the_primarykey_column == 4, use_labels=True)
        x = select([q])
        print str(x)

if __name__ == '__main__':
    testbase.main()
<commit_msg>some notes on a labeling issue that arises when label truncation doesnt match col truncation<commit_after>import testbase

from sqlalchemy import *

# TODO: either create a mock dialect with named paramstyle and a short identifier length,
# or find a way to just use sqlite dialect and make those changes

class LabelTypeTest(testbase.PersistTest):
    def test_type(self):
        m = MetaData()
        t = Table('sometable', m,
            Column('col1', Integer),
            Column('col2', Float))
        assert isinstance(t.c.col1.label('hi').type, Integer)
        assert isinstance(select([t.c.col2], scalar=True).label('lala').type, Float)

class LongLabelsTest(testbase.PersistTest):
    def setUpAll(self):
        global metadata, table1
        metadata = MetaData(engine=testbase.db)
        table1 = Table("some_large_named_table", metadata,
            Column("this_is_the_primarykey_column", Integer, Sequence("this_is_some_large_seq"), primary_key=True),
            Column("this_is_the_data_column", String(30))
            )
        metadata.create_all()
    def tearDown(self):
        table1.delete().execute()

    def tearDownAll(self):
        metadata.drop_all()

    def test_result(self):
        table1.insert().execute(**{"this_is_the_primarykey_column":1, "this_is_the_data_column":"data1"})
        table1.insert().execute(**{"this_is_the_primarykey_column":2, "this_is_the_data_column":"data2"})
        table1.insert().execute(**{"this_is_the_primarykey_column":3, "this_is_the_data_column":"data3"})
        table1.insert().execute(**{"this_is_the_primarykey_column":4, "this_is_the_data_column":"data4"})

        r = table1.select(use_labels=True, order_by=[table1.c.this_is_the_primarykey_column]).execute()
        result = []
        for row in r:
            result.append((row[table1.c.this_is_the_primarykey_column], row[table1.c.this_is_the_data_column]))
        assert result == [
            (1, "data1"),
            (2, "data2"),
            (3, "data3"),
            (4, "data4"),
        ], repr(result)

    def test_colbinds(self):
        table1.insert().execute(**{"this_is_the_primarykey_column":1, "this_is_the_data_column":"data1"})
        table1.insert().execute(**{"this_is_the_primarykey_column":2, "this_is_the_data_column":"data2"})
        table1.insert().execute(**{"this_is_the_primarykey_column":3, "this_is_the_data_column":"data3"})
        table1.insert().execute(**{"this_is_the_primarykey_column":4, "this_is_the_data_column":"data4"})

        r = table1.select(table1.c.this_is_the_primarykey_column == 4).execute()
        assert r.fetchall() == [(4, "data4")]

        r = table1.select(or_(
            table1.c.this_is_the_primarykey_column == 4,
            table1.c.this_is_the_primarykey_column == 2
        )).execute()
        assert r.fetchall() == [(2, "data2"), (4, "data4")]

    def test_insert_no_pk(self):
        table1.insert().execute(**{"this_is_the_data_column":"data1"})
        table1.insert().execute(**{"this_is_the_data_column":"data2"})
        table1.insert().execute(**{"this_is_the_data_column":"data3"})
        table1.insert().execute(**{"this_is_the_data_column":"data4"})

    def test_subquery(self):
        q = table1.select(table1.c.this_is_the_primarykey_column == 4, use_labels=True)
        x = select([q])
        print x.execute().fetchall()

    def test_subquery2(self):
      # this is the test that fails if the "max identifier length" is shorter than the
      # length of the actual columns created, because the column names get truncated.
      # if you try to separate "physical columns" from "labels", and only truncate the labels,
      # the ansisql.visit_select() logic which auto-labels columns in a subquery (for the purposes of sqlite compat) breaks the code,
      # since it is creating "labels" on the fly but not affecting derived columns, which think they are
      # still "physical"
      q = table1.select(table1.c.this_is_the_primarykey_column == 4).alias('foo')
      x = select([q])
      print x.execute().fetchall()

if __name__ == '__main__':
    testbase.main()
<|endoftext|>


<commit_before><commit_msg>exchanged 'solve_ivp_pwc' with 'solve_ivp' in pwc_model_run_fd.py hoping to avoid list issues in memory when running dask.array.map_blocks<commit_after><|endoftext|>


<commit_before>r"""``sphobjinv`` *CLI UI functions*.

``sphobjinv`` is a toolkit for manipulation and inspection of
Sphinx |objects.inv| files.

**Author**
    Brian Skinn (bskinn@alum.mit.edu)

**File Created**
    19 Nov 2020

**Copyright**
    \(c) Brian Skinn 2016-2022

**Source Repository**
    https://github.com/bskinn/sphobjinv

**Documentation**
    https://sphobjinv.readthedocs.io/en/latest

**License**
    The MIT License; see |license_txt|_ for full license terms

**Members**

"""

import sys

from sphobjinv.cli.parser import PrsConst

def log_print(thing, params):
    """Print `thing` to stderr if not in quiet mode.

    Quiet mode is indicated by the value at the |cli:QUIET| key
    within `params`.

    Quiet mode is not implemented for the ":doc:`suggest </cli/suggest>`"
    CLI mode.

    Parameters
    ----------
    thing

        *any* -- Object to be printed

    params

        |dict| -- Parameters/values mapping from the active subparser

    """
    if not params[PrsConst.SUBPARSER_NAME][:2] == "co" or not params[PrsConst.QUIET]:
        print(thing, file=sys.stderr)

def err_format(exc):
    r"""Pretty-format an exception.

    Parameters
    ----------
    exc

        :class:`Exception` -- Exception instance to pretty-format

    Returns
    -------
    pretty_exc

        |str| -- Exception type and message formatted as
        |cour|\ '{type}: {message}'\ |/cour|

    """
    return f"{type(exc).__name__}: {str(exc)}"

def yesno_prompt(prompt):
    r"""Query user at `stdin` for yes/no confirmation.

    Uses :func:`input`, so will hang if used programmatically
    unless `stdin` is suitably mocked.

    The value returned from :func:`input` must satisfy either
    |cour|\ resp.lower() == 'n'\ |/cour| or
    |cour|\ resp.lower() == 'y'\ |/cour|,
    or else the query will be repeated *ad infinitum*.
    This function does **NOT** augment `prompt`
    to indicate the constraints on the accepted values.

    Parameters
    ----------
    prompt

        |str| -- Prompt to display to user that
        requests a 'Y' or 'N' response

    Returns
    -------
    resp

        |str| -- User response

    """
    resp = ""
    while not (resp.lower() == "n" or resp.lower() == "y"):
        resp = input(prompt)
    return resp
<commit_msg>Add 'end' pass-thru to log_print<commit_after>r"""``sphobjinv`` *CLI UI functions*.

``sphobjinv`` is a toolkit for manipulation and inspection of
Sphinx |objects.inv| files.

**Author**
    Brian Skinn (bskinn@alum.mit.edu)

**File Created**
    19 Nov 2020

**Copyright**
    \(c) Brian Skinn 2016-2022

**Source Repository**
    https://github.com/bskinn/sphobjinv

**Documentation**
    https://sphobjinv.readthedocs.io/en/latest

**License**
    The MIT License; see |license_txt|_ for full license terms

**Members**

"""

import sys

from sphobjinv.cli.parser import PrsConst

def log_print(thing, params, *, end="\n"):
    r"""Print `thing` to stderr if not in quiet mode.

    Quiet mode is indicated by the value at the |cli:QUIET| key
    within `params`.

    Quiet mode is not implemented for the ":doc:`suggest </cli/suggest>`"
    CLI mode.

    Parameters
    ----------
    thing

        *any* -- Object to be printed

    params

        |dict| -- Parameters/values mapping from the active subparser

    end

        |str| -- String to append to printed content (default: ``\n``\ )

    """
    if params[PrsConst.SUBPARSER_NAME][:2] == "su" or not params[PrsConst.QUIET]:
        print(thing, file=sys.stderr, end=end)

def err_format(exc):
    r"""Pretty-format an exception.

    Parameters
    ----------
    exc

        :class:`Exception` -- Exception instance to pretty-format

    Returns
    -------
    pretty_exc

        |str| -- Exception type and message formatted as
        |cour|\ '{type}: {message}'\ |/cour|

    """
    return f"{type(exc).__name__}: {str(exc)}"

def yesno_prompt(prompt):
    r"""Query user at `stdin` for yes/no confirmation.

    Uses :func:`input`, so will hang if used programmatically
    unless `stdin` is suitably mocked.

    The value returned from :func:`input` must satisfy either
    |cour|\ resp.lower() == 'n'\ |/cour| or
    |cour|\ resp.lower() == 'y'\ |/cour|,
    or else the query will be repeated *ad infinitum*.
    This function does **NOT** augment `prompt`
    to indicate the constraints on the accepted values.

    Parameters
    ----------
    prompt

        |str| -- Prompt to display to user that
        requests a 'Y' or 'N' response

    Returns
    -------
    resp

        |str| -- User response

    """
    resp = ""
    while not (resp.lower() == "n" or resp.lower() == "y"):
        resp = input(prompt)
    return resp
<|endoftext|>


<commit_before>__all__ = ["session", "user"]

import _spotify

def handle_sp_error(errcode):
    if errcode != 0:
        msg = _spotify.error_message(errcode)
        raise LibSpotifyError(msg)

class LibSpotifyError(Exception):
    pass

class SessionCallbacks:
    def logged_in(self, session, error):
        pass

    def logged_out(self, session):
        pass

    def metadata_updated(self, session):
        pass

    def connection_error(self, session, error):
        pass

    def message_to_user(self, session, message):
        pass

    def music_delivery(self, format, frames, num_frames):
        pass

    def play_token_lost(self, session):
        pass

    def log_message(self, session, message):
        pass

    def end_of_track(self, session, error):
        pass

    def userinfo_updated(self, session):
        pass

    def start_playback(self, session):
        pass

    def stop_playback(self, session):
        pass

    def get_audio_buffer_stats(self, session, stats):
        pass
<commit_msg>spotify: Added a MainLoop class, which takes care of waits and notifications.<commit_after>__all__ = ["session", "user"]

import _spotify
import threading

def handle_sp_error(errcode):
    if errcode != 0:
        msg = _spotify.error_message(errcode)
        raise LibSpotifyError(msg)

class LibSpotifyError(Exception):
    pass

class MainLoop:
    _event = None

    def __init__(self):
        self._event = threading.Event()

    def loop(self, session):
        timeout = None

        while True:
            self._event.wait(timeout)
            timeout = session.process_events()
            self._event.clear()

    def notify(self):
        self._event.set()

class SessionCallbacks:
    def logged_in(self, session, error):
        pass

    def logged_out(self, session):
        pass

    def metadata_updated(self, session):
        pass

    def connection_error(self, session, error):
        pass

    def message_to_user(self, session, message):
        pass

    def notify_main_thread(self, session):
        pass

    def music_delivery(self, format, frames, num_frames):
        pass

    def play_token_lost(self, session):
        pass

    def log_message(self, session, message):
        pass

    def end_of_track(self, session, error):
        pass

    def userinfo_updated(self, session):
        pass

    def start_playback(self, session):
        pass

    def stop_playback(self, session):
        pass

    def get_audio_buffer_stats(self, session, stats):
        pass
<|endoftext|>


<commit_before>import os
try:
    from tempfile import TemporaryDirectory
except ImportError:
    from backports.tempfile import TemporaryDirectory
import pytest
from PilLite import Image

BASE_DIR = os.path.dirname(__file__)
SIZE = (256, 184)

@pytest.fixture(scope='function')
def tmpdir():
    with TemporaryDirectory() as folder:
        yield folder

@pytest.fixture(params=[
    '.png', '.jpg', '.bmp'
], scope='function')
def infile(request):
    ext = request.param
    yield os.path.join(BASE_DIR, 'data', 'image' + ext)

@pytest.fixture(params=[
    '.png', '.jpg', '.bmp'
], scope='function')
def outfile(tmpdir, request):
    ext = request.param
    yield os.path.join(tmpdir, 'image' + ext)

@pytest.fixture
def png_file():
    yield os.path.join(BASE_DIR, 'data', 'image.png')

@pytest.fixture
def missing_file():
    yield os.path.join(BASE_DIR, 'data', 'missing.png')

@pytest.fixture
def unsupported_file():
    yield os.path.join(BASE_DIR, 'data', 'image.tif')

def test_open(infile):
    img = Image.open(infile)
    assert img.size == SIZE

def test_open_file_desciptor(infile):
    with open(infile, 'rb') as fd:
        img = Image.open(fd)
        assert img.size == SIZE

def test_open_and_save(infile, outfile):
    img = Image.open(infile)
    img.save(outfile)
    assert os.path.exists(outfile)
    img = Image.open(outfile)
    assert img.size == SIZE

def test_open_and_save_file_descriptor(infile, outfile):
    img = Image.open(infile)
    with open(outfile, 'wb') as fd:
        img.save(fd)
    assert os.path.exists(outfile)
    img = Image.open(outfile)
    assert img.size == SIZE

@pytest.fixture(params=[(128, 90), (512, 360)])
def resize(request):
    yield request.param

def test_resize(png_file, resize):
    img = Image.open(png_file)
    img2 = img.resize(resize)
    assert img2 is not img
    assert img2.size == resize
    assert img.size == SIZE

def test_thumbnail(png_file):
    img = Image.open(png_file)
    img.thumbnail((128, 90))
    assert img.size == (125, 90)

def test_not_found(missing_file):
    with pytest.raises(FileNotFoundError):
        Image.open(missing_file)

def test_unsupported(unsupported_file):
    with pytest.raises(IOError):
        Image.open(unsupported_file)

def test_save_unsupported(png_file, tmpdir):
    img = Image.open(png_file)
    outfile = os.path.join(tmpdir, 'image.tif')
    with pytest.raises(ValueError):
        img.save(outfile)

def test_save_unsupported2(png_file, tmpdir):
    img = Image.open(png_file)
    outfile = os.path.join(tmpdir, 'image')
    with pytest.raises(ValueError):
        img.save(outfile, format='TIFF')
<commit_msg>fix exception type for python 2.7<commit_after>import os
try:
    from tempfile import TemporaryDirectory
except ImportError:
    from backports.tempfile import TemporaryDirectory
import pytest
from PilLite import Image

BASE_DIR = os.path.dirname(__file__)
SIZE = (256, 184)

@pytest.fixture(scope='function')
def tmpdir():
    with TemporaryDirectory() as folder:
        yield folder

@pytest.fixture(params=[
    '.png', '.jpg', '.bmp'
], scope='function')
def infile(request):
    ext = request.param
    yield os.path.join(BASE_DIR, 'data', 'image' + ext)

@pytest.fixture(params=[
    '.png', '.jpg', '.bmp'
], scope='function')
def outfile(tmpdir, request):
    ext = request.param
    yield os.path.join(tmpdir, 'image' + ext)

@pytest.fixture
def png_file():
    yield os.path.join(BASE_DIR, 'data', 'image.png')

@pytest.fixture
def missing_file():
    yield os.path.join(BASE_DIR, 'data', 'missing.png')

@pytest.fixture
def unsupported_file():
    yield os.path.join(BASE_DIR, 'data', 'image.tif')

def test_open(infile):
    img = Image.open(infile)
    assert img.size == SIZE

def test_open_file_desciptor(infile):
    with open(infile, 'rb') as fd:
        img = Image.open(fd)
        assert img.size == SIZE

def test_open_and_save(infile, outfile):
    img = Image.open(infile)
    img.save(outfile)
    assert os.path.exists(outfile)
    img = Image.open(outfile)
    assert img.size == SIZE

def test_open_and_save_file_descriptor(infile, outfile):
    img = Image.open(infile)
    with open(outfile, 'wb') as fd:
        img.save(fd)
    assert os.path.exists(outfile)
    img = Image.open(outfile)
    assert img.size == SIZE

@pytest.fixture(params=[(128, 90), (512, 360)])
def resize(request):
    yield request.param

def test_resize(png_file, resize):
    img = Image.open(png_file)
    img2 = img.resize(resize)
    assert img2 is not img
    assert img2.size == resize
    assert img.size == SIZE

def test_thumbnail(png_file):
    img = Image.open(png_file)
    img.thumbnail((128, 90))
    assert img.size == (125, 90)

def test_not_found(missing_file):
    with pytest.raises(IOError):
        Image.open(missing_file)

def test_unsupported(unsupported_file):
    with pytest.raises(IOError):
        Image.open(unsupported_file)

def test_save_unsupported(png_file, tmpdir):
    img = Image.open(png_file)
    outfile = os.path.join(tmpdir, 'image.tif')
    with pytest.raises(ValueError):
        img.save(outfile)

def test_save_unsupported2(png_file, tmpdir):
    img = Image.open(png_file)
    outfile = os.path.join(tmpdir, 'image')
    with pytest.raises(ValueError):
        img.save(outfile, format='TIFF')
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from unittest import TestCase

import numpy as np

from sldc import WorkflowBuilder, Segmenter, PolygonClassifier, WorkflowChainBuilder, DispatchingRule, PolygonFilter
from .util import NumpyImage, relative_error, draw_multisquare, draw_multicircle, circularity

__author__ = "Mormont Romain <romain.mormont@gmail.com>"
__version__ = "0.1"

from sldc.util import has_alpha_channel

class BigShapeSegmenter(Segmenter):
    """A segmenter which matches pixels greater than 0"""
    def segment(self, image):
        if has_alpha_channel(image):
            image = np.squeeze(image[:, :, 0:-1])
        return (image > 0).astype(np.uint8)

class SmallSquareSegmenter(Segmenter):
    """A segmenter which matches pixels in the range ]100, 200["""
    def segment(self, image):
        if has_alpha_channel(image):
            image = np.squeeze(image[:, :, 0:-1])
        return np.logical_and(image < 200, image > 100).astype(np.uint8)

class SmallCircleSegmenter(Segmenter):
    """A segementer which matches pixels in the range ]50, 100["""
    def segment(self, image):
        if has_alpha_channel(image):
            image = np.squeeze(image[:, :, 0:-1])
        return np.logical_and(image < 100, image > 50).astype(np.uint8)

class DumbClassifier(PolygonClassifier):
    """A classifier which always return class 1 and probability 1.0"""
    def predict(self, image, polygon):
        return 1, 1.0

class CircleDispatch(DispatchingRule):
    """A rule that dispatches circles"""
    def evaluate(self, image, polygon):
        return circularity(polygon) > 0.85

class SquareDispatch(DispatchingRule):
    """A rule that dispatches squares"""
    def evaluate(self, image, polygon):
        return circularity(polygon) < 0.85

class CircleShapeFilter(PolygonFilter):
    """A filter which excludes all shapes which were not detected by the first workflow and which are not circles"""
    def filter(self, chain_information):
        return [polygon for workflow, polygon, dispatch, _, _ in chain_information
                if workflow == 0 and dispatch == "circle"]

class SquareShapeFilter(PolygonFilter):
    """A filter which excludes all shapes which were not detected by the first workflow and which are not squares"""
    def filter(self, chain_information):
        return [polygon for workflow, polygon, dispatch, _, _ in chain_information
                if workflow == 0 and dispatch == "square"]

class TestChaining(TestCase):
    """A test case for testing the chaining"""
    def testSquareIncluded(self):
        # generate the image to be processed
        w, h = 2000, 2000
        image = np.zeros((h, w), dtype=np.uint8)

        # locations of the 9 multi-squares
        positions = [
            (w / 7, h / 7),
            (3 * w / 7, h / 7),
            (5 * w / 7, h / 7),
            (w / 7, 3 * h / 7),
            (3 * w / 7, 3 * h / 7),
            (5 * w / 7, 3 * h / 7),
            (w / 7, 5 * h / 7),
            (3 * w / 7, 5 * h / 7),
            (5 * w / 7, 5 * h / 7)
        ]

        for position in positions:
            image = draw_multisquare(image, position, w / 7, color_in=127)

        # Build workflow
        builder = WorkflowBuilder()

        # Build workflow 1
        builder.set_segmenter(BigShapeSegmenter())
        builder.add_catchall_classifier(DumbClassifier())
        builder.set_tile_size(512, 512)
        workflow1 = builder.get()

        # Build workflow 2
        builder.set_segmenter(SmallSquareSegmenter())
        builder.add_catchall_classifier(DumbClassifier())
        workflow2 = builder.get()

        # Build chaining
        chain_builder = WorkflowChainBuilder()
        chain_builder.set_first_workflow(workflow1, label="big_squares")
        chain_builder.add_executor(workflow2, label="small_squares")
        chain = chain_builder.get()

        # Launch
        chain_info = chain.process(NumpyImage(image))

        # check results
        big_area = (w / 7) ** 2
        small_area = (w / 35) ** 2

        info1 = chain_info["big_squares"]
        self.assertEqual(9, len(info1))
        for polygon, disp, cls, proba in info1:
            self.assertTrue(relative_error(polygon.area, big_area) < 0.005)
            self.assertEqual("catchall", disp)
            self.assertEqual(1, cls)
            self.assertAlmostEqual(1.0, proba)

        info2 = chain_info["small_squares"]
        self.assertEqual(36, len(info2))
        for polygon, disp, cls, proba in info2:
            self.assertTrue(relative_error(polygon.area, small_area) < 0.005)
            self.assertEqual("catchall", disp)
            self.assertEqual(1, cls)
            self.assertAlmostEqual(1.0, proba)

    def testSquareAndCircleIncluded(self):
        w, h = 2000, 2000
        image = np.zeros((h, w), dtype=np.uint8)
        # locations of the 9 multi-squares
        shapes = [
            ("c", (w / 7, h / 7)),
            ("s", (3 * w / 7, h / 7)),
            ("s", (5 * w / 7, h / 7)),
            ("s", (w / 7, 3 * h / 7)),
            ("c", (3 * w / 7, 3 * h / 7)),
            ("s", (5 * w / 7, 3 * h / 7)),
            ("c", (w / 7, 5 * h / 7)),
            ("s", (3 * w / 7, 5 * h / 7)),
            ("c", (5 * w / 7, 5 * h / 7))
        ]

        for shape, position in shapes:
            if shape == "c":
                image = draw_multicircle(image, position, w / 7, color_in=87)
            elif shape == "s":
                image = draw_multisquare(image, position, w / 7, color_in=187)

        # Build workflows
        # 1st: find big shapes and dispatch them as circle or square
        # 2nd: find small circles in found circle shapes
        # 3rd: find small squares in found square shape
        builder = WorkflowBuilder()

        builder.set_segmenter(BigShapeSegmenter())
        builder.add_classifier(CircleDispatch(), DumbClassifier(), dispatching_label="circle")
        builder.add_classifier(SquareDispatch(), DumbClassifier(), dispatching_label="square")
        builder.set_tile_size(512, 512)
        workflow1 = builder.get()

        builder.set_segmenter(SmallCircleSegmenter())
        builder.add_catchall_classifier(DumbClassifier())
        workflow2 = builder.get()

        builder.set_segmenter(SmallSquareSegmenter())
        builder.add_catchall_classifier(DumbClassifier())
        workflow3 = builder.get()

        # Build chain
        chain_builder = WorkflowChainBuilder()
        chain_builder.set_first_workflow(workflow1)
        chain_builder.add_executor(workflow2, filter=CircleShapeFilter())
        chain_builder.add_executor(workflow3, filter=SquareShapeFilter(), n_jobs=2)
        chain = chain_builder.get()

        chain_info = chain.process(NumpyImage(image))

        info1 = chain_info[0]
        self.assertEqual(9, len(info1))
        self.assertEqual(4, len([d for d in info1.dispatch if d == "circle"]))
        self.assertEqual(5, len([d for d in info1.dispatch if d == "square"]))

        info2 = chain_info[1]
        self.assertEqual(16, len(info2))

        info3 = chain_info[2]
        self.assertEqual(20, len(info3))
<commit_msg>fix test_chain by using // integer division<commit_after># -*- coding: utf-8 -*-
from unittest import TestCase

import numpy as np

from sldc import WorkflowBuilder, Segmenter, PolygonClassifier, WorkflowChainBuilder, DispatchingRule, PolygonFilter
from sldc.util import has_alpha_channel
from .util import NumpyImage, relative_error, draw_multisquare, draw_multicircle, circularity

__author__ = "Mormont Romain <romain.mormont@gmail.com>"
__version__ = "0.1"

class BigShapeSegmenter(Segmenter):
    """A segmenter which matches pixels greater than 0"""
    def segment(self, image):
        if has_alpha_channel(image):
            image = np.squeeze(image[:, :, 0:-1])
        return (image > 0).astype(np.uint8)

class SmallSquareSegmenter(Segmenter):
    """A segmenter which matches pixels in the range ]100, 200["""
    def segment(self, image):
        if has_alpha_channel(image):
            image = np.squeeze(image[:, :, 0:-1])
        return np.logical_and(image < 200, image > 100).astype(np.uint8)

class SmallCircleSegmenter(Segmenter):
    """A segementer which matches pixels in the range ]50, 100["""
    def segment(self, image):
        if has_alpha_channel(image):
            image = np.squeeze(image[:, :, 0:-1])
        return np.logical_and(image < 100, image > 50).astype(np.uint8)

class DumbClassifier(PolygonClassifier):
    """A classifier which always return class 1 and probability 1.0"""
    def predict(self, image, polygon):
        return 1, 1.0

class CircleDispatch(DispatchingRule):
    """A rule that dispatches circles"""
    def evaluate(self, image, polygon):
        return circularity(polygon) > 0.85

class SquareDispatch(DispatchingRule):
    """A rule that dispatches squares"""
    def evaluate(self, image, polygon):
        return circularity(polygon) < 0.85

class CircleShapeFilter(PolygonFilter):
    """A filter which excludes all shapes which were not detected by the first workflow and which are not circles"""
    def filter(self, chain_information):
        return [polygon for workflow, polygon, dispatch, _, _ in chain_information
                if workflow == 0 and dispatch == "circle"]

class SquareShapeFilter(PolygonFilter):
    """A filter which excludes all shapes which were not detected by the first workflow and which are not squares"""
    def filter(self, chain_information):
        return [polygon for workflow, polygon, dispatch, _, _ in chain_information
                if workflow == 0 and dispatch == "square"]

class TestChaining(TestCase):
    """A test case for testing the chaining"""
    def testSquareIncluded(self):
        # generate the image to be processed
        w, h = 2000, 2000
        image = np.zeros((h, w), dtype=np.uint8)

        # locations of the 9 multi-squares
        positions = [
            (w // 7, h // 7),
            (3 * w // 7, h // 7),
            (5 * w // 7, h // 7),
            (w // 7, 3 * h // 7),
            (3 * w // 7, 3 * h // 7),
            (5 * w // 7, 3 * h // 7),
            (w // 7, 5 * h // 7),
            (3 * w // 7, 5 * h // 7),
            (5 * w // 7, 5 * h // 7)
        ]

        for position in positions:
            image = draw_multisquare(image, position, w // 7, color_in=127)

        # Build workflow
        builder = WorkflowBuilder()

        # Build workflow 1
        builder.set_segmenter(BigShapeSegmenter())
        builder.add_catchall_classifier(DumbClassifier())
        builder.set_tile_size(512, 512)
        workflow1 = builder.get()

        # Build workflow 2
        builder.set_segmenter(SmallSquareSegmenter())
        builder.add_catchall_classifier(DumbClassifier())
        workflow2 = builder.get()

        # Build chaining
        chain_builder = WorkflowChainBuilder()
        chain_builder.set_first_workflow(workflow1, label="big_squares")
        chain_builder.add_executor(workflow2, label="small_squares")
        chain = chain_builder.get()

        # Launch
        chain_info = chain.process(NumpyImage(image))

        # check results
        big_area = (w // 7) ** 2
        small_area = (w / 35) ** 2

        info1 = chain_info["big_squares"]
        self.assertEqual(9, len(info1))
        for polygon, disp, cls, proba in info1:
            self.assertTrue(relative_error(polygon.area, big_area) < 0.005)
            self.assertEqual("catchall", disp)
            self.assertEqual(1, cls)
            self.assertAlmostEqual(1.0, proba)

        info2 = chain_info["small_squares"]
        self.assertEqual(36, len(info2))
        for polygon, disp, cls, proba in info2:
            self.assertTrue(relative_error(polygon.area, small_area) < 0.005)
            self.assertEqual("catchall", disp)
            self.assertEqual(1, cls)
            self.assertAlmostEqual(1.0, proba)

    def testSquareAndCircleIncluded(self):
        w, h = 2000, 2000
        image = np.zeros((h, w), dtype=np.uint8)
        # locations of the 9 multi-squares
        shapes = [
            ("c", (w // 7, h // 7)),
            ("s", (3 * w // 7, h // 7)),
            ("s", (5 * w // 7, h // 7)),
            ("s", (w // 7, 3 * h // 7)),
            ("c", (3 * w // 7, 3 * h // 7)),
            ("s", (5 * w // 7, 3 * h // 7)),
            ("c", (w // 7, 5 * h // 7)),
            ("s", (3 * w // 7, 5 * h // 7)),
            ("c", (5 * w // 7, 5 * h // 7))
        ]

        for shape, position in shapes:
            if shape == "c":
                image = draw_multicircle(image, position, w // 7, color_in=87)
            elif shape == "s":
                image = draw_multisquare(image, position, w // 7, color_in=187)

        # Build workflows
        # 1st: find big shapes and dispatch them as circle or square
        # 2nd: find small circles in found circle shapes
        # 3rd: find small squares in found square shape
        builder = WorkflowBuilder()

        builder.set_segmenter(BigShapeSegmenter())
        builder.add_classifier(CircleDispatch(), DumbClassifier(), dispatching_label="circle")
        builder.add_classifier(SquareDispatch(), DumbClassifier(), dispatching_label="square")
        builder.set_tile_size(512, 512)
        workflow1 = builder.get()

        builder.set_segmenter(SmallCircleSegmenter())
        builder.add_catchall_classifier(DumbClassifier())
        workflow2 = builder.get()

        builder.set_segmenter(SmallSquareSegmenter())
        builder.add_catchall_classifier(DumbClassifier())
        workflow3 = builder.get()

        # Build chain
        chain_builder = WorkflowChainBuilder()
        chain_builder.set_first_workflow(workflow1)
        chain_builder.add_executor(workflow2, filter=CircleShapeFilter())
        chain_builder.add_executor(workflow3, filter=SquareShapeFilter(), n_jobs=2)
        chain = chain_builder.get()

        chain_info = chain.process(NumpyImage(image))

        info1 = chain_info[0]
        self.assertEqual(9, len(info1))
        self.assertEqual(4, len([d for d in info1.dispatch if d == "circle"]))
        self.assertEqual(5, len([d for d in info1.dispatch if d == "square"]))

        info2 = chain_info[1]
        self.assertEqual(16, len(info2))

        info3 = chain_info[2]
        self.assertEqual(20, len(info3))
<|endoftext|>


<commit_before># coding=utf-8
from .context import jsuite, fixture

import unittest

class CountSuite(unittest.TestCase):
  def test_count_figures_in_body(self):
    with fixture('count/4-figures') as f:
      doc = jsuite.from_file(f)
      count = jsuite.count.of_type_in_body(doc, 'fig')
      assert(count == 4)

  def test_count_figures(self):
    with fixture('count/4-figures') as f:
      doc = jsuite.from_file(f)
      count = jsuite.count.of_type(doc, 'fig')
      assert(count == 4)<commit_msg>fix test: There are 5 figures in the 4-figures file<commit_after># coding=utf-8
from .context import jsuite, fixture

import unittest

class CountSuite(unittest.TestCase):
  def test_count_figures_in_body(self):
    with fixture('count/4-figures') as f:
      doc = jsuite.from_file(f)
      count = jsuite.count.of_type_in_body(doc, 'fig')
      assert(count == 4)

  def test_count_figures(self):
    with fixture('count/4-figures') as f:
      doc = jsuite.from_file(f)
      count = jsuite.count.of_type(doc, 'fig')
      assert(count == 5)<|endoftext|>


<commit_before>import unittest
from unittest.mock import patch, MagicMock
import json
import os
import time
import numpy as np
from datetime import datetime, timedelta

from tweetfeels import (TweetFeels, Tweet, TweetData)

class Test_Feels(unittest.TestCase):
    def setUp(self):
        TweetFeels._db_factory = (lambda db: MagicMock())
        TweetFeels._auth_factory = (lambda cred: MagicMock())
        TweetFeels._listener_factory = (lambda ctrl: MagicMock())
        TweetFeels._stream_factory = (lambda auth, listener: MagicMock())
        self.tweets_data_path = 'test/sample.json'
        self.tweets = [
            {'created_at': 'Sun Feb 19 19:14:18 +0000 2017',
             'id_str': '833394296418082817',
             'text': 'Tweetfeels is tremendous! Believe me. I know.',
             'user': {'followers_count': '100', 'friends_count': '200',
                      'location':None}
            }, # sentiment value = 0
            {'created_at': 'Sun Feb 20 19:14:19 +0000 2017',
             'id_str': '833394296418082818',
             'text': 'Fake news. Sad!',
             'user': {'followers_count': '100', 'friends_count': '200',
                      'location':None}
            }, # sentiment value = -0.7351
            {'created_at': 'Sun Feb 21 19:14:20 +0000 2017',
             'id_str': '833394296418082819',
             'text': 'I hate it.',
             'user': {'followers_count': '100', 'friends_count': '200',
                      'location':None}
            } # sentiment value = -0.5719
            ]
        self.mock_feels = TweetFeels('abcd')
        self.feels_db = TweetData(file='./test/db.sqlite')
        self.mock_feels._feels = self.feels_db
        self.mock_tweets = [Tweet(t) for t in self.tweets]

    def tearDown(self):
        os.remove('./test/db.sqlite')

    def test_start(self):
        mock_feels = TweetFeels("abcd")
        mock_feels.tracking = []
        mock_feels.start()
        mock_feels._stream.filter.assert_not_called()
        mock_feels.tracking = ['tsla']
        mock_feels.start()
        mock_feels._stream.filter.assert_called_once()

    def test_stop(self):
        mock_feels = TweetFeels("abcd")
        mock_feels.stop()
        mock_feels._stream.disconnect.assert_called_once()

    def test_on_data(self):
        mock_feels = TweetFeels("abcd")
        mock_feels.buffer_limit = 0
        data = {'filter_level': 'low', 'text': 'test data'}
        mock_feels.on_data(data)
        mock_feels._feels.insert_tweet.assert_called_once()

        # test filtering levels
        mock_feels2 = TweetFeels("abcd")
        mock_feels2._filter_level = 'medium'
        mock_feels2.on_data(data)
        mock_feels2._feels.insert_tweet.assert_not_called()

        # test buffer limit. no inserts until we are over limit
        mock_feels2.buffer_limit = 2
        mock_feels2.filter_level = 'low'
        mock_feels2.on_data(data)
        mock_feels2._feels.insert_tweet.assert_not_called()
        mock_feels2.on_data(data)
        mock_feels2.on_data(data)
        mock_feels._feels.insert_tweet.assert_called_once()

    def test_sentiment(self):
        mock_feels = TweetFeels("abcd")
        mock_feels._feels.tweets_since = MagicMock(return_value=[])
        mock_feels._sentiment = 0.5
        self.assertEqual(mock_feels.sentiment, 0.5)

    def test_buffer(self):
        mock_feels = TweetFeels('abcd')
        mock_feels.buffer_limit = 5
        feels_db = TweetData(file='sample.sqlite')
        mock_feels._feels = feels_db
        with open(self.tweets_data_path) as tweets_file:
            lines = list(filter(None, (line.rstrip() for line in tweets_file)))
            for line in lines[0:3]:
                t = Tweet(json.loads(line))
                mock_feels.on_data(t)
            self.assertEqual(len(mock_feels._tweet_buffer), 3)
            for line in lines[3:6]:
                t = Tweet(json.loads(line))
                mock_feels.on_data(t)
            time.sleep(1) #this waits for items to finish popping off the buffer
            self.assertEqual(len(mock_feels._tweet_buffer), 0)
            dfs = [df for df in mock_feels._feels.all]
            self.assertEqual(len(dfs[0]), 6)
        os.remove('sample.sqlite')

    def test_sentiment_comprehensive(self):
        sentiment = 0.0
        for t in self.mock_tweets:
            self.feels_db.insert_tweet(t)
            if t['sentiment']!=0:
                print(f'0.99*{sentiment} + 0.01*{t["sentiment"]}')
                sentiment = 0.99*sentiment + 0.01*t['sentiment']
                print(f'sentiment = {sentiment}')
        self.mock_feels.clear_buffer()
        self.mock_feels.calc_every_n = 1
        # calc = 0*0.99**2 + 0.01*0.99*-0.7531 + 0.01*-0.5719
        #      = -0.01299649
        now = datetime.now()
        self.assertEqual(self.mock_feels.sentiment, sentiment)
        self.assertTrue(self.mock_feels._latest_calc > now)

    def test_sentiments(self):
        for t in self.mock_tweets:
            self.feels_db.insert_tweet(t)
        self.mock_feels.clear_buffer()
        self.mock_feels.calc_every_n = 1
        start = datetime(2017, 2, 19, 0, 0, 0)
        dt = timedelta(days=1)
        sentiment = self.mock_feels.sentiments(start, dt)
        self.assertEqual(next(sentiment), 0)
        self.assertEqual(next(sentiment), -0.007351)
        self.assertEqual(next(sentiment), -0.01299649)
<commit_msg>Test that _latest_calc b being updated.<commit_after>import unittest
from unittest.mock import patch, MagicMock
import json
import os
import time
import numpy as np
from datetime import datetime, timedelta

from tweetfeels import (TweetFeels, Tweet, TweetData)

class Test_Feels(unittest.TestCase):
    def setUp(self):
        TweetFeels._db_factory = (lambda db: MagicMock())
        TweetFeels._auth_factory = (lambda cred: MagicMock())
        TweetFeels._listener_factory = (lambda ctrl: MagicMock())
        TweetFeels._stream_factory = (lambda auth, listener: MagicMock())
        self.tweets_data_path = 'test/sample.json'
        self.tweets = [
            {'created_at': 'Sun Feb 19 19:14:18 +0000 2017',
             'id_str': '833394296418082817',
             'text': 'Tweetfeels is tremendous! Believe me. I know.',
             'user': {'followers_count': '100', 'friends_count': '200',
                      'location':None}
            }, # sentiment value = 0
            {'created_at': 'Sun Feb 20 19:14:19 +0000 2017',
             'id_str': '833394296418082818',
             'text': 'Fake news. Sad!',
             'user': {'followers_count': '100', 'friends_count': '200',
                      'location':None}
            }, # sentiment value = -0.7351
            {'created_at': 'Sun Feb 21 19:14:20 +0000 2017',
             'id_str': '833394296418082819',
             'text': 'I hate it.',
             'user': {'followers_count': '100', 'friends_count': '200',
                      'location':None}
            } # sentiment value = -0.5719
            ]
        self.mock_feels = TweetFeels('abcd')
        self.feels_db = TweetData(file='./test/db.sqlite')
        self.mock_feels._feels = self.feels_db
        self.mock_tweets = [Tweet(t) for t in self.tweets]

    def tearDown(self):
        os.remove('./test/db.sqlite')

    def test_start(self):
        mock_feels = TweetFeels("abcd")
        mock_feels.tracking = []
        mock_feels.start()
        mock_feels._stream.filter.assert_not_called()
        mock_feels.tracking = ['tsla']
        mock_feels.start()
        mock_feels._stream.filter.assert_called_once()

    def test_stop(self):
        mock_feels = TweetFeels("abcd")
        mock_feels.stop()
        mock_feels._stream.disconnect.assert_called_once()

    def test_on_data(self):
        mock_feels = TweetFeels("abcd")
        mock_feels.buffer_limit = 0
        data = {'filter_level': 'low', 'text': 'test data'}
        mock_feels.on_data(data)
        mock_feels._feels.insert_tweet.assert_called_once()

        # test filtering levels
        mock_feels2 = TweetFeels("abcd")
        mock_feels2._filter_level = 'medium'
        mock_feels2.on_data(data)
        mock_feels2._feels.insert_tweet.assert_not_called()

        # test buffer limit. no inserts until we are over limit
        mock_feels2.buffer_limit = 2
        mock_feels2.filter_level = 'low'
        mock_feels2.on_data(data)
        mock_feels2._feels.insert_tweet.assert_not_called()
        mock_feels2.on_data(data)
        mock_feels2.on_data(data)
        mock_feels._feels.insert_tweet.assert_called_once()

    def test_sentiment(self):
        mock_feels = TweetFeels("abcd")
        mock_feels._feels.tweets_since = MagicMock(return_value=[])
        mock_feels._sentiment = 0.5
        self.assertEqual(mock_feels.sentiment, 0.5)

    def test_buffer(self):
        mock_feels = TweetFeels('abcd')
        mock_feels.buffer_limit = 5
        feels_db = TweetData(file='sample.sqlite')
        mock_feels._feels = feels_db
        with open(self.tweets_data_path) as tweets_file:
            lines = list(filter(None, (line.rstrip() for line in tweets_file)))
            for line in lines[0:3]:
                t = Tweet(json.loads(line))
                mock_feels.on_data(t)
            self.assertEqual(len(mock_feels._tweet_buffer), 3)
            for line in lines[3:6]:
                t = Tweet(json.loads(line))
                mock_feels.on_data(t)
            time.sleep(1) #this waits for items to finish popping off the buffer
            self.assertEqual(len(mock_feels._tweet_buffer), 0)
            dfs = [df for df in mock_feels._feels.all]
            self.assertEqual(len(dfs[0]), 6)
        os.remove('sample.sqlite')

    def test_sentiment_comprehensive(self):
        sentiment = 0.0
        for t in self.mock_tweets:
            self.feels_db.insert_tweet(t)
            if t['sentiment']!=0:
                print(f'0.99*{sentiment} + 0.01*{t["sentiment"]}')
                sentiment = 0.99*sentiment + 0.01*t['sentiment']
                print(f'sentiment = {sentiment}')
        self.mock_feels.clear_buffer()
        self.mock_feels.calc_every_n = 1
        # calc = 0*0.99**2 + 0.01*0.99*-0.7531 + 0.01*-0.5719
        #      = -0.01299649
        now = datetime.now()
        self.assertEqual(self.mock_feels.sentiment, sentiment)
        self.assertTrue(self.mock_feels._latest_calc > now)

    def test_sentiments(self):
        for t in self.mock_tweets:
            self.feels_db.insert_tweet(t)
        self.mock_feels.clear_buffer()
        self.mock_feels.calc_every_n = 1
        start = datetime(2017, 2, 19, 0, 0, 0)
        dt = timedelta(days=1)
        now = datetime.now()
        sentiment = self.mock_feels.sentiments(start, dt)
        self.assertEqual(next(sentiment), 0)
        self.assertEqual(next(sentiment), -0.007351)
        self.assertEqual(next(sentiment), -0.01299649)
        for s in sentiment:
            print(s)
        self.assertTrue(self.mock_feels._latest_calc > now)
<|endoftext|>


<commit_before>import sys
import os
import unittest

from tempfile import mkdtemp, mkstemp
import shutil

from rdflib import URIRef, Graph, plugin
from rdflib.exceptions import ParserError
from rdflib.plugin import PluginException

from nose.exc import SkipTest

class GraphTestCase(unittest.TestCase):
    store = "default"
    tmppath = None

    def setUp(self):
        try:
            self.graph = Graph(store=self.store)
        except ImportError:
            raise SkipTest("Dependencies for store '%s' not available!" % self.store)
        if self.store == "SQLite":
            _, self.tmppath = mkstemp(prefix="test", dir="/tmp", suffix=".sqlite")
        else:
            self.tmppath = mkdtemp()
        self.graph.open(self.tmppath, create=True)

        self.michel = URIRef("michel")
        self.tarek = URIRef("tarek")
        self.bob = URIRef("bob")
        self.likes = URIRef("likes")
        self.hates = URIRef("hates")
        self.pizza = URIRef("pizza")
        self.cheese = URIRef("cheese")

    def tearDown(self):
        self.graph.close()
        if os.path.isdir(self.tmppath):
            shutil.rmtree(self.tmppath)
        else:
            os.remove(self.tmppath)

    def addStuff(self):
        tarek = self.tarek
        michel = self.michel
        bob = self.bob
        likes = self.likes
        hates = self.hates
        pizza = self.pizza
        cheese = self.cheese

        self.graph.add((tarek, likes, pizza))
        self.graph.add((tarek, likes, cheese))
        self.graph.add((michel, likes, pizza))
        self.graph.add((michel, likes, cheese))
        self.graph.add((bob, likes, cheese))
        self.graph.add((bob, hates, pizza))
        self.graph.add((bob, hates, michel))  # gasp!

    def removeStuff(self):
        tarek = self.tarek
        michel = self.michel
        bob = self.bob
        likes = self.likes
        hates = self.hates
        pizza = self.pizza
        cheese = self.cheese

        self.graph.remove((tarek, likes, pizza))
        self.graph.remove((tarek, likes, cheese))
        self.graph.remove((michel, likes, pizza))
        self.graph.remove((michel, likes, cheese))
        self.graph.remove((bob, likes, cheese))
        self.graph.remove((bob, hates, pizza))
        self.graph.remove((bob, hates, michel))  # gasp!

    def testAdd(self):
        self.addStuff()

    def testRemove(self):
        self.addStuff()
        self.removeStuff()

    def testTriples(self):
        tarek = self.tarek
        michel = self.michel
        bob = self.bob
        likes = self.likes
        hates = self.hates
        pizza = self.pizza
        cheese = self.cheese
        asserte = self.assertEqual
        triples = self.graph.triples
        Any = None

        self.addStuff()

        # unbound subjects
        asserte(len(list(triples((Any, likes, pizza)))), 2)
        asserte(len(list(triples((Any, hates, pizza)))), 1)
        asserte(len(list(triples((Any, likes, cheese)))), 3)
        asserte(len(list(triples((Any, hates, cheese)))), 0)

        # unbound objects
        asserte(len(list(triples((michel, likes, Any)))), 2)
        asserte(len(list(triples((tarek, likes, Any)))), 2)
        asserte(len(list(triples((bob, hates, Any)))), 2)
        asserte(len(list(triples((bob, likes, Any)))), 1)

        # unbound predicates
        asserte(len(list(triples((michel, Any, cheese)))), 1)
        asserte(len(list(triples((tarek, Any, cheese)))), 1)
        asserte(len(list(triples((bob, Any, pizza)))), 1)
        asserte(len(list(triples((bob, Any, michel)))), 1)

        # unbound subject, objects
        asserte(len(list(triples((Any, hates, Any)))), 2)
        asserte(len(list(triples((Any, likes, Any)))), 5)

        # unbound predicates, objects
        asserte(len(list(triples((michel, Any, Any)))), 2)
        asserte(len(list(triples((bob, Any, Any)))), 3)
        asserte(len(list(triples((tarek, Any, Any)))), 2)

        # unbound subjects, predicates
        asserte(len(list(triples((Any, Any, pizza)))), 3)
        asserte(len(list(triples((Any, Any, cheese)))), 3)
        asserte(len(list(triples((Any, Any, michel)))), 1)

        # all unbound
        asserte(len(list(triples((Any, Any, Any)))), 7)
        self.removeStuff()
        asserte(len(list(triples((Any, Any, Any)))), 0)

    def testConnected(self):
        graph = self.graph
        self.addStuff()
        self.assertEqual(True, graph.connected())

        jeroen = URIRef("jeroen")
        unconnected = URIRef("unconnected")

        graph.add((jeroen, self.likes, unconnected))

        self.assertEqual(False, graph.connected())

    def testSub(self):
        g1 = self.graph
        g2 = Graph(store=g1.store)

        tarek = self.tarek
        # michel = self.michel
        bob = self.bob
        likes = self.likes
        # hates = self.hates
        pizza = self.pizza
        cheese = self.cheese

        g1.add((tarek, likes, pizza))
        g1.add((bob, likes, cheese))

        g2.add((bob, likes, cheese))

        g3 = g1 - g2

        self.assertEqual(len(g3), 1)
        self.assertEqual((tarek, likes, pizza) in g3, True)
        self.assertEqual((tarek, likes, cheese) in g3, False)

        self.assertEqual((bob, likes, cheese) in g3, False)

        g1 -= g2

        self.assertEqual(len(g1), 1)
        self.assertEqual((tarek, likes, pizza) in g1, True)
        self.assertEqual((tarek, likes, cheese) in g1, False)

        self.assertEqual((bob, likes, cheese) in g1, False)

    def testGraphAdd(self):
        g1 = self.graph
        g2 = Graph(store=g1.store)

        tarek = self.tarek
        # michel = self.michel
        bob = self.bob
        likes = self.likes
        # hates = self.hates
        pizza = self.pizza
        cheese = self.cheese

        g1.add((tarek, likes, pizza))

        g2.add((bob, likes, cheese))

        g3 = g1 + g2

        self.assertEqual(len(g3), 2)
        self.assertEqual((tarek, likes, pizza) in g3, True)
        self.assertEqual((tarek, likes, cheese) in g3, False)

        self.assertEqual((bob, likes, cheese) in g3, True)

        g1 += g2

        self.assertEqual(len(g1), 2)
        self.assertEqual((tarek, likes, pizza) in g1, True)
        self.assertEqual((tarek, likes, cheese) in g1, False)

        self.assertEqual((bob, likes, cheese) in g1, True)

    def testGraphIntersection(self):
        g1 = self.graph
        g2 = Graph(store=g1.store)

        tarek = self.tarek
        michel = self.michel
        bob = self.bob
        likes = self.likes
        # hates = self.hates
        pizza = self.pizza
        cheese = self.cheese

        g1.add((tarek, likes, pizza))
        g1.add((michel, likes, cheese))

        g2.add((bob, likes, cheese))
        g2.add((michel, likes, cheese))

        g3 = g1 * g2

        self.assertEqual(len(g3), 1)
        self.assertEqual((tarek, likes, pizza) in g3, False)
        self.assertEqual((tarek, likes, cheese) in g3, False)

        self.assertEqual((bob, likes, cheese) in g3, False)

        self.assertEqual((michel, likes, cheese) in g3, True)

        g1 *= g2

        self.assertEqual(len(g1), 1)

        self.assertEqual((tarek, likes, pizza) in g1, False)
        self.assertEqual((tarek, likes, cheese) in g1, False)

        self.assertEqual((bob, likes, cheese) in g1, False)

        self.assertEqual((michel, likes, cheese) in g1, True)

    def testGuessFormatForParse(self):
        self.graph = Graph()

        # files
        with self.assertRaises(ParserError):
            self.graph.parse(__file__)  # here we are trying to parse a Python file!!

        # .nt can be parsed by Turtle Parser
        self.graph.parse("test/nt/anons-01.nt")
        # RDF/XML
        self.graph.parse("test/rdf/datatypes/test001.rdf")  # XML
        # bad filename but set format
        self.graph.parse("test/rdf/datatypes/test001.borked", format="xml")

        # strings
        self.graph = Graph()

        with self.assertRaises(ParserError):
            self.graph.parse(data="rubbish")

        # Turtle - default
        self.graph.parse(data="<http://example.com/a> <http://example.com/a> <http://example.com/a> .")

        # Turtle - format given
        self.graph.parse(data="<http://example.com/a> <http://example.com/a> <http://example.com/a> .", format="turtle")

        # RDF/XML - format given
        rdf = """<rdf:RDF
  xmlns:ns1="http://example.org/#"
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
>
  <rdf:Description rdf:nodeID="ub63bL2C1">
    <ns1:p rdf:resource="http://example.org/q"/>
    <ns1:r rdf:resource="http://example.org/s"/>
  </rdf:Description>
  <rdf:Description rdf:nodeID="ub63bL5C1">
    <ns1:r>
      <rdf:Description rdf:nodeID="ub63bL6C11">
        <ns1:s rdf:resource="http://example.org/#t"/>
      </rdf:Description>
    </ns1:r>
    <ns1:p rdf:resource="http://example.org/q"/>
  </rdf:Description>
</rdf:RDF>
        """
        self.graph.parse(data=rdf, format="xml")

        # URI
        self.graph = Graph()

        # only getting HTML
        with self.assertRaises(PluginException):
            self.graph.parse(location="https://www.google.com")

        self.graph.parse(location="http://www.w3.org/ns/adms.ttl")
        self.graph.parse(location="http://www.w3.org/ns/adms.rdf")
        # persistent Australian Government online RDF resource without a file-like ending
        self.graph.parse(location="https://linked.data.gov.au/def/agrif?_format=text/turtle")

# dynamically create classes for each registered Store

pluginname = None
if __name__ == "__main__":
    if len(sys.argv) > 1:
        pluginname = sys.argv[1]

tests = 0
for s in plugin.plugins(pluginname, plugin.Store):
    if s.name in (
        "default",
        "Memory",
        "Auditable",
        "Concurrent",
        "SPARQLStore",
        "SPARQLUpdateStore",
    ):
        continue  # these are tested by default

    if s.name in ("SimpleMemory",):
        # these (by design) won't pass some of the tests (like Intersection)
        continue

    locals()["t%d" % tests] = type(
        "%sGraphTestCase" % s.name, (GraphTestCase,), {"store": s.name}
    )
    tests += 1

if __name__ == "__main__":
    unittest.main(argv=sys.argv[:1])
<commit_msg>Fix graphFormatGuess test for when one of the expected endpoints are down<commit_after>import sys
import os
import unittest

from tempfile import mkdtemp, mkstemp
import shutil
from urllib.error import URLError, HTTPError

from rdflib import URIRef, Graph, plugin
from rdflib.exceptions import ParserError
from rdflib.plugin import PluginException

from nose.exc import SkipTest

class GraphTestCase(unittest.TestCase):
    store = "default"
    tmppath = None

    def setUp(self):
        try:
            self.graph = Graph(store=self.store)
        except ImportError:
            raise SkipTest("Dependencies for store '%s' not available!" % self.store)
        if self.store == "SQLite":
            _, self.tmppath = mkstemp(prefix="test", dir="/tmp", suffix=".sqlite")
        else:
            self.tmppath = mkdtemp()
        self.graph.open(self.tmppath, create=True)

        self.michel = URIRef("michel")
        self.tarek = URIRef("tarek")
        self.bob = URIRef("bob")
        self.likes = URIRef("likes")
        self.hates = URIRef("hates")
        self.pizza = URIRef("pizza")
        self.cheese = URIRef("cheese")

    def tearDown(self):
        self.graph.close()
        if os.path.isdir(self.tmppath):
            shutil.rmtree(self.tmppath)
        else:
            os.remove(self.tmppath)

    def addStuff(self):
        tarek = self.tarek
        michel = self.michel
        bob = self.bob
        likes = self.likes
        hates = self.hates
        pizza = self.pizza
        cheese = self.cheese

        self.graph.add((tarek, likes, pizza))
        self.graph.add((tarek, likes, cheese))
        self.graph.add((michel, likes, pizza))
        self.graph.add((michel, likes, cheese))
        self.graph.add((bob, likes, cheese))
        self.graph.add((bob, hates, pizza))
        self.graph.add((bob, hates, michel))  # gasp!

    def removeStuff(self):
        tarek = self.tarek
        michel = self.michel
        bob = self.bob
        likes = self.likes
        hates = self.hates
        pizza = self.pizza
        cheese = self.cheese

        self.graph.remove((tarek, likes, pizza))
        self.graph.remove((tarek, likes, cheese))
        self.graph.remove((michel, likes, pizza))
        self.graph.remove((michel, likes, cheese))
        self.graph.remove((bob, likes, cheese))
        self.graph.remove((bob, hates, pizza))
        self.graph.remove((bob, hates, michel))  # gasp!

    def testAdd(self):
        self.addStuff()

    def testRemove(self):
        self.addStuff()
        self.removeStuff()

    def testTriples(self):
        tarek = self.tarek
        michel = self.michel
        bob = self.bob
        likes = self.likes
        hates = self.hates
        pizza = self.pizza
        cheese = self.cheese
        asserte = self.assertEqual
        triples = self.graph.triples
        Any = None

        self.addStuff()

        # unbound subjects
        asserte(len(list(triples((Any, likes, pizza)))), 2)
        asserte(len(list(triples((Any, hates, pizza)))), 1)
        asserte(len(list(triples((Any, likes, cheese)))), 3)
        asserte(len(list(triples((Any, hates, cheese)))), 0)

        # unbound objects
        asserte(len(list(triples((michel, likes, Any)))), 2)
        asserte(len(list(triples((tarek, likes, Any)))), 2)
        asserte(len(list(triples((bob, hates, Any)))), 2)
        asserte(len(list(triples((bob, likes, Any)))), 1)

        # unbound predicates
        asserte(len(list(triples((michel, Any, cheese)))), 1)
        asserte(len(list(triples((tarek, Any, cheese)))), 1)
        asserte(len(list(triples((bob, Any, pizza)))), 1)
        asserte(len(list(triples((bob, Any, michel)))), 1)

        # unbound subject, objects
        asserte(len(list(triples((Any, hates, Any)))), 2)
        asserte(len(list(triples((Any, likes, Any)))), 5)

        # unbound predicates, objects
        asserte(len(list(triples((michel, Any, Any)))), 2)
        asserte(len(list(triples((bob, Any, Any)))), 3)
        asserte(len(list(triples((tarek, Any, Any)))), 2)

        # unbound subjects, predicates
        asserte(len(list(triples((Any, Any, pizza)))), 3)
        asserte(len(list(triples((Any, Any, cheese)))), 3)
        asserte(len(list(triples((Any, Any, michel)))), 1)

        # all unbound
        asserte(len(list(triples((Any, Any, Any)))), 7)
        self.removeStuff()
        asserte(len(list(triples((Any, Any, Any)))), 0)

    def testConnected(self):
        graph = self.graph
        self.addStuff()
        self.assertEqual(True, graph.connected())

        jeroen = URIRef("jeroen")
        unconnected = URIRef("unconnected")

        graph.add((jeroen, self.likes, unconnected))

        self.assertEqual(False, graph.connected())

    def testSub(self):
        g1 = self.graph
        g2 = Graph(store=g1.store)

        tarek = self.tarek
        # michel = self.michel
        bob = self.bob
        likes = self.likes
        # hates = self.hates
        pizza = self.pizza
        cheese = self.cheese

        g1.add((tarek, likes, pizza))
        g1.add((bob, likes, cheese))

        g2.add((bob, likes, cheese))

        g3 = g1 - g2

        self.assertEqual(len(g3), 1)
        self.assertEqual((tarek, likes, pizza) in g3, True)
        self.assertEqual((tarek, likes, cheese) in g3, False)

        self.assertEqual((bob, likes, cheese) in g3, False)

        g1 -= g2

        self.assertEqual(len(g1), 1)
        self.assertEqual((tarek, likes, pizza) in g1, True)
        self.assertEqual((tarek, likes, cheese) in g1, False)

        self.assertEqual((bob, likes, cheese) in g1, False)

    def testGraphAdd(self):
        g1 = self.graph
        g2 = Graph(store=g1.store)

        tarek = self.tarek
        # michel = self.michel
        bob = self.bob
        likes = self.likes
        # hates = self.hates
        pizza = self.pizza
        cheese = self.cheese

        g1.add((tarek, likes, pizza))

        g2.add((bob, likes, cheese))

        g3 = g1 + g2

        self.assertEqual(len(g3), 2)
        self.assertEqual((tarek, likes, pizza) in g3, True)
        self.assertEqual((tarek, likes, cheese) in g3, False)

        self.assertEqual((bob, likes, cheese) in g3, True)

        g1 += g2

        self.assertEqual(len(g1), 2)
        self.assertEqual((tarek, likes, pizza) in g1, True)
        self.assertEqual((tarek, likes, cheese) in g1, False)

        self.assertEqual((bob, likes, cheese) in g1, True)

    def testGraphIntersection(self):
        g1 = self.graph
        g2 = Graph(store=g1.store)

        tarek = self.tarek
        michel = self.michel
        bob = self.bob
        likes = self.likes
        # hates = self.hates
        pizza = self.pizza
        cheese = self.cheese

        g1.add((tarek, likes, pizza))
        g1.add((michel, likes, cheese))

        g2.add((bob, likes, cheese))
        g2.add((michel, likes, cheese))

        g3 = g1 * g2

        self.assertEqual(len(g3), 1)
        self.assertEqual((tarek, likes, pizza) in g3, False)
        self.assertEqual((tarek, likes, cheese) in g3, False)

        self.assertEqual((bob, likes, cheese) in g3, False)

        self.assertEqual((michel, likes, cheese) in g3, True)

        g1 *= g2

        self.assertEqual(len(g1), 1)

        self.assertEqual((tarek, likes, pizza) in g1, False)
        self.assertEqual((tarek, likes, cheese) in g1, False)

        self.assertEqual((bob, likes, cheese) in g1, False)

        self.assertEqual((michel, likes, cheese) in g1, True)

    def testGuessFormatForParse(self):
        self.graph = Graph()

        # files
        with self.assertRaises(ParserError):
            self.graph.parse(__file__)  # here we are trying to parse a Python file!!

        # .nt can be parsed by Turtle Parser
        self.graph.parse("test/nt/anons-01.nt")
        # RDF/XML
        self.graph.parse("test/rdf/datatypes/test001.rdf")  # XML
        # bad filename but set format
        self.graph.parse("test/rdf/datatypes/test001.borked", format="xml")

        # strings
        self.graph = Graph()

        with self.assertRaises(ParserError):
            self.graph.parse(data="rubbish")

        # Turtle - default
        self.graph.parse(data="<http://example.com/a> <http://example.com/a> <http://example.com/a> .")

        # Turtle - format given
        self.graph.parse(data="<http://example.com/a> <http://example.com/a> <http://example.com/a> .", format="turtle")

        # RDF/XML - format given
        rdf = """<rdf:RDF
  xmlns:ns1="http://example.org/#"
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
>
  <rdf:Description rdf:nodeID="ub63bL2C1">
    <ns1:p rdf:resource="http://example.org/q"/>
    <ns1:r rdf:resource="http://example.org/s"/>
  </rdf:Description>
  <rdf:Description rdf:nodeID="ub63bL5C1">
    <ns1:r>
      <rdf:Description rdf:nodeID="ub63bL6C11">
        <ns1:s rdf:resource="http://example.org/#t"/>
      </rdf:Description>
    </ns1:r>
    <ns1:p rdf:resource="http://example.org/q"/>
  </rdf:Description>
</rdf:RDF>
        """
        self.graph.parse(data=rdf, format="xml")

        # URI
        self.graph = Graph()

        # only getting HTML
        with self.assertRaises(PluginException):
            self.graph.parse(location="https://www.google.com")

        try:
            self.graph.parse(location="http://www.w3.org/ns/adms.ttl")
            self.graph.parse(location="http://www.w3.org/ns/adms.rdf")
        except (URLError, HTTPError):
            #this endpoint is currently not available, ignore this test.
            pass

        try:
            # persistent Australian Government online RDF resource without a file-like ending
            self.graph.parse(location="https://linked.data.gov.au/def/agrif?_format=text/turtle")
        except (URLError, HTTPError):
            # this endpoint is currently not available, ignore this test.
            pass

# dynamically create classes for each registered Store

pluginname = None
if __name__ == "__main__":
    if len(sys.argv) > 1:
        pluginname = sys.argv[1]

tests = 0
for s in plugin.plugins(pluginname, plugin.Store):
    if s.name in (
        "default",
        "Memory",
        "Auditable",
        "Concurrent",
        "SPARQLStore",
        "SPARQLUpdateStore",
    ):
        continue  # these are tested by default

    if s.name in ("SimpleMemory",):
        # these (by design) won't pass some of the tests (like Intersection)
        continue

    locals()["t%d" % tests] = type(
        "%sGraphTestCase" % s.name, (GraphTestCase,), {"store": s.name}
    )
    tests += 1

if __name__ == "__main__":
    unittest.main(argv=sys.argv[:1])
<|endoftext|>