<commit_before># © 2013 Mark Harviston <mark.harviston@gmail.com>
# © 2014 Arve Knudsen <arve.knudsen@gmail.com>
# BSD License
import asyncio
import locale
import logging
import sys
import ctypes
import multiprocessing
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
import socket

import quamash

import pytest

class _SubprocessProtocol(asyncio.SubprocessProtocol):
	def __init__(self, *args, **kwds):
		super(_SubprocessProtocol, self).__init__(*args, **kwds)
		self.received_stdout = None

	def pipe_data_received(self, fd, data):
		text = data.decode(locale.getpreferredencoding(False))
		if fd == 1:
			self.received_stdout = text.strip()

	def process_exited(self):
		asyncio.get_event_loop().stop()

@pytest.fixture(scope='session')
def application():
	return quamash.QApplication.instance() or quamash.QApplication([])

@pytest.fixture
def loop(request, application):
	lp = quamash.QEventLoop(application)
	asyncio.set_event_loop(lp)

	def fin():
		try:
			lp.close()
		finally:
			asyncio.set_event_loop(None)

	request.addfinalizer(fin)
	return lp

@pytest.fixture(
	params=[None, quamash.QThreadExecutor, ThreadPoolExecutor, ProcessPoolExecutor]
)
def executor(request):
	exc_cls = request.param
	if exc_cls is None:
		return None

	exc = exc_cls(1)  # FIXME? fixed number of workers?
	request.addfinalizer(exc.shutdown)
	return exc

class TestCanRunTasksInExecutor:
	"""
	This needs to be a class because pickle can't serialize closures,
	but can serialize bound methods.
	multiprocessing can only handle pickleable functions.
	"""
	def test_can_run_tasks_in_executor(self, loop, executor):
		"""Verify that tasks can be run in an executor."""
		logging.debug('Loop: {!r}'.format(loop))
		logging.debug('Executor: {!r}'.format(executor))

		manager = multiprocessing.Manager()
		was_invoked = manager.Value(ctypes.c_int, 0)
		logging.debug('running until complete')
		loop.run_until_complete(self.blocking_task(loop, executor, was_invoked))
		logging.debug('ran')

		assert was_invoked.value == 1

	def blocking_func(self, was_invoked):
		logging.debug('start blocking_func()')
		was_invoked.value = 1
		logging.debug('end blocking_func()')

	@asyncio.coroutine
	def blocking_task(self, loop, executor, was_invoked):
		logging.debug('start blocking task()')
		fut = loop.run_in_executor(executor, self.blocking_func, was_invoked)
		yield from asyncio.wait_for(fut, timeout=5.0)
		logging.debug('start blocking task()')

def test_can_handle_exception_in_default_executor(loop):
	"""Verify that exceptions from tasks run in default (threaded) executor are handled."""
	def blocking_func():
		raise Exception('Testing')

	with pytest.raises(Exception) as excinfo:
		loop.run_until_complete(loop.run_in_executor(None, blocking_func))

	assert str(excinfo.value) == 'Testing'

def test_can_execute_subprocess(loop):
	"""Verify that a subprocess can be executed."""
	transport, protocol = loop.run_until_complete(loop.subprocess_exec(
		_SubprocessProtocol, sys.executable or 'python', '-c', 'print(\'Hello async world!\')'))
	loop.run_forever()
	assert transport.get_returncode() == 0
	assert protocol.received_stdout == 'Hello async world!'

def test_can_terminate_subprocess(loop):
	"""Verify that a subprocess can be terminated."""
	# Start a never-ending process
	transport = loop.run_until_complete(
		loop.subprocess_exec(
			_SubprocessProtocol, sys.executable or 'python', '-c', 'import time\nwhile True: time.sleep(1)',
		),
	)[0]
	# Terminate!
	transport.kill()
	# Wait for process to die
	loop.run_forever()

	assert transport.get_returncode() != 0

def test_loop_running(loop):
	"""Verify that loop.is_running returns True when running"""
	@asyncio.coroutine
	def is_running():
		nonlocal loop
		assert loop.is_running()

	loop.run_until_complete(is_running())

def test_loop_not_running(loop):
	"""Verify that loop.is_running returns False when not running"""
	assert not loop.is_running()

def test_can_function_as_context_manager(application):
	"""Verify that a QEventLoop can function as its own context manager."""
	with quamash.QEventLoop(application) as loop:
		assert isinstance(loop, quamash.QEventLoop)
		loop.call_soon(loop.stop)
		loop.run_forever()

def test_future_not_done_on_loop_shutdown(loop):
	"""Verify RuntimError occurs when loop stopped before Future completed with run_until_complete."""
	loop.call_later(1, loop.stop)
	fut = asyncio.Future()
	with pytest.raises(RuntimeError):
		loop.run_until_complete(fut)

def test_call_later_must_not_coroutine(loop):
	"""Verify TypeError occurs call_later is given a coroutine."""
	mycoro = asyncio.coroutine(lambda: None)

	with pytest.raises(TypeError):
		loop.call_soon(mycoro)

def test_call_later_must_be_callable(loop):
	"""Verify TypeError occurs call_later is not given a callable."""
	not_callable = object()
	with pytest.raises(TypeError):
		loop.call_soon(not_callable)

def test_call_at(loop):
	"""Verify that loop.call_at works as expected."""
	def mycallback():
		nonlocal was_invoked
		was_invoked = True
	was_invoked = False

	loop.call_at(loop.time() + .1, mycallback)
	loop.run_until_complete(asyncio.sleep(.5))

	assert was_invoked

def test_get_set_debug(loop):
	"""Verify get_debug and set_debug work as expected."""
	loop.set_debug(True)
	assert loop.get_debug()
	loop.set_debug(False)
	assert not loop.get_debug()

@pytest.fixture
def sock_pair(request):
	"""Create socket pair.

	If socket.socketpair isn't available, we emulate it.
	"""
	def fin():
		if client_sock is not None:
			client_sock.close()
		if srv_sock is not None:
			srv_sock.close()

	client_sock = srv_sock = None
	request.addfinalizer(fin)

	# See if socketpair() is available.
	have_socketpair = hasattr(socket, 'socketpair')
	if have_socketpair:
		client_sock, srv_sock = socket.socketpair()
		return client_sock, srv_sock

	# Create a non-blocking temporary server socket
	temp_srv_sock = socket.socket()
	temp_srv_sock.setblocking(False)
	temp_srv_sock.bind(('', 0))
	port = temp_srv_sock.getsockname()[1]
	temp_srv_sock.listen(1)

	# Create non-blocking client socket
	client_sock = socket.socket()
	client_sock.setblocking(False)
	try:
		client_sock.connect(('localhost', port))
	except socket.error as err:
		# Error 10035 (operation would block) is not an error, as we're doing this with a
		# non-blocking socket.
		if err.errno != 10035:
			raise

	# Use select to wait for connect() to succeed.
	import select
	timeout = 1
	readable = select.select([temp_srv_sock], [], [], timeout)[0]
	if temp_srv_sock not in readable:
		raise Exception('Client socket not connected in {} second(s)'.format(timeout))
	srv_sock, _ = temp_srv_sock.accept()

	return client_sock, srv_sock

def test_can_add_reader(loop, sock_pair):
	"""Verify that we can add a reader callback to an event loop."""
	def can_read():
		data = srv_sock.recv(1)
		if len(data) != 1:
			return

		nonlocal got_msg
		got_msg = data
		# Indicate that we're done
		fut.set_result(None)

	def write():
		client_sock.send(ref_msg)
		client_sock.close()

	ref_msg = b'a'
	client_sock, srv_sock = sock_pair
	loop.call_soon(write)

	got_msg = None
	fut = asyncio.Future()
	loop.add_reader(srv_sock.fileno(), can_read)
	assert len(loop._read_notifiers) == 1, 'Notifier should be added'
	loop.run_until_complete(asyncio.wait_for(fut, timeout=1.0))

	assert got_msg == ref_msg

def test_can_remove_reader(loop, sock_pair):
	"""Verify that we can remove a reader callback from an event loop."""
	def can_read():
		data = srv_sock.recv(1)
		if len(data) != 1:
			return

		nonlocal got_msg
		got_msg = data

	client_sock, srv_sock = sock_pair

	got_msg = None
	loop.add_reader(srv_sock.fileno(), can_read)
	loop.remove_reader(srv_sock.fileno())
	assert not loop._read_notifiers, 'Notifier should be removed'
	client_sock.send(b'a')
	client_sock.close()
	# Run for a short while to see if we get a read notification
	loop.call_later(0.1, loop.stop)
	loop.run_forever()

	assert got_msg is None, 'Should not have received a read notification'

def test_can_add_writer(loop, sock_pair):
	"""Verify that we can add a writer callback to an event loop."""
	def can_write():
		# Indicate that we're done
		fut.set_result(None)

	client_sock, srv_sock = sock_pair
	fut = asyncio.Future()
	loop.add_writer(client_sock.fileno(), can_write)
	assert len(loop._write_notifiers) == 1, 'Notifier should be added'
	loop.run_until_complete(asyncio.wait_for(fut, timeout=1.0))

def test_can_remove_writer(loop, sock_pair):
	"""Verify that we can remove a writer callback from an event loop."""
	def can_write():
		pass

	client_sock, srv_sock = sock_pair
	loop.add_writer(client_sock.fileno(), can_write)
	loop.remove_writer(client_sock.fileno())
	assert not loop._write_notifiers, 'Notifier should be removed'
<commit_msg>Fix test<commit_after># © 2013 Mark Harviston <mark.harviston@gmail.com>
# © 2014 Arve Knudsen <arve.knudsen@gmail.com>
# BSD License
import asyncio
import locale
import logging
import sys
import ctypes
import multiprocessing
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
import socket

import quamash

import pytest

class _SubprocessProtocol(asyncio.SubprocessProtocol):
	def __init__(self, *args, **kwds):
		super(_SubprocessProtocol, self).__init__(*args, **kwds)
		self.received_stdout = None

	def pipe_data_received(self, fd, data):
		text = data.decode(locale.getpreferredencoding(False))
		if fd == 1:
			self.received_stdout = text.strip()

	def process_exited(self):
		asyncio.get_event_loop().stop()

@pytest.fixture(scope='session')
def application():
	return quamash.QApplication.instance() or quamash.QApplication([])

@pytest.fixture
def loop(request, application):
	lp = quamash.QEventLoop(application)
	asyncio.set_event_loop(lp)

	def fin():
		try:
			lp.close()
		finally:
			asyncio.set_event_loop(None)

	request.addfinalizer(fin)
	return lp

@pytest.fixture(
	params=[None, quamash.QThreadExecutor, ThreadPoolExecutor, ProcessPoolExecutor]
)
def executor(request):
	exc_cls = request.param
	if exc_cls is None:
		return None

	exc = exc_cls(1)  # FIXME? fixed number of workers?
	request.addfinalizer(exc.shutdown)
	return exc

