<commit_before>"""Tests for autocompletion."""
import sublime
import platform
from os import path

from EasyClangComplete.plugin.settings.settings_manager import SettingsManager
from EasyClangComplete.plugin.tools import ActionRequest
from EasyClangComplete.plugin.view_config import ViewConfigManager

from EasyClangComplete.tests.gui_test_wrapper import GuiTestWrapper

def has_libclang():
    """Ensure libclang tests will run only on platforms that support this.

    Returns:
        str: row contents
    """
    # Older version of Sublime Text x64 have ctypes crash bug.
    if platform.system() == "Windows" and sublime.arch() == "x64" and \
            int(sublime.version()) < 3123:
        return False
    return True

def should_run_objc_tests():
    """Decide if Objective C tests should be run.

    For now, run only on Mac OS due to difficulties getting the GNUstep
    environment setup with GNUstep and clang to run properly in
    Windows and Linux CI's, and nearly all ObjC development is done on
    Mac OS anyway.
    """
    return platform.system() == "Darwin"

class BaseTestCompleter(object):
    """
    Base class for tests that are independent of the Completer implementation.

    Attributes:
        view (sublime.View): view
        use_libclang (bool): decides if we use libclang in tests
    """

    def set_up_completer(self):
        """Utility method to set up a completer for the current view.

        Returns:
            BaseCompleter: completer for the current view.
        """
        manager = SettingsManager()
        settings = manager.settings_for_view(self.view)
        settings.use_libclang = self.use_libclang

        view_config_manager = ViewConfigManager()
        view_config = view_config_manager.load_for_view(self.view, settings)
        completer = view_config.completer
        return completer

    def tear_down_completer(self):
        """Utility method to set up a completer for the current view.

        Returns:
            BaseCompleter: completer for the current view.
        """
        view_config_manager = ViewConfigManager()
        view_config_manager.clear_for_view(self.view.buffer_id())

    def test_setup_view(self):
        """Test that setup view correctly sets up the view."""
        file_name = path.join(path.dirname(__file__),
                              'test_files',
                              'test.cpp')
        self.check_view(file_name)
        self.tear_down()

    def test_init(self):
        """Test that the completer is properly initialized."""
        file_name = path.join(path.dirname(__file__),
                              'test_files',
                              'test.cpp')
        self.set_up_view(file_name)
        completer = self.set_up_completer()

        self.assertIsNotNone(completer.version_str)
        self.tear_down_completer()
        self.tear_down()

    def test_complete(self):
        """Test autocompletion for user type."""
        file_name = path.join(path.dirname(__file__),
                              'test_files',
                              'test.cpp')
        self.set_up_view(file_name)

        completer = self.set_up_completer()

        # Check the current cursor position is completable.
        self.assertEqual(self.get_row(8), "  a.")
        pos = self.view.text_point(8, 4)
        current_word = self.view.substr(self.view.word(pos))
        self.assertEqual(current_word, ".\n")

        # Load the completions.
        request = ActionRequest(self.view, pos)
        (_, completions) = completer.complete(request)

        # Verify that we got the expected completions back.
        self.assertIsNotNone(completions)
        expected = ['foo\tvoid foo(double a)', 'foo(${1:double a})']

        self.assertIn(expected, completions)
        self.tear_down_completer()
        self.tear_down()

    def test_excluded_private(self):
        """Test autocompletion for user type."""
        file_name = path.join(path.dirname(__file__),
                              'test_files',
                              'test.cpp')
        self.set_up_view(file_name)

        completer = self.set_up_completer()

        # Check the current cursor position is completable.
        self.assertEqual(self.get_row(8), "  a.")
        pos = self.view.text_point(8, 4)
        current_word = self.view.substr(self.view.word(pos))
        self.assertEqual(current_word, ".\n")

        # Load the completions.
        request = ActionRequest(self.view, pos)
        (_, completions) = completer.complete(request)

        # Verify that we got the expected completions back.
        self.assertIsNotNone(completions)
        expected = ['foo\tvoid foo(double a)', 'foo(${1:double a})']
        unexpected = ['foo\tvoid foo(int a)', 'foo(${1:int a})']
        if self.use_libclang:
            self.assertIn(expected, completions)
            self.assertNotIn(unexpected, completions)
        self.tear_down_completer()
        self.tear_down()

    def test_excluded_destructor(self):
        """Test autocompletion for user type."""
        file_name = path.join(path.dirname(__file__),
                              'test_files',
                              'test.cpp')
        self.set_up_view(file_name)

        completer = self.set_up_completer()

        # Check the current cursor position is completable.
        self.assertEqual(self.get_row(8), "  a.")
        pos = self.view.text_point(8, 4)
        current_word = self.view.substr(self.view.word(pos))
        self.assertEqual(current_word, ".\n")

        # Load the completions.
        request = ActionRequest(self.view, pos)
        (_, completions) = completer.complete(request)

        # Verify that we got the expected completions back.
        self.assertIsNotNone(completions)
        destructor = ['~A\tvoid ~A()', '~A()']
        if self.use_libclang:
            self.assertNotIn(destructor, completions)
        else:
            self.assertIn(destructor, completions)
        self.tear_down_completer()
        self.tear_down()

    def test_complete_vector(self):
        """Test that we can complete vector members."""
        file_name = path.join(path.dirname(__file__),
                              'test_files',
                              'test_vector.cpp')
        self.set_up_view(file_name)

        completer = self.set_up_completer()

        # Check the current cursor position is completable.
        self.assertEqual(self.get_row(3), "  vec.")
        pos = self.view.text_point(3, 6)
        current_word = self.view.substr(self.view.word(pos))
        self.assertEqual(current_word, ".\n")

        # Load the completions.
        request = ActionRequest(self.view, pos)
        (_, completions) = completer.complete(request)

        # Verify that we got the expected completions back.
        self.assertIsNotNone(completions)
        expected = ['begin\titerator begin()', 'begin()']
        self.assertIn(expected, completions)
        self.tear_down_completer()
        self.tear_down()

    def test_complete_objc_property(self):
        """Test that we can complete Objective C properties."""
        if not should_run_objc_tests():
            return
        file_name = path.join(path.dirname(__file__),
                              'test_files',
                              'test_property.m')
        self.set_up_view(file_name)

        completer = self.set_up_completer()

        # Check the current cursor position is completable.
        self.assertEqual(self.get_row(6), "  foo.")
        pos = self.view.text_point(6, 6)
        current_word = self.view.substr(self.view.word(pos))
        self.assertEqual(current_word, ".\n")

        # Load the completions.
        request = ActionRequest(self.view, pos)
        (_, completions) = completer.complete(request)

        # Verify that we got the expected completions back.
        self.assertIsNotNone(completions)
        expected = ['boolProperty\tBOOL boolProperty', 'boolProperty']
        self.assertIn(expected, completions)
        self.tear_down_completer()
        self.tear_down()

    def test_complete_objc_void_method(self):
        """Test that we can complete Objective C void methods."""
        if not should_run_objc_tests():
            return
        file_name = path.join(path.dirname(__file__),
                              'test_files',
                              'test_void_method.m')
        self.set_up_view(file_name)

        completer = self.set_up_completer()

        # Check the current cursor position is completable.
        self.assertEqual(self.get_row(6), "  [foo ")
        pos = self.view.text_point(6, 7)
        current_word = self.view.substr(self.view.word(pos))
        self.assertEqual(current_word, " \n")

        # Load the completions.
        request = ActionRequest(self.view, pos)
        (_, completions) = completer.complete(request)

        # Verify that we got the expected completions back.
        self.assertIsNotNone(completions)
        expected = ['voidMethod\tvoid voidMethod', 'voidMethod']
        self.assertIn(expected, completions)
        self.tear_down_completer()
        self.tear_down()

    def test_complete_objc_method_one_parameter(self):
        """Test that we can complete Objective C methods with one parameter."""
        if not should_run_objc_tests():
            return
        file_name = path.join(path.dirname(__file__),
                              'test_files',
                              'test_method_one_parameter.m')
        self.set_up_view(file_name)

        completer = self.set_up_completer()

        # Check the current cursor position is completable.
        self.assertEqual(self.get_row(6), "  [foo ")
        pos = self.view.text_point(6, 7)
        current_word = self.view.substr(self.view.word(pos))
        self.assertEqual(current_word, " \n")

        # Load the completions.
        request = ActionRequest(self.view, pos)
        (_, completions) = completer.complete(request)

        # Verify that we got the expected completions back.
        self.assertIsNotNone(completions)
        expected = ['oneParameterMethod:\tvoid oneParameterMethod:(BOOL)',
                    'oneParameterMethod:${1:(BOOL)}']
        self.assertIn(expected, completions)
        self.tear_down_completer()
        self.tear_down()

    def test_complete_objc_method_multiple_parameters(self):
        """Test that we can complete Objective C methods with 2+ parameters."""
        if not should_run_objc_tests():
            return
        file_name = path.join(path.dirname(__file__),
                              'test_files',
                              'test_method_two_parameters.m')
        self.set_up_view(file_name)

        completer = self.set_up_completer()

        # Check the current cursor position is completable.
        self.assertEqual(self.get_row(6), "  [foo ")
        pos = self.view.text_point(6, 7)
        current_word = self.view.substr(self.view.word(pos))
        self.assertEqual(current_word, " \n")

        # Load the completions.
        request = ActionRequest(self.view, pos)
        (_, completions) = completer.complete(request)

        # Verify that we got the expected completions back.
        self.assertIsNotNone(completions)
        expected = [
            'bar:strParam:\tNSInteger * bar:(BOOL) strParam:(NSString *)',
            'bar:${1:(BOOL)} strParam:${2:(NSString *)}']

        self.assertIn(expected, completions)
        self.tear_down_completer()
        self.tear_down()

    def test_complete_objcpp(self):
        """Test that we can complete code in Objective-C++ files."""
        if not should_run_objc_tests():
            return
        file_name = path.join(path.dirname(__file__),
                              'test_files',
                              'test_objective_cpp.mm')
        self.set_up_view(file_name)

        completer = self.set_up_completer()

        # Check the current cursor position is completable.
        self.assertEqual(self.get_row(3), "  str.")
        pos = self.view.text_point(3, 6)
        current_word = self.view.substr(self.view.word(pos))
        self.assertEqual(current_word, ".\n")

        # Load the completions.
        request = ActionRequest(self.view, pos)
        (_, completions) = completer.complete(request)

        # Verify that we got the expected completions back.
        self.assertIsNotNone(completions)
        expected = [
            'clear\tvoid clear()', 'clear()']
        self.assertIn(expected, completions)
        self.tear_down_completer()
        self.tear_down()

    def test_unsaved_views(self):
        """Test that we gracefully handle unsaved views."""
        # Construct an unsaved scratch view.
        self.view = sublime.active_window().new_file()
        self.view.set_scratch(True)

        # Manually set up a completer.
        manager = SettingsManager()
        settings = manager.settings_for_view(self.view)
        view_config_manager = ViewConfigManager()
        view_config = view_config_manager.load_for_view(self.view, settings)
        self.assertIsNone(view_config)
        self.tear_down()

    def test_cooperation_with_default_completions(self):
        """Empty clang completions should not hide default completions."""
        file_name = path.join(path.dirname(__file__),
                              'test_files',
                              'test_errors.cpp')
        self.set_up_view(file_name)

        self.set_up_completer()

        # Undefined foo object has no completions.
        self.assertEqual(self.get_row(1), "  foo.")
        pos = self.view.text_point(1, 6)
        current_word = self.view.substr(self.view.word(pos))
        self.assertEqual(current_word, ".\n")

        # Trigger default completions popup.
        self.view.run_command('auto_complete')
        self.assertTrue(self.view.is_auto_complete_visible())
        self.tear_down_completer()
        self.tear_down()

    def test_get_declaration_location(self):
        """Test getting declaration location."""
        if not self.use_libclang:
            return
        file_name = path.join(path.dirname(__file__),
                              'test_files',
                              'test_location.cpp')
        self.set_up_view(file_name)

        completer = self.set_up_completer()

        # Check the current cursor position is completable.
        row = 9
        col = 15
        self.assertEqual(self.get_row(row), "  cool_class.foo();")
        pos = self.view.text_point(row, col)
        current_word = self.view.substr(self.view.word(pos))
        self.assertEqual(current_word, "foo")

        loc = completer.get_declaration_location(self.view, row + 1, col)
        self.assertEqual(loc.file.name, file_name)
        self.assertEqual(loc.line, 3)
        self.assertEqual(loc.column, 8)

        self.tear_down_completer()
        self.tear_down()

