Пример #1
0
def test_execute():
    """Test for execution,

    with unexisting ID and test ID
    """
    default_symbol = '0'
    assert executor.execute('-1') is False
    assert executor.execute(default_symbol) is True
Пример #2
0
def test_execute():
    """Test for execution,

    with unexisting ID and test ID
    """
    default_symbol = '0'
    assert executor.execute('-1') is False
    assert executor.execute(default_symbol) is True
Пример #3
0
 def test_count_all(self):
     """
     Test simply counting everything.
     """
     query = tree([Count(), [MemScan(_RECORDS)]])
     result = list(execute(query))
     self.assertListEqual(result, [tuple([10])])
Пример #4
0
def send_points_to_interpreter(signal_list, learning_mode, classifier):
    """Interpret the signals from the signal list.

    It prints the first 10 signals from the signal_list. All of them are
    points.

    If the symbol has been recognized by the classifier module
    then the command_id is passed to executor which tries to find and run an
    appropriate command.

    Args:
        signal_list (list): List of signals captured from the touchpad.
        learning_mode (bool): Tells if it is a learning session or not.
        classifier (Classifier): The Classifier class object.

    """
    if not signal_list:
        return
    point_on_the_list = False
    for one_signal in signal_list:
        if one_signal.is_proper_signal_of_point():
            point_on_the_list = True
    if not point_on_the_list:
        return

    print()
    print("New portion of events:")
    counter = 0
    length = len(signal_list)
    for one_signal in signal_list:
        counter += 1
        if counter == 11:
            print("...")
            break
        print("Event #", counter, "\tout of", length, "in the list. x:",
              one_signal.get_x(), "y:", one_signal.get_y())
    print()

    if learning_mode:
        classifier.add_to_training_set(signal_list)
    else:
        item = classifier.classify(signal_list)
        if item is not None:
            print("execution")
            executor.execute(item)
        else:
            print("not similar to any symbol")
Пример #5
0
def send_points_to_interpreter(signal_list, learning_mode, classifier):
    """Interpret the signals from the signal list.

    It prints the first 10 signals from the signal_list. All of them are
    points.

    If the symbol has been recognized by the classifier module
    then the command_id is passed to executor which tries to find and run an
    appropriate command.

    Args:
        signal_list (list): List of signals captured from the touchpad.
        learning_mode (bool): Tells if it is a learning session or not.
        classifier (Classifier): The Classifier class object.

    """
    if not signal_list:
        return
    point_on_the_list = False
    for one_signal in signal_list:
        if one_signal.is_proper_signal_of_point():
            point_on_the_list = True
    if not point_on_the_list:
        return

    print()
    print("New portion of events:")
    counter = 0
    length = len(signal_list)
    for one_signal in signal_list:
        counter += 1
        if counter == 11:
            print("...")
            break
        print("Event #", counter, "\tout of", length, "in the list. x:",
              one_signal.get_x(), "y:", one_signal.get_y())
    print()

    if learning_mode:
        classifier.add_to_training_set(signal_list)
    else:
        item = classifier.classify(signal_list)
        if item is not None:
            print("execution")
            executor.execute(item)
        else:
            print("not similar to any symbol")
Пример #6
0
 def test_distinct_sorted(self):
     """
     Typical case: expect input to be sorted.
     """
     records = [tuple([n]) for n in (0, 0, 1, 2, 2, 2, 3, 3)]
     query = tree([Distinct(), [MemScan(records)]])
     result = list(execute(query))
     self.assertListEqual(result, [tuple([n]) for n in (0, 1, 2, 3)])
Пример #7
0
 def test_average_all(self):
     """
     Find the average of all numbers 0..9
     """
     query = tree(
         [Average(), [Projection(lambda r: [r[0]]), [MemScan(_RECORDS)]]])
     result = list(execute(query))
     self.assertListEqual(result, [tuple([4.5])])
Пример #8
0
 def test_distinct_unsorted(self):
     """
     Distinct is not expected to handle out-of-order repeated values.
     """
     records = [tuple([n]) for n in (0, 1, 1, 0, 0, 1, 1, 1)]
     query = tree([Distinct(), [MemScan(records)]])
     result = list(execute(query))
     self.assertListEqual(result, [tuple([n]) for n in (0, 1, 0, 1)])
Пример #9
0
 def test_count_selected(self):
     """
     Test counting the output of a selection.
     """
     for p in _PREDICATES:
         query = tree([Count(), [Selection(p), [MemScan(_RECORDS)]]])
         result = list(execute(query))
         expected = [tuple([len([r for r in _RECORDS if p(r)])])]
         self.assertListEqual(result, expected)