class TestCanRunTasksInExecutor:
	"""
	This needs to be a class because pickle can't serialize closures,
	but can serialize bound methods.
	multiprocessing can only handle pickleable functions.
	"""
	def test_can_run_tasks_in_executor(self, loop, executor):
		"""Verify that tasks can be run in an executor."""
		logging.debug('Loop: {!r}'.format(loop))
		logging.debug('Executor: {!r}'.format(executor))

		manager = multiprocessing.Manager()
		was_invoked = manager.Value(ctypes.c_int, 0)
		logging.debug('running until complete')
		loop.run_until_complete(self.blocking_task(loop, executor, was_invoked))
		logging.debug('ran')

		assert was_invoked.value == 1

	def blocking_func(self, was_invoked):
		logging.debug('start blocking_func()')
		was_invoked.value = 1
		logging.debug('end blocking_func()')

	@asyncio.coroutine
	def blocking_task(self, loop, executor, was_invoked):
		logging.debug('start blocking task()')
		fut = loop.run_in_executor(executor, self.blocking_func, was_invoked)
		yield from asyncio.wait_for(fut, timeout=5.0)
		logging.debug('start blocking task()')

def test_can_handle_exception_in_default_executor(loop):
	"""Verify that exceptions from tasks run in default (threaded) executor are handled."""
	def blocking_func():
		raise Exception('Testing')

	with pytest.raises(Exception) as excinfo:
		loop.run_until_complete(loop.run_in_executor(None, blocking_func))

	assert str(excinfo.value) == 'Testing'

def test_can_execute_subprocess(loop):
	"""Verify that a subprocess can be executed."""
	transport, protocol = loop.run_until_complete(loop.subprocess_exec(
		_SubprocessProtocol, sys.executable or 'python', '-c', 'print(\'Hello async world!\')'))
	loop.run_forever()
	assert transport.get_returncode() == 0
	assert protocol.received_stdout == 'Hello async world!'

def test_can_terminate_subprocess(loop):
	"""Verify that a subprocess can be terminated."""
	# Start a never-ending process
	transport = loop.run_until_complete(
		loop.subprocess_exec(
			_SubprocessProtocol, sys.executable or 'python', '-c', 'import time\nwhile True: time.sleep(1)',
		),
	)[0]
	# Terminate!
	transport.kill()
	# Wait for process to die
	loop.run_forever()

	assert transport.get_returncode() != 0

def test_loop_running(loop):
	"""Verify that loop.is_running returns True when running"""
	@asyncio.coroutine
	def is_running():
		nonlocal loop
		assert loop.is_running()

	loop.run_until_complete(is_running())

def test_loop_not_running(loop):
	"""Verify that loop.is_running returns False when not running"""
	assert not loop.is_running()

def test_can_function_as_context_manager(application):
	"""Verify that a QEventLoop can function as its own context manager."""
	with quamash.QEventLoop(application) as loop:
		assert isinstance(loop, quamash.QEventLoop)
		loop.call_soon(loop.stop)
		loop.run_forever()

def test_future_not_done_on_loop_shutdown(loop):
	"""Verify RuntimError occurs when loop stopped before Future completed with run_until_complete."""
	loop.call_later(1, loop.stop)
	fut = asyncio.Future()
	with pytest.raises(RuntimeError):
		loop.run_until_complete(fut)

def test_call_later_must_not_coroutine(loop):
	"""Verify TypeError occurs call_later is given a coroutine."""
	mycoro = asyncio.coroutine(lambda: None)

	with pytest.raises(TypeError):
		loop.call_soon(mycoro)

def test_call_later_must_be_callable(loop):
	"""Verify TypeError occurs call_later is not given a callable."""
	not_callable = object()
	with pytest.raises(TypeError):
		loop.call_soon(not_callable)

def test_call_at(loop):
	"""Verify that loop.call_at works as expected."""
	def mycallback():
		nonlocal was_invoked
		was_invoked = True
	was_invoked = False

	loop.call_at(loop.time() + .1, mycallback)
	loop.run_until_complete(asyncio.sleep(.5))

	assert was_invoked

def test_get_set_debug(loop):
	"""Verify get_debug and set_debug work as expected."""
	loop.set_debug(True)
	assert loop.get_debug()
	loop.set_debug(False)
	assert not loop.get_debug()

@pytest.fixture
def sock_pair(request):
	"""Create socket pair.

	If socket.socketpair isn't available, we emulate it.
	"""
	def fin():
		if client_sock is not None:
			client_sock.close()
		if srv_sock is not None:
			srv_sock.close()

	client_sock = srv_sock = None
	request.addfinalizer(fin)

	# See if socketpair() is available.
	have_socketpair = hasattr(socket, 'socketpair')
	if have_socketpair:
		client_sock, srv_sock = socket.socketpair()
		return client_sock, srv_sock

	# Create a non-blocking temporary server socket
	temp_srv_sock = socket.socket()
	temp_srv_sock.setblocking(False)
	temp_srv_sock.bind(('', 0))
	port = temp_srv_sock.getsockname()[1]
	temp_srv_sock.listen(1)

	# Create non-blocking client socket
	client_sock = socket.socket()
	client_sock.setblocking(False)
	try:
		client_sock.connect(('localhost', port))
	except socket.error as err:
		# Error 10035 (operation would block) is not an error, as we're doing this with a
		# non-blocking socket.
		if err.errno != 10035:
			raise

	# Use select to wait for connect() to succeed.
	import select
	timeout = 1
	readable = select.select([temp_srv_sock], [], [], timeout)[0]
	if temp_srv_sock not in readable:
		raise Exception('Client socket not connected in {} second(s)'.format(timeout))
	srv_sock, _ = temp_srv_sock.accept()

	return client_sock, srv_sock

def test_can_add_reader(loop, sock_pair):
	"""Verify that we can add a reader callback to an event loop."""
	def can_read():
		data = srv_sock.recv(1)
		if len(data) != 1:
			return

		nonlocal got_msg
		got_msg = data
		# Indicate that we're done
		fut.set_result(None)

	def write():
		client_sock.send(ref_msg)
		client_sock.close()

	ref_msg = b'a'
	client_sock, srv_sock = sock_pair
	loop.call_soon(write)

	exp_num_notifiers = len(loop._read_notifiers) + 1
	got_msg = None
	fut = asyncio.Future()
	loop.add_reader(srv_sock.fileno(), can_read)
	assert len(loop._read_notifiers) == exp_num_notifiers, 'Notifier should be added'
	loop.run_until_complete(asyncio.wait_for(fut, timeout=1.0))

	assert got_msg == ref_msg

def test_can_remove_reader(loop, sock_pair):
	"""Verify that we can remove a reader callback from an event loop."""
	def can_read():
		data = srv_sock.recv(1)
		if len(data) != 1:
			return

		nonlocal got_msg
		got_msg = data

	client_sock, srv_sock = sock_pair

	got_msg = None
	loop.add_reader(srv_sock.fileno(), can_read)
	loop.remove_reader(srv_sock.fileno())
	assert not loop._read_notifiers, 'Notifier should be removed'
	client_sock.send(b'a')
	client_sock.close()
	# Run for a short while to see if we get a read notification
	loop.call_later(0.1, loop.stop)
	loop.run_forever()

	assert got_msg is None, 'Should not have received a read notification'

def test_can_add_writer(loop, sock_pair):
	"""Verify that we can add a writer callback to an event loop."""
	def can_write():
		# Indicate that we're done
		fut.set_result(None)

	client_sock, srv_sock = sock_pair
	fut = asyncio.Future()
	loop.add_writer(client_sock.fileno(), can_write)
	assert len(loop._write_notifiers) == 1, 'Notifier should be added'
	loop.run_until_complete(asyncio.wait_for(fut, timeout=1.0))

def test_can_remove_writer(loop, sock_pair):
	"""Verify that we can remove a writer callback from an event loop."""
	def can_write():
		pass

	client_sock, srv_sock = sock_pair
	loop.add_writer(client_sock.fileno(), can_write)
	loop.remove_writer(client_sock.fileno())
	assert not loop._write_notifiers, 'Notifier should be removed'
<|endoftext|>


<commit_before>import unittest, pytest, os
from declarativeunittest import raises
ontravis = 'TRAVIS' in os.environ

from construct import *
from construct.lib import *

class TestThis(unittest.TestCase):

    def test_this(self):
        this_example = Struct(
            # straight-forward usage: instead of passing (lambda ctx: ctx["length"]) use this.length
            "length" / Int8ub,
            "value" / Bytes(this.length),
            # an example of nesting: '_' refers to the parent's scope
            "nested" / Struct(
                "b1" / Int8ub,
                "b2" / Int8ub,
                "b3" / Computed(this.b1 * this.b2 + this._.length),
            ),
            # and conditions work as expected
            "condition" / IfThenElse(
                this.nested.b1 > 50,
                "c1" / Int32ub,
                "c2" / Int8ub,
            ),
        )
        assert this_example.parse(b"\x05helloABXXXX") == Container(length=5)(value=b'hello')(nested=Container(b1=65)(b2=66)(b3=4295))(condition=1482184792)
        assert this_example.build(dict(length=5, value=b'hello', nested=dict(b1=65, b2=66), condition=1482184792)) == b"\x05helloABXXXX"

    def test_path(self):
        path = Path("path")
        x = ~((path.foo * 2 + 3 << 2) % 11)
        assert str(x) == 'not ((((path.foo * 2) + 3) >> 2) % 11)'
        assert repr(x) == 'not ((((path.foo * 2) + 3) >> 2) % 11)'
        assert not x(dict(foo=7))

    def test_functions(self):
        assert repr(len_(this.x)) == "len_(this.x)"
        assert repr(sum_(this.x)) == "sum_(this.x)"
        assert repr(len_) == "len_"
        assert repr(sum_) == "sum_"

        example = Struct(
            "items" / Byte[2],
            Check(len_(this.items) == 2),
            Check(sum_(this.items) == 10),
            Check(min_(this.items) == 3),
            Check(max_(this.items) == 7),
            "nega" / Int8sb,
            Check(this.nega == -1),
            Check(abs_(this.nega) == 1),
        )
        assert example.parse(b"\x03\x07\xff") == dict(items=[3,7], nega=-1)
        assert example.build(dict(items=[3,7], nega=-1)) == b"\x03\x07\xff"

    def test_obj(self):
        assert repr(obj_ + 1 == 12) == "((obj_ + 1) == 12)"
        assert (obj_)(1,{}) == 1
        assert (obj_ + 10)(1,{}) == 11
        assert (obj_ == 12)(12,{})
        assert (obj_ != 12)(13,{})

<commit_msg>Testing expr updated<commit_after>import unittest, pytest, os
from declarativeunittest import raises
ontravis = 'TRAVIS' in os.environ

from construct import *
from construct.lib import *

