Grid - Snailz Docs

Skip to content

# Grid

Sample grids.

## `Grid`

Bases: `BaseModel`

Create and fill an integer grid.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`id` | `str` | unique ID | required
`size` | `int` | grid size | required
`grid` | `list` | grid values | `[]`

Source code in `src/snailz/grid.py`

    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    class Grid(BaseModel):
        """Create and fill an integer grid."""

        id_stem: ClassVar[str] = "G"
        id_digits: ClassVar[int] = 4

        id: str = Field(min_length=1, description="unique ID")
        size: int = Field(
            gt=0,
            description="grid size",
        )
        grid: list = Field(default=[], description="grid values")

        @staticmethod
        def make(params):
            """Make a grid."""

            utils.ensure_id_generator(Grid)
            grid = Grid(id=next(Grid._id_gen), size=params.grid_size)
            grid.fill()
            return grid

        @staticmethod
        def tidy(grids):
            """Convert all grids to tidy table."""

            result = ["grid_id,x,y,pollution"]
            for g in grids:
                for x in range(g.size):
                    for y in range(g.size):
                        result.append(f"{g.id},{x},{y},{g[x, y]}")
            return "\n".join(result)

        def __getitem__(self, key):
            """Get grid element."""

            x, y = key
            return self.grid[y * self.size + x]

        def __setitem__(self, key, value):
            """Set grid element."""

            x, y = key
            self.grid[y * self.size + x] = value

        def __str__(self):
            """Convert to CSV string."""

            result = []
            for y in range(self.size - 1, -1, -1):
                result.append(",".join([str(self[x, y]) for x in range(self.size)]))
            return "\n".join(result)

        def fill(self):
            """Fill in a grid."""

            center = self.size // 2
            size_1 = self.size - 1
            x, y = center, center

            self.grid = [0 for _ in range(self.size * self.size)]
            while (x != 0) and (y != 0) and (x != size_1) and (y != size_1):
                self[x, y] += 1
                m = random.choice(MOVES)
                x += m[0]
                y += m[1]

### `make(params)` `staticmethod`

Make a grid.

Source code in `src/snailz/grid.py`

    28
    29
    30
    31
    32
    33
    34
    35
    @staticmethod
    def make(params):
        """Make a grid."""

        utils.ensure_id_generator(Grid)
        grid = Grid(id=next(Grid._id_gen), size=params.grid_size)
        grid.fill()
        return grid

### `tidy(grids)` `staticmethod`

Convert all grids to tidy table.

Source code in `src/snailz/grid.py`

    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    @staticmethod
    def tidy(grids):
        """Convert all grids to tidy table."""

        result = ["grid_id,x,y,pollution"]
        for g in grids:
            for x in range(g.size):
                for y in range(g.size):
                    result.append(f"{g.id},{x},{y},{g[x, y]}")
        return "\n".join(result)

### `__getitem__(key)`

Get grid element.

Source code in `src/snailz/grid.py`

    48
    49
    50
    51
    52
    def __getitem__(self, key):
        """Get grid element."""

        x, y = key
        return self.grid[y * self.size + x]

### `__setitem__(key, value)`

Set grid element.

Source code in `src/snailz/grid.py`

    54
    55
    56
    57
    58
    def __setitem__(self, key, value):
        """Set grid element."""

        x, y = key
        self.grid[y * self.size + x] = value

### `__str__()`

Convert to CSV string.

Source code in `src/snailz/grid.py`

    60
    61
    62
    63
    64
    65
    66
    def __str__(self):
        """Convert to CSV string."""

        result = []
        for y in range(self.size - 1, -1, -1):
            result.append(",".join([str(self[x, y]) for x in range(self.size)]))
        return "\n".join(result)

### `fill()`

Fill in a grid.

Source code in `src/snailz/grid.py`

    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    def fill(self):
        """Fill in a grid."""

        center = self.size // 2
        size_1 = self.size - 1
        x, y = center, center

        self.grid = [0 for _ in range(self.size * self.size)]
        while (x != 0) and (y != 0) and (x != size_1) and (y != size_1):
            self[x, y] += 1
            m = random.choice(MOVES)
            x += m[0]
            y += m[1]