Пример #10
0
 def test_cartesian_product(self):
     """
     When the theta function simply returns True, we achieve cross product.
     """
     query = tree(
         [NestedLoopsJoin(lambda r, s: True),
             [MemScan(self.R_RECORDS)],
             [MemScan(self.S_RECORDS)]])
     result = list(execute(query))
     expected = [r + s for r in self.R_RECORDS for s in self.S_RECORDS]
     self.assertListEqual(result, expected)
Пример #11
0
 def test_self_join(self):
     """
     It's fine for a table to be joined to itself.
     """
     query = tree(
         [NestedLoopsJoin(lambda r, s: True),
             [MemScan(self.R_RECORDS)],
             [MemScan(self.R_RECORDS)]])
     result = list(execute(query))
     expected = [r + s for r in self.R_RECORDS for s in self.R_RECORDS]
     self.assertListEqual(result, expected)
Пример #12
0
 def test_project_various(self):
     mapping_functions = [
         lambda r: [r[1]],  # project to a single column
         lambda r: [r[1], r[0]],  # switch columns
         lambda r: [r[0] + r[1]],  # sums of fields
         lambda r: [r[1] > 8],  # results of boolean operations
     ]
     records = [(n, n * n) for n in range(10)]
     for f in mapping_functions:
         query = tree([Projection(f), [MemScan(records)]])
         result = list(execute(query))
         expected = [tuple(f(r)) for r in records]
         self.assertListEqual(result, expected)
Пример #13
0
 def test_average_selected(self):
     """
     Find the average of only filtered numbers in the range 0..9
     """
     query = tree([
         Average(),
         [
             Projection(lambda r: [r[0]]),
             [Selection(lambda r: r[0] % 2 == 1), [MemScan(_RECORDS)]]
         ]
     ])
     result = list(execute(query))
     self.assertListEqual(result, [tuple([5])])
Пример #14
0
 def test_select_various_predicates(self):
     predicates = [
         lambda r: r[0] % 2 == 1,  # odd values
         lambda r: r[1] % 2 == 1,  # odd squares
         lambda r: r[0] + r[1] == 30,  # just (5, 25)
         lambda r: r[1] == 'Reginald',  # no records
     ]
     records = [(n, n * n) for n in range(10)]
     for p in predicates:
         query = tree([Selection(p), [MemScan(records)]])
         result = list(execute(query))
         expected = [r for r in records if p(r)]
         self.assertListEqual(result, expected)
Пример #15
0
 def test_select_by_id(self):
     """
     What is the name of the movie with id 5000?
     """
     query = tree([
         Projection(lambda r: tuple([r.name])),
         [
             Selection(lambda r: r.id == '5000'),
             [FileScan(movies_file, MovieRecord)]
         ]
     ])
     self.assertListEqual(['Medium Cool (1969)'],
                          [r[0] for r in execute(query)])
Пример #16
0
def test_file(filename, print_exception=True):
    """
    测试filename代码的编译与执行
    :param filename:
    :param print_exception: 是否打印异常
    :return: 是否正确编译与执行
    """
    output_filename = filename.split('.')[0] + ".ll"

    if print_exception:
        generate_result = generate(input_filename=filename,
                                   output_filename=output_filename)
    else:
        try:
            generate_result = generate(input_filename=filename,
                                       output_filename=output_filename)
        except Exception:
            traceback.print_exc()
            print("generate", filename, "failed.")
            return False

    if not generate_result:
        traceback.print_exc()
        print("generate", filename, "failed.")
        return False
    else:
        if print_exception:
            execute_result = execute(output_filename)
        else:
            try:
                execute_result = execute(output_filename)
            except Exception as e:
                print("execute", output_filename, "failed.")
                traceback.print_exc()
                return False

        return True
Пример #17
0
    def schedule(self):
        from time import time
        from executor import executor
        current_time = int(round(time() * 1000))
        _is_task_interval_satisfied = lambda task: \
            task.create_time + task.delay <= current_time

        next_tasks = [t for t in self._pending_queue if \
                      _is_task_interval_satisfied(t) and \
                      self._is_netloc_interval_satisfied(t)]

        if not next_tasks:
            next_tasks = [t for t in self._retry_queue if \
                          _is_task_interval_satisfied(t) and \
                          self._is_netloc_interval_satisfied(t)]

        if not next_tasks:
            print("Finish all tasks")

        next_task = max(next_tasks, key=lambda t: t.priority)

        executor.execute(next_task)

        if next_task.status ==
Пример #18
0
 def test_equijoin(self):
     """
     The join condition can be that the value in one field equals another.
     """
     query = tree(
         [NestedLoopsJoin(lambda r, s: r[1] == s[0]),
             [MemScan(self.R_RECORDS)],
             [MemScan(self.S_RECORDS)]])
     result = list(execute(query))
     expected = [
         (0, 'a', 'a', 'apple'),
         (1, 'b', 'b', 'banana'),
         (1, 'b', 'b', 'burger')
     ]
     self.assertListEqual(result, expected)
