示例#1
0
    def testFlatten(self):
        """Tests if List.flatten() works as expected."""
        number_groups = types.List([[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]])
        numbers = number_groups.flatten()

        assert isinstance(numbers, types.List)
        assert numbers == types.List(range(10))
示例#2
0
    def testAnd(self):
        """Tests if List.__and__() works as expected."""
        numbers = types.List(range(10))
        common_numbers = numbers & types.List(range(5))

        assert isinstance(common_numbers, types.List)
        assert common_numbers == [0, 1, 2, 3, 4]

        with pytest.raises(ValueError):
            numbers & range(5)
示例#3
0
    def testIntersection(self):
        """Tests if List.intersection() works as expected."""
        numbers = types.List(range(10))
        common_numbers = numbers.intersection(types.List(range(5)))

        assert isinstance(common_numbers, types.List)
        assert common_numbers == [0, 1, 2, 3, 4]

        with pytest.raises(ValueError):
            numbers.intersection(range(5))
示例#4
0
    def testDifference(self):
        """Tests if List.difference() works as expected."""
        numbers = types.List(range(10))
        diff_numbers = numbers.difference(types.List(range(5)))

        assert isinstance(diff_numbers, types.List)
        assert diff_numbers == [5, 6, 7, 8, 9]

        with pytest.raises(ValueError):
            numbers.difference(range(5))
示例#5
0
    def testUnion(self):
        """Tests if List.union() works as expected."""
        numbers = types.List(range(10))
        unique_numbers = numbers.union(types.List(range(15)))

        assert isinstance(unique_numbers, types.List)
        assert len(unique_numbers) == 15
        assert unique_numbers == types.List(range(15))

        with pytest.raises(ValueError):
            numbers.union(range(15))
示例#6
0
    def testTranspose(self):
        """Tests if List.transpose() works as expected."""
        items = types.List([['x', 'y', 'z'], [0, 1, 2]])
        coords = items.transpose()

        assert isinstance(coords, types.List)
        assert coords == [('x', 0), ('y', 1), ('z', 2)]

        with pytest.raises(ValueError):
            types.List([['x', 'y'], 0]).transpose()

        with pytest.raises(ValueError):
            types.List([['x', 'y'], [0]]).transpose()
示例#7
0
    def testShuffle(self):
        """Tests if List.shuffle() works as expected."""
        numbers = types.List(range(10))
        shuffled_numbers = numbers.shuffle()

        assert isinstance(shuffled_numbers, types.List)
        assert shuffled_numbers != numbers
