Showing 1 of 2 files from the diff.
Other files ignored by Codecov
mesa/agent.py has changed.

@@ -18,6 +18,11 @@
Loading
18 18
19 19
import numpy as np
20 20
21 +
from typing import Iterable, Iterator, List, Optional, Set, Tuple, Union
22 +
from .agent import Agent
23 +
Coordinate = Tuple[int, int]
24 +
GridContent = Union[Optional[Agent], Set[Agent]]
25 +
21 26
22 27
def accept_tuple_argument(wrapped_function):
23 28
    """ Decorator to allow grid methods that take a list of (x, y) coord tuples
@@ -70,7 +75,7 @@
Loading
70 75
71 76
    """
72 77
73 -
    def __init__(self, width, height, torus):
78 +
    def __init__(self, width: int, height: int, torus: bool) -> None:
74 79
        """ Create a new grid.
75 80
76 81
        Args:
@@ -82,10 +87,10 @@
Loading
82 87
        self.width = width
83 88
        self.torus = torus
84 89
85 -
        self.grid = []
90 +
        self.grid = []  # type: List[List[GridContent]]
86 91
87 92
        for x in range(self.width):
88 -
            col = []
93 +
            col = []  # type: List[GridContent]
89 94
            for y in range(self.height):
90 95
                col.append(self.default_val())
91 96
            self.grid.append(col)
@@ -95,25 +100,27 @@
Loading
95 100
            *(range(self.width), range(self.height))))
96 101
97 102
    @staticmethod
98 -
    def default_val():
103 +
    def default_val() -> None:
99 104
        """ Default value for new cell elements. """
100 105
        return None
101 106
102 -
    def __getitem__(self, index):
107 +
    def __getitem__(self, index: int) -> List[GridContent]:
103 108
        return self.grid[index]
104 109
105 -
    def __iter__(self):
106 -
        # create an iterator that chains the
107 -
        #  rows of grid together as if one list:
110 +
    def __iter__(self) -> Iterator[GridContent]:
111 +
        """
112 +
        create an iterator that chains the
113 +
        rows of grid together as if one list:
114 +
        """
108 115
        return itertools.chain(*self.grid)
109 116
110 -
    def coord_iter(self):
117 +
    def coord_iter(self) -> Iterator[Tuple[GridContent, int, int]]:
111 118
        """ An iterator that returns coordinates as well as cell contents. """
112 119
        for row in range(self.width):
113 120
            for col in range(self.height):
114 121
                yield self.grid[row][col], row, col  # agent, x, y
115 122
116 -
    def neighbor_iter(self, pos, moore=True):
123 +
    def neighbor_iter(self, pos: Coordinate, moore: bool = True) -> Iterator[GridContent]:
117 124
        """ Iterate over position neighbors.
118 125
119 126
        Args:
@@ -125,8 +132,8 @@
Loading
125 132
        neighborhood = self.iter_neighborhood(pos, moore=moore)
126 133
        return self.iter_cell_list_contents(neighborhood)
127 134
128 -
    def iter_neighborhood(self, pos, moore,
129 -
                          include_center=False, radius=1):
135 +
    def iter_neighborhood(self, pos: Coordinate, moore: bool,
136 +
                          include_center: bool = False, radius: int = 1) -> Iterator[Coordinate]:
130 137
        """ Return an iterator over cell coordinates that are in the
131 138
        neighborhood of a certain point.
132 139
@@ -148,7 +155,7 @@
Loading
148 155
149 156
        """
150 157
        x, y = pos
151 -
        coordinates = set()
158 +
        coordinates = set()  # type: Set[Coordinate]
152 159
        for dy in range(-radius, radius + 1):
153 160
            for dx in range(-radius, radius + 1):
154 161
                if dx == 0 and dy == 0 and not include_center:
@@ -171,8 +178,8 @@
Loading
171 178
                    coordinates.add(coords)
172 179
                    yield coords
173 180
174 -
    def get_neighborhood(self, pos, moore,
175 -
                         include_center=False, radius=1):
181 +
    def get_neighborhood(self, pos: Coordinate, moore: bool,
182 +
                         include_center: bool = False, radius: int = 1) -> List[Coordinate]:
176 183
        """ Return a list of cells that are in the neighborhood of a
177 184
        certain point.
178 185
@@ -194,8 +201,8 @@
Loading
194 201
        """
