Code Duplication    Length = 114-117 lines in 2 locations

galactic/context/mixins.py 2 locations

@@ 372-488 (lines=117) @@
369
370
371
# pylint: disable=too-few-public-methods
372
class IndividualsHolder(_Mixin, Generic[X], MutableMapping[str, X]):
373
    """
374
    The :class:`IndividualsHolder[X] <IndividualsHolder>` class  is a mixin used for storing the
375
    population individuals in memory.
376
377
    It's a generic class that depends of an :class:`Individual <galactic.context.Individual>`
378
    subclass :class:`X`.
379
380
    .. versionadded:: 0.0.1
381
    """
382
383
    def __init__(self, **kwargs):
384
        """
385
        Initialise an individuals holder.
386
387
        Keyword Arguments
388
        -----------------
389
            individuals : :class:`Iterable[A] <python:collections.abc.Iterable>`
390
                the individuals
391
392
        .. versionadded:: 0.0.1
393
        """
394
        super().__init__(**kwargs)
395
        self._individuals = {individual.identifier: individual for individual in
396
                             kwargs['individuals']}
397
398
    def __getitem__(self, identifier: str) -> X:
399
        """
400
        Get an individual using its identifier.
401
402
        Parameters
403
        ----------
404
            identifier : :class:`str`
405
                the individual identifier
406
407
        Returns
408
        -------
409
            the individual : :class: `X`
410
411
        Raises
412
        ------
413
            KeyError
414
                if the identifier does not belong to the individuals holder.
415
416
        .. versionadded:: 0.0.1
417
        """
418
        return self._individuals[identifier]
419
420
    def __delitem__(self, identifier: str) -> None:
421
        """
422
        Delete an individual using its identifier.
423
424
        Parameters
425
        ----------
426
            identifier : :class:`str`
427
                the individual identifier
428
429
        Raises
430
        ------
431
            KeyError
432
                if the identifier does not belong to the individuals holder.
433
434
        .. versionadded:: 0.0.1
435
        """
436
        del self._individuals[identifier]
437
438
    def __setitem__(self, identifier: str, individual: X) -> None:
439
        """
440
        Set an individual using its identifier.
441
442
        Parameters
443
        ----------
444
            identifier : :class:`str`
445
                the individual identifier
446
447
            individual: :class:`X`
448
                an individual
449
450
        .. versionadded:: 0.0.1
451
        """
452
        self._individuals[identifier] = individual
453
454
    def __iter__(self) -> Iterator[str]:
455
        """
456
        Get an iterator over the individual identifiers.
457
458
        Returns
459
        -------
460
            an iterator : :class:`Iterator[str] <python:collections.abc.Iterator>`
461
462
        .. versionadded:: 0.0.1
463
        """
464
        return iter(self._individuals)
465
466
    def __len__(self) -> int:
467
        """
468
        Get the number of individuals.
469
470
        Returns
471
        -------
472
            the number of indidivuals : :class:`int`
473
474
        .. versionadded:: 0.0.1
475
        """
476
        return len(self._individuals)
477
478
    def __bool__(self) -> bool:
479
        """
480
        Get the boolean representation of the individuals holder.
481
482
        Returns
483
        -------
484
            the boolean representation : :class:`bool`
485
486
        .. versionadded:: 0.0.1
487
        """
488
        return bool(self._individuals)
489
490
491
# pylint: disable=too-few-public-methods
@@ 255-368 (lines=114) @@
252
253
254
# pylint: disable=too-few-public-methods
255
class AttributesHolder(_Mixin, Generic[A], MutableMapping[str, A]):
256
    """
257
    The :class:`AttributesHolder[A] <AttributesHolder>` class is a mixin used for storing the model
258
    attributes in memory.
259
260
    It's a generic class that depends of an :class:`Attribute <galactic.context.Attribute>`
261
    subclass :class:`A`.
262
263
    .. versionadded:: 0.0.1
264
    """
265
266
    def __init__(self, **kwargs):
267
        """
268
        Initialise an attribute holder.
269
270
        Keyword Arguments
271
        -----------------
272
            attributes : :class:`Iterable[A] <python:collections.abc.Iterable>`
273
                the attributes
274
275
        .. versionadded:: 0.0.1
276
        """
277
        super().__init__(**kwargs)
278
        self._attributes = {attribute.name: attribute for attribute in kwargs['attributes']}
279
280
    def __getitem__(self, name: str) -> A:
281
        """
282
        Get an attribute using its name
283
284
        Parameters
285
        ----------
286
            name : :class:`str`
287
                the attribute name
288
289
        Returns
290
        -------
291
            the attribute : A
292
293
        Raises
294
        ------
295
            KeyError
296
                if this attributes holder does not contain an attribute with this name.
297
298
        .. versionadded:: 0.0.1
299
        """
300
        return self._attributes[name]
301
302
    def __setitem__(self, name: str, attribute: A) -> None:
303
        """
304
        Set an attribute using its name
305
306
        Parameters
307
        ----------
308
            name : :class:`str`
309
                the attribute name
310
            attribute: :class:`A`
311
312
        .. versionadded:: 0.0.1
313
        """
314
        self._attributes[name] = attribute
315
316
    def __delitem__(self, name: str) -> None:
317
        """
318
        Delete an attribute using its name
319
320
        Parameters
321
        ----------
322
            name : :class:`str`
323
                the attribute name
324
325
        Raises
326
        ------
327
            KeyError
328
                if this attributes holder does not contain an attribute with this name.
329
330
        .. versionadded:: 0.0.1
331
        """
332
        del self._attributes[name]
333
334
    def __iter__(self) -> Iterator[str]:
335
        """
336
        Get an iterator over the attribute names.
337
338
        Returns
339
        -------
340
            an iterator : :class:`Iterator[str] <python:collections.abc.Iterator>`
341
342
        .. versionadded:: 0.0.1
343
        """
344
        return iter(self._attributes)
345
346
    def __len__(self) -> int:
347
        """
348
        Get the number of attributes.
349
350
        Returns
351
        -------
352
            the number of attributes : :class:`int`
353
354
        .. versionadded:: 0.0.1
355
        """
356
        return len(self._attributes)
357
358
    def __bool__(self):
359
        """
360
        Get the boolean value of an attribute holder.
361
362
        Returns
363
        -------
364
            the boolean value : :class:`bool`
365
366
        .. versionadded:: 0.0.1
367
        """
368
        return bool(self._attributes)
369
370
371
# pylint: disable=too-few-public-methods