Exemple #1
0
def exactly_one(l: Sequence[T]) -> T:
    if len(l) > 1:
        raise TooManyItemsException('Found {n} items when expecing 1 in `{l}`.'.format(n=len(l), l=l))
    try:
        return l[0]
    except IndexError:
        raise DoesNotExistException('Did not find an item when expecting one.')
Exemple #2
0
def load_archetype(archetype):
    try:
        archetype_id = int(archetype)
    except ValueError:
        name = titlecase.titlecase(archetype.replace('-', ' '))
        archetype_id = db().value('SELECT id FROM archetype WHERE name = ?',
                                  [name])
        if not archetype_id:
            raise DoesNotExistException(
                'Did not find archetype with name of `{name}`'.format(
                    name=name))
    archetypes = load_archetypes(
        'd.archetype_id IN (SELECT descendant FROM archetype_closure WHERE ancestor = {archetype_id})'
        .format(archetype_id=sqlescape(archetype_id)), True)
    if len(archetypes) > 1:
        raise TooManyItemsException(
            'Found {n} archetypes when expecting 1 at most'.format(
                n=len(archetypes)))
    archetype = archetypes[0] if len(archetypes) == 1 else Archetype()
    # Because load_archetypes loads the root archetype and all below merged the id and name might not be those of the root archetype. Overwrite.
    archetype.id = int(archetype_id)
    archetype.name = db().value('SELECT name FROM archetype WHERE id = ?',
                                [archetype_id])
    if len(archetypes) == 0:
        archetype.decks = []
    return archetype
Exemple #3
0
def at_most_one(l: Sequence[T]) -> Optional[T]:
    if len(l) > 1:
        raise TooManyItemsException('Found {n} items when expecing at most 1 in `{l}`.'.format(n=len(l), l=l))
    elif len(l) == 0:
        return None
    else:
        return l[0]
def exactly_one(sequence: Sequence[T], noun: str = 'items') -> T:
    if len(sequence) > 1:
        raise TooManyItemsException('Found {n} {noun} when expecting 1 in `{l}`.'.format(n=len(sequence), l=sequence, noun=noun))
    try:
        return sequence[0]
    except IndexError as e:
        raise DoesNotExistException('Did not find an item when expecting one.') from e
def at_most_one(l: Sequence[T], noun: str = 'items') -> Optional[T]:
    if len(l) > 1:
        raise TooManyItemsException(
            'Found {n} {noun} when expecting at most 1 in `{l}`.'.format(
                n=len(l), l=l, noun=noun))
    if len(l) == 0:
        return None
    return l[0]
def load_archetype(archetype_id):
    archetypes = load_archetypes(
        'd.archetype_id IN (SELECT descendant FROM archetype_closure WHERE ancestor = {archetype_id})'
        .format(archetype_id=sqlescape(archetype_id)), True)
    if len(archetypes) > 1:
        raise TooManyItemsException(
            'Found {n} archetypes when expecting 1 at most'.format(
                n=len(archetypes)))
    archetype = archetypes[0] if len(archetypes) == 1 else Archetype()
    # Because load_archetypes loads the root archetype and all below merged the id and name might not be those of the root archetype. Overwrite.
    archetype.id = int(archetype_id)
    archetype.name = db().value('SELECT name FROM archetype WHERE id = ?',
                                [archetype_id])
    if len(archetypes) == 0:
        archetype.decks = []
    return archetype
def test_500() -> Response:
    if configuration.get_bool('production'):
        return return_json(generate_error(
            'ON_PROD', 'This only works on test environments'),
                           status=404)
    raise TooManyItemsException()