Пример #19
0
 def test_equijoin(self):
     """
     The a simple join by field field value.
     """
     query = tree([
         MergeJoin(lambda r: r[1], lambda s: s[0]),
         [MemScan(self.R_RECORDS)], [MemScan(self.S_RECORDS)]
     ])
     result = list(execute(query))
     expected = [
         (0, 'a', 'a', 'apple'),
         (1, 'b', 'b', 'banana'),
         (1, 'b', 'b', 'burger'),
         (3, 'd', 'd', 'domato'),
     ]
     self.assertListEqual(result, expected)
Пример #20
0
 def test_inequality(self):
     """
     The join condition can be an inequality.
     """
     query = tree(
         [NestedLoopsJoin(lambda r, s: r[0] > s[0]),
             [MemScan(self.R_RECORDS)],
             [MemScan(self.R_RECORDS)]])
     result = list(execute(query))
     expected = [
         (1, 'b', 0, 'a'),
         (1, 'c', 0, 'a'),
         (2, 'c', 0, 'a'),
         (2, 'c', 1, 'b'),
         (2, 'c', 1, 'c'),
     ]
     self.assertListEqual(result, expected)
Пример #21
0
    def test_average_rating(self):
        """
        What is the average rating for movie with id 5000?

        WARNING: super slow
        """
        query = tree([
            Average(),
            [
                Projection(lambda r: tuple([float(r.rating)])),
                [
                    Selection(lambda r: r.movie_id == '5000'),
                    [FileScan(ratings_file, RatingRecord)]
                ]
            ]
        ])
        self.assertAlmostEqual(3.5, next(execute(query))[0], places=2)
Пример #22
0
 def test_self_join(self):
     """
     It's fine for a table to be joined to itself.
     """
     query = tree([
         MergeJoin(lambda r: r[0], lambda s: s[0]),
         [MemScan(self.S_RECORDS)], [MemScan(self.S_RECORDS)]
     ])
     result = list(execute(query))
     expected = [
         ('a', 'apple', 'a', 'apple'),
         ('b', 'banana', 'b', 'banana'),
         ('b', 'banana', 'b', 'burger'),
         ('b', 'burger', 'b', 'banana'),
         ('b', 'burger', 'b', 'burger'),
         ('d', 'domato', 'd', 'domato'),
     ]
     self.assertListEqual(result, expected)
Пример #23
0
 def test_three_way(self):
     """
     The input to a join can be a join.
     """
     query = tree(
         [NestedLoopsJoin(lambda r, s: r[3] == s[0]),
             [NestedLoopsJoin(lambda r, s: r[0] > s[0]),
                 [MemScan(self.R_RECORDS)],
                 [MemScan(self.R_RECORDS)]],
             [MemScan(self.S_RECORDS)]])
     result = list(execute(query))
     expected = [
         (1, 'b', 0, 'a', 'a', 'apple'),
         (1, 'c', 0, 'a', 'a', 'apple'),
         (2, 'c', 0, 'a', 'a', 'apple'),
         (2, 'c', 1, 'b', 'b', 'banana'),
         (2, 'c', 1, 'b', 'b', 'burger'),
     ]
     self.assertListEqual(result, expected)
Пример #24
0
    def test_count_rated_movies(self):
        """
        How many distinct movies have a rating?

        WARNING: super slow
        """
        query = tree([
            Count(),
            [
                Distinct(),
                [
                    Sort(lambda r: r[0]),
                    [
                        Projection(lambda r: tuple([r.movie_id])),
                        [FileScan(ratings_file, RatingRecord)]
                    ]
                ]
            ]
        ])
        # TODO: shouldn't this be 26744?
        self.assertEqual(26745, next(execute(query))[0])
Пример #25
0
 def test_three_way(self):
     """
     The input to a join can be a join.
     """
     query = tree([
         MergeJoin(lambda r: r[1], lambda s: s[0]),
         [MemScan(self.R_RECORDS)],
         [
             MergeJoin(lambda r: r[0], lambda s: s[0]),
             [MemScan(self.S_RECORDS)], [MemScan(self.S_RECORDS)]
         ]
     ])
     result = list(execute(query))
     expected = [
         (0, 'a', 'a', 'apple', 'a', 'apple'),
         (1, 'b', 'b', 'banana', 'b', 'banana'),
         (1, 'b', 'b', 'banana', 'b', 'burger'),
         (1, 'b', 'b', 'burger', 'b', 'banana'),
         (1, 'b', 'b', 'burger', 'b', 'burger'),
         (3, 'd', 'd', 'domato', 'd', 'domato'),
     ]
     self.assertListEqual(result, expected)
Пример #26
0
def cli(conf_file):
    """Example script."""
    execute(conf_file)