class TestThis(unittest.TestCase):

    def test_this(self):
        assert repr(this) == "this"

        this_example = Struct(
            # straight-forward usage: instead of passing (lambda ctx: ctx["length"]) use this.length
            "length" / Int8ub,
            "value" / Bytes(this.length),
            # an example of nesting: '_' refers to the parent's scope
            "nested" / Struct(
                "b1" / Int8ub,
                "b2" / Int8ub,
                "b3" / Computed(this.b1 * this.b2 + this._.length),
            ),
            # and conditions work as expected
            "condition" / IfThenElse(
                this.nested.b1 > 50,
                "c1" / Int32ub,
                "c2" / Int8ub,
            ),
        )
        assert this_example.parse(b"\x05helloABXXXX") == Container(length=5)(value=b'hello')(nested=Container(b1=65)(b2=66)(b3=4295))(condition=1482184792)
        assert this_example.build(dict(length=5, value=b'hello', nested=dict(b1=65, b2=66), condition=1482184792)) == b"\x05helloABXXXX"

    def test_path(self):
        path = Path("path")
        x = ~((path.foo * 2 + 3 << 2) % 11)
        assert str(x) == 'not ((((path.foo * 2) + 3) >> 2) % 11)'
        assert repr(x) == 'not ((((path.foo * 2) + 3) >> 2) % 11)'
        assert not x(dict(foo=7))

    def test_obj(self):
        assert repr(obj_) == "obj_"
        assert repr(obj_ + 1 == 12) == "((obj_ + 1) == 12)"
        assert (obj_)(1,{}) == 1
        assert (obj_ + 10)(1,{}) == 11
        assert (obj_ == 12)(12,{})
        assert (obj_ != 12)(13,{})

    def test_functions(self):
        assert repr(len_(this.x)) == "len_(this.x)"
        assert repr(sum_(this.x)) == "sum_(this.x)"
        assert repr(len_) == "len_"
        assert repr(sum_) == "sum_"

        example = Struct(
            "items" / Byte[2],
            Check(len_(this.items) == 2),
            Check(sum_(this.items) == 10),
            Check(min_(this.items) == 3),
            Check(max_(this.items) == 7),
            "nega" / Int8sb,
            Check(this.nega == -1),
            Check(abs_(this.nega) == 1),
        )
        assert example.parse(b"\x03\x07\xff") == dict(items=[3,7], nega=-1)
        assert example.build(dict(items=[3,7], nega=-1)) == b"\x03\x07\xff"

        example = Struct(
            "items" / RepeatUntil(obj_ == 255, Byte),
        )
        assert example.parse(b"\x03\x07\xff") == dict(items=[3,7,255])
        assert example.build(dict(items=[3,7,255])) == b"\x03\x07\xff"

<|endoftext|>


<commit_before>import sys, os
from PySide import QtGui, QtCore

from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
import matplotlib.pyplot as plt

class PlotDisplay(QtGui.QDialog):

    def __init__(self, spectrum_collec, parent=None):

        super(PlotDisplay, self).__init__(parent)

        # Set spectrum collection instance variable/member
        self.my_collec = spectrum_collec

        # a figure instance to plot on
        self.figure = plt.figure()

        # this is the Canvas Widget that displays the `figure`
        # it takes the `figure` instance as a parameter to __init__
        self.canvas = FigureCanvas(self.figure)

        # this is the Navigation widget
        # it takes the Canvas widget and a parent
        self.toolbar = NavigationToolbar(self.canvas, self)

        # a button connected to `plot` method
        self.button = QtGui.QPushButton('Plot')
        self.button.clicked.connect(self.plot)

        # add a combobox for all X values
        self.drop_down_x = QtGui.QComboBox()
        x_list = list(self.my_collec.x_to_y.keys())
        x_list = [str(each) for each in x_list]
        self.drop_down_x.addItems(x_list)

        # add a combobox for Y's corresponding to first X
        self.drop_down_y = QtGui.QComboBox()
        y_list = list(self.my_collec.x_to_y.values())
        y_list = [str(each) for each in y_list[0]]
        self.drop_down_y.addItems(y_list)

        self.drop_down_x.currentIndexChanged.connect(self.selection_change)

        # create label
        self.label_x = QtGui.QLabel("Select X:")
        self.label_y = QtGui.QLabel("Select Y:")

        # create split layout for displaying combo boxes side by side
        splitter = QtGui.QSplitter(QtCore.Qt.Horizontal)
        splitter.addWidget(self.label_x)
        splitter.addWidget(self.drop_down_x)
        splitter.addWidget(self.label_y)
        splitter.addWidget(self.drop_down_y)

        # add slider
        self.sld = QtGui.QSlider(QtCore.Qt.Horizontal)
        self.sld.setMinimum(0)
        self.wavenums = self.get_wavenums()
        self.sld.setMaximum(len(self.wavenums)-1)
        self.sld.setTickInterval(1)
        curr_sld = self.sld.sliderPosition()
        self.sld.valueChanged.connect(self.sld_change)

        # text edit to display wavenumber
        self.textedit = QtGui.QTextEdit()

        # set the layout
        layout = QtGui.QVBoxLayout()
        layout.addWidget(self.toolbar)
        layout.addWidget(self.canvas)
        layout.addWidget(self.button)
        layout.addWidget(splitter)
        layout.addWidget(self.sld)
        layout.addWidget(self.textedit)
        #layout.addWidget(self.drop_down_x)
        #layout.addWidget(self.drop_down_y)
        self.setLayout(layout)

    def plot(self):
        """"Create matplotlib plot for specific X and Y contained in my_collec"""

        # create an axis
        ax = self.figure.add_subplot(111)

        # discards the old graph
        ax.hold(False)

        # get data from combo box
        curr_x = self.drop_down_x.itemText(self.drop_down_x.currentIndex())
        curr_y = self.drop_down_y.itemText(self.drop_down_y.currentIndex())

        spectra_list = self.my_collec.spectra
        print(spectra_list)
        for i, spectrum in enumerate(spectra_list):
            if (spectrum.x == float(curr_x)) and (spectrum.y == float(curr_y)):
                ax.scatter(*zip(*self.my_collec.spectra[i].info_flipped))

        # plot data
        #ax.scatter(*zip(*self.my_collec.spectra[0].info_flipped))

        # refresh canvas
        self.canvas.draw()

    def selection_change(self):
        """If the X drop down value changes, get the matching Y's corresponding to the new X"""

        curr_x = self.drop_down_x.itemText(self.drop_down_x.currentIndex())
        curr_ys = list(self.my_collec.x_to_y.get(float(curr_x)))
        curr_ys = [str(each) for each in curr_ys]

        self.drop_down_y.clear()
        self.drop_down_y.addItems(curr_ys)

    def get_wavenums(self):
        wavenums = []
        for spectrum in self.my_collec.spectra:
            wavenums = spectrum.info[1]

        return wavenums

    def sld_change(self):
        curr_pos = self.sld.sliderPosition()
        curr_wavenum = self.wavenums[curr_pos]

        self.textedit.setText(str(curr_wavenum))

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)

    main = PlotDisplay(None)
    main.show()

    sys.exit(app.exec_())<commit_msg>Added incomplete line movement with slider<commit_after>import sys, os
from PySide import QtGui, QtCore

from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
import matplotlib.pyplot as plt

class PlotDisplay(QtGui.QDialog):

    def __init__(self, spectrum_collec, parent=None):

        super(PlotDisplay, self).__init__(parent)

        # Set spectrum collection instance variable/member
        self.my_collec = spectrum_collec

        # a figure instance to plot on
        self.figure = plt.figure()

        self.ax = self.figure.add_subplot(111)

        # this is the Canvas Widget that displays the `figure`
        # it takes the `figure` instance as a parameter to __init__
        self.canvas = FigureCanvas(self.figure)

        # this is the Navigation widget
        # it takes the Canvas widget and a parent
        self.toolbar = NavigationToolbar(self.canvas, self)

        # a button connected to `plot` method
        self.button = QtGui.QPushButton('Plot')
        self.button.clicked.connect(self.plot)

        # add a combobox for all X values
        self.drop_down_x = QtGui.QComboBox()
        x_list = list(self.my_collec.x_to_y.keys())
        x_list = [str(each) for each in x_list]
        self.drop_down_x.addItems(x_list)

        # add a combobox for Y's corresponding to first X
        self.drop_down_y = QtGui.QComboBox()
        y_list = list(self.my_collec.x_to_y.values())
        y_list = [str(each) for each in y_list[0]]
        self.drop_down_y.addItems(y_list)

        self.drop_down_x.currentIndexChanged.connect(self.selection_change)

        # create label
        self.label_x = QtGui.QLabel("Select X:")
        self.label_y = QtGui.QLabel("Select Y:")

        # create split layout for displaying combo boxes side by side
        splitter = QtGui.QSplitter(QtCore.Qt.Horizontal)
        splitter.addWidget(self.label_x)
        splitter.addWidget(self.drop_down_x)
        splitter.addWidget(self.label_y)
        splitter.addWidget(self.drop_down_y)

        # add slider
        self.sld = QtGui.QSlider(QtCore.Qt.Horizontal)
        self.sld.setMinimum(0)
        self.wavenums = self.get_wavenums()
        self.sld.setMaximum(len(self.wavenums)-1)
        self.sld.setTickInterval(1)
        curr_sld = self.sld.sliderPosition()
        self.sld.valueChanged.connect(self.sld_change)

        # add line
        self.vert_line = self.ax.axvline(x = float(self.wavenums[curr_sld]))

        # text edit to display wavenumber
        self.textedit = QtGui.QLabel()
        self.textedit.setStyleSheet('background-color: white')

        # horizontal layout to hold slider and text edit box
        hslider_text = QtGui.QHBoxLayout()
        hslider_text.addWidget(self.sld, 2)
        hslider_text.addWidget(self.textedit, 1)

        # group box to group elements together
        gbox = QtGui.QGroupBox("Peak Selection")
        gbox.setLayout(hslider_text)
        qpalette = QtGui.QPalette()
        qpalette.setColor(QtGui.QPalette.Dark, QtCore.Qt.white)
        gbox.setPalette(qpalette)
        #gbox.setStyleSheet("QGroupBox {border-radius: 9px; border:1px solid rgb(0, 0, 0); margin-top: 0.5em}")

        # set the layout
        layout = QtGui.QVBoxLayout()
        layout.addWidget(self.toolbar)
        layout.addWidget(self.canvas)
        layout.addWidget(self.button)
        layout.addWidget(splitter)
        layout.addWidget(gbox)
        self.setLayout(layout)

    def plot(self):
        """"Create matplotlib plot for specific X and Y contained in my_collec"""

        # create an axis

        # discards the old graph
        self.ax.hold(False)

        # get data from combo box
        curr_x = self.drop_down_x.itemText(self.drop_down_x.currentIndex())
        curr_y = self.drop_down_y.itemText(self.drop_down_y.currentIndex())

        # plot data
        spectra_list = self.my_collec.spectra
        print(spectra_list)
        for i, spectrum in enumerate(spectra_list):
            if (spectrum.x == float(curr_x)) and (spectrum.y == float(curr_y)):
                self.ax.scatter(*zip(*self.my_collec.spectra[i].info_flipped))

        # refresh canvas
        self.canvas.draw()

    def selection_change(self):
        """If the X drop down value changes, get the matching Y's corresponding to the new X"""

        curr_x = self.drop_down_x.itemText(self.drop_down_x.currentIndex())
        curr_ys = list(self.my_collec.x_to_y.get(float(curr_x)))
        curr_ys = [str(each) for each in curr_ys]

        self.drop_down_y.clear()
        self.drop_down_y.addItems(curr_ys)

    def get_wavenums(self):
        wavenums = []
        for spectrum in self.my_collec.spectra:
            wavenums = spectrum.info[1]

        return wavenums

    def sld_change(self):
        curr_pos = self.sld.sliderPosition()
        curr_wavenum = self.wavenums[curr_pos]

        self.textedit.setText(str(curr_wavenum))

        self.ax.lines.remove(self.vert_line)
        self.vert_line = self.ax.axvline(x = curr_wavenum)

        self.canvas.blit()

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)

    main = PlotDisplay(None)
    main.show()

    sys.exit(app.exec_())<|endoftext|>


<commit_before># Copyright (c) 2015 Fabian Kochem