class TestBinCompleter(BaseTestCompleter, GuiTestWrapper):
    """Test class for the binary based completer."""
    use_libclang = False

if has_libclang():
    class TestLibCompleter(BaseTestCompleter, GuiTestWrapper):
        """Test class for the library based completer."""
        use_libclang = True
<commit_msg>Minor test update to disconnect output from clang version<commit_after>"""Tests for autocompletion."""
import sublime
import platform
from os import path

from EasyClangComplete.plugin.settings.settings_manager import SettingsManager
from EasyClangComplete.plugin.tools import ActionRequest
from EasyClangComplete.plugin.view_config import ViewConfigManager

from EasyClangComplete.tests.gui_test_wrapper import GuiTestWrapper

def has_libclang():
    """Ensure libclang tests will run only on platforms that support this.

    Returns:
        str: row contents
    """
    # Older version of Sublime Text x64 have ctypes crash bug.
    if platform.system() == "Windows" and sublime.arch() == "x64" and \
            int(sublime.version()) < 3123:
        return False
    return True

def should_run_objc_tests():
    """Decide if Objective C tests should be run.

    For now, run only on Mac OS due to difficulties getting the GNUstep
    environment setup with GNUstep and clang to run properly in
    Windows and Linux CI's, and nearly all ObjC development is done on
    Mac OS anyway.
    """
    return platform.system() == "Darwin"

class BaseTestCompleter(object):
    """Base class for tests independent of the Completer implementation.

    Attributes:
        view (sublime.View): view
        use_libclang (bool): decides if we use libclang in tests
    """

    def set_up_completer(self):
        """Set up a completer for the current view.

        Returns:
            BaseCompleter: completer for the current view.
        """
        manager = SettingsManager()
        settings = manager.settings_for_view(self.view)
        settings.use_libclang = self.use_libclang

        view_config_manager = ViewConfigManager()
        view_config = view_config_manager.load_for_view(self.view, settings)
        completer = view_config.completer
        return completer

    def tear_down_completer(self):
        """Tear down completer for the current view.

        Returns:
            BaseCompleter: completer for the current view.
        """
        view_config_manager = ViewConfigManager()
        view_config_manager.clear_for_view(self.view.buffer_id())

    def test_setup_view(self):
        """Test that setup view correctly sets up the view."""
        file_name = path.join(path.dirname(__file__),
                              'test_files',
                              'test.cpp')
        self.check_view(file_name)
        self.tear_down()

    def test_init(self):
        """Test that the completer is properly initialized."""
        file_name = path.join(path.dirname(__file__),
                              'test_files',
                              'test.cpp')
        self.set_up_view(file_name)
        completer = self.set_up_completer()

        self.assertIsNotNone(completer.version_str)
        self.tear_down_completer()
        self.tear_down()

    def test_complete(self):
        """Test autocompletion for user type."""
        file_name = path.join(path.dirname(__file__),
                              'test_files',
                              'test.cpp')
        self.set_up_view(file_name)

        completer = self.set_up_completer()

        # Check the current cursor position is completable.
        self.assertEqual(self.get_row(8), "  a.")
        pos = self.view.text_point(8, 4)
        current_word = self.view.substr(self.view.word(pos))
        self.assertEqual(current_word, ".\n")

        # Load the completions.
        request = ActionRequest(self.view, pos)
        (_, completions) = completer.complete(request)

        # Verify that we got the expected completions back.
        self.assertIsNotNone(completions)
        expected = ['foo\tvoid foo(double a)', 'foo(${1:double a})']

        self.assertIn(expected, completions)
        self.tear_down_completer()
        self.tear_down()

    def test_excluded_private(self):
        """Test autocompletion for user type."""
        file_name = path.join(path.dirname(__file__),
                              'test_files',
                              'test.cpp')
        self.set_up_view(file_name)

        completer = self.set_up_completer()

        # Check the current cursor position is completable.
        self.assertEqual(self.get_row(8), "  a.")
        pos = self.view.text_point(8, 4)
        current_word = self.view.substr(self.view.word(pos))
        self.assertEqual(current_word, ".\n")

        # Load the completions.
        request = ActionRequest(self.view, pos)
        (_, completions) = completer.complete(request)

        # Verify that we got the expected completions back.
        self.assertIsNotNone(completions)
        expected = ['foo\tvoid foo(double a)', 'foo(${1:double a})']
        unexpected = ['foo\tvoid foo(int a)', 'foo(${1:int a})']
        if self.use_libclang:
            self.assertIn(expected, completions)
            self.assertNotIn(unexpected, completions)
        self.tear_down_completer()
        self.tear_down()

    def test_excluded_destructor(self):
        """Test autocompletion for user type."""
        file_name = path.join(path.dirname(__file__),
                              'test_files',
                              'test.cpp')
        self.set_up_view(file_name)

        completer = self.set_up_completer()

        # Check the current cursor position is completable.
        self.assertEqual(self.get_row(8), "  a.")
        pos = self.view.text_point(8, 4)
        current_word = self.view.substr(self.view.word(pos))
        self.assertEqual(current_word, ".\n")

        # Load the completions.
        request = ActionRequest(self.view, pos)
        (_, completions) = completer.complete(request)

        # Verify that we got the expected completions back.
        self.assertIsNotNone(completions)
        destructor = ['~A\tvoid ~A()', '~A()']
        if self.use_libclang:
            self.assertNotIn(destructor, completions)
        else:
            self.assertIn(destructor, completions)
        self.tear_down_completer()
        self.tear_down()

    def test_complete_vector(self):
        """Test that we can complete vector members."""
        file_name = path.join(path.dirname(__file__),
                              'test_files',
                              'test_vector.cpp')
        self.set_up_view(file_name)

        completer = self.set_up_completer()

        # Check the current cursor position is completable.
        self.assertEqual(self.get_row(3), "  vec.")
        pos = self.view.text_point(3, 6)
        current_word = self.view.substr(self.view.word(pos))
        self.assertEqual(current_word, ".\n")

        # Load the completions.
        request = ActionRequest(self.view, pos)
        (_, completions) = completer.complete(request)

        # Verify that we got the expected completions back.
        self.assertIsNotNone(completions)
        expected = ['clear\tvoid clear()', 'clear()']
        self.assertIn(expected, completions)
        self.tear_down_completer()
        self.tear_down()

    def test_complete_objc_property(self):
        """Test that we can complete Objective C properties."""
        if not should_run_objc_tests():
            return
        file_name = path.join(path.dirname(__file__),
                              'test_files',
                              'test_property.m')
        self.set_up_view(file_name)

        completer = self.set_up_completer()

        # Check the current cursor position is completable.
        self.assertEqual(self.get_row(6), "  foo.")
        pos = self.view.text_point(6, 6)
        current_word = self.view.substr(self.view.word(pos))
        self.assertEqual(current_word, ".\n")

        # Load the completions.
        request = ActionRequest(self.view, pos)
        (_, completions) = completer.complete(request)

        # Verify that we got the expected completions back.
        self.assertIsNotNone(completions)
        expected = ['boolProperty\tBOOL boolProperty', 'boolProperty']
        self.assertIn(expected, completions)
        self.tear_down_completer()
        self.tear_down()

    def test_complete_objc_void_method(self):
        """Test that we can complete Objective C void methods."""
        if not should_run_objc_tests():
            return
        file_name = path.join(path.dirname(__file__),
                              'test_files',
                              'test_void_method.m')
        self.set_up_view(file_name)

        completer = self.set_up_completer()

        # Check the current cursor position is completable.
        self.assertEqual(self.get_row(6), "  [foo ")
        pos = self.view.text_point(6, 7)
        current_word = self.view.substr(self.view.word(pos))
        self.assertEqual(current_word, " \n")

        # Load the completions.
        request = ActionRequest(self.view, pos)
        (_, completions) = completer.complete(request)

        # Verify that we got the expected completions back.
        self.assertIsNotNone(completions)
        expected = ['voidMethod\tvoid voidMethod', 'voidMethod']
        self.assertIn(expected, completions)
        self.tear_down_completer()
        self.tear_down()

    def test_complete_objc_method_one_parameter(self):
        """Test that we can complete Objective C methods with one parameter."""
        if not should_run_objc_tests():
            return
        file_name = path.join(path.dirname(__file__),
                              'test_files',
                              'test_method_one_parameter.m')
        self.set_up_view(file_name)

        completer = self.set_up_completer()

        # Check the current cursor position is completable.
        self.assertEqual(self.get_row(6), "  [foo ")
        pos = self.view.text_point(6, 7)
        current_word = self.view.substr(self.view.word(pos))
        self.assertEqual(current_word, " \n")

        # Load the completions.
        request = ActionRequest(self.view, pos)
        (_, completions) = completer.complete(request)

        # Verify that we got the expected completions back.
        self.assertIsNotNone(completions)
        expected = ['oneParameterMethod:\tvoid oneParameterMethod:(BOOL)',
                    'oneParameterMethod:${1:(BOOL)}']
        self.assertIn(expected, completions)
        self.tear_down_completer()
        self.tear_down()

    def test_complete_objc_method_multiple_parameters(self):
        """Test that we can complete Objective C methods with 2+ parameters."""
        if not should_run_objc_tests():
            return
        file_name = path.join(path.dirname(__file__),
                              'test_files',
                              'test_method_two_parameters.m')
        self.set_up_view(file_name)

        completer = self.set_up_completer()

        # Check the current cursor position is completable.
        self.assertEqual(self.get_row(6), "  [foo ")
        pos = self.view.text_point(6, 7)
        current_word = self.view.substr(self.view.word(pos))
        self.assertEqual(current_word, " \n")

        # Load the completions.
        request = ActionRequest(self.view, pos)
        (_, completions) = completer.complete(request)

        # Verify that we got the expected completions back.
        self.assertIsNotNone(completions)
        expected = [
            'bar:strParam:\tNSInteger * bar:(BOOL) strParam:(NSString *)',
            'bar:${1:(BOOL)} strParam:${2:(NSString *)}']

        self.assertIn(expected, completions)
        self.tear_down_completer()
        self.tear_down()

    def test_complete_objcpp(self):
        """Test that we can complete code in Objective-C++ files."""
        if not should_run_objc_tests():
            return
        file_name = path.join(path.dirname(__file__),
                              'test_files',
                              'test_objective_cpp.mm')
        self.set_up_view(file_name)

        completer = self.set_up_completer()

        # Check the current cursor position is completable.
        self.assertEqual(self.get_row(3), "  str.")
        pos = self.view.text_point(3, 6)
        current_word = self.view.substr(self.view.word(pos))
        self.assertEqual(current_word, ".\n")

        # Load the completions.
        request = ActionRequest(self.view, pos)
        (_, completions) = completer.complete(request)

        # Verify that we got the expected completions back.
        self.assertIsNotNone(completions)
        expected = [
            'clear\tvoid clear()', 'clear()']
        self.assertIn(expected, completions)
        self.tear_down_completer()
        self.tear_down()

    def test_unsaved_views(self):
        """Test that we gracefully handle unsaved views."""
        # Construct an unsaved scratch view.
        self.view = sublime.active_window().new_file()
        self.view.set_scratch(True)

        # Manually set up a completer.
        manager = SettingsManager()
        settings = manager.settings_for_view(self.view)
        view_config_manager = ViewConfigManager()
        view_config = view_config_manager.load_for_view(self.view, settings)
        self.assertIsNone(view_config)
        self.tear_down()

    def test_cooperation_with_default_completions(self):
        """Empty clang completions should not hide default completions."""
        file_name = path.join(path.dirname(__file__),
                              'test_files',
                              'test_errors.cpp')
        self.set_up_view(file_name)

        self.set_up_completer()

        # Undefined foo object has no completions.
        self.assertEqual(self.get_row(1), "  foo.")
        pos = self.view.text_point(1, 6)
        current_word = self.view.substr(self.view.word(pos))
        self.assertEqual(current_word, ".\n")

        # Trigger default completions popup.
        self.view.run_command('auto_complete')
        self.assertTrue(self.view.is_auto_complete_visible())
        self.tear_down_completer()
        self.tear_down()

    def test_get_declaration_location(self):
        """Test getting declaration location."""
        if not self.use_libclang:
            return
        file_name = path.join(path.dirname(__file__),
                              'test_files',
                              'test_location.cpp')
        self.set_up_view(file_name)

        completer = self.set_up_completer()

        # Check the current cursor position is completable.
        row = 9
        col = 15
        self.assertEqual(self.get_row(row), "  cool_class.foo();")
        pos = self.view.text_point(row, col)
        current_word = self.view.substr(self.view.word(pos))
        self.assertEqual(current_word, "foo")

        loc = completer.get_declaration_location(self.view, row + 1, col)
        self.assertEqual(loc.file.name, file_name)
        self.assertEqual(loc.line, 3)
        self.assertEqual(loc.column, 8)

        self.tear_down_completer()
        self.tear_down()