195 202
        return list(self.iter_neighborhood(pos, moore, include_center, radius))
196 203
197 -
    def iter_neighbors(self, pos, moore,
198 -
                       include_center=False, radius=1):
204 +
    def iter_neighbors(self, pos: Coordinate, moore: bool,
205 +
                       include_center: bool = False, radius: int = 1) -> Iterator[GridContent]:
199 206
        """ Return an iterator over neighbors to a certain point.
200 207
201 208
        Args:
@@ -219,8 +226,8 @@
Loading
219 226
            pos, moore, include_center, radius)
220 227
        return self.iter_cell_list_contents(neighborhood)
221 228
222 -
    def get_neighbors(self, pos, moore,
223 -
                      include_center=False, radius=1):
229 +
    def get_neighbors(self, pos: Coordinate, moore: bool,
230 +
                      include_center: bool = False, radius: int = 1) -> List[Coordinate]:
224 231
        """ Return a list of neighbors to a certain point.
225 232
226 233
        Args:
@@ -243,7 +250,7 @@
Loading
243 250
        return list(self.iter_neighbors(
244 251
            pos, moore, include_center, radius))
245 252
246 -
    def torus_adj(self, pos):
253 +
    def torus_adj(self, pos: Coordinate) -> Coordinate:
247 254
        """ Convert coordinate, handling torus looping. """
248 255
        if not self.out_of_bounds(pos):
249 256
            return pos
@@ -253,7 +260,7 @@
Loading
253 260
            x, y = pos[0] % self.width, pos[1] % self.height
254 261
        return x, y
255 262
256 -
    def out_of_bounds(self, pos):
263 +
    def out_of_bounds(self, pos: Coordinate) -> bool:
257 264
        """
258 265
        Determines whether position is off the grid, returns the out of
259 266
        bounds coordinate.
@@ -262,7 +269,7 @@
Loading
262 269
        return x < 0 or x >= self.width or y < 0 or y >= self.height
263 270
264 271
    @accept_tuple_argument
265 -
    def iter_cell_list_contents(self, cell_list):
272 +
    def iter_cell_list_contents(self, cell_list: Iterable[Coordinate]) -> Iterator[GridContent]:
266 273
        """
267 274
        Args:
268 275
            cell_list: Array-like of (x, y) tuples, or single tuple.
@@ -275,7 +282,7 @@
Loading
275 282
            self[x][y] for x, y in cell_list if not self.is_cell_empty((x, y)))
276 283
277 284
    @accept_tuple_argument
278 -
    def get_cell_list_contents(self, cell_list):
285 +
    def get_cell_list_contents(self, cell_list: Iterable[Coordinate]) -> List[GridContent]:
279 286
        """
280 287
        Args:
281 288
            cell_list: Array-like of (x, y) tuples, or single tuple.
@@ -286,7 +293,7 @@
Loading
286 293
        """
287 294
        return list(self.iter_cell_list_contents(cell_list))
288 295
289 -
    def move_agent(self, agent, pos):
296 +
    def move_agent(self, agent: Agent, pos: Coordinate) -> None:
290 297
        """
291 298
        Move an agent from its current position to a new position.
292 299
@@ -301,35 +308,35 @@
Loading
301 308
        self._place_agent(pos, agent)
302 309
        agent.pos = pos
303 310
304 -
    def place_agent(self, agent, pos):
311 +
    def place_agent(self, agent: Agent, pos: Coordinate) -> None:
305 312
        """ Position an agent on the grid, and set its pos variable. """
306 313
        self._place_agent(pos, agent)
307 314
        agent.pos = pos
308 315
309 -
    def _place_agent(self, pos, agent):
316 +
    def _place_agent(self, pos: Coordinate, agent: Agent) -> None:
310 317
        """ Place the agent at the correct location. """
311 318
        x, y = pos
312 319
        self.grid[x][y] = agent
313 320
        self.empties.discard(pos)
314 321
315 -
    def remove_agent(self, agent):
322 +
    def remove_agent(self, agent: Agent) -> None:
316 323
        """ Remove the agent from the grid and set its pos variable to None. """
317 324
        pos = agent.pos
318 325
        self._remove_agent(pos, agent)
319 326
        agent.pos = None
320 327
321 -
    def _remove_agent(self, pos, agent):
328 +
    def _remove_agent(self, pos: Coordinate, agent: Agent) -> None:
322 329
        """ Remove the agent from the given location. """
323 330
        x, y = pos
324 331
        self.grid[x][y] = None
325 332
        self.empties.add(pos)
326 333
327 -
    def is_cell_empty(self, pos):
334 +
    def is_cell_empty(self, pos: Coordinate) -> bool:
328 335
        """ Returns a bool of the contents of a cell. """
329 336
        x, y = pos
330 337
        return self.grid[x][y] == self.default_val()
331 338
332 -
    def move_to_empty(self, agent):
339 +
    def move_to_empty(self, agent: Agent) -> None:
333 340
        """ Moves agent to a random empty cell, vacating agent's old cell. """