示例#8
0
    def testRotate(self):
        """Tests if List.rotate() works as expected."""
        numbers = types.List(range(10))
        rotated_numbers = numbers.rotate(1)

        assert isinstance(rotated_numbers, types.List)
        assert rotated_numbers == [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
示例#9
0
    def testNth(self):
        """Tests if List.nth() works as expected."""
        numbers = types.List(range(10))

        assert isinstance(numbers.nth(3), types.List)
        assert numbers.nth(3) == [0, 3, 6, 9]
        assert numbers.nth(3, 2) == [2, 5, 8]
示例#10
0
    def testTake(self):
        """Tests if List.take() works as expected."""
        numbers = types.List(range(10))
        sliced_numbers = numbers.take(3)

        assert isinstance(sliced_numbers, types.List)
        assert sliced_numbers == [0, 1, 2]
示例#11
0
    def testUnique(self):
        """Tests if List.unique() works as expected."""
        numbers = types.List([0, 1, 2, 2, 3, 3, 3])
        unique_numbers = numbers.unique()

        assert isinstance(unique_numbers, types.List)
        assert len(unique_numbers) == 4
        assert unique_numbers == [0, 1, 2, 3]
示例#12
0
    def testPreprend(self):
        """Tests if List.prepend() works as expected."""
        numbers = types.List()
        numbers.prepend(1)
        numbers.prepend(0)

        assert numbers[0] == 0
        assert numbers[1] == 1
示例#13
0
    def findAll(cls) -> types.List:
        """
        Retrieves all entity items.

        Returns:
            A list with all entity items
        """
        return types.List(cls.db.query(cls.entity).all())
示例#14
0
    def testLast(self):
        """Tests if List.last() works as expected."""
        numbers = types.List(range(10))

        assert numbers.last() == 9
        assert numbers.last(lambda item: not item & 1) == 8

        with pytest.raises(ValueError):
            numbers.last(1)
示例#15
0
    def testSplit(self):
        """Tests if List.split() works as expected."""
        numbers = types.List(range(10))
        number_groups = numbers.split(3)

        assert isinstance(number_groups, types.List)
        assert len(number_groups) == 3
        assert number_groups[0] == [0, 1, 2]
        assert number_groups[-1] == [6, 7, 8, 9]
示例#16
0
    def testChunk(self):
        """Tests if List.chunk() method work as expected."""
        numbers = types.List(range(10))
        number_groups = numbers.chunk(3)

        assert isinstance(number_groups, types.List)
        assert len(number_groups) == 4
        assert number_groups[-1] == [9]
        assert isinstance(number_groups[-1], types.List)
示例#17
0
    def __init__(self, dialect: str = None):
        """
        Creates a new schema compiler instance.

        Args:
            dialect: The SQL dialect name to use
        """
        self.tables = types.List()

        super().__init__(dialect)
示例#18
0
    def testFilter(self):
        """Tests if List.filter() works as expected."""
        numbers = types.List(range(10))
        odd_numbers = numbers.filter(lambda item: not item & 1)

        assert isinstance(odd_numbers, types.List)
        assert odd_numbers == [0, 2, 4, 6, 8]

        with pytest.raises(ValueError):
            numbers.filter(1)
示例#19
0
    def testReduce(self):
        """Tests if List.reduce() works as expected."""
        numbers = types.List(range(10))

        assert numbers.reduce(lambda x, y: x + y, 0) == 45
        assert numbers.reduce(lambda x, y: x - y, 0) == -45
        assert numbers.reduce(lambda x, y: x * y, 0) == 0

        with pytest.raises(ValueError):
            numbers.reduce(1)
示例#20
0
    def testSplice(self):
        """Tests if List.splice() works as expected."""
        numbers = types.List(range(10))
        sliced_numbers = numbers.splice(5)

        assert isinstance(sliced_numbers, types.List)
        assert sliced_numbers == [5, 6, 7, 8, 9]
        assert numbers == [0, 1, 2, 3, 4]
        assert numbers.splice(1, 3) == [1, 2, 3]
        assert numbers == [0, 4]
示例#21
0
    def groupByType(cls) -> types.List:
        """
        Returns a list with all encoder formats grouped by their type.

        Returns:
            A list with all encoder formats grouped by their type
        """
        return types.List([(format_type,
                            types.List(
                                sorted(
                                    formats,
                                    key=lambda _format: _format.friendlyName)))
                           for format_type, formats in itertools.groupby(
                               sorted([
                                   encoder.format()
                                   for encoder in Encoder.childs()
                                   if getattr(encoder, 'format')
                               ],
                                      key=lambda _format: _format.type),
                               key=lambda _format: _format.type)])
示例#22
0
    def testPartition(self):
        """Tests if List.partition() works as expected."""
        numbers = types.List(range(10))
        number_groups = numbers.partition(lambda item: item & 1)

        assert isinstance(number_groups, types.List)
        assert isinstance(number_groups[0], types.List)
        assert number_groups[0] == [1, 3, 5, 7, 9]

        with pytest.raises(ValueError):
            numbers.partition(1)
示例#23
0
    def testSample(self):
        """Tests if List.sample() works as expected."""
        numbers = types.List(range(10))

        assert numbers.sample()[0] in numbers
        assert len(numbers.sample(2)) == 2
        assert all(number in numbers for number in numbers.sample(2))
        assert numbers.sample(0) == []

        with pytest.raises(ValueError):
            numbers.sample(-1)
示例#24
0
    def listNames(cls) -> types.List:
        """
        Returns a list with all encoder format names.

        Returns:
            A list with all encoder format names
        """
        return types.List(
            sorted([
                encoder.format.name for encoder in Encoder.childs()
                if getattr(encoder, 'format')
            ]))
示例#25
0
    def findByCriteria(cls, *criterias) -> types.List:
        """
        Retrieves all entity items that matches the given criterias.

        Args:
            criterias: A list of Criteria objects

        Returns:
            A list with all matching entity items
        """
        query = cls.db.query(cls.entity)

        for criteria in criterias:
            query = criteria.apply(query)

        return types.List(query.all())
示例#26
0
    def addTable(self, table: schema.Table, rows: types.List):
        """
        Add the given table to schema.

        Args:
            table: The Table instance to add
            rows: The table rows list
        """
        # pylint: disable=protected-access
        table.rows = rows

        # Workaround to render table indexes in the order they were declared
        table._sorted_indexes = types.List([
            index for column in table.columns for index in table.indexes
            if column in iter(index.columns)
        ])

        self.tables.append(table)
示例#27
0
    def serialize(self, entities: Iterator[Entity]) -> types.OrderedMap:
        """
        Serializes the dataset rows.

        Args:
            entities: The list of entities to serialize

        Returns:
            The serialized dataset rows mapping
        """
        rows = types.OrderedMap()

        for entity in entities:
            table_name = str(entity.__table__.name)
            repository = RepositoryFactory.fromEntity(entity)
            _rows = repository.findAll()

            if not _rows:
                continue

            if self._options.localize:
                table_name = i18n._(table_name)

            rows[table_name] = types.List()

            for _row in _rows:
                row = types.OrderedMap()

                for column, value in _row.serialize().items():
                    if self._options.localize:
                        column = i18n._(column)

                    if self._options.forceStr or column == 'name':
                        value = str(value)

                    row[column] = value

                rows[table_name].append(row)

        return rows
示例#28
0
    def testCopy(self):
        """Tests if List.copy() works as expected."""
        numbers = types.List(range(10))

        assert isinstance(numbers.copy(), types.List)
        assert numbers.copy() == numbers
示例#29
0
    def testShift(self):
        """Tests if List.shift() works as expected."""
        numbers = types.List(range(10))

        assert numbers.shift() == 0
        assert numbers.shift() == 1
示例#30
0
    def testStringRepresentation(self):
        """Tests if List.__repr__() works as expected."""
        numbers = types.List(range(10))

        assert repr(numbers) == 'List([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])'