class TestBinCompleter(BaseTestCompleter, GuiTestWrapper):
    """Test class for the binary based completer."""
    use_libclang = False

if has_libclang():
    class TestLibCompleter(BaseTestCompleter, GuiTestWrapper):
        """Test class for the library based completer."""
        use_libclang = True
<|endoftext|>


<commit_before># Django settings file for a project based on the playdoh template.
# import * into your settings_local.py
import logging
import os
import socket

from django.utils.functional import lazy

from .manage import ROOT, path

ROOT_PACKAGE = os.path.basename(ROOT)

# Is this a dev instance?
DEV = False

DEBUG = False
TEMPLATE_DEBUG = DEBUG

ADMINS = ()
MANAGERS = ADMINS

DATABASES = {}  # See settings_local.

# Site ID is used by Django's Sites framework.
SITE_ID = 1
# Logging
LOG_LEVEL = logging.DEBUG
HAS_SYSLOG = True
SYSLOG_TAG = "http_app_playdoh"  # Change this after you fork.
LOGGING_CONFIG = None
LOGGING = { }

## Internationalization.

# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# On Unix systems, a value of None will cause Django to use the same
# timezone as the operating system.
# If running in a Windows environment this must be set to the same as your
# system time zone.
TIME_ZONE = 'America/Los_Angeles'

# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True

# If you set this to False, Django will not format dates, numbers and
# calendars according to the current locale
USE_L10N = True

# Gettext text domain
TEXT_DOMAIN = 'messages'
STANDALONE_DOMAINS = [TEXT_DOMAIN, 'javascript']
TOWER_KEYWORDS = {'_lazy': None}
TOWER_ADD_HEADERS = True

# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en-US'

## Accepted locales

# On dev instances, the list of accepted locales defaults to the contents of
# the `locale` directory.  A localizer can add their locale in the l10n
# repository (copy of which is checked out into `locale`) in order to start
# testing the localization on the dev server.
try:
    DEV_LANGUAGES = [
        loc.replace('_', '-') for loc in os.listdir(path('locale'))
        if os.path.isdir(path('locale', loc)) and loc != 'templates'
    ]
except OSError:
    DEV_LANGUAGES = ('en-US',)

# On stage/prod, the list of accepted locales is manually maintained.  Only
# locales whose localizers have signed off on their work should be listed here.
PROD_LANGUAGES = (
    'en-US',
)

def lazy_lang_url_map():
    from django.conf import settings
    langs = settings.DEV_LANGUAGES if settings.DEV else settings.PROD_LANGUAGES
    return dict([(i.lower(), i) for i in langs])

LANGUAGE_URL_MAP = lazy(lazy_lang_url_map, dict)()

# Override Django's built-in with our native names
def lazy_langs():
    from django.conf import settings
    from product_details import product_details
    langs = DEV_LANGUAGES if settings.DEV else PROD_LANGUAGES
    return dict([(lang.lower(), product_details.languages[lang]['native'])
                 for lang in langs])

# Where to store product details etc.
PROD_DETAILS_DIR = path('lib/product_details_json')

LANGUAGES = lazy(lazy_langs, dict)()

# Paths that don't require a locale code in the URL.
SUPPORTED_NONLOCALES = ['media']

## Media and templates.

# Absolute path to the directory that holds media.
# Example: "/home/media/media.lawrence.com/"
MEDIA_ROOT = path('media')

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash if there is a path component (optional in other cases).
# Examples: "http://media.lawrence.com", "http://example.com/media/"
MEDIA_URL = '/media/'

# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
# trailing slash.
# Examples: "http://foo.com/media/", "/media/".
ADMIN_MEDIA_PREFIX = '/admin-media/'

# Make this unique, and don't share it with anybody.
SECRET_KEY = '1iz#v0m55@h26^m6hxk3a7at*h$qj_2a$juu1#nv50548j(x1v'

# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
#     'django.template.loaders.eggs.Loader',
)

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.contrib.auth.context_processors.auth',
    'django.core.context_processors.debug',
    'django.core.context_processors.media',
    'django.core.context_processors.request',
    'session_csrf.context_processor',
    'django.contrib.messages.context_processors.messages',
    'funfactory.context_processors.i18n',
    'funfactory.context_processors.globals',
    #'jingo_minify.helpers.build_ids',
)

TEMPLATE_DIRS = (
    path('templates'),
)

def JINJA_CONFIG():
    import jinja2
    from django.conf import settings