from libtree import Transaction, Tree
from mock import Mock
import pytest

def test_it_takes_a_connection(dsn):
    conn = Mock()
    assert Tree(connection=conn).connection is conn

def test_it_takes_a_connection_pool(dsn):
    pool = Mock()
    assert Tree(pool=pool).pool is pool

def test_it_takes_a_table_prefix():
    prefix = 'vortec_'
    assert Tree(pool=Mock(), prefix=prefix).prefix == prefix

def test_it_requires_either_connection_or_pool():
    with pytest.raises(TypeError):
        Tree()

def test_it_cant_deal_with_both_connection_and_pool():
    with pytest.raises(TypeError):
        Tree(connection=Mock(), pool=Mock())

def test_make_transaction_returns_a_transaction_object():
    conn = Mock()
    tree = Tree(connection=conn)
    transaction = tree.make_transaction()
    assert transaction.__class__ == Transaction

def test_returned_transaction_uses_assigned_transaction_object():
    conn = Mock()
    tree = Tree(connection=conn)
    transaction = tree.make_transaction()
    assert transaction.connection is conn

def test_returned_transaction_uses_connection_from_pool():
    pool, conn = Mock(), Mock()
    pool.getconn.return_value = conn
    tree = Tree(pool=pool)
    transaction = tree.make_transaction()
    assert transaction.connection is conn

def test_cm_puts_connection_back_into_pool():
    pool, conn = Mock(), Mock()
    pool.getconn.return_value = conn
    tree = Tree(pool=pool)
    with tree() as transaction:  # noqa
        pass
    assert pool.putconn.called
    pool.putconn.assert_called_with(conn)

def test_cm_commits_transaction():
    conn = Mock()
    tree = Tree(connection=conn)
    with tree() as transaction:  # noqa
        pass
    assert conn.commit.called

def test_cm_rolls_back_transaction():
    conn = Mock()
    tree = Tree(connection=conn)
    with pytest.raises(ValueError):
        with tree() as transaction:  # noqa
            raise ValueError()
    assert conn.rollback.called

def test_close_connection():
    conn = Mock()
    Tree(connection=conn).close()
    assert conn.close.called

def test_close_pool():
    pool = Mock()
    Tree(pool=pool).close()
    assert pool.closeall.called
<commit_msg>remove failing test (it fails because I removed an unused argument)<commit_after># Copyright (c) 2015 Fabian Kochem

from libtree import Transaction, Tree
from mock import Mock
import pytest

def test_it_takes_a_connection(dsn):
    conn = Mock()
    assert Tree(connection=conn).connection is conn

def test_it_takes_a_connection_pool(dsn):
    pool = Mock()
    assert Tree(pool=pool).pool is pool

def test_it_requires_either_connection_or_pool():
    with pytest.raises(TypeError):
        Tree()

def test_it_cant_deal_with_both_connection_and_pool():
    with pytest.raises(TypeError):
        Tree(connection=Mock(), pool=Mock())

def test_make_transaction_returns_a_transaction_object():
    conn = Mock()
    tree = Tree(connection=conn)
    transaction = tree.make_transaction()
    assert transaction.__class__ == Transaction

def test_returned_transaction_uses_assigned_transaction_object():
    conn = Mock()
    tree = Tree(connection=conn)
    transaction = tree.make_transaction()
    assert transaction.connection is conn

def test_returned_transaction_uses_connection_from_pool():
    pool, conn = Mock(), Mock()
    pool.getconn.return_value = conn
    tree = Tree(pool=pool)
    transaction = tree.make_transaction()
    assert transaction.connection is conn

def test_cm_puts_connection_back_into_pool():
    pool, conn = Mock(), Mock()
    pool.getconn.return_value = conn
    tree = Tree(pool=pool)
    with tree() as transaction:  # noqa
        pass
    assert pool.putconn.called
    pool.putconn.assert_called_with(conn)

def test_cm_commits_transaction():
    conn = Mock()
    tree = Tree(connection=conn)
    with tree() as transaction:  # noqa
        pass
    assert conn.commit.called

def test_cm_rolls_back_transaction():
    conn = Mock()
    tree = Tree(connection=conn)
    with pytest.raises(ValueError):
        with tree() as transaction:  # noqa
            raise ValueError()
    assert conn.rollback.called

def test_close_connection():
    conn = Mock()
    Tree(connection=conn).close()
    assert conn.close.called

def test_close_pool():
    pool = Mock()
    Tree(pool=pool).close()
    assert pool.closeall.called
<|endoftext|>


<commit_before>def add_one(number):
    return number + 1

<commit_msg>Removed blank lines<commit_after>def add_one(number):
    return number + 1
<|endoftext|>


<commit_before>import unittest

from compiler import ast, type
from tests import parser_db

class TestTypeAPI(unittest.TestCase, parser_db.ParserDB):
    """Test the API of the type module."""

    @staticmethod
    def test_is_array():
        type.is_array(ast.Array(ast.Int())).should.be.true

    @staticmethod
    def test_array_of_array_error():
        exc = type.ArrayOfArrayError
        issubclass(exc, type.InvalidTypeError).should.be.true

    @staticmethod
    def test_array_return_error():
        exc = type.ArrayReturnError
        issubclass(exc, type.InvalidTypeError).should.be.true

    @staticmethod
    def test_ref_of_array_error():
        exc = type.RefOfArrayError
        issubclass(exc, type.InvalidTypeError).should.be.true

    @staticmethod
    def test_validate():
        type.validate(ast.Int())

    @staticmethod
    def test_redef_builtin_type_error():
        exc = type.RedefBuiltinTypeError
        issubclass(exc, type.BadTypeDefError).should.be.true

    @staticmethod
    def test_redef_constructor_error():
        exc = type.RedefConstructorError
        issubclass(exc, type.BadTypeDefError).should.be.true

    @staticmethod
    def test_redef_user_type_error():
        exc = type.RedefUserTypeError
        issubclass(exc, type.BadTypeDefError).should.be.true

    @staticmethod
    def test_undef_type_error():
        exc = type.UndefTypeError
        issubclass(exc, type.BadTypeDefError).should.be.true

    @staticmethod
    def test_table_init():
        type.Table()

class TestTable(unittest.TestCase, parser_db.ParserDB):
    """Test the Table's processing of type definitions."""

    @classmethod
    def _process_typedef(cls, typedefListList):
        typeTable = type.Table()
        for typedefList in typedefListList:
            typeTable.process(typedefList)

    def test_type_process(self):
        proc = self._process_typedef
        error = type.BadTypeDefError

        right_testcases = (
            "type color = Red | Green | Blue",
            "type list = Nil | Cons of int list",
            """
            type number = Integer of int | Real of float
                        | Complex of float float
            """,
            """
            type tree = Leaf | Node of int forest
            and  forest = Empty | NonEmpty of tree forest
            """
        )

        for case in right_testcases:
            tree = self._parse(case)
            proc.when.called_with(tree).shouldnt.throw(error)

        wrong_testcases = (
            (
                (
                    "type bool = BoolCon",
                    "type char = CharCon",
                    "type float = FloatCon",
                    "type int = IntCon",
                    "type unit = UnitCon",
                ),
                type.RedefBuiltinTypeError
            ),
            (
                (
                    "type dup = ConDup | ConDup",
                    """
                    type one = Con
                    type two = Con
                    """,
                ),
                type.RedefConstructorError
            ),
            (
                (
                    """
                    type same = Foo1
                    type same = Foo2
                    """,
                ),
                type.RedefUserTypeError
            ),
            (
                (
                    "type what = What of undeftype",
                ),
                type.UndefTypeError
            )
        )

        for cases, error in wrong_testcases:
            for case in cases:
                tree = self._parse(case)
                proc.when.called_with(tree).should.throw(error)

class TestValidating(unittest.TestCase, parser_db.ParserDB):
    """Test the validating of types."""

    def test_is_array(self):
        for typecon in ast.builtin_types_map.values():
            type.is_array(typecon()).should.be.false

        right_testcases = (
            "array of int",
            "array of foo",
            "array [*, *] of int"
        )

        for case in right_testcases:
            tree = self._parse(case, 'type')
            type.is_array(tree).should.be.true

        wrong_testcases = (
            "foo",
            "int ref",
            "int -> int",
        )

        for case in wrong_testcases:
            tree = self._parse(case, 'type')
            type.is_array(tree).should.be.false

    def test_validate(self):
        proc = type.validate
        error = type.InvalidTypeError

        for typecon in ast.builtin_types_map.values():
            proc.when.called_with(typecon()).shouldnt.throw(error)

        right_testcases = (
            "foo",

            "int ref",
            "foo ref",
            "(int -> int) ref",
            "(int ref) ref",

            "array of int",
            "array of foo",
            "array of (int ref)",
            "array of (foo ref)",
            "array [*, *] of int",

            "int -> int",
            "foo -> int",
            "int -> foo",
            "int ref -> int",
            "int -> (int ref)",
            "(array of int) -> int",
            "int -> (array of int -> int)",
            "(int -> int) -> int"
        )

        for case in right_testcases:
            tree = self._parse(case, 'type')
            proc.when.called_with(tree).shouldnt.throw(error)

        wrong_testcases = (
            (
                (
                    "array of (array of int)",
                    "(array of (array of int)) -> int",
                    "((array of (array of int)) -> int) ref",
                ),
                type.ArrayOfArrayError
            ),
            (
                (
                    "(array of int) ref",
                    "((array of int) ref) -> int",
                    "array of ((array of int) ref)",
                ),
                type.RefOfArrayError
            ),
            (
                (
                    "int -> array of int",
                    "int -> (int -> array of int)",
                    "(int -> array of int) ref",
                ),
                type.ArrayReturnError
            ),
        )

        for cases, error in wrong_testcases:
            for case in cases:
                tree = self._parse(case, "type")
                with self.assertRaises(error) as context:
                    proc(tree)
                exc = context.exception
                exc.should.have.property("node")

                node = exc.node
                node.should.have.property("lineno")
                node.lineno.shouldnt.be(None)
                node.should.have.property("lexpos")
                node.lexpos.shouldnt.be(None)
<commit_msg>Type test: Separate and refactor testing for correct typedefs.<commit_after>import unittest

from compiler import ast, type
from tests import parser_db

class TestTypeAPI(unittest.TestCase, parser_db.ParserDB):
    """Test the API of the type module."""

    @staticmethod
    def test_is_array():
        type.is_array(ast.Array(ast.Int())).should.be.true

    @staticmethod
    def test_array_of_array_error():
        exc = type.ArrayOfArrayError
        issubclass(exc, type.InvalidTypeError).should.be.true

    @staticmethod
    def test_array_return_error():
        exc = type.ArrayReturnError
        issubclass(exc, type.InvalidTypeError).should.be.true

    @staticmethod
    def test_ref_of_array_error():
        exc = type.RefOfArrayError
        issubclass(exc, type.InvalidTypeError).should.be.true

    @staticmethod
    def test_validate():
        type.validate(ast.Int())

    @staticmethod
    def test_redef_builtin_type_error():
        exc = type.RedefBuiltinTypeError
        issubclass(exc, type.BadTypeDefError).should.be.true

    @staticmethod
    def test_redef_constructor_error():
        exc = type.RedefConstructorError
        issubclass(exc, type.BadTypeDefError).should.be.true

    @staticmethod
    def test_redef_user_type_error():
        exc = type.RedefUserTypeError
        issubclass(exc, type.BadTypeDefError).should.be.true

    @staticmethod
    def test_undef_type_error():
        exc = type.UndefTypeError
        issubclass(exc, type.BadTypeDefError).should.be.true

    @staticmethod
    def test_table_init():
        type.Table()

