tdk.internal.utils

This module contains miscellaneous utilities for the library.

Module Contents

Functions

make_sync

Make an async function run synchronously.

int_or_none_as_str

Convert a string to an int or a None.

str_or_none_as_str

Convert a string to a str or None.

sound_url_validator

Convert a sound code to a valid sound URL.

image_url_validator

Convert an image code to a valid image URL.

adapt_input_to_enum

Get an enum member from an enum instance, value or name.

assert_not_found

Assert that the data is a NOT_FOUND response.

validate_property

Validate a meaning property.

Data

IntOrNone

pydantic.BeforeValidator type hint for an integer or None.

StrOrNone

pydantic.BeforeValidator type hint for a string or None.

SoundURL

pydantic.AfterValidator type hint for a sound URL.

ImageURL

pydantic.AfterValidator type hint for an image URL.

NOT_FOUND

The constant “not found” response of the API, decoded from JSON.

ValidatedProperty

pydantic.BeforeValidator type hint for a validated meaning property.

API

tdk.internal.utils.make_sync(func_to_be_cloned, /)

Make an async function run synchronously.

Creates a decorator that runs the async function given as a parameter synchronously.

Important

The wrapped function is discarded and not used.

Example usage

from tdk.internal.utils import make_sync

async def wait():
    from asyncio import sleep
    await sleep(1)
    return "Hello, world!"

@make_sync(wait)
def wait_sync(): ...

print(wait_sync())  # Hello, world!
tdk.internal.utils.int_or_none_as_str(value: str, /) int | None

Convert a string to an int or a None.

Empty strings are converted to None, other strings are converted to type int.

tdk.internal.utils.IntOrNone

None

pydantic.BeforeValidator type hint for an integer or None.

Works using int_or_none_as_str.

tdk.internal.utils.str_or_none_as_str(value: str, /) str | None

Convert a string to a str or None.

Empty strings are converted to None, other strings are converted to type str.

tdk.internal.utils.StrOrNone

None

pydantic.BeforeValidator type hint for a string or None.

Works using str_or_none_as_str.

tdk.internal.utils.sound_url_validator(v: str, /) str

Convert a sound code to a valid sound URL.

Strings that do not start with https:// are converted to https://sozluk.gov.tr/ses/{}.wav.

tdk.internal.utils.SoundURL

None

pydantic.AfterValidator type hint for a sound URL.

Works using sound_url_validator.

tdk.internal.utils.image_url_validator(v: str, /) str

Convert an image code to a valid image URL.

Strings that do not start with https:// are converted to https://sozluk.gov.tr/dosyalar/tarornek/{}.gif.

tdk.internal.utils.ImageURL

None

pydantic.AfterValidator type hint for an image URL.

Works using image_url_validator.

tdk.internal.utils.adapt_input_to_enum(input: Any, enum: Type[enum.Enum]) enum.Enum

Get an enum member from an enum instance, value or name.

>>> from enum import Enum
>>> class Color(Enum):
...     RED = 1
...     GREEN = 2
...     BLUE = 3
...
>>> adapt_input_to_enum(Color.RED, Color)
<Color.RED: 1>
>>> adapt_input_to_enum(2, Color)
<Color.GREEN: 2>
>>> adapt_input_to_enum("blue", Color)
<Color.BLUE: 3>
Raises:

ValueError – If input is not found in enum.

tdk.internal.utils.NOT_FOUND: dict

None

The constant “not found” response of the API, decoded from JSON.

The TDK APIs always return this response when the requested data is not found.

tdk.internal.utils.assert_not_found(data: Any, /)

Assert that the data is a NOT_FOUND response.

Raises:
tdk.internal.utils.validate_property(v: str | int | tdk.enums.MeaningProperty, /)

Validate a meaning property.

If the input is a MeaningProperty instance, it is returned as is. Otherwise, MeaningProperty.get is used to find the correct MeaningProperty instance, and it is returned.

tdk.internal.utils.ValidatedProperty

None

pydantic.BeforeValidator type hint for a validated meaning property.

Works using validate_property.