#    from caching.base import cache
    config = {'extensions': ['tower.template.i18n', 'jinja2.ext.do',
                             'jinja2.ext.with_', 'jinja2.ext.loopcontrols'],
              'finalize': lambda x: x if x is not None else ''}
#    if 'memcached' in cache.scheme and not settings.DEBUG:
        # We're passing the _cache object directly to jinja because
        # Django can't store binary directly; it enforces unicode on it.
        # Details: http://jinja.pocoo.org/2/documentation/api#bytecode-cache
        # and in the errors you get when you try it the other way.
#        bc = jinja2.MemcachedBytecodeCache(cache._cache,
#                                           "%sj2:" % settings.CACHE_PREFIX)
#        config['cache_size'] = -1 # Never clear the cache
#        config['bytecode_cache'] = bc
    return config

## Middlewares, apps, URL configs.

MIDDLEWARE_CLASSES = (
    'funfactory.middleware.LocaleURLMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'session_csrf.CsrfMiddleware',  # Must be after auth middleware.
    'django.contrib.messages.middleware.MessageMiddleware',
    'commonware.middleware.FrameOptionsHeader',
)

ROOT_URLCONF = '%s.urls' % ROOT_PACKAGE

INSTALLED_APPS = (
    # Local apps
    'funfactory',  # Content common to most playdoh-based apps.
    'jingo_minify',
    'tower',  # for ./manage.py extract (L10n)

    # We need this so the jsi18n view will pick up our locale directory.
    ROOT_PACKAGE,

    # Django contrib apps
    'django.contrib.auth',
    'django_sha2',  # Load after auth to monkey-patch it.
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    # 'django.contrib.sites',
    # 'django.contrib.messages',
    # Uncomment the next line to enable the admin:
    # 'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',

    # Third-party apps, patches, fixes
    'commonware.response.cookies',
    'djcelery',
    'django_nose',
    'session_csrf',

    # L10n
    'product_details',
)

# Tells the extract script what files to look for L10n in and what function
# handles the extraction. The Tower library expects this.
DOMAIN_METHODS = {
    'messages': [
        ('apps/**.py',
            'tower.management.commands.extract.extract_tower_python'),
        ('**/templates/**.html',
            'tower.management.commands.extract.extract_tower_template'),
    ],

    ## Use this if you have localizable HTML files:
    #'lhtml': [
    #    ('**/templates/**.lhtml',
    #        'tower.management.commands.extract.extract_tower_template'),
    #],

    ## Use this if you have localizable JS files:
    #'javascript': [
        # Make sure that this won't pull in strings from external libraries you
        # may use.
    #    ('media/js/**.js', 'javascript'),
    #],
}

# Path to Java. Used for compress_assets.
JAVA_BIN = '/usr/bin/java'

## Auth
PWD_ALGORITHM = 'sha512'  # recommended: 'bcrypt'
HMAC_KEYS = {  # for bcrypt only
    #'2011-01-01': 'cheesecake',
}

## Tests
TEST_RUNNER = 'test_utils.runner.RadicalTestSuiteRunner'

## Celery

# True says to simulate background tasks without actually using celeryd.
# Good for local development in case celeryd is not running.
CELERY_ALWAYS_EAGER = True

BROKER_CONNECTION_TIMEOUT = 0.1
CELERY_RESULT_BACKEND = 'amqp'
CELERY_IGNORE_RESULT = True
CELERY_EAGER_PROPAGATES_EXCEPTIONS = True

# For absolute urls
try:
    DOMAIN = socket.gethostname()
except socket.error:
    DOMAIN = 'localhost'
PROTOCOL = "http://"
PORT = 80
<commit_msg>Adds django-mobility middleware and settings<commit_after># Django settings file for a project based on the playdoh template.
# import * into your settings_local.py
import logging
import os
import socket

from django.utils.functional import lazy

from .manage import ROOT, path

ROOT_PACKAGE = os.path.basename(ROOT)

# Is this a dev instance?
DEV = False

DEBUG = False
TEMPLATE_DEBUG = DEBUG

ADMINS = ()
MANAGERS = ADMINS

DATABASES = {}  # See settings_local.

# Site ID is used by Django's Sites framework.
SITE_ID = 1
# Logging
LOG_LEVEL = logging.DEBUG
HAS_SYSLOG = True
SYSLOG_TAG = "http_app_playdoh"  # Change this after you fork.
LOGGING_CONFIG = None
LOGGING = { }

## Internationalization.

# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# On Unix systems, a value of None will cause Django to use the same
# timezone as the operating system.
# If running in a Windows environment this must be set to the same as your
# system time zone.
TIME_ZONE = 'America/Los_Angeles'

# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True

# If you set this to False, Django will not format dates, numbers and
# calendars according to the current locale
USE_L10N = True

# Gettext text domain
TEXT_DOMAIN = 'messages'
STANDALONE_DOMAINS = [TEXT_DOMAIN, 'javascript']
TOWER_KEYWORDS = {'_lazy': None}
TOWER_ADD_HEADERS = True

# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en-US'

## Accepted locales

# On dev instances, the list of accepted locales defaults to the contents of
# the `locale` directory.  A localizer can add their locale in the l10n
# repository (copy of which is checked out into `locale`) in order to start
# testing the localization on the dev server.
try:
    DEV_LANGUAGES = [
        loc.replace('_', '-') for loc in os.listdir(path('locale'))
        if os.path.isdir(path('locale', loc)) and loc != 'templates'
    ]
except OSError:
    DEV_LANGUAGES = ('en-US',)

# On stage/prod, the list of accepted locales is manually maintained.  Only
# locales whose localizers have signed off on their work should be listed here.
PROD_LANGUAGES = (
    'en-US',
)

def lazy_lang_url_map():
    from django.conf import settings
    langs = settings.DEV_LANGUAGES if settings.DEV else settings.PROD_LANGUAGES
    return dict([(i.lower(), i) for i in langs])

LANGUAGE_URL_MAP = lazy(lazy_lang_url_map, dict)()

# Override Django's built-in with our native names
def lazy_langs():
    from django.conf import settings
    from product_details import product_details
    langs = DEV_LANGUAGES if settings.DEV else PROD_LANGUAGES
    return dict([(lang.lower(), product_details.languages[lang]['native'])
                 for lang in langs])

# Where to store product details etc.
PROD_DETAILS_DIR = path('lib/product_details_json')

LANGUAGES = lazy(lazy_langs, dict)()

# Paths that don't require a locale code in the URL.
SUPPORTED_NONLOCALES = ['media']

## Media and templates.

# Absolute path to the directory that holds media.
# Example: "/home/media/media.lawrence.com/"
MEDIA_ROOT = path('media')

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash if there is a path component (optional in other cases).
# Examples: "http://media.lawrence.com", "http://example.com/media/"
MEDIA_URL = '/media/'

# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
# trailing slash.
# Examples: "http://foo.com/media/", "/media/".
ADMIN_MEDIA_PREFIX = '/admin-media/'

# Make this unique, and don't share it with anybody.
SECRET_KEY = '1iz#v0m55@h26^m6hxk3a7at*h$qj_2a$juu1#nv50548j(x1v'

# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
#     'django.template.loaders.eggs.Loader',
)

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.contrib.auth.context_processors.auth',
    'django.core.context_processors.debug',
    'django.core.context_processors.media',
    'django.core.context_processors.request',
    'session_csrf.context_processor',
    'django.contrib.messages.context_processors.messages',
    'funfactory.context_processors.i18n',
    'funfactory.context_processors.globals',
    #'jingo_minify.helpers.build_ids',
)

TEMPLATE_DIRS = (
    path('templates'),
)

def JINJA_CONFIG():
    import jinja2
    from django.conf import settings
#    from caching.base import cache
    config = {'extensions': ['tower.template.i18n', 'jinja2.ext.do',
                             'jinja2.ext.with_', 'jinja2.ext.loopcontrols'],
              'finalize': lambda x: x if x is not None else ''}
#    if 'memcached' in cache.scheme and not settings.DEBUG:
        # We're passing the _cache object directly to jinja because
        # Django can't store binary directly; it enforces unicode on it.
        # Details: http://jinja.pocoo.org/2/documentation/api#bytecode-cache
        # and in the errors you get when you try it the other way.
#        bc = jinja2.MemcachedBytecodeCache(cache._cache,
#                                           "%sj2:" % settings.CACHE_PREFIX)
#        config['cache_size'] = -1 # Never clear the cache
#        config['bytecode_cache'] = bc
    return config

## Middlewares, apps, URL configs.

MIDDLEWARE_CLASSES = (
    'funfactory.middleware.LocaleURLMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'session_csrf.CsrfMiddleware',  # Must be after auth middleware.
    'django.contrib.messages.middleware.MessageMiddleware',
    'commonware.middleware.FrameOptionsHeader',
    'mobility.middleware.DetectMobileMiddleware',
    'mobility.middleware.XMobileMiddleware',
)

ROOT_URLCONF = '%s.urls' % ROOT_PACKAGE

INSTALLED_APPS = (
    # Local apps
    'funfactory',  # Content common to most playdoh-based apps.
    'jingo_minify',
    'tower',  # for ./manage.py extract (L10n)

    # We need this so the jsi18n view will pick up our locale directory.
    ROOT_PACKAGE,

    # Django contrib apps
    'django.contrib.auth',
    'django_sha2',  # Load after auth to monkey-patch it.
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    # 'django.contrib.sites',
    # 'django.contrib.messages',
    # Uncomment the next line to enable the admin:
    # 'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',

    # Third-party apps, patches, fixes
    'commonware.response.cookies',
    'djcelery',
    'django_nose',
    'session_csrf',

    # L10n
    'product_details',
)