class TestTable(unittest.TestCase, parser_db.ParserDB):
    """Test the Table's processing of type definitions."""

    def _process_typedef(self, typeDefListList):
        typeTable = type.Table()
        for typeDefList in typeDefListList:
            typeTable.process(typeDefList)

    def test_type_process_correct(self):
        right_testcases = (
            "type color = Red | Green | Blue",
            "type list = Nil | Cons of int list",
            """
            type number = Integer of int | Real of float
                        | Complex of float float
            """,
            """
            type tree = Leaf | Node of int forest
            and  forest = Empty | NonEmpty of tree forest
            """
        )

        table = type.Table()
        proc = table.process
        for case in right_testcases:
            tree = self._parse(case, "typedef")
            proc.when.called_with(tree).shouldnt.throw(type.BadTypeDefError)

    def test_type_process_wrong(self):
        wrong_testcases = (
            (
                (
                    "type bool = BoolCon",
                    "type char = CharCon",
                    "type float = FloatCon",
                    "type int = IntCon",
                    "type unit = UnitCon",
                ),
                type.RedefBuiltinTypeError
            ),
            (
                (
                    "type dup = ConDup | ConDup",
                    """
                    type one = Con
                    type two = Con
                    """,
                ),
                type.RedefConstructorError
            ),
            (
                (
                    """
                    type same = Foo1
                    type same = Foo2
                    """,
                ),
                type.RedefUserTypeError
            ),
            (
                (
                    "type what = What of undeftype",
                ),
                type.UndefTypeError
            )
        )

        proc = self._process_typedef
        for cases, error in wrong_testcases:
            for case in cases:
                tree = self._parse(case)
                proc.when.called_with(tree).should.throw(error)

class TestValidating(unittest.TestCase, parser_db.ParserDB):
    """Test the validating of types."""

    def test_is_array(self):
        for typecon in ast.builtin_types_map.values():
            type.is_array(typecon()).should.be.false

        right_testcases = (
            "array of int",
            "array of foo",
            "array [*, *] of int"
        )

        for case in right_testcases:
            tree = self._parse(case, 'type')
            type.is_array(tree).should.be.true

        wrong_testcases = (
            "foo",
            "int ref",
            "int -> int",
        )

        for case in wrong_testcases:
            tree = self._parse(case, 'type')
            type.is_array(tree).should.be.false

    def test_validate(self):
        proc = type.validate
        error = type.InvalidTypeError

        for typecon in ast.builtin_types_map.values():
            proc.when.called_with(typecon()).shouldnt.throw(error)

        right_testcases = (
            "foo",

            "int ref",
            "foo ref",
            "(int -> int) ref",
            "(int ref) ref",

            "array of int",
            "array of foo",
            "array of (int ref)",
            "array of (foo ref)",
            "array [*, *] of int",

            "int -> int",
            "foo -> int",
            "int -> foo",
            "int ref -> int",
            "int -> (int ref)",
            "(array of int) -> int",
            "int -> (array of int -> int)",
            "(int -> int) -> int"
        )

        for case in right_testcases:
            tree = self._parse(case, 'type')
            proc.when.called_with(tree).shouldnt.throw(error)

        wrong_testcases = (
            (
                (
                    "array of (array of int)",
                    "(array of (array of int)) -> int",
                    "((array of (array of int)) -> int) ref",
                ),
                type.ArrayOfArrayError
            ),
            (
                (
                    "(array of int) ref",
                    "((array of int) ref) -> int",
                    "array of ((array of int) ref)",
                ),
                type.RefOfArrayError
            ),
            (
                (
                    "int -> array of int",
                    "int -> (int -> array of int)",
                    "(int -> array of int) ref",
                ),
                type.ArrayReturnError
            ),
        )

        for cases, error in wrong_testcases:
            for case in cases:
                tree = self._parse(case, "type")
                with self.assertRaises(error) as context:
                    proc(tree)
                exc = context.exception
                exc.should.have.property("node")

                node = exc.node
                node.should.have.property("lineno")
                node.lineno.shouldnt.be(None)
                node.should.have.property("lexpos")
                node.lexpos.shouldnt.be(None)
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from __future__ import unicode_literals, absolute_import

import datetime
import locale
import os
import sys
# This code is so you can run the samples without installing the package,
# and should be before any import touching nikola, in any file under tests/
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))

import dateutil.parser
import dateutil.tz
import pytest

from .base import BaseTestCase

try:
    from freezegun import freeze_time
    _freeze_time = True
except ImportError:
    _freeze_time = False
    freeze_time = lambda x: lambda y: y

_NOW = datetime.datetime(  # Thursday
    2013, 8, 22, 10, 0, 0, tzinfo=dateutil.tz.tzutc())

@pytest.mark.skipif(not _freeze_time, reason="freezegun not installed.")
class TestScheduling(BaseTestCase):

    @classmethod
    def setUp(cls):
        d = [name for name in sys.modules if name.startswith("six.moves.")]
        cls.deleted = {}
        for name in d:
            cls.deleted[name] = sys.modules[name]
            del sys.modules[name]

    @classmethod
    def tearDown(cls):
        for name, mod in cls.deleted.items():
            sys.modules[name] = mod

    @freeze_time(_NOW)
    def test_get_date(self):
        from nikola.plugins.command.new_post import get_date

        # This is now locale-dependent, so do it here, where
        # locale is set.
        FMT = '{0} {1} %Z'.format(
            locale.nl_langinfo(locale.D_FMT),
            locale.nl_langinfo(locale.T_FMT),
        )
        NOW = _NOW.strftime(FMT)
        TODAY = dateutil.parser.parse(NOW)
        RULE_TH = 'RRULE:FREQ=WEEKLY;BYDAY=TH'
        RULE_FR = 'RRULE:FREQ=WEEKLY;BYDAY=FR'
        UTC = dateutil.tz.tzutc()

        # NOW does not match rule #########################################
        # No last date
        expected = TODAY.replace(day=23).strftime(FMT)
        self.assertEqual(expected, get_date(True, RULE_FR, tz=UTC))
        self.assertEqual(expected, get_date(True, RULE_FR, tz=UTC))

        # Last date in the past; doesn't match rule
        date = TODAY.replace(hour=7)
        expected = TODAY.replace(day=23, hour=7).strftime(FMT)
        self.assertEqual(expected, get_date(True, RULE_FR, date, tz=UTC))

        # Last date in the future; doesn't match rule
        date = TODAY.replace(day=24, hour=7)
        expected = TODAY.replace(day=30, hour=7).strftime(FMT)
        self.assertEqual(expected, get_date(True, RULE_FR, date, tz=UTC))

        # Last date in the past; matches rule
        date = TODAY.replace(day=16, hour=8)
        expected = TODAY.replace(day=23, hour=8).strftime(FMT)
        self.assertEqual(expected, get_date(True, RULE_FR, date, tz=UTC))

        # Last date in the future; matches rule
        date = TODAY.replace(day=23, hour=18)
        expected = TODAY.replace(day=30, hour=18).strftime(FMT)
        self.assertEqual(expected, get_date(True, RULE_FR, date, tz=UTC))

        # NOW matches rule ################################################
        # Not scheduling should return NOW
        self.assertEqual(NOW, get_date(False, RULE_TH, tz=UTC))
        # No last date
        self.assertEqual(NOW, get_date(True, RULE_TH, tz=UTC))
        self.assertEqual(NOW, get_date(True, RULE_TH, tz=UTC))

        # Last date in the past; doesn't match rule
        # Corresponding time has already passed, today
        date = TODAY.replace(day=21, hour=7)
        expected = TODAY.replace(day=29, hour=7).strftime(FMT)
        self.assertEqual(expected, get_date(True, RULE_TH, date, tz=UTC))
        # Corresponding time has not passed today
        date = TODAY.replace(day=21, hour=18)
        expected = TODAY.replace(day=22, hour=18).strftime(FMT)
        self.assertEqual(expected, get_date(True, RULE_TH, date, tz=UTC))

        # Last date in the future; doesn't match rule
        # Corresponding time has already passed, today
        date = TODAY.replace(day=24, hour=7)
        expected = TODAY.replace(day=29, hour=7).strftime(FMT)
        self.assertEqual(expected, get_date(True, RULE_TH, date, tz=UTC))
        # Corresponding time has not passed today
        date = TODAY.replace(day=24, hour=18)
        expected = TODAY.replace(day=29, hour=18).strftime(FMT)
        self.assertEqual(expected, get_date(True, RULE_TH, date, tz=UTC))

        # Last date in the past; matches rule
        # Corresponding time has already passed, today
        date = TODAY.replace(day=15, hour=7)
        expected = TODAY.replace(day=29, hour=7).strftime(FMT)
        self.assertEqual(expected, get_date(True, RULE_TH, date, tz=UTC))
        # Corresponding time has already passed, today; rule specifies HOUR
        date = TODAY.replace(day=15, hour=7)
        expected = TODAY.replace(day=29, hour=9).strftime(FMT)
        self.assertEqual(expected, get_date(True, RULE_TH + ';BYHOUR=9', date, tz=UTC))
        # Corresponding time has not passed today
        date = TODAY.replace(day=15, hour=18)
        expected = TODAY.replace(day=22, hour=18).strftime(FMT)
        self.assertEqual(expected, get_date(True, RULE_TH, date, tz=UTC))

        # Last date in the future; matches rule
        # Corresponding time has already passed, today
        date = TODAY.replace(day=29, hour=7)
        expected = TODAY.replace(day=5, month=9, hour=7).strftime(FMT)
        self.assertEqual(expected, get_date(True, RULE_TH, date, tz=UTC))
        # Corresponding time has not passed today
        date = TODAY.replace(day=22, hour=18)
        expected = TODAY.replace(day=29, hour=18).strftime(FMT)
        self.assertEqual(expected, get_date(True, RULE_TH, date, tz=UTC))

if __name__ == '__main__':
    import unittest
    unittest.main()
<commit_msg>hardcode the new date format in a test<commit_after># -*- coding: utf-8 -*-
from __future__ import unicode_literals, absolute_import

import datetime
import locale
import os
import sys
# This code is so you can run the samples without installing the package,
# and should be before any import touching nikola, in any file under tests/
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))

import dateutil.parser
import dateutil.tz
import pytest

from .base import BaseTestCase

try:
    from freezegun import freeze_time
    _freeze_time = True
except ImportError:
    _freeze_time = False
    freeze_time = lambda x: lambda y: y

_NOW = datetime.datetime(  # Thursday
    2013, 8, 22, 10, 0, 0, tzinfo=dateutil.tz.tzutc())

