def process(self, element, *_args, **_kwargs):
        """For the incarceration sentences, supervision sentences, and sentence groups of
        a given person, sets the hydrated sentences onto the corresponding sentence groups.

        Args:
            element: a tuple containing person_id and a dictionary of the person's StateIncarcerationSentences,
                StateSupervisionSentences, and StateSentenceGroups

        Yields:
            For each sentence group, a tuple containing the person_id and the hydrated sentence group
        """
        person_id, person_entities = element

        # Get the StateIncarcerationSentences in a list
        incarceration_sentences = list(
            person_entities["incarceration_sentences"])

        # Get the StateSupervisionSentences in a list
        supervision_sentences = list(person_entities["supervision_sentences"])

        # Ge the StateSentenceGroups in a list
        sentence_groups = list(person_entities["sentence_groups"])

        if sentence_groups:
            for sentence_group in sentence_groups:
                if sentence_group.incarceration_sentences:
                    incarceration_sentence_ids = get_ids(
                        sentence_group.incarceration_sentences)

                    if incarceration_sentences:
                        sentence_group_incarceration_sentences = [
                            inc_sent for inc_sent in incarceration_sentences
                            if inc_sent.incarceration_sentence_id in
                            incarceration_sentence_ids
                        ]

                        sentence_group.incarceration_sentences = (
                            sentence_group_incarceration_sentences)

                        for incarceration_sentence in incarceration_sentences:
                            incarceration_sentence.sentence_group = sentence_group

                if sentence_group.supervision_sentences:
                    supervision_sentence_ids = get_ids(
                        sentence_group.supervision_sentences)

                    if supervision_sentences:
                        sentence_group_supervision_sentences = [
                            sup_sent for sup_sent in supervision_sentences
                            if sup_sent.supervision_sentence_id in
                            supervision_sentence_ids
                        ]

                        sentence_group.supervision_sentences = (
                            sentence_group_supervision_sentences)

                        for supervision_sentence in supervision_sentences:
                            supervision_sentence.sentence_group = sentence_group

                yield person_id, sentence_group
    def process(self, element, *_args, **_kwargs):
        """For the incarceration sentences, supervision sentences, and supervision
        periods of a given person, sets the hydrated supervision periods onto the
        corresponding sentences.

        Args:
            element: a tuple containing person_id and a dictionary of the person's
                StateSupervisionPeriods and either their StateIncarcerationSentences
                or StateSupervisionSentences

        Yields:
            For each sentence, a tuple containing the person_id and the hydrated
            sentence
        """
        person_id, person_entities = element

        # Get the StateSupervisionPeriods in a list
        supervision_periods = list(person_entities["supervision_periods"])

        # Get the sentences in a list
        sentences = list(person_entities["sentences"])

        if sentences:
            for sentence in sentences:
                if supervision_periods and sentence.supervision_periods:
                    supervision_period_ids = get_ids(
                        sentence.supervision_periods)

                    sentence_sps = [
                        sp for sp in supervision_periods
                        if sp.supervision_period_id in supervision_period_ids
                    ]

                    sentence.supervision_periods = sentence_sps
                yield person_id, sentence
예제 #3
0
def _filter_attached_sentences(
        sentences: List[SentenceType],
        supervision_period: StateSupervisionPeriod) -> List[SentenceType]:
    """Returns sentences that are attached to the given period, in a map indexed by the sentence primary key id."""
    attached_sentences: List[SentenceType] = []

    for sentence in sentences:
        if not sentence.get_id():
            raise ValueError('All objects should have database ids.')

        sentence_supervision_period_ids = get_ids(sentence.supervision_periods)

        if supervision_period.supervision_period_id in sentence_supervision_period_ids:
            attached_sentences.append(sentence)

    return attached_sentences