# Tells the extract script what files to look for L10n in and what function
# handles the extraction. The Tower library expects this.
DOMAIN_METHODS = {
    'messages': [
        ('apps/**.py',
            'tower.management.commands.extract.extract_tower_python'),
        ('**/templates/**.html',
            'tower.management.commands.extract.extract_tower_template'),
    ],

    ## Use this if you have localizable HTML files:
    #'lhtml': [
    #    ('**/templates/**.lhtml',
    #        'tower.management.commands.extract.extract_tower_template'),
    #],

    ## Use this if you have localizable JS files:
    #'javascript': [
        # Make sure that this won't pull in strings from external libraries you
        # may use.
    #    ('media/js/**.js', 'javascript'),
    #],
}

# Path to Java. Used for compress_assets.
JAVA_BIN = '/usr/bin/java'

## Auth
PWD_ALGORITHM = 'sha512'  # recommended: 'bcrypt'
HMAC_KEYS = {  # for bcrypt only
    #'2011-01-01': 'cheesecake',
}

## Tests
TEST_RUNNER = 'test_utils.runner.RadicalTestSuiteRunner'

## Celery

# True says to simulate background tasks without actually using celeryd.
# Good for local development in case celeryd is not running.
CELERY_ALWAYS_EAGER = True

BROKER_CONNECTION_TIMEOUT = 0.1
CELERY_RESULT_BACKEND = 'amqp'
CELERY_IGNORE_RESULT = True
CELERY_EAGER_PROPAGATES_EXCEPTIONS = True

# For absolute urls
try:
    DOMAIN = socket.gethostname()
except socket.error:
    DOMAIN = 'localhost'
PROTOCOL = "http://"
PORT = 80

## django-mobility
MOBILE_COOKIE = 'mobile'
<|endoftext|>


<commit_before>from django.views.generic.edit import FormView
from django.views.generic import TemplateView, View
from django.http import HttpResponse
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.db import transaction

from bythebook.models import BookForm, AuthorForm, TopicForm, Author

class BookView(FormView):
    template_name = 'book.html'
    form_class = BookForm
    success_url = '/book-added'

    def form_valid(self, form):
        form.save(self.request, True)
        return super(BookView, self).form_valid(form)

class BookAddedView(TemplateView):
    template_name = 'book-added.html'

class BookManualView(View):
    form_class = BookForm
    template_name = 'manual.html'
    success_url = '/manual'

    def get(self, request):
        book = BookForm()
        topic = TopicForm()
        authors = []
        for ii in xrange(3):
            authors.append(AuthorForm())

        c = RequestContext(
            request,
            {'authors': authors,
             'book': book,
             'topic': topic})
        return render_to_response(self.template_name, c)

    @transaction.atomic
    def post(self, request):
        name = request.POST.get('name')
        print name

        authors = request.POST.getlist('author_name')
        titles =  request.POST.getlist('author_title')
        topic_name =  request.POST.getlist('topic_name')

        author_models = []
        for ii,author in enumerate(authors):
            author_name = author
            author_title = titles[ii]
            cur = {"author_name": author_name, "author_title": author_title}
            a = AuthorForm(cur)

            if a.is_valid():
                obj = Author.objects.filter(
                    name=author_name, title=author_title)
                if obj:
                    a_model = obj[0]
                else:
                    a_model = a.save(commit=False)
                author_models.append(a_model)
            else:
                print a.errors
            print "---"

        print author_models

        author_ids = []
        for a_model in author_models:
            a_model.save()
            author_ids.append(a_model.id)

        book_form = BookForm({"name": name, "authors": author_ids})
        if book_form.is_valid():
            book_model = book_form.save(commit=True)
        else:
            print book_form.errors

        print book_model.id
        topic_form = TopicForm(
            {"topic_name": topic_name[0], "topic_book": book_model.id})
        if topic_form.is_valid():
            topic_model = topic_form.save()
        else:
            print topic_form.errors

        return HttpResponse("Hi")

    def form_valid(self, form):
        print "form valid"
        return super(BookManualView, self).form_valid(form)

    def form_invalid(self, form):
        print "form not valid"
        return super(BookManualView, self).form_valid(form)
<commit_msg>get topic as a list<commit_after>from django.views.generic.edit import FormView
from django.views.generic import TemplateView, View
from django.http import HttpResponse
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.db import transaction

from bythebook.models import BookForm, AuthorForm, TopicForm, Author

class BookView(FormView):
    template_name = 'book.html'
    form_class = BookForm
    success_url = '/book-added'

    def form_valid(self, form):
        form.save(self.request, True)
        return super(BookView, self).form_valid(form)

class BookAddedView(TemplateView):
    template_name = 'book-added.html'

class BookManualView(View):
    form_class = BookForm
    template_name = 'manual.html'
    success_url = '/manual'

    def get(self, request):
        book = BookForm()
        topic = TopicForm()
        authors = []
        for ii in xrange(3):
            authors.append(AuthorForm())

        c = RequestContext(
            request,
            {'authors': authors,
             'book': book,
             'topic': topic})
        return render_to_response(self.template_name, c)

    @transaction.atomic
    def post(self, request):
        name = request.POST.get('name')
        print name

        authors = request.POST.getlist('author_name')
        titles =  request.POST.getlist('author_title')
        topic_name =  request.POST.get('topic_name')

        author_models = []
        for ii,author in enumerate(authors):
            author_name = author
            author_title = titles[ii]
            cur = {"author_name": author_name, "author_title": author_title}
            a = AuthorForm(cur)

            if a.is_valid():
                obj = Author.objects.filter(
                    name=author_name, title=author_title)
                if obj:
                    a_model = obj[0]
                else:
                    a_model = a.save(commit=False)
                author_models.append(a_model)
            else:
                print a.errors
            print "---"

        print author_models

        author_ids = []
        for a_model in author_models:
            a_model.save()
            author_ids.append(a_model.id)

        book_form = BookForm({"name": name, "authors": author_ids})
        if book_form.is_valid():
            book_model = book_form.save(commit=True)
        else:
            print book_form.errors

        print book_model.id
        topic_form = TopicForm(
            {"topic_name": topic_name, "topic_book": book_model.id})
        if topic_form.is_valid():
            topic_model = topic_form.save()
        else:
            print topic_form.errors

        return HttpResponse("Hi")

    def form_valid(self, form):
        print "form valid"
        return super(BookManualView, self).form_valid(form)

    def form_invalid(self, form):
        print "form not valid"
        return super(BookManualView, self).form_valid(form)
<|endoftext|>


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

"""
Furious context may be used to group a collection of Async tasks together.

Usage:

    with context.new() as current_context:
        # An explicitly constructed Async object may be passed in.
        async_job = Async(some_function,
                          [args, for, other],
                          {'kwargs': 'for', 'other': 'function'},
                          queue='workgroup')
        current_context.add(async_job)

        # Alternatively, add will construct an Async object when given
        # a function path or reference as the first argument.
        async_job = current_context.add(
            another_function,
            [args, for, other],
            {'kwargs': 'for', 'other': 'function'},
            queue='workgroup')

"""

from furious.context import _local
from furious.context.context import Context

from furious.context import _execution

from furious import errors

execution_context_from_async = _execution.execution_context_from_async

def new(**options):
    """Get a new furious context and add it to the registry."""

    new_context = Context(**options)

    _local.get_local_context().registry.append(new_context)

    return new_context

def get_current_async():
    """Return a reference to the currently executing Async job object
    or None if not in an Async job.
    """
    local_context = _local.get_local_context()

    if local_context._executing_async:
        return local_context._executing_async[-1]

    raise errors.NotInContextError('Not in an _ExecutionContext.')

def get_current_context():
    """Return a reference to the current Context object.
    """
    local_context = _local.get_local_context()

    if local_context.registry:
        return local_context.registry[-1]

    raise errors.NotInContextError('Not in a Context.')

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

"""
Furious context may be used to group a collection of Async tasks together.

Usage:

    with context.new() as current_context:
        # An explicitly constructed Async object may be passed in.
        async_job = Async(some_function,
                          [args, for, other],
                          {'kwargs': 'for', 'other': 'function'},
                          queue='workgroup')
        current_context.add(async_job)

        # Alternatively, add will construct an Async object when given
        # a function path or reference as the first argument.
        async_job = current_context.add(
            another_function,
            [args, for, other],
            {'kwargs': 'for', 'other': 'function'},
            queue='workgroup')

"""

from furious.context import _local
from furious.context.context import Context

from furious.context import _execution

from furious import errors

execution_context_from_async = _execution.execution_context_from_async

def new(**options):
    """Get a new furious context and add it to the registry."""

    new_context = Context(**options)

    _local.get_local_context().registry.append(new_context)

    return new_context

def get_current_async():
    """Return a reference to the currently executing Async job object
    or None if not in an Async job.
    """
    local_context = _local.get_local_context()

    if local_context._executing_async:
        return local_context._executing_async[-1]

    raise errors.NotInContextError('Not in an _ExecutionContext.')

def get_current_context():
    """Return a reference to the current Context object.
    """
    local_context = _local.get_local_context()

    if local_context.registry:
        return local_context.registry[-1]

    raise errors.NotInContextError('Not in a Context.')

def get_async_with_context():
    """Return a reference to the currently executing Async job object and it's
    triggering context. Return None for the async if not in an Async job and
    None for the context if the Async was not triggered within a context.
    """
    async = get_current_async()

    if not async:
        return None, None

    if not async.context_id:
        return async, None

    return async, Context.load(async.context_id)
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

# pyswip -- Python SWI-Prolog bridge
# Copyright (c) 2007-2018 Yüce Tekol
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

"""
Run several complex examples using pySwip. The main goal of these tests is
ensure stability in several platforms.
"""

import unittest

from pyswip import *