@pytest.mark.skipif(not _freeze_time, reason="freezegun not installed.")
class TestScheduling(BaseTestCase):

    @classmethod
    def setUp(cls):
        d = [name for name in sys.modules if name.startswith("six.moves.")]
        cls.deleted = {}
        for name in d:
            cls.deleted[name] = sys.modules[name]
            del sys.modules[name]

    @classmethod
    def tearDown(cls):
        for name, mod in cls.deleted.items():
            sys.modules[name] = mod

    @freeze_time(_NOW)
    def test_get_date(self):
        from nikola.plugins.command.new_post import get_date

        FMT = '%Y-%m-%d %H:%M:%S %Z'.format(
            locale.nl_langinfo(locale.D_FMT),
            locale.nl_langinfo(locale.T_FMT),
        )
        NOW = _NOW.strftime(FMT)
        TODAY = dateutil.parser.parse(NOW)
        RULE_TH = 'RRULE:FREQ=WEEKLY;BYDAY=TH'
        RULE_FR = 'RRULE:FREQ=WEEKLY;BYDAY=FR'
        UTC = dateutil.tz.tzutc()

        # NOW does not match rule #########################################
        # No last date
        expected = TODAY.replace(day=23).strftime(FMT)
        self.assertEqual(expected, get_date(True, RULE_FR, tz=UTC))
        self.assertEqual(expected, get_date(True, RULE_FR, tz=UTC))

        # Last date in the past; doesn't match rule
        date = TODAY.replace(hour=7)
        expected = TODAY.replace(day=23, hour=7).strftime(FMT)
        self.assertEqual(expected, get_date(True, RULE_FR, date, tz=UTC))

        # Last date in the future; doesn't match rule
        date = TODAY.replace(day=24, hour=7)
        expected = TODAY.replace(day=30, hour=7).strftime(FMT)
        self.assertEqual(expected, get_date(True, RULE_FR, date, tz=UTC))

        # Last date in the past; matches rule
        date = TODAY.replace(day=16, hour=8)
        expected = TODAY.replace(day=23, hour=8).strftime(FMT)
        self.assertEqual(expected, get_date(True, RULE_FR, date, tz=UTC))

        # Last date in the future; matches rule
        date = TODAY.replace(day=23, hour=18)
        expected = TODAY.replace(day=30, hour=18).strftime(FMT)
        self.assertEqual(expected, get_date(True, RULE_FR, date, tz=UTC))

        # NOW matches rule ################################################
        # Not scheduling should return NOW
        self.assertEqual(NOW, get_date(False, RULE_TH, tz=UTC))
        # No last date
        self.assertEqual(NOW, get_date(True, RULE_TH, tz=UTC))
        self.assertEqual(NOW, get_date(True, RULE_TH, tz=UTC))

        # Last date in the past; doesn't match rule
        # Corresponding time has already passed, today
        date = TODAY.replace(day=21, hour=7)
        expected = TODAY.replace(day=29, hour=7).strftime(FMT)
        self.assertEqual(expected, get_date(True, RULE_TH, date, tz=UTC))
        # Corresponding time has not passed today
        date = TODAY.replace(day=21, hour=18)
        expected = TODAY.replace(day=22, hour=18).strftime(FMT)
        self.assertEqual(expected, get_date(True, RULE_TH, date, tz=UTC))

        # Last date in the future; doesn't match rule
        # Corresponding time has already passed, today
        date = TODAY.replace(day=24, hour=7)
        expected = TODAY.replace(day=29, hour=7).strftime(FMT)
        self.assertEqual(expected, get_date(True, RULE_TH, date, tz=UTC))
        # Corresponding time has not passed today
        date = TODAY.replace(day=24, hour=18)
        expected = TODAY.replace(day=29, hour=18).strftime(FMT)
        self.assertEqual(expected, get_date(True, RULE_TH, date, tz=UTC))

        # Last date in the past; matches rule
        # Corresponding time has already passed, today
        date = TODAY.replace(day=15, hour=7)
        expected = TODAY.replace(day=29, hour=7).strftime(FMT)
        self.assertEqual(expected, get_date(True, RULE_TH, date, tz=UTC))
        # Corresponding time has already passed, today; rule specifies HOUR
        date = TODAY.replace(day=15, hour=7)
        expected = TODAY.replace(day=29, hour=9).strftime(FMT)
        self.assertEqual(expected, get_date(True, RULE_TH + ';BYHOUR=9', date, tz=UTC))
        # Corresponding time has not passed today
        date = TODAY.replace(day=15, hour=18)
        expected = TODAY.replace(day=22, hour=18).strftime(FMT)
        self.assertEqual(expected, get_date(True, RULE_TH, date, tz=UTC))

        # Last date in the future; matches rule
        # Corresponding time has already passed, today
        date = TODAY.replace(day=29, hour=7)
        expected = TODAY.replace(day=5, month=9, hour=7).strftime(FMT)
        self.assertEqual(expected, get_date(True, RULE_TH, date, tz=UTC))
        # Corresponding time has not passed today
        date = TODAY.replace(day=22, hour=18)
        expected = TODAY.replace(day=29, hour=18).strftime(FMT)
        self.assertEqual(expected, get_date(True, RULE_TH, date, tz=UTC))

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


<commit_before>#!/usr/bin/python

""" Test suite for ScreenFlow class. """

from screenflow import ScreenFlow, NavigationException
from screenflow.constants import XML_TYPE
from mocks.surface_mock import SurfaceMock, DEFAULT_SURFACE_SIZE
from mocks.screen_mock import ScreenMock
from nose.tools import raises

def test_add_screen():
    """ Test case for screen insertion. """
    name = 'foo'
    screen = ScreenMock(name)
    screenflow = ScreenFlow()
    screenflow.add_screen(screen)
    assert screen.font_manager == screenflow
    assert name in screenflow.screens.keys()
    assert screenflow.screens[name] == screen
    assert screenflow.foo == screen

def test_navigate_to():
    """ Test case for navigating to another screen. """
    screenflow = ScreenFlow()
    foo = ScreenMock('foo')
    bar = ScreenMock('bar')
    screenflow.stack.append(foo)
    screenflow.navigate_to(bar)
    assert screenflow.state = ScreenFlow.IN_TRANSITION
    assert len(screenflow.stack) == 2
    assert screenflow.stack[0] == foo
    assert screenflow.stack[1] == bar

@raises(NavigationException)
def test_navigate_back_error():
    """ Test case for navigating back error handling. """
    screenflow = ScreenFlow()
    screenflow.navigate_back()

def test_navigate_back():
    """ Test case for navigating back. """
    screenflow = ScreenFlow()
    foo = ScreenMock('foo')
    bar = ScreenMock('bar')
    screenflow.stack.append(foo)
    screenflow.stack.append(bar)
    screenflow.navigate_back()
    assert screenflow.state == ScreenFlow.IN_TRANSITION
    assert len(screenflow.stack) == 1
    assert screenflow.stack[0] == foo

def test_main_loop():
    """ Test case for the main loop. """
    assert False

def test_register_factory():
    """ Test case for screen factory registration. """
    screenflow = ScreenFlow()
    def factory(metadata):
        pass
    name = 'foo'
    screenflow.register_factory(name, factory)
    assert name in screenflow.factories.keys()
    assert screenflow.factories[name] == factory

@raises(ValueError)
def test_register_factory_duplicate():
    """ Test case for screen factory duplicate registration. """
    screenflow = ScreenFlow()
    name = 'foo'
    screenflow.register_factory(name, None)
    screenflow.register_factory(name, None)

def test_create_screen():
    """ """
    screenflow = ScreenFlow()
    # TODO : Consider registering factory first ?
    assert False

@raises(AttributeError)
def test_create_screen_not_valid_xml():
    """ Test case for creating screen without type. """
    screenflow = ScreenFlow()
    screenflow.create_screen({})

@raises(ValueError)
def test_create_unknown_type_screen():
    """ Test case for creating screen with unknown type. """
    screenflow = ScreenFlow()
    screenflow.create_screen({XML_TYPE: 'foo'})

def test_load_from_file():
    """ Test case for XML file loading. """
    screenflow = ScreenFlow()
    assert False

@raises(IOError)
def test_load_from_not_existing_file():
    """ Test case for XML file loading error handling (file not exists). """
    screenflow = ScreenFlow()
    screenflow.load_from_file('ghost_file.xml')

@raises(AttributeError)
def test_load_from_file_without_root():
    """ Test case for XML file loading error handling (no root element). """
    screenflow = ScreenFlow()
    screenflow.load_from_file('resources/test_screenflow_without_root.xml')

@raises(AttributeError)
def test_load_from_file_without_screen():
    """ Test case for XML file loading error handling (no screen element). """
    screenflow = ScreenFlow()
    screenflow.load_from_file('resources/test_screenflow_without_screen.xml')<commit_msg>Fix syntax error<commit_after>#!/usr/bin/python

""" Test suite for ScreenFlow class. """

from screenflow import ScreenFlow, NavigationException
from screenflow.constants import XML_TYPE
from mocks.surface_mock import SurfaceMock, DEFAULT_SURFACE_SIZE
from mocks.screen_mock import ScreenMock
from nose.tools import raises

def test_add_screen():
    """ Test case for screen insertion. """
    name = 'foo'
    screen = ScreenMock(name)
    screenflow = ScreenFlow()
    screenflow.add_screen(screen)
    assert screen.font_manager == screenflow
    assert name in screenflow.screens.keys()
    assert screenflow.screens[name] == screen
    assert screenflow.foo == screen

def test_navigate_to():
    """ Test case for navigating to another screen. """
    screenflow = ScreenFlow()
    foo = ScreenMock('foo')
    bar = ScreenMock('bar')
    screenflow.stack.append(foo)
    screenflow.navigate_to(bar)
    assert screenflow.state == ScreenFlow.IN_TRANSITION
    assert len(screenflow.stack) == 2
    assert screenflow.stack[0] == foo
    assert screenflow.stack[1] == bar

@raises(NavigationException)
def test_navigate_back_error():
    """ Test case for navigating back error handling. """
    screenflow = ScreenFlow()
    screenflow.navigate_back()

def test_navigate_back():
    """ Test case for navigating back. """
    screenflow = ScreenFlow()
    foo = ScreenMock('foo')
    bar = ScreenMock('bar')
    screenflow.stack.append(foo)
    screenflow.stack.append(bar)
    screenflow.navigate_back()
    assert screenflow.state == ScreenFlow.IN_TRANSITION
    assert len(screenflow.stack) == 1
    assert screenflow.stack[0] == foo

def test_main_loop():
    """ Test case for the main loop. """
    assert False

def test_register_factory():
    """ Test case for screen factory registration. """
    screenflow = ScreenFlow()
    def factory(metadata):
        pass
    name = 'foo'
    screenflow.register_factory(name, factory)
    assert name in screenflow.factories.keys()
    assert screenflow.factories[name] == factory

@raises(ValueError)
def test_register_factory_duplicate():
    """ Test case for screen factory duplicate registration. """
    screenflow = ScreenFlow()
    name = 'foo'
    screenflow.register_factory(name, None)
    screenflow.register_factory(name, None)

def test_create_screen():
    """ """
    screenflow = ScreenFlow()
    # TODO : Consider registering factory first ?
    assert False

@raises(AttributeError)
def test_create_screen_not_valid_xml():
    """ Test case for creating screen without type. """
    screenflow = ScreenFlow()
    screenflow.create_screen({})

@raises(ValueError)
def test_create_unknown_type_screen():
    """ Test case for creating screen with unknown type. """
    screenflow = ScreenFlow()
    screenflow.create_screen({XML_TYPE: 'foo'})

def test_load_from_file():
    """ Test case for XML file loading. """
    screenflow = ScreenFlow()
    assert False

