Skip to content

Commit

Permalink
Update syntax to Python 3.9 (#452)
Browse files Browse the repository at this point in the history
* Update syntax to Python 3.9

* Increase code quality for _put_media()

* Revert "Increase code quality for _put_media()"

This reverts commit bbbe082.
  • Loading branch information
hagenw authored Oct 31, 2024
1 parent 1d8ab57 commit e86059a
Show file tree
Hide file tree
Showing 17 changed files with 229 additions and 216 deletions.
12 changes: 7 additions & 5 deletions audb/core/api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from __future__ import annotations

from collections.abc import Sequence
import os
import tempfile
import typing

import pandas as pd

Expand Down Expand Up @@ -286,7 +288,7 @@ def exists(
*,
version: str = None,
bit_depth: int = None,
channels: typing.Union[int, typing.Sequence[int]] = None,
channels: int | Sequence[int] = None,
format: str = None,
mixdown: bool = False,
sampling_rate: int = None,
Expand Down Expand Up @@ -367,7 +369,7 @@ def flavor_path(
version: str,
*,
bit_depth: int = None,
channels: typing.Union[int, typing.Sequence[int]] = None,
channels: int | Sequence[int] = None,
format: str = None,
mixdown: bool = False,
sampling_rate: int = None,
Expand Down Expand Up @@ -453,7 +455,7 @@ def latest_version(

def remove_media(
name: str,
files: typing.Union[str, typing.Sequence[str]],
files: str | Sequence[str],
*,
verbose: bool = False,
):
Expand Down Expand Up @@ -560,7 +562,7 @@ def repository(

def versions(
name: str,
) -> typing.List[str]:
) -> list[str]:
r"""Available versions of database.
Args:
Expand Down
2 changes: 1 addition & 1 deletion audb/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def load_configuration_file(config_file: str):
"""
if os.path.exists(config_file):
with open(config_file, "r") as cf:
with open(config_file) as cf:
config = yaml.load(cf, Loader=yaml.BaseLoader)
else:
config = {}
Expand Down
51 changes: 27 additions & 24 deletions audb/core/dependencies.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from __future__ import annotations

from collections.abc import Callable
from collections.abc import Sequence
import errno
import os
import re
import tempfile
import typing

import pandas as pd
import pyarrow as pa
Expand Down Expand Up @@ -110,7 +113,7 @@ def __eq__(self, other: "Dependencies") -> bool:
"""
return self._df.equals(other._df)

def __getitem__(self, file: str) -> typing.List:
def __getitem__(self, file: str) -> list:
r"""File information.
Args:
Expand All @@ -130,7 +133,7 @@ def __str__(self) -> str: # noqa: D105
return str(self._df)

@property
def archives(self) -> typing.List[str]:
def archives(self) -> list[str]:
r"""All media, table, attachment archives.
Return:
Expand All @@ -140,7 +143,7 @@ def archives(self) -> typing.List[str]:
return sorted(self._df.archive.unique().tolist())

@property
def attachments(self) -> typing.List[str]:
def attachments(self) -> list[str]:
r"""Attachment paths (can be a file or a folder).
Returns:
Expand All @@ -152,7 +155,7 @@ def attachments(self) -> typing.List[str]:
].index.tolist()

@property
def attachment_ids(self) -> typing.List[str]:
def attachment_ids(self) -> list[str]:
r"""Attachment IDs.
Returns:
Expand All @@ -164,7 +167,7 @@ def attachment_ids(self) -> typing.List[str]:
].archive.tolist()

@property
def files(self) -> typing.List[str]:
def files(self) -> list[str]:
r"""All media, table, attachments.
Returns:
Expand All @@ -174,7 +177,7 @@ def files(self) -> typing.List[str]:
return self._df.index.tolist()

@property
def media(self) -> typing.List[str]:
def media(self) -> list[str]:
r"""Media files.
Returns:
Expand All @@ -186,7 +189,7 @@ def media(self) -> typing.List[str]:
].index.tolist()

@property
def removed_media(self) -> typing.List[str]:
def removed_media(self) -> list[str]:
r"""Removed media files.
Returns:
Expand All @@ -199,7 +202,7 @@ def removed_media(self) -> typing.List[str]:
].index.tolist()

@property
def table_ids(self) -> typing.List[str]:
def table_ids(self) -> list[str]:
r"""Table IDs.
Like :meth:`audb.Dependencies.tables`,
Expand All @@ -213,7 +216,7 @@ def table_ids(self) -> typing.List[str]:
return [os.path.splitext(table[3:])[0] for table in self.tables]

@property
def tables(self) -> typing.List[str]:
def tables(self) -> list[str]:
r"""Tables files.
Returns:
Expand Down Expand Up @@ -455,8 +458,8 @@ def _add_attachment(

def _add_media(
self,
values: typing.Sequence[
typing.Tuple[
values: Sequence[
tuple[
str, # file
str, # archive
int, # bit_depth
Expand Down Expand Up @@ -522,8 +525,8 @@ def _column_loc(
self,
column: str,
file: str,
dtype: typing.Callable = None,
) -> typing.Any:
dtype: Callable = None,
) -> object:
r"""Column content for selected file.
Args:
Expand Down Expand Up @@ -569,7 +572,7 @@ def _dataframe_to_table(
table = table.rename_columns(columns)
return table

def _drop(self, files: typing.Sequence[str]):
def _drop(self, files: Sequence[str]):
r"""Drop files from table.
Args:
Expand Down Expand Up @@ -638,8 +641,8 @@ def _table_to_dataframe(self, table: pa.Table) -> pd.DataFrame:

def _update_media(
self,
values: typing.Sequence[
typing.Tuple[
values: Sequence[
tuple[
str, # file
str, # archive
int, # bit_depth
Expand Down Expand Up @@ -670,7 +673,7 @@ def _update_media(

def _update_media_version(
self,
files: typing.Sequence[str],
files: Sequence[str],
version: str,
):
r"""Update version of media files.
Expand All @@ -685,7 +688,7 @@ def _update_media_version(

def error_message_missing_object(
object_type: str,
missing_object_id: typing.Union[str, typing.Sequence],
missing_object_id: str | Sequence,
database_name: str = None,
database_version: str = None,
) -> str:
Expand Down Expand Up @@ -720,12 +723,12 @@ def error_message_missing_object(


def filter_deps(
requested_deps: typing.Optional[typing.Union[str, typing.Sequence[str]]],
available_deps: typing.Sequence[str],
requested_deps: str | Sequence[str] | None,
available_deps: Sequence[str],
deps_type: str,
database_name: str = None,
database_version: str = None,
) -> typing.Sequence[str]:
) -> Sequence[str]:
r"""Filter dependency files by requested files.
Args:
Expand Down Expand Up @@ -778,7 +781,7 @@ def filter_deps(


def download_dependencies(
backend_interface: typing.Type[audbackend.interface.Base],
backend_interface: type[audbackend.interface.Base],
name: str,
version: str,
verbose: bool,
Expand Down Expand Up @@ -833,7 +836,7 @@ def download_dependencies(


def upload_dependencies(
backend_interface: typing.Type[audbackend.interface.Base],
backend_interface: type[audbackend.interface.Base],
deps: Dependencies,
db_root: str,
name: str,
Expand Down
12 changes: 7 additions & 5 deletions audb/core/flavor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from __future__ import annotations

from collections.abc import Sequence
import os
import shutil
import typing

import numpy as np

Expand Down Expand Up @@ -53,7 +55,7 @@ def __init__(
self,
*,
bit_depth: int = None,
channels: typing.Union[int, typing.Sequence[int]] = None,
channels: int | Sequence[int] = None,
format: str = None,
mixdown: bool = False,
sampling_rate: int = None,
Expand Down Expand Up @@ -150,9 +152,9 @@ def short_id(
def _check_convert(
self,
file: str,
bit_depth: typing.Optional[int],
channels: typing.Optional[int],
sampling_rate: typing.Optional[int],
bit_depth: int | None,
channels: int | None,
sampling_rate: int | None,
) -> bool:
r"""Check if file needs to be converted to flavor."""
format = audeer.file_extension(file).lower()
Expand Down
Loading

0 comments on commit e86059a

Please sign in to comment.