class TestExamples(unittest.TestCase):
    """
    Each test method is named after one example in $PYSWIP/examples.

    WARNING: Since it is not possible to unload things from the Prolog base, the
    examples have to be 'orthogonal'.

    """

    def test_create_term(self):
        """
        Simple example of term creation.
        """

        prolog = Prolog()

        a1 = PL_new_term_refs(2)
        a2 = a1 + 1
        t = PL_new_term_ref()
        ta = PL_new_term_ref()

        animal2 = PL_new_functor(PL_new_atom("animal"), 2)
        assertz = PL_new_functor(PL_new_atom("assertz"), 1)

        PL_put_atom_chars(a1, "gnu")
        PL_put_integer(a2, 50)
        PL_cons_functor_v(t, animal2, a1)
        PL_cons_functor_v(ta, assertz, t)
        PL_call(ta, None)

        result = list(prolog.query("animal(X,Y)", catcherrors=True))
        self.assertEqual(len(result), 1)
        self.assertEqual(result[0], {'X': 'gnu', 'Y': 50})

    def test_knowledgebase(self):
        """
        Tests usage of modules.
        """

        p = Prolog()

        assertz = Functor("assertz")
        parent = Functor("parent", 2)
        test1 = newModule("test1")
        test2 = newModule("test2")

        call(assertz(parent("john", "bob")), module=test1)
        call(assertz(parent("jane", "bob")), module=test1)

        call(assertz(parent("mike", "bob")), module=test2)
        call(assertz(parent("gina", "bob")), module=test2)

        # Test knowledgebase module test1

        result = set()
        X = Variable()
        q = Query(parent(X, "bob"), module=test1)
        while q.nextSolution():
            result.add(X.value.value)    # X.value is an Atom
        q.closeQuery()
        self.assertEqual(result, set(["john", "jane"]))

        # Test knowledgebase module test2

        result = set()
        q = Query(parent(X, "bob"), module=test2)
        while q.nextSolution():
            result.add(X.value.value)
        q.closeQuery()
        self.assertEqual(result, set(["mike", "gina"]))

    def test_father(self):
        """
        Tests basic inferences.
        """

        p = Prolog()

        father = Functor("father", 2)
        mother = Functor("mother", 2)

        p.assertz("father(john,mich)")
        p.assertz("father(john,gina)")
        p.assertz("mother(jane,mich)")

        X = Variable()
        Y = Variable()
        Z = Variable()

        result = []
        q = Query(father("john", Y), mother(Z, Y))
        while q.nextSolution():
            y = Y.value.value
            z = Z.value.value
            result.append({'Y': y, 'Z': z})
        q.closeQuery()

        self.assertEqual(len(result), 1)
        self.assertEqual(result[0], {'Y': 'mich', 'Z': 'jane'})

        # Repeat the same query but using strings
        result = []
        for s in p.query("father(john,Y),mother(Z,Y)"):
            result.append(s)
        self.assertEqual(len(result), 1)
        self.assertEqual(result[0], {'Y': 'mich', 'Z': 'jane'})

    def test_coins(self):
        """
        Runs the coins example (uses clp library of SWI-Prolog).
        """

        prolog = Prolog()
        prolog.consult(example_path("coins/coins.pl"))
        count = 100
        total = 500
        coins = Functor("coins", 3)
        S = Variable()
        q = Query(coins(S, count, total))

        solutions = []
        while q.nextSolution():
            solutions.append(S.value)
        q.closeQuery()

        self.assertEqual(len(solutions), 105)

        # Now do the same test, but using the prolog.query interface
        solutions = list(prolog.query("coins(S, %d, %d)." % (count,total)))
        self.assertEqual(len(solutions), 105)

    def test_draughts(self):
        """
        Runs the draughts example (uses clp library of SWI-Prolog).
        """

        prolog = Prolog()
        prolog.consult(example_path("draughts/puzzle1.pl"))
        solutions = []
        for soln in prolog.query("solve(B)."):
            solutions.append(soln["B"])
        self.assertEqual(len(solutions), 37)

        # Now do the same test, but using the prolog.query interface
        solutions = list(prolog.query("solve(B)."))
        self.assertEqual(len(solutions), 37)

    def test_hanoi(self):
        """
        Runs the hanoi example.
        """

        N = 3  # Number of disks

        result = []
        def notify(t):
            result.append((t[0].value, t[1].value))
        notify.arity = 1

        prolog = Prolog()
        registerForeign(notify)
        prolog.consult(example_path("hanoi/hanoi.pl"))
        list(prolog.query("hanoi(%d)" % N)) # Forces the query to run completely

        self.assertEqual(len(result), 7)
        self.assertEqual(result[0], ('left', 'right'))
        self.assertEqual(result[1], ('left', 'center'))
        self.assertEqual(result[2], ('right', 'center'))

    def test_sendmoremoney(self):
        """
        Runs the sendmoremoney example::

            S E N D
            M O R E
          + -------
          M O N E Y

        So, what should be the values of S, E, N, D, M, O, R, Y
        if they are all distinct digits.
        """

        letters = "S E N D M O R Y".split()
        prolog = Prolog()
        sendmore = Functor("sendmore")
        prolog.consult(example_path("sendmoremoney/money.pl"))

        X = Variable()
        call(sendmore(X))
        r = X.value
        val = {}
        for i, letter in enumerate(letters):
            val[letter] = r[i]

        self.assertEqual(len(val), 8)

        send = val['S']*1e3 + val['E']*1e2 + val['N']*1e1 + val['D']*1e0
        more = val['M']*1e3 + val['O']*1e2 + val['R']*1e1 + val['E']*1e0
        money = val['M']*1e4 + val['O']*1e3 + val['N']*1e2 + val['E']*1e1 + val['Y']*1e0
        self.assertEqual(money, send + more)

    def test_sudoku(self):
        """
        Runs the sudoku example (uses clp library of SWI-Prolog).
        """

        _ = 0
        puzzle1 = [
            [_,6,_,1,_,4,_,5,_],
            [_,_,8,3,_,5,6,_,_],
            [2,_,_,_,_,_,_,_,1],
            [8,_,_,4,_,7,_,_,6],
            [_,_,6,_,_,_,3,_,_],
            [7,_,_,9,_,1,_,_,4],
            [5,_,_,_,_,_,_,_,2],
            [_,_,7,2,_,6,9,_,_],
            [_,4,_,5,_,8,_,7,_]
            ]

        puzzle2 = [
            [_,_,1,_,8,_,6,_,4],
            [_,3,7,6,_,_,_,_,_],
            [5,_,_,_,_,_,_,_,_],
            [_,_,_,_,_,5,_,_,_],
            [_,_,6,_,1,_,8,_,_],
            [_,_,_,4,_,_,_,_,_],
            [_,_,_,_,_,_,_,_,3],
            [_,_,_,_,_,7,5,2,_],
            [8,_,2,_,9,_,7,_,_]
                  ]

        prolog = Prolog()
        prolog.consult(example_path("sudoku/sudoku.pl"))

        for i, problem in enumerate((puzzle1, puzzle2)):
            p = str(problem).replace("0", "_")
            result = list(prolog.query("L=%s,sudoku(L)" % p, maxresult=1))
            if result:
                # Does a simple check on the result
                result = result[0]
                for j, line in enumerate(result["L"]):
                    self.assertEqual(len(set(line)), 9,
                                     "Failure in line %d: %s" % (j, line))
            else:
                self.fail("Failed while running example number %d" % i)

def example_path(path):
    import os.path
    return os.path.normpath(os.path.join(os.path.split(os.path.abspath(__file__))[0], "..", "examples", path))

<commit_msg>Test added to check large db crash<commit_after># -*- coding: utf-8 -*-

# pyswip -- Python SWI-Prolog bridge
# Copyright (c) 2007-2018 Yüce Tekol
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

"""
Run several complex examples using pySwip. The main goal of these tests is
ensure stability in several platforms.
"""

import unittest

from pyswip import *