@raises(IOError)
def test_load_from_not_existing_file():
    """ Test case for XML file loading error handling (file not exists). """
    screenflow = ScreenFlow()
    screenflow.load_from_file('ghost_file.xml')

@raises(AttributeError)
def test_load_from_file_without_root():
    """ Test case for XML file loading error handling (no root element). """
    screenflow = ScreenFlow()
    screenflow.load_from_file('resources/test_screenflow_without_root.xml')

@raises(AttributeError)
def test_load_from_file_without_screen():
    """ Test case for XML file loading error handling (no screen element). """
    screenflow = ScreenFlow()
    screenflow.load_from_file('resources/test_screenflow_without_screen.xml')<|endoftext|>


<commit_before>'''
Most of the functions in this file write information to other files. The
functions make use of noun_remove.py, verb_remove.py, noun_verb_remove.py,
and ____________ to remove nouns, verbs, or subordinate conjunctions from
sentences. Each function reads textfile data from the specified file
located in the folder called orginalSentences and processes each line. I
will explain each function in detail below:

make_upd_sentence(s,match_found,removal_type):
This function takes in a string and a list of tuples. It updates the
string by removing any words that are in the list of tuples. For
example, if thestring is "My family loves hiking." and the list of
tuples is [('family', 'nsubj', 'NOUN'), ('went', 'ROOT', 'VERB')] then
it returns "My hiking."
Args:
    s: A string that is a sentence.
    match_found: An lst of words to remove. Check the comments on
                 nlpSpacy.py to see what an lst is.
    removal_type: This is the kind of removal that needs to be made and
                  represents the pos of words found in match_found.
                  Possible words are "noun", and "verb".
Return:
    This returns an updated string with the same words as s, but with
    words in match found removed.
-----------------------------------------------------------------------
* Refer to nlpSpacy.py to see what lst is. lst will be used here.
The functions below handle writing information to two seperate files.
Information is writtten to a textfile and another file ending in .p,
which uses pickle. The textfile is for a person to understand how each
line is processed. Each line in the textfile is in the following format:
 Original sentence: | Word removed: | Updated sentence: |
 POS lst for sentence | POS lst of removed words| POS lit of updated sentence
The information written into the file ending in .p uses serialization.
Here I write tuples, with three inputs, directly to the file. Each tuple
contains information for a sentence that was processed. The first argument
is the pos lst of the line being processed. The second input is a pos lst
of the original sentence, but does not contain the speficied pos being
removed. The third input is a lst of the words that were removed. I use
serialization here so that others could easily reuse the final outputs
of the data.
-----------------------------------------------------------------------
noun_textfile():
This function reads from a texfile called nounScreening.txt in the folder
originalSentences. It processes each line and removes the first noun or
group of consecutive nouns found in the line. Each line is a complete
sentence. Once a sentence is processed, it writes information to two
seperate files.
Args: None
Return: Nothing is returned, instead information is written to other files.

verb_textfile():
This function reads from a texfile called verbScreening.txt in the folder
originalSentences. It processes each line and removes the first verb or
group of consecutive verbs found in the line. Each line is a complete
sentence. Once a sentence is processed, it writes information to two
seperate files.
Args: None
Return: Nothing is returned, instead information is written to other files.

noun_verb_textfile():
This function reads from a texfile called nounverbScreening.txt in the folder
originalSentences. It processes each line and removes the first noun or
group of consecutive nouns and the first verb or group of consecutive verbs
found in the line. Each line is a complete sentence. Once a sentence is
processed, it writes information to the two seperate files.
Args: None
Return: Nothing is returned, instead information is written to other files.

subordinate_textfile():
This function reads from a textfiles called subScreening.txt in the folder
originalSentences. All of the sentences in subScreening contain subordinate
conjunctions. Each line in the function subordinate_textfile() is
processed and the function subordinate_removal removes subordinate
conjunctions at the beggining of a sentence or in the middle of a sentence.
Args: None
Return: Nothing is returned, instead information is written to other files.
'''
import pickle
from nlpSpacy import *
from noun_remove import noun_removal
from verb_remove import verb_removal
from noun_verb_remove import noun_verb_removal
from conjunction_remove import subordinate_removal

def load(filename):
    with open(filename, "rb") as f:
        while True:
            try:
                yield pickle.load(f)
            except EOFError:
                break

# items = load('./updatedSentences/nounSentences/nounRemoval.p')
# lst = []
# for i in items:
#     lst.append(i)
# print("At this point i should already have a list: ")
# print(len(lst))
# for tup in lst:
#     print(tup)

def make_upd_sentence(s,match_found,removal_type):
    sentence_wo_noun = delete_words_string(s,match_found)
    if (removal_type == "noun_verb"):
        return sentence_wo_noun
    else:
        return sentence_wo_noun[0].upper() + sentence_wo_noun[1:]

def noun_textfile():
    noun_dir = "./updatedSentences/nounSentences/"
    with open(noun_dir + "errorSentences.txt","w") as err, \
         open(noun_dir + "./updatedSentences/nounSentences/detailedRemoval.txt","w") as det, \
         open(noun_dir + "./updatedSentences/nounSentences/nounRemoval.p","wb") as tup_write, \
         open("./originalSentences/testingFile.txt","r") as f:
            for line in f:
                line = line.rstrip("\n")
                line_wo_n = noun_removal(line)
                if (line_wo_n[0] != "ERROR"):
                    noun_pos = line_wo_n[2][0]
                    pickle.dump((line_wo_n[0], #serialization
                                 line_wo_n[1],
                                 noun_pos),
                                tup_write)
                    n_found = make_str(noun_pos).rstrip("\n")
                    det.write(line + " ||| "
                              + n_found + " ||| "
                              + make_upd_sentence(line,n_found,"").rstrip("\n") + " ||| "
                              + tup_list_to_string(line_wo_n[0]) + " ||| "
                              + tup_list_to_string(noun_pos) + " ||| "
                              + tup_list_to_string(line_wo_n[1]) + "\n")
                else:
                    err.write(line + ' ||| '
                              + tup_list_to_string(line_wo_n[1]) + "\n")

def verb_textfile():
    verb_dir = "./updatedSentences/verbSentences/"
    with open(verb_dir + "errorSentences.txt","w") as err, \
         open(verb_dir + "detailedRemoval.txt","w") as det, \
         open(verb_dir + "verbRemoval.p","wb") as tup_write, \
         open("./originalSentences/testingFile.txt","r") as f:
            for line in f:
                line = line.rstrip("\n")
                line_wo_v = verb_removal(line)
                if (line_wo_v[0] != "ERROR"):
                    pickle.dump(line_wo_v, tup_write)
                    verb_pos = line_wo_v[2]
                    v_found = make_str(verb_pos)
                    det.write(line + " ||| "
                              + v_found.rstrip("\n") + " ||| "
                              + make_upd_sentence(line, v_found,"").rstrip("\n") + " ||| "
                              + tup_list_to_string(line_wo_v[0]) + " ||| "
                              + tup_list_to_string(verb_pos) + " ||| "
                              + tup_list_to_string(line_wo_v[1]) + "\n")
                else:
                    err.write(line + " ||| "
                              + tup_list_to_string(line_wo_v[1]) + "\n")

def noun_verb_textfile():
    nv_dir = "./updatedSentences/nounverbSentences/"
    with open(nv_dir + "errorSentences.txt","w") as err, \
         open(nv_dir + "detailedRemoval.txt","w") as det, \
         open(nv_dir + "nounverbRemoval.p","wb") as tup_write, \
         open("./originalSentences/testingFile.txt","r") as f:
            for line in f:
                line = line.rstrip("\n")
                line_wo_nv = noun_verb_removal(line)
                if (line_wo_nv[0] != "ERROR"):
                    n_pos_lst = line_wo_nv[2][0]
                    v_pos_lst = line_wo_nv[2][1]
                    pickle.dump((line_wo_nv[0],
                                 line_wo_nv[1],
                                 n_pos_lst
                                 + v_pos_lst),
                                tup_write)
                    noun_str = make_str(n_pos_lst).rstrip("\n")
                    verb_str = make_str(v_pos_lst).rstrip("\n")
                    rem_noun = make_upd_sentence(line, noun_str,"noun_verb")
                    remove_verb = make_upd_sentence(rem_noun, verb_str,"")
                    det.write(line + " ||| "
                              + noun_str + ' ' + verb_str + " ||| "
                              + remove_verb.rstrip("\n") + " ||| "
                              + tup_list_to_string(line_wo_nv[0]) + " ||| "
                              + tup_list_to_string(n_pos_lst + v_pos_lst) + " ||| "
                              + tup_list_to_string(line_wo_nv[1]) + "\n")
                else:
                    err.write(line + " ||| "
                              + tup_list_to_string(line_wo_nv[1]) + "\n")

def subordinate_textfile():
    sub_dir = "./updatedSentences/conjunctionSentences/"
    with open(sub_dir + "errorSentences.txt","w") as err, \
         open(sub_dir + "detailedRemoval.txt","w") as det, \
         open(sub_dir + "nounverbRemoval.p","wb") as tup_write, \
         open("./originalSentences/testingFile.txt","r") as f:
            for line in f:
                line = line.rstrip("\n")
                line_wo_sub = subordinate_removal(line)
                if (line_wo_sub != "ERROR"):
                    pickle.dump(line_wo_sub, tup_write)
                    sub_conj_lst = line_wo_sub[2]
                    sub_found = make_str(sub_conj_lst)
                    det.write(line + " ||| "
                              + sub_found + " ||| "
                              + make_upd_sentence(line,sub_found,"") + " ||| "
                              + tup_list_to_string(line_wo_sub[0]) + " ||| "
                              + tup_list_to_string(sub_conj_lst) + " ||| "
                              + tup_list_to_string(line_wo_sub[1]) + "\n")
                else:
                    err.write(line + " ||| "
                              + tup_list_to_string(line_wo_sub[1]) + "\n")
<commit_msg>I edited comments and changed some functions.<commit_after>'''
Most of the functions in this file write information to other files. The
functions make use of noun_remove.py, verb_remove.py, noun_verb_remove.py,
and conjunction_remove.py to remove nouns, verbs, or subordinate
conjunctions from sentences. Each function below reads textfile data
from the specified file located in the folder called orginalSentences
and processes each line. I will explain each function in detail below:

make_upd_sentence(s,match_found,removal_type):
This sentence removes any words from a string and outputs a string
that looks like a sentence, meaning the first character in the
string is capitalized and there is a period at the end.
Args:
    s: A string that is a sentence.
    match_found: A substring that needs to be removed from s.
    removal_type: This argument is only used by the function
                  noun_verb_textfile.
Return:
    This returns an updated string with the same words as s, but with
    words in match found removed.

For example, if the string is "My family loves hiking." and the string we
want to remove is "My family" then the function returns "Loves hiking."
-----------------------------------------------------------------------
* Refer to nlpSpacy.py to see what lst is. lst will be used here.
The functions below handle writing information to three seperate files.
Information is written to two textfile and a file ending in .p, which
uses pickle.
The textfile called detailedRemoval.txt is for a human to
understand how each line was line was processed. Each line in the
detailedRemoval.txt is in the following format:
Original sentence: | Word removed: | Updated sentence: |
POS lst for sentence | POS lst of removed words| POS lst of updated sentence
The information written into the file ending in .p uses serialization. For
each line I dump tuples into the file p. Each tuple has three inputs and
contains information for a sentence that was processed. The first argument
is the pos lst of the line being processed. The second input is a pos lst
of the original sentence, but with the pos matches removed. The third
input is a lst of the words that were removed.
The third file is called errorSentences.txt and contains the lst of
sentences that could not be processed.
-----------------------------------------------------------------------
The names of the functions below describe the kind of pos removal that
was done. All of the outputs for the functions below follow the format
described above.
'''
import pickle
from nlpSpacy import *
from noun_remove import noun_removal
from verb_remove import verb_removal
from noun_verb_remove import noun_verb_removal
from conjunction_remove import subordinate_removal

