id_translation.Translator#
- class Translator(fetcher=None, fmt='{id}:{name}', mapper=None, default_fmt='<Failed: id={id!r}>', default_fmt_placeholders=None, enable_uuid_heuristics=False, transformers=None)[source]#
Bases:
Generic[NameType,SourceType,IdType],HasSources[SourceType]End-user interface for all translation tasks.
See
Translator.translate()for runtime configuration options. Any argument chosen when theTranslatoris created can be overridden withTranslator.copy(). Usego_offline()to store translations in memory.- Parameters:
fetcher – A
Fetcheror ready-to-use translations.fmt – String
Formatspecification for translations.mapper – A
Mapperinstance for binding names to sources.default_fmt – Alternative
Formatto use fallback translation of unknown IDs.default_fmt_placeholders – Shared and/or source-specific default placeholder values for unknown IDs. See
InheritedKeysDict.make()for details.enable_uuid_heuristics – Improves matching when
UUID-like IDs are in use.transformers – A dict
{source: transformer}of initializedTransformerinstances.
Examples
Basic usage. For a more complete use case, see the Sakila DVD Rental Database example.
Assume that we have data for people and animals as in the tables below:
people: animals: id | name id | name | is_nice ------+--------- ----+--------+--------- 1991 | Richard 0 | Tarzan | false 1999 | Sofia 1 | Morris | true 1904 | Fred 2 | Simba | true
In most real cases we’d fetch this table from somewhere. In this case, however, there’s so little data that we can simply enumerate the components needed for translation ourselves.
>>> from id_translation import Translator >>> translation_data = { ... "animals": { ... "id": [0, 1, 2], ... "name": ["Tarzan", "Morris", "Simba"], ... "is_nice": [False, True, True], ... }, ... "people": {1999: "Sofia", 1991: "Richard", 1904: "Fred"}, ... } >>> fmt = "{id}:{name}[, nice={is_nice}]" >>> translator = Translator(translation_data, fmt=fmt)
Since people only has columns id and name, we can use the simplified
{id: name}data format. We’re using the full format for animals since we have an additional is_nice column in this table. We didn’t define aMapper, so the column names must match exactly.>>> import pandas as pd >>> df = pd.DataFrame({"animals": [0, 2], "people": [1991, 1999]}) >>> translator.translate(df) # Returns a copy animals people 0 0:Tarzan, nice=False 1991:Richard 1 2:Simba, nice=True 1999:Sofia
Check out the Translation primer to learn how this is done “under the hood”.
Methods
copy(**overrides)Make a copy of this
Translator.extract_names(translatable, *[, ...])Extract names in translatable.
fetch([translatable, names, ignore_names, ...])Fetch translations.
from_config(path[, extra_fetchers])Create a
Translatorfrom TOML inputs.go_offline([translatable, names, ...])Retrieve and store translations in memory.
initialize_sources([task_id, force])Perform source discovery (fetcher initialization).
load_persistent_instance(cache_dir, config_path)Load or create a persistent
fetch_all-instance.map(translatable[, names, ignore_names, ...])Map names to translation sources.
map_scores(translatable[, names, ...])Returns raw match scores for name-to-source mapping.
restore(path)Restore a serialized
Translator.translate(translatable[, names, ...])Translate IDs to human-readable strings.
translated_names([with_source])Return the names that were translated by the most recent
translate()-call.Attributes
Return a
TranslationMapof cached translations.Return
from_config()initializationmetadata.Alternative translation
Format, used for unknown IDs.Improves matching when
UUID-like IDs are in use.Return the
Fetcherinstance used to retrieve translations.Main translation
Formatfor thisTranslatorinstance.Return the
Mapperinstance used for name-to-source binding.Return connectivity status.
A dict
{source: [placeholders, ...]}.A list of known sources names.
Get a dict
{source: transformer}ofTransformerinstances used by thisTranslator.