class TestExamples(unittest.TestCase):
    """
    Each test method is named after one example in $PYSWIP/examples.

    WARNING: Since it is not possible to unload things from the Prolog base, the
    examples have to be 'orthogonal'.

    """

    def test_create_term(self):
        """
        Simple example of term creation.
        """

        prolog = Prolog()

        a1 = PL_new_term_refs(2)
        a2 = a1 + 1
        t = PL_new_term_ref()
        ta = PL_new_term_ref()

        animal2 = PL_new_functor(PL_new_atom("animal"), 2)
        assertz = PL_new_functor(PL_new_atom("assertz"), 1)

        PL_put_atom_chars(a1, "gnu")
        PL_put_integer(a2, 50)
        PL_cons_functor_v(t, animal2, a1)
        PL_cons_functor_v(ta, assertz, t)
        PL_call(ta, None)

        result = list(prolog.query("animal(X,Y)", catcherrors=True))
        self.assertEqual(len(result), 1)
        self.assertEqual(result[0], {'X': 'gnu', 'Y': 50})

    def test_knowledgebase(self):
        """
        Tests usage of modules.
        """

        p = Prolog()

        assertz = Functor("assertz")
        parent = Functor("parent", 2)
        test1 = newModule("test1")
        test2 = newModule("test2")

        call(assertz(parent("john", "bob")), module=test1)
        call(assertz(parent("jane", "bob")), module=test1)

        call(assertz(parent("mike", "bob")), module=test2)
        call(assertz(parent("gina", "bob")), module=test2)

        # Test knowledgebase module test1

        result = set()
        X = Variable()
        q = Query(parent(X, "bob"), module=test1)
        while q.nextSolution():
            result.add(X.value.value)    # X.value is an Atom
        q.closeQuery()
        self.assertEqual(result, set(["john", "jane"]))

        # Test knowledgebase module test2

        result = set()
        q = Query(parent(X, "bob"), module=test2)
        while q.nextSolution():
            result.add(X.value.value)
        q.closeQuery()
        self.assertEqual(result, set(["mike", "gina"]))

    def test_father(self):
        """
        Tests basic inferences.
        """

        p = Prolog()

        father = Functor("father", 2)
        mother = Functor("mother", 2)

        p.assertz("father(john,mich)")
        p.assertz("father(john,gina)")
        p.assertz("mother(jane,mich)")

        X = Variable()
        Y = Variable()
        Z = Variable()

        result = []
        q = Query(father("john", Y), mother(Z, Y))
        while q.nextSolution():
            y = Y.value.value
            z = Z.value.value
            result.append({'Y': y, 'Z': z})
        q.closeQuery()

        self.assertEqual(len(result), 1)
        self.assertEqual(result[0], {'Y': 'mich', 'Z': 'jane'})

        # Repeat the same query but using strings
        result = []
        for s in p.query("father(john,Y),mother(Z,Y)"):
            result.append(s)
        self.assertEqual(len(result), 1)
        self.assertEqual(result[0], {'Y': 'mich', 'Z': 'jane'})

    def test_coins(self):
        """
        Runs the coins example (uses clp library of SWI-Prolog).
        """

        prolog = Prolog()
        prolog.consult(example_path("coins/coins.pl"))
        count = 100
        total = 500
        coins = Functor("coins", 3)
        S = Variable()
        q = Query(coins(S, count, total))

        solutions = []
        while q.nextSolution():
            solutions.append(S.value)
        q.closeQuery()

        self.assertEqual(len(solutions), 105)

        # Now do the same test, but using the prolog.query interface
        solutions = list(prolog.query("coins(S, %d, %d)." % (count,total)))
        self.assertEqual(len(solutions), 105)

    def test_draughts(self):
        """
        Runs the draughts example (uses clp library of SWI-Prolog).
        """

        prolog = Prolog()
        prolog.consult(example_path("draughts/puzzle1.pl"))
        solutions = []
        for soln in prolog.query("solve(B)."):
            solutions.append(soln["B"])
        self.assertEqual(len(solutions), 37)

        # Now do the same test, but using the prolog.query interface
        solutions = list(prolog.query("solve(B)."))
        self.assertEqual(len(solutions), 37)

    def test_hanoi(self):
        """
        Runs the hanoi example.
        """

        N = 3  # Number of disks

        result = []
        def notify(t):
            result.append((t[0].value, t[1].value))
        notify.arity = 1

        prolog = Prolog()
        registerForeign(notify)
        prolog.consult(example_path("hanoi/hanoi.pl"))
        list(prolog.query("hanoi(%d)" % N)) # Forces the query to run completely

        self.assertEqual(len(result), 7)
        self.assertEqual(result[0], ('left', 'right'))
        self.assertEqual(result[1], ('left', 'center'))
        self.assertEqual(result[2], ('right', 'center'))

    def test_sendmoremoney(self):
        """
        Runs the sendmoremoney example::

            S E N D
            M O R E
          + -------
          M O N E Y

        So, what should be the values of S, E, N, D, M, O, R, Y
        if they are all distinct digits.
        """

        letters = "S E N D M O R Y".split()
        prolog = Prolog()
        sendmore = Functor("sendmore")
        prolog.consult(example_path("sendmoremoney/money.pl"))

        X = Variable()
        call(sendmore(X))
        r = X.value
        val = {}
        for i, letter in enumerate(letters):
            val[letter] = r[i]

        self.assertEqual(len(val), 8)

        send = val['S']*1e3 + val['E']*1e2 + val['N']*1e1 + val['D']*1e0
        more = val['M']*1e3 + val['O']*1e2 + val['R']*1e1 + val['E']*1e0
        money = val['M']*1e4 + val['O']*1e3 + val['N']*1e2 + val['E']*1e1 + val['Y']*1e0
        self.assertEqual(money, send + more)

    def test_sudoku(self):
        """
        Runs the sudoku example (uses clp library of SWI-Prolog).
        """

        _ = 0
        puzzle1 = [
            [_,6,_,1,_,4,_,5,_],
            [_,_,8,3,_,5,6,_,_],
            [2,_,_,_,_,_,_,_,1],
            [8,_,_,4,_,7,_,_,6],
            [_,_,6,_,_,_,3,_,_],
            [7,_,_,9,_,1,_,_,4],
            [5,_,_,_,_,_,_,_,2],
            [_,_,7,2,_,6,9,_,_],
            [_,4,_,5,_,8,_,7,_]
            ]

        puzzle2 = [
            [_,_,1,_,8,_,6,_,4],
            [_,3,7,6,_,_,_,_,_],
            [5,_,_,_,_,_,_,_,_],
            [_,_,_,_,_,5,_,_,_],
            [_,_,6,_,1,_,8,_,_],
            [_,_,_,4,_,_,_,_,_],
            [_,_,_,_,_,_,_,_,3],
            [_,_,_,_,_,7,5,2,_],
            [8,_,2,_,9,_,7,_,_]
                  ]

        prolog = Prolog()
        prolog.consult(example_path("sudoku/sudoku.pl"))

        for i, problem in enumerate((puzzle1, puzzle2)):
            p = str(problem).replace("0", "_")
            result = list(prolog.query("L=%s,sudoku(L)" % p, maxresult=1))
            if result:
                # Does a simple check on the result
                result = result[0]
                for j, line in enumerate(result["L"]):
                    self.assertEqual(len(set(line)), 9,
                                     "Failure in line %d: %s" % (j, line))
            else:
                self.fail("Failed while running example number %d" % i)

    def test_large_db(self):
        """
        Generates a large database, then runs query
        """

        num_facts = 1250000
        prolog = Prolog()
        _ = [prolog.assertz(f'p({i})') for i in range(num_facts)]

        results = [r for r in prolog.query('p(I)')]
        self.assertEqual(len(results), num_facts)

def example_path(path):
    import os.path
    return os.path.normpath(os.path.join(os.path.split(os.path.abspath(__file__))[0], "..", "examples", path))

<|endoftext|>


<commit_before>import sys, os
sys.path.insert(1, "../../../")
import h2o
from tests import pyunit_utils
from h2o.estimators.glrm import H2OGeneralizedLowRankEstimator
from random import randint
import re

def glrm_mojo():
    h2o.remove_all()
    NTESTROWS = 200    # number of test dataset rows
    df = pyunit_utils.random_dataset("regression")       # generate random dataset
    train = df[NTESTROWS:, :]
    test = df[:NTESTROWS, :]
    x = df.names

    transform_types = ["NONE", "STANDARDIZE", "NORMALIZE", "DEMEAN", "DESCALE"]
    transformN = transform_types[randint(0, len(transform_types)-1)]
    # build a GLRM model with random dataset generated earlier
    glrmModel = H2OGeneralizedLowRankEstimator(k=3, transform=transformN, max_iterations=10)
    glrmModel.train(x=x, training_frame=train)
    glrmTrainFactor = h2o.get_frame(glrmModel._model_json['output']['representation_name'])

    assert glrmTrainFactor.nrows==train.nrows, \
        "X factor row number {0} should equal training row number {1}.".format(glrmTrainFactor.nrows, train.nrows)
    save_GLRM_mojo(glrmModel) # ave mojo model

    MOJONAME = pyunit_utils.getMojoName(glrmModel._id)
    TMPDIR = os.path.normpath(os.path.join(os.path.dirname(os.path.realpath('__file__')), "..", "results", MOJONAME))
    h2o.download_csv(test[x], os.path.join(TMPDIR, 'in.csv'))  # save test file, h2o predict/mojo use same file
    pred_h2o, pred_mojo = pyunit_utils.mojo_predict(glrmModel, TMPDIR, MOJONAME, glrmReconstruct=True) # save mojo predict

    h2o.save_model(glrmModel, TMPDIR)   # save GLRM model
    glrmModel2 = h2o.load_model(os.path.join(TMPDIR,MOJONAME))
    predict_model = glrmModel2.predict(test)
    for col in range(pred_h2o.ncols):
        if pred_h2o[col].isfactor():
            pred_h2o[col] = pred_h2o[col].asnumeric()
            predict_model[col] = predict_model[col].asnumeric()
    print("Comparing mojo predict and h2o predict...")
    pyunit_utils.compare_frames_local(pred_h2o, pred_mojo, 1, tol=1e-10)
    print("Comparing mojo predict and h2o predict from saved model...")
    pyunit_utils.compare_frames_local(pred_mojo, predict_model, 1, tol=1e-10)
    frameID, mojoXFactor = pyunit_utils.mojo_predict(glrmModel, TMPDIR, MOJONAME, glrmReconstruct=False) # save mojo XFactor
    glrmTestFactor = h2o.get_frame("GLRMLoading_"+frameID)   # store the x Factor for new test dataset
    print("Comparing mojo x Factor and model x Factor ...")
    pyunit_utils.compare_frames_local(glrmTestFactor, mojoXFactor, 1, tol=1e-10)

def save_GLRM_mojo(model):
    # save model
    regex = re.compile("[+\\-* !@#$%^&()={}\\[\\]|;:'\"<>,.?/]")
    MOJONAME = regex.sub("_", model._id)

    print("Downloading Java prediction model code from H2O")
    TMPDIR = os.path.normpath(os.path.join(os.path.dirname(os.path.realpath('__file__')), "..", "results", MOJONAME))
    os.makedirs(TMPDIR)
    model.download_mojo(path=TMPDIR)    # save mojo

if __name__ == "__main__":
    pyunit_utils.standalone_test(glrm_mojo)
else:
    glrm_mojo()
