id_translation.types#
Types used for translation.
Hint
Use register_io() to register custom DataStructureIO implementations.
Rules of thumb#
The
IdTypesare the only “truly” translatable types. Collections thereof are also supported.Non-inplace (copy-translation, default) always tries to return a collection of the same type, with all
IdTypesconverted tostr.In-place translation always returns
None, or raisesNotInplaceTranslatableError.
Overloads err on the overly-permissive side. Static type checkers (only MyPy is tested) may incorrectly allow things
like translator.translate("a-string-id") for int-only Translator instances.
Limitations#
Python does not support generics-of-generics, which is the primary domain in which the Translator.translate-method
operates. See python/typing#548 for details on this subject.
Numpy is supported, but not typed.
Pandas typing must be enabled using the
ID_TRANSLATION_PANDAS_IS_TYPED-flag.
pip install pandas-stubs
mypy --always-true=ID_TRANSLATION_PANDAS_IS_TYPED /path/to/file.py
All pandas types will be None without stubs, which will break the overloads.
Module Attributes
Name of the ID placeholder in the translation |
|
Type of the value being translated into human-readable labels. |
|
Type variable bound by |
|
Type used to label collections of IDs, such as the column names in a DataFrame or the keys of a dict. |
|
Enumeration of translatable types. |
|
Type used to describe sources. |
|
A mapping from name to source. |
|
A predicate type on names. |
|
A union of a name type, or an iterable thereof. |
|
Acceptable name types. |
|
Simplified |
Classes
Indicates that sources and placeholders are available. |
- class HasSources[source]#
Bases:
ABC,Generic[SourceType]Indicates that sources and placeholders are available.
- abstract property placeholders#
Placeholders for all known Source names, such as
idorname.These are the (possibly unmapped) placeholders that may be used for translation.
- Returns:
A dict
{source: [placeholders..]}.
- property sources#
A list of known Source names, such as
citiesorlanguages.
- ID = 'id'#
Name of the ID placeholder in the translation
Format.Cannot be changed (yet); see rsundqvist/id-translation#151.
- IdTypes = int | str | uuid.UUID#
Type of the value being translated into human-readable labels.
- NameToSource#
A mapping from name to source.
alias of
dict[NameType,SourceType]
- class NameType#
Type used to label collections of IDs, such as the column names in a DataFrame or the keys of a dict.
alias of TypeVar(‘NameType’, bound=
Hashable)
- class SourceType#
Type used to describe sources. Typically a string for things like files and database tables.
alias of TypeVar(‘SourceType’, bound=
Hashable)
- Translatable#
Enumeration of translatable types.
Types
int,str, andUUIDcan be translated, or a collection thereof. Somenumpyandpandastypes are also supported. TheTranslatoris quite flexible when it comes to the encapsulating data structure, and will do its best to return a data structure of the same type (albeit with elements converted tostr).Note
Dict values are always copied for translation.
Setting
copy=Falsemerely controls whether the original dict is modified.alias of
dict[NameType,IdType] |dict[NameType,list[IdType]] |dict[NameType,set[IdType]] |dict[NameType,tuple[IdType]] |dict[NameType,tuple[IdType,IdType]] |dict[NameType,tuple[IdType,IdType,IdType]] |dict[NameType,tuple[IdType, …]] |list[IdType] |list[list[IdType]] |set[IdType] |IdType|tuple[IdType] |tuple[IdType,IdType] |tuple[IdType,IdType,IdType] |tuple[IdType, …] |pandas.DataFrame|pandas.Series|pandas.Index
- class TranslatableT#
Simplified
Translatabletype.alias of TypeVar(‘TranslatableT’, bound=
dict[Any,Any] |dict[Any,list[Any]] |dict[Any,set[Any]] |dict[Any,tuple[Any]] |dict[Any,tuple[Any,Any]] |dict[Any,tuple[Any,Any,Any]] |dict[Any,tuple[Any, …]] |list[Any] |list[list[Any]] |set[Any] |Any|tuple[Any] |tuple[Any,Any] |tuple[Any,Any,Any] |tuple[Any, …] |pandas.DataFrame|pandas.Series|pandas.Index)