Ejemplo n.º 1
0
def test_uid(from_string):
    uid = audeer.uid(from_string=from_string)
    assert len(uid) == 36
    for pos in [8, 13, 18, 23]:
        assert uid[pos] == '-'
    uid2 = audeer.uid(from_string=from_string)
    if from_string is not None:
        assert uid == uid2
    else:
        assert uid != uid2
Ejemplo n.º 2
0
def test_append(lookup_table):
    with pytest.raises(RuntimeError):
        lookup_table.append({'a': 1})
    # Extend lookup to 3 columns and add two new rows
    lookup_table.extend(('a', 'b', 'c'))
    uid1 = lookup_table.append({'a': 1, 'b': 2, 'c': 3})
    uid2 = lookup_table.append({'a': 4, 'b': 5, 'c': 6})
    assert uid1 != uid2
    assert len(uid1) == len(uid2)
    unique_string = (
        str({'a': 4, 'b': 5, 'c': 6})
        + lookup_table.group_id
        + lookup_table.name
        + lookup_table.version
        + lookup_table.repository
    )
    assert uid2 == audeer.uid(from_string=unique_string)
    table = lookup_table.table
    assert table[0] == ['id', 'a', 'b', 'c']
    assert table[1][1:] == [1, 2, 3]
    assert table[2][1:] == [4, 5, 6]
    assert table[1][0] != table[2][0]  # Checks for different IDs
    # Fail for trying to add the same row again
    with pytest.raises(RuntimeError):
        lookup_table.append({'a': 1, 'b': 2, 'c': 3})
    # Fail for trying to add a new row with wrong number of columns
    with pytest.raises(RuntimeError):
        lookup_table.append({'a': 1})
    # Fail for non-supported data types
    with pytest.raises(ValueError):
        lookup_table.append({'a': len})
Ejemplo n.º 3
0
def _find_media(
    db: audformat.Database,
    db_root: str,
    version: str,
    deps: Dependencies,
    archives: typing.Mapping[str, str],
    verbose: bool,
) -> typing.Set[str]:

    # release dependencies to removed media
    # and select according archives for upload
    media = set()
    db_media = db.files
    for file in set(deps.media) - set(db_media):
        media.add(deps.archive(file))
        deps._drop(file)

    # update version of altered media and insert new ones

    for file in audeer.progress_bar(
            db_media,
            desc='Find media',
            disable=not verbose,
    ):
        path = os.path.join(db_root, file)
        if file not in deps:
            checksum = audbackend.md5(path)
            if file in archives:
                archive = archives[file]
            else:
                archive = audeer.uid(from_string=file.replace('\\', '/'))
            deps._add_media(db_root, file, version, archive, checksum)
        elif not deps.removed(file):
            checksum = audbackend.md5(path)
            if checksum != deps.checksum(file):
                archive = deps.archive(file)
                deps._add_media(db_root, file, version, archive, checksum)

    return media
Ejemplo n.º 4
0
    def generate_uid(
        *,
        params: typing.Dict[str, typing.Any],
        name: str,
        group_id: str,
        version: str,
        repository: str,
    ) -> str:
        r"""Generate unique ID.

        It converts ``params`` to a string,
        and concatenates it with ``name``, ``group_id``, ``version``,
        and ``repository``.
        From that concatenated string a unique ID is derived.

        Args:
            params: params in the form of ``{column: parameter}``
            group_id: group ID of lookup table
            name: name of lookup table
            version: version of lookup table
            repository: repository of lookup table

        Returns:
            unique identifier of length 36

        Example:
            >>> Lookup.generate_uid(
            ...     params={0: None},
            ...     name='name',
            ...     group_id='group.id',
            ...     version='1.0.0',
            ...     repository='models-public-local',
            ... )
            'afe694a5-8bad-4dbc-73ba-636f18340615'

        """
        unique_string = (str(params) + group_id + name + version + repository)
        uid = audeer.uid(from_string=unique_string)
        return uid
Ejemplo n.º 5
0
    def id(self) -> str:
        r"""Object identifier.

        The ID of an object ID is created from its non-hidden arguments.

        Example:
            >>> class Foo(Object):
            ...    def __init__(self, bar: str):
            ...        self.bar = bar
            >>> foo1 = Foo('I am unique!')
            >>> print(foo1.id)
            893df240-babe-d796-cdf1-c436171b7a96
            >>> foo2 = Foo('I am different!')
            >>> print(foo2.id)
            9303f2a5-bfc9-e5ff-0ffa-a9846e2d2190
            >>> foo3 = Foo('I am unique!')
            >>> print(foo1.id == foo3.id)
            True

        """
        string = self.to_yaml_s(include_version=False)
        return audeer.uid(from_string=string)
Ejemplo n.º 6
0
import pytest

import audb
import audeer

pytest.ROOT = audeer.safe_path(
    os.path.join(
        os.path.dirname(os.path.realpath(__file__)),
        'tmp',
    ))

pytest.BACKEND = 'file-system'
pytest.CACHE_ROOT = os.path.join(pytest.ROOT, 'cache')
pytest.FILE_SYSTEM_HOST = os.path.join(pytest.ROOT, 'repo')
pytest.ID = audeer.uid()
pytest.NUM_WORKERS = 5
pytest.REPOSITORY_NAME = 'data-unittests-local'
pytest.REPOSITORIES = [
    audb.Repository(
        name=pytest.REPOSITORY_NAME,
        host=pytest.FILE_SYSTEM_HOST,
        backend=pytest.BACKEND,
    ),
]
pytest.PUBLISH_REPOSITORY = pytest.REPOSITORIES[0]
pytest.SHARED_CACHE_ROOT = os.path.join(pytest.ROOT, 'shared')


@pytest.fixture(scope='session', autouse=True)
def cleanup_session():
Ejemplo n.º 7
0
            True,
        ),
        (
            '1.0.0-alpha+001',
            True,
        ),
    ]
)
def test_is_semantic_version(version, is_semantic):
    assert audeer.is_semantic_version(version) == is_semantic


@pytest.mark.parametrize(
    'uid, expected',
    [
        (audeer.uid(), True),
        (audeer.uid(from_string='from string'), True),
        (None, False),
        (1234, False),
        ('', False),
        ('some random string', False),
        (audeer.uid()[:-1], False),
    ]
)
def test_is_uid(uid, expected):
    assert audeer.is_uid(uid) == expected


def power(a: int = 0, *, b: int = 1):
    return a ** b