<commit_msg>fix seeds for data generation and model building. (#3086)<commit_after>import sys, os
sys.path.insert(1, "../../../")
import h2o
from tests import pyunit_utils
from h2o.estimators.glrm import H2OGeneralizedLowRankEstimator
from random import randint
import re

def glrm_mojo():
    h2o.remove_all()
    NTESTROWS = 200    # number of test dataset rows
    df = pyunit_utils.random_dataset("regression", seed=1234)       # generate random dataset
    train = df[NTESTROWS:, :]
    test = df[:NTESTROWS, :]
    x = df.names

    transform_types = ["NONE", "STANDARDIZE", "NORMALIZE", "DEMEAN", "DESCALE"]
    transformN = transform_types[randint(0, len(transform_types)-1)]

    # build a GLRM model with random dataset generated earlier
    glrmModel = H2OGeneralizedLowRankEstimator(k=3, transform=transformN, max_iterations=10, seed=1234)
    glrmModel.train(x=x, training_frame=train)
    glrmTrainFactor = h2o.get_frame(glrmModel._model_json['output']['representation_name'])

    assert glrmTrainFactor.nrows==train.nrows, \
        "X factor row number {0} should equal training row number {1}.".format(glrmTrainFactor.nrows, train.nrows)
    save_GLRM_mojo(glrmModel) # ave mojo model

    MOJONAME = pyunit_utils.getMojoName(glrmModel._id)
    TMPDIR = os.path.normpath(os.path.join(os.path.dirname(os.path.realpath('__file__')), "..", "results", MOJONAME))
    h2o.download_csv(test[x], os.path.join(TMPDIR, 'in.csv'))  # save test file, h2o predict/mojo use same file
    pred_h2o, pred_mojo = pyunit_utils.mojo_predict(glrmModel, TMPDIR, MOJONAME, glrmReconstruct=True) # save mojo predict

    h2o.save_model(glrmModel, TMPDIR)   # save GLRM model
    glrmModel2 = h2o.load_model(os.path.join(TMPDIR,MOJONAME))
    predict_model = glrmModel2.predict(test)
    for col in range(pred_h2o.ncols):
        if pred_h2o[col].isfactor():
            pred_h2o[col] = pred_h2o[col].asnumeric()
            predict_model[col] = predict_model[col].asnumeric()
    print("Comparing mojo predict and h2o predict...")
    pyunit_utils.compare_frames_local(pred_h2o, pred_mojo, 1, tol=1e-10)
    print("Comparing mojo predict and h2o predict from saved model...")
    pyunit_utils.compare_frames_local(pred_mojo, predict_model, 1, tol=1e-10)
    frameID, mojoXFactor = pyunit_utils.mojo_predict(glrmModel, TMPDIR, MOJONAME, glrmReconstruct=False) # save mojo XFactor
    glrmTestFactor = h2o.get_frame("GLRMLoading_"+frameID)   # store the x Factor for new test dataset
    print("Comparing mojo x Factor and model x Factor ...")
    pyunit_utils.compare_frames_local(glrmTestFactor, mojoXFactor, 1, tol=1e-10)

def save_GLRM_mojo(model):
    # save model
    regex = re.compile("[+\\-* !@#$%^&()={}\\[\\]|;:'\"<>,.?/]")
    MOJONAME = regex.sub("_", model._id)

    print("Downloading Java prediction model code from H2O")
    TMPDIR = os.path.normpath(os.path.join(os.path.dirname(os.path.realpath('__file__')), "..", "results", MOJONAME))
    os.makedirs(TMPDIR)
    model.download_mojo(path=TMPDIR)    # save mojo

if __name__ == "__main__":
    pyunit_utils.standalone_test(glrm_mojo)
else:
    glrm_mojo()
<|endoftext|>


<commit_before>import matplotlib
matplotlib.use('Agg')

import traceback, sys
from os import path
from glob import glob

def test_examples():
    print example_files()
    for path in example_files():
        yield check_example, path

def example_files():
    import pymc
    d = path.dirname(pymc.__file__)

    return glob(path.join(d, "../examples/*.py"))

def check_example(path):
    with open(path) as f :
        c = compile(f.read(), path, 'exec')
        exec(c)
<commit_msg>examples now found recursively<commit_after>import matplotlib
matplotlib.use('Agg')

import os
from os import path
from glob import glob
import fnmatch

def test_examples():
    for path in all_matching_files(example_dir(), '*.py'):
        yield check_example, path

def all_matching_files(d, pattern):

    def addfiles(fls, dir, nfiles):
        nfiles = fnmatch.filter(nfiles, pattern)
        nfiles = [path.join(dir,f) for f in nfiles]
        fls.extend(nfiles)

    files = []
    path.walk(d, addfiles, files)
    return files

def example_dir():
    import pymc
    d = path.dirname(pymc.__file__)

    return path.join(d, '../examples/')

def check_example(p):
    with open(p) as f :
        #c = compile(f.read(), p, 'exec')
        #exec(c)
        os.chdir(path.dirname(p))
        execfile(path.basename(p))
<|endoftext|>


<commit_before># encoding: utf-8

import unittest
import pystache

from examples.comments import Comments
from examples.double_section import DoubleSection
from examples.escaped import Escaped
from examples.unescaped import Unescaped
from examples.template_partial import TemplatePartial
from examples.delimiters import Delimiters
from examples.unicode_output import UnicodeOutput
from examples.unicode_input import UnicodeInput
from examples.nested_context import NestedContext

class TestView(unittest.TestCase):
    def test_comments(self):
        self.assertEquals(Comments().render(), """
A Comedy of Errors

""")

    def test_double_section(self):
        self.assertEquals(DoubleSection().render(), """* first
* second
* third""")

    def test_unicode_output(self):
        self.assertEquals(UnicodeOutput().render(), u'
Name: Henri Poincaré
')

    def test_encoded_output(self):
        self.assertEquals(UnicodeOutput().render('utf8'), '
Name: Henri Poincar\xc3\xa9
')

    def test_unicode_input(self):
        self.assertEquals(UnicodeInput().render(),
            u'
If alive today, Henri Poincaré would be 156 years old.
')

    def test_escaped(self):
        self.assertEquals(Escaped().render(), "
Bear > Shark
")

    def test_unescaped(self):
        self.assertEquals(Unescaped().render(), "
Bear > Shark
")

    def test_unescaped_sigil(self):
        view = Escaped(template="
{{& thing}}
", context={
                'thing': 'Bear > Giraffe'
                })
        self.assertEquals(view.render(), "
Bear > Giraffe
")

    def test_template_partial(self):
        self.assertEquals(TemplatePartial().render(), """
Welcome

Again, Welcome!""")

    def test_template_partial_extension(self):
        view = TemplatePartial()
        view.template_extension = 'txt'
        self.assertEquals(view.render(), """Welcome
-------

Again, Welcome!
""")

    def test_delimiters(self):
        self.assertEquals(Delimiters().render(), """
* It worked the first time.

* And it worked the second time.

* Then, surprisingly, it worked the third time.
""")

    def test_nested_context(self):
        self.assertEquals(NestedContext().render(), "one and foo and two")

if __name__ == '__main__':
    unittest.main()
<commit_msg>Testing for section/partial bug<commit_after># encoding: utf-8

import unittest
import pystache

from examples.comments import Comments
from examples.double_section import DoubleSection
from examples.escaped import Escaped
from examples.unescaped import Unescaped
from examples.template_partial import TemplatePartial
from examples.delimiters import Delimiters
from examples.unicode_output import UnicodeOutput
from examples.unicode_input import UnicodeInput
from examples.nested_context import NestedContext
from examples.partial_section import PartialSection

class TestView(unittest.TestCase):
    def test_comments(self):
        self.assertEquals(Comments().render(), """
A Comedy of Errors

""")

    def test_double_section(self):
        self.assertEquals(DoubleSection().render(), """* first
* second
* third""")

    def test_unicode_output(self):
        self.assertEquals(UnicodeOutput().render(), u'
Name: Henri Poincaré
')

    def test_encoded_output(self):
        self.assertEquals(UnicodeOutput().render('utf8'), '
Name: Henri Poincar\xc3\xa9
')

    def test_unicode_input(self):
        self.assertEquals(UnicodeInput().render(),
            u'
If alive today, Henri Poincaré would be 156 years old.
')

    def test_escaped(self):
        self.assertEquals(Escaped().render(), "
Bear > Shark
")

    def test_unescaped(self):
        self.assertEquals(Unescaped().render(), "
Bear > Shark
")

    def test_unescaped_sigil(self):
        view = Escaped(template="
{{& thing}}
", context={
                'thing': 'Bear > Giraffe'
                })
        self.assertEquals(view.render(), "
Bear > Giraffe
")

    def test_template_partial(self):
        self.assertEquals(TemplatePartial().render(), """
Welcome

Again, Welcome!""")

    def test_template_partial_extension(self):
        view = TemplatePartial()
        view.template_extension = 'txt'
        self.assertEquals(view.render(), """Welcome
-------

## Again, Welcome! ##

""")

    def test_delimiters(self):
        self.assertEquals(Delimiters().render(), """
* It worked the first time.

* And it worked the second time.

* Then, surprisingly, it worked the third time.
""")

    def test_partial_sections(self):
        view = PartialSection()
        self.assertEquals(view.render(), """Welcome, we're loading partials
This is item aThis is item b""")

    def test_nested_context(self):
        self.assertEquals(NestedContext().render(), "one and foo and two")

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


<commit_before>import matplotlib
matplotlib.use('Agg')

import os
from os import path
from glob import glob
import fnmatch

def test_examples():
    for path in all_matching_files(example_dir(), '*.py'):
        yield check_example, path

def all_matching_files(d, pattern):

    def addfiles(fls, dir, nfiles):
        nfiles = fnmatch.filter(nfiles, pattern)
        nfiles = [path.join(dir,f) for f in nfiles]
        fls.extend(nfiles)

    files = []
    path.walk(d, addfiles, files)
    return files

def example_dir():
    import pymc
    d = path.dirname(pymc.__file__)

    return path.join(d, '../examples/')

def check_example(p):
    with open(p) as f :
        #c = compile(f.read(), p, 'exec')
        #exec(c)
        os.chdir(path.dirname(p))
        execfile(path.basename(p))
<commit_msg>changed how examples are run<commit_after>import matplotlib
matplotlib.use('Agg')

from os import path
import fnmatch
import imp

def test_examples():
    for path in all_matching_files(example_dir(), '*.py'):
        yield check_example, path

def all_matching_files(d, pattern):

    def addfiles(fls, dir, nfiles):
        nfiles = fnmatch.filter(nfiles, pattern)
        nfiles = [path.join(dir,f) for f in nfiles]
        fls.extend(nfiles)

    files = []
    path.walk(d, addfiles, files)
    return files

def example_dir():
    import pymc
    d = path.dirname(pymc.__file__)

    return path.join(d, '../examples/')

def check_example(p):
    imp.load_source('__main__', p)

<|endoftext|>