From 57b6d586efe60e9c7e808583a51d21d6e0c61ad6 Mon Sep 17 00:00:00 2001 From: Mauko Quiroga Date: Mon, 14 Oct 2024 17:49:56 +0200 Subject: [PATCH] test: get index (#1238 #1276 #1277) --- .../populations/_core_population.py | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/openfisca_core/populations/_core_population.py b/openfisca_core/populations/_core_population.py index 7e160d314..4196469d0 100644 --- a/openfisca_core/populations/_core_population.py +++ b/openfisca_core/populations/_core_population.py @@ -132,9 +132,9 @@ def filled_array( Examples: >>> import numpy - >>> from openfisca_core import populations as p + >>> from openfisca_core import populations - >>> class Population(p.CorePopulation): ... + >>> class Population(populations.CorePopulation): ... >>> population = Population(None) >>> population.count = 3 @@ -155,6 +155,31 @@ def filled_array( return numpy.full(self.count, value, dtype) def get_index(self, id: str) -> int: + """Return the index of an `id``. + + Args: + id: The id to get the index for. + + Returns: + int: The index of the id. + + Examples: + >>> from openfisca_core import entities, populations + + >>> class Person(entities.SingleEntity): ... + + >>> person = Person("person", "people", "", "") + >>> population = populations.CorePopulation(person) + >>> population.ids = ["Juan", "Megan", "Brahim"] + + >>> population.get_index("Megan") + 1 + + >>> population.get_index("Ibrahim") + Traceback (most recent call last): + ValueError: 'Ibrahim' is not in list + + """ return self.ids.index(id) # Calculations