334 341
        pos = agent.pos
335 342
        if len(self.empties) == 0:
@@ -339,7 +346,7 @@
Loading
339 346
        agent.pos = new_pos
340 347
        self._remove_agent(pos, agent)
341 348
342 -
    def find_empty(self):
349 +
    def find_empty(self) -> Optional[Coordinate]:
343 350
        """ Pick a random empty cell. """
344 351
        from warnings import warn
345 352
        import random
@@ -348,7 +355,7 @@
Loading
348 355
              "`random` instead of the model-level random-number generator. "
349 356
              "Consider replacing it with having a model or agent object "
350 357
              "explicitly pick one of the grid's list of empty cells."),
351 -
            DeprecationWarning)
358 +
             DeprecationWarning)
352 359
353 360
        if self.exists_empty_cells():
354 361
            pos = random.choice(sorted(self.empties))
@@ -356,16 +363,16 @@
Loading
356 363
        else:
357 364
            return None
358 365
359 -
    def exists_empty_cells(self):
366 +
    def exists_empty_cells(self) -> bool:
360 367
        """ Return True if any cells empty else False. """
361 368
        return len(self.empties) > 0
362 369
363 370
364 371
class SingleGrid(Grid):
365 372
    """ Grid where each cell contains exactly at most one object. """
366 -
    empties = set()
373 +
    empties = set()  # type: Set[Coordinate]
367 374
368 -
    def __init__(self, width, height, torus):
375 +
    def __init__(self, width: int, height: int, torus: bool) -> None:
369 376
        """ Create a new single-item grid.
370 377
371 378
        Args:
@@ -394,7 +401,7 @@
Loading
394 401
        agent.pos = coords
395 402
        self._place_agent(coords, agent)
396 403
397 -
    def _place_agent(self, pos, agent):
404 +
    def _place_agent(self, pos: Coordinate, agent: Agent) -> None:
398 405
        if self.is_cell_empty(pos):
399 406
            super()._place_agent(pos, agent)
400 407
        else:
@@ -426,13 +433,13 @@
Loading
426 433
        """ Default value for new cell elements. """
427 434
        return set()
428 435
429 -
    def _place_agent(self, pos, agent):
436 +
    def _place_agent(self, pos: Coordinate, agent: Agent) -> None:
430 437
        """ Place the agent at the correct location. """
431 438
        x, y = pos
432 439
        self.grid[x][y].add(agent)
433 440
        self.empties.discard(pos)
434 441
435 -
    def _remove_agent(self, pos, agent):
442 +
    def _remove_agent(self, pos: Coordinate, agent: Agent) -> None:
436 443
        """ Remove the agent from the given location. """
437 444
        x, y = pos
438 445
        self.grid[x][y].remove(agent)
Files Coverage
mesa 84.65%
Project Totals (17 files) 84.65%
1657.2
TRAVIS_PYTHON_VERSION=3.6
TRAVIS_OS_NAME=linux
1657.3
TRAVIS_PYTHON_VERSION=3.7
TRAVIS_OS_NAME=linux
1657.4
TRAVIS_PYTHON_VERSION=3.8
TRAVIS_OS_NAME=linux
1657.1
TRAVIS_PYTHON_VERSION=3.5
TRAVIS_OS_NAME=linux

No yaml found.

Create your codecov.yml to customize your Codecov experience

Sunburst
The inner-most circle is the entire project, moving away from the center are folders then, finally, a single file. The size and color of each slice is representing the number of statements and the coverage, respectively.
Icicle
The top section represents the entire project. Proceeding with folders and finally individual files. The size and color of each slice is representing the number of statements and the coverage, respectively.
Grid
Each block represents a single file in the project. The size and color of each block is represented by the number of statements and the coverage, respectively.
Loading