Exemplo n.º 1
0
def relative_to(iterable, item, *, offset, n=0, default=MISSING):
    """Return the item relative to the nth occurrence of some existing item.

    Args:
        iterable: The iterable series from which to search for item.
        item: The value to relative to which the returned item should be.
        offset: The positive or negative offset relative to the found item.
        n: Where it is the nth occurrence of item which will be searched for.
        default: The default value to return if not found.

    Returns:
        The item at a given offset from a specific value, or the default value if given.

    Raises:
        ValueError: If there is not item matching the criteria an no default
            value has been specified.
    """
    count = 0
    for p, q in stagger(iterable, (0, offset), longest=True,
                        fillvalue=MISSING):
        if p == item:
            if count == n:
                if q is MISSING:
                    if default is MISSING:
                        raise ValueError
                    return default
                return q
            count += 1
    if default is MISSING:
        raise ValueError
    return default
Exemplo n.º 2
0
    def split(self, text: str) -> List[Sentence]:
        plain_sentences: List[str] = list(split_multi(text))

        try:
            sentence_offset: Optional[int] = text.index(plain_sentences[0])
        except ValueError as error:
            raise AssertionError(
                f"Can't find the sentence offset for sentence {repr(plain_sentences[0])} "
                f"from the text's starting position") from error

        sentences: List[Sentence] = []
        for sentence, next_sentence in stagger(plain_sentences,
                                               offsets=(0, 1),
                                               longest=True):

            sentences.append(
                Sentence(text=sentence,
                         use_tokenizer=self._tokenizer,
                         start_position=sentence_offset))

            offset: int = sentence_offset + len(sentence)
            try:
                sentence_offset = text.index(
                    next_sentence,
                    offset) if next_sentence is not None else None
            except ValueError as error:
                raise AssertionError(
                    f"Can't find the sentence offset for sentence {repr(sentence)} "
                    f"starting from position {repr(offset)}") from error

        return sentences
Exemplo n.º 3
0
 def _wrapped_minmax(self):
     returns = [
         np.log(nxt / prv)
         for prv, nxt in stagger(collapse(self.datums), offsets=(0, 1))
     ]
     low = min(returns)
     high = max(returns)
     return high, low
def crafting_shapeless(i, item_info, method, **kwargs):
    if len(method['recipe']) == 9 and all(l == r for l, r in more_itertools.stagger(method['recipe'], offsets=(0, 1))):
        return crafting_shaped(i, item_info, method, **kwargs)
    return bottle.template("""
        <p>{{item_info['name']}} can {{'' if i == 0 else 'also '}}be crafted using the following shapeless recipe:</p>
        {{!inventory_table([method['recipe']], style={'margin-left': 'auto', 'margin-right': 'auto'})}}
        %if method.get('outputAmount', 1) > 1:
            <p>This will create {{method['outputAmount']}} items per crafting process.</p>
        %end
    """, i=i, inventory_table=alltheitems.util.inventory_table, item_info=item_info, method=method)
Exemplo n.º 5
0
def tick(state, change_rules):
    # For this generation, pots 2 before
    # as well as 2 after might be affected
    min_, max_ = min(state), max(state)
    state[min_ - 1], state[min_ - 2] = (False, ) * 2
    state[max_ + 1], state[max_ + 2] = (False, ) * 2

    state_with_surrounding_points =\
        list(more_itertools.stagger(
            state.values(), offsets=tuple(range(-2, 3)),
            longest=True,
            fillvalue=False))[:-1]

    result_state = SortedDict()

    for i, surrounding_points in zip(state.keys(),
                                     state_with_surrounding_points):
        if surrounding_points in change_rules:
            result_state[i] = change_rules[surrounding_points]
        else:
            result_state[i] = False

    return result_state
Exemplo n.º 6
0
def has_exactly_2_adjacent_similar_digits(num_str):
    iterator = stagger(num_str,
                       offsets=(-2, -1, 0, 1),
                       longest=True,
                       fillvalue='$')
    return any(v != x == y != z for (v, x, y, z) in iterator)