def make_upd_sentence(s,match_found,removal_type):
    sentence_wo_match = delete_words_string(s,match_found)
    if (removal_type == "noun_verb"):
        return sentence_wo_match
    else:
        return sentence_wo_match[0].upper() + sentence_wo_match[1:]

def noun_textfile():
    noun_dir = "./updatedSentences/nounSentences/"
    with open(noun_dir + "errorSentences.txt","w") as err, \
         open(noun_dir + "detailedRemoval.txt","w") as det, \
         open(noun_dir + "nounRemoval.p","wb") as tup_write, \
         open("./originalSentences/nounScreening.txt","r") as f:
            for line in f:
                print(line)
                line = line.rstrip("\n")
                line_wo_n = noun_removal(line)
                if (line_wo_n[0] != "ERROR"):
                    noun_pos = line_wo_n[2][0]
                    pickle.dump((line_wo_n[0], #serialization
                                 line_wo_n[1],
                                 noun_pos),
                                tup_write)
                    n_found = make_str(noun_pos).rstrip("\n")
                    det.write(line + " ||| "
                              + n_found + " ||| "
                              + make_upd_sentence(line,n_found,"").rstrip("\n") + " ||| "
                              + tup_list_to_string(line_wo_n[0]) + " ||| "
                              + tup_list_to_string(noun_pos) + " ||| "
                              + tup_list_to_string(line_wo_n[1]) + "\n")
                else:
                    err.write(line + ' ||| '
                              + tup_list_to_string(line_wo_n[1]) + "\n")

def verb_textfile():
    verb_dir = "./updatedSentences/verbSentences/"
    with open(verb_dir + "errorSentences.txt","w") as err, \
         open(verb_dir + "detailedRemoval.txt","w") as det, \
         open(verb_dir + "verbRemoval.p","wb") as tup_write, \
         open("./originalSentences/verbScreening.txt","r") as f:
            for line in f:
                print(line)
                line = line.rstrip("\n")
                line_wo_v = verb_removal(line)
                if (line_wo_v[0] != "ERROR"):
                    pickle.dump(line_wo_v, tup_write)
                    verb_pos = line_wo_v[2]
                    v_found = make_str(verb_pos)
                    # print("This is the verb found: " + v_found)
                    det.write(line + " ||| "
                              + v_found.rstrip("\n") + " ||| "
                              + make_upd_sentence(line, v_found,"").rstrip("\n") + " ||| "
                              + tup_list_to_string(line_wo_v[0]) + " ||| "
                              + tup_list_to_string(verb_pos) + " ||| "
                              + tup_list_to_string(line_wo_v[1]) + "\n")
                else:
                    err.write(line + " ||| "
                              + tup_list_to_string(line_wo_v[1]) + "\n")

def noun_verb_textfile():
    nv_dir = "./updatedSentences/nounverbSentences/"
    with open(nv_dir + "errorSentences.txt","w") as err, \
         open(nv_dir + "detailedRemoval.txt","w") as det, \
         open(nv_dir + "nounverbRemoval.p","wb") as tup_write, \
         open("./originalSentences/nounverbScreening.txt","r") as f:
            for line in f:
                line = line.rstrip("\n")
                line_wo_nv = noun_verb_removal(line)
                if (line_wo_nv[0] != "ERROR"):
                    n_pos_lst = line_wo_nv[2][0]
                    v_pos_lst = line_wo_nv[2][1]
                    pickle.dump((line_wo_nv[0],
                                 line_wo_nv[1],
                                 n_pos_lst
                                 + v_pos_lst),
                                tup_write)
                    noun_str = make_str(n_pos_lst).rstrip("\n")
                    verb_str = make_str(v_pos_lst).rstrip("\n")
                    rem_noun = make_upd_sentence(line, noun_str,"noun_verb")
                    remove_verb = make_upd_sentence(rem_noun, verb_str,"")
                    det.write(line + " ||| "
                              + noun_str + ' ' + verb_str + " ||| "
                              + remove_verb.rstrip("\n") + " ||| "
                              + tup_list_to_string(line_wo_nv[0]) + " ||| "
                              + tup_list_to_string(n_pos_lst + v_pos_lst) + " ||| "
                              + tup_list_to_string(line_wo_nv[1]) + "\n")
                else:
                    err.write(line + " ||| "
                              + tup_list_to_string(line_wo_nv[1]) + "\n")

def subordinate_textfile():
    sub_dir = "./updatedSentences/conjunctionSentences/"
    with open(sub_dir + "errorSentences.txt","w") as err, \
         open(sub_dir + "detailedRemoval.txt","w") as det, \
         open(sub_dir + "conjunctionRemoval.p","wb") as tup_write, \
         open("./originalSentences/subScreening.txt","r") as f:
            for line in f:
                print(line)
                line = line.rstrip("\n")
                line_wo_sub = subordinate_removal(line)
                if (line_wo_sub != "ERROR"):
                    pickle.dump(line_wo_sub, tup_write)
                    sub_conj_lst = line_wo_sub[2]
                    sub_found = make_str(sub_conj_lst)
                    det.write(line + " ||| "
                              + sub_found + " ||| "
                              + line.replace(sub_found,"") + " ||| "
                              + tup_list_to_string(line_wo_sub[0]) + " ||| "
                              + tup_list_to_string(sub_conj_lst) + " ||| "
                              + tup_list_to_string(line_wo_sub[1]) + "\n")
                else:
                    err.write(line + " ||| "
                              + tup_list_to_string(line_wo_sub[1]) + "\n")

#Here I am calling the functions written above.
noun_textfile()
verb_textfile()
noun_verb_textfile()
subordinate_textfile()
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""Test that syntax highlighting for USD files works correctly."""

import textwrap
import unittest

from pygments import lexers
from pygments import token

class UsdTest(unittest.TestCase):
    def setUp(self):
        self.lexer = lexers.UsdLexer()

    def _get(self, code):
        return list(self.lexer.get_tokens(code))

    def test_asset_path(self):
        code = '@/some/path/to/a/file/foo.usda@'
        expected = [
            (token.String.Interpol, code),
            (token.Text, '\n'),
        ]

        self.assertEqual(expected, self._get(code))

    def test_asset_path_uri(self):
        code = r'@file://SPECIALIZED(syntax_here)?with_arbitrary@#)(%*&)\characters.tar.gz@'
        expected = [
            (token.String.Interpol, code),
            (token.Text, '\n'),
        ]

        self.assertEqual(expected, self._get(code))

    def test_target_absolute(self):
        for code in [
            '</some/path/here>',
            '</some/another_one/here>',
            '</some/path/here.property_name>',
        ]:
            self.assertEqual(
                [(token.Name.Namespace, code), (token.Text, '\n')],
                self._get(code),
            )

    def test_target_relative(self):
        for code in [
            '<../some/path/here>',
            '<../some/another_one/here>',
            '<../some/path/here.property_name>',
        ]:
            self.assertEqual(
                [(token.Name.Namespace, code), (token.Text, '\n')],
                self._get(code),
            )

    # def test_attribute(self):
    #     normal = 'double foo = 8.0'
    #
    #     custom = 'custom double foo = 8.0'
    #     underscore = 'custom double foo_asfd = 8.0'
    #     underscore_no_custom = 'double foo_asfd = 8.0'
    #     array = 'double[] foo_asfd = [10.1, 12.0, 13]'
    #     namespaced = 'double[] primvar:foo_thing = [10.1, 12.0, 13]'
    #
    #     timeSamples = textwrap.dedent(
    #         '''\
    #         custom int[] foo = [8, 10, 14]
    #         custom int[] foo.timeSamples = {
    #             1: [8, 0, 14],
    #             2: [-8, 0, 14],
    #         }
    #         '''
    #     )
    #
    # def test_namespace_attribute(self):
    #     raise NotImplementedError()
    #
    # def test_numbers(self):
    #     raise NotImplementedError()
    #
    # def test_variant_set(self):
    #     raise NotImplementedError()
    #
    # def test_metadata(self):
    #     raise NotImplementedError()
    #
    # def test_string_single_line(self):
    #     raise NotImplementedError()
    #
    # def test_string_multiple_line(self):
    #     raise NotImplementedError()
<commit_msg>Changed test_asset_path_uri to be more complex<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""Test that syntax highlighting for USD files works correctly."""

import textwrap
import unittest

from pygments import lexers
from pygments import token

class UsdTest(unittest.TestCase):
    def setUp(self):
        self.lexer = lexers.UsdLexer()

    def _get(self, code):
        return list(self.lexer.get_tokens(code))

    def test_asset_path(self):
        code = '@/some/path/to/a/file/foo.usda@'
        expected = [
            (token.String.Interpol, code),
            (token.Text, '\n'),
        ]

        self.assertEqual(expected, self._get(code))

    def test_asset_path_uri(self):
        code = r'@file://SPECI__Z-_ALIZED(syntax_here)?with_arbitrary@#)(%*&)\characters.tar.gz@'
        expected = [
            (token.String.Interpol, code),
            (token.Text, '\n'),
        ]

        self.assertEqual(expected, self._get(code))

    def test_target_absolute(self):
        for code in [
            '</some/path/here>',
            '</some/another_one/here>',
            '</some/path/here.property_name>',
        ]:
            self.assertEqual(
                [(token.Name.Namespace, code), (token.Text, '\n')],
                self._get(code),
            )

    def test_target_relative(self):
        for code in [
            '<../some/path/here>',
            '<../some/another_one/here>',
            '<../some/path/here.property_name>',
        ]:
            self.assertEqual(
                [(token.Name.Namespace, code), (token.Text, '\n')],
                self._get(code),
            )

    # def test_attribute(self):
    #     normal = 'double foo = 8.0'
    #
    #     custom = 'custom double foo = 8.0'
    #     underscore = 'custom double foo_asfd = 8.0'
    #     underscore_no_custom = 'double foo_asfd = 8.0'
    #     array = 'double[] foo_asfd = [10.1, 12.0, 13]'
    #     namespaced = 'double[] primvar:foo_thing = [10.1, 12.0, 13]'
    #
    #     timeSamples = textwrap.dedent(
    #         '''\
    #         custom int[] foo = [8, 10, 14]
    #         custom int[] foo.timeSamples = {
    #             1: [8, 0, 14],
    #             2: [-8, 0, 14],
    #         }
    #         '''
    #     )
    #
    # def test_namespace_attribute(self):
    #     raise NotImplementedError()
    #
    # def test_numbers(self):
    #     raise NotImplementedError()
    #
    # def test_variant_set(self):
    #     raise NotImplementedError()
    #
    # def test_metadata(self):
    #     raise NotImplementedError()
    #
    # def test_string_single_line(self):
    #     raise NotImplementedError()
    #
    # def test_string_multiple_line(self):
    #     raise NotImplementedError()
<|endoftext|>