id_translation.Translator.fetch#
- Translator.fetch(translatable=None, names=None, *, ignore_names=None, override_function=None, max_fails=1.0, fmt=None)[source]#
Fetch translations.
Calling
fetchwithout arguments will perform aFetcher.fetch_all()-operation, without going offline.The returned
TranslationMapmay be converted to native types withTranslationMap.to_dicts().- Parameters:
translatable – A data structure to translate.
names – Explicit names to translate. Derive from translatable if
None. Alternatively, you may pass adicton the form{name_in_translatable: source_to_use}.ignore_names – Names not to translate, or a predicate
(NameType) -> bool.override_function – A callable
(name, sources, ids) -> Source | None. SeeMapper.apply()for details.max_fails – The maximum fraction of IDs for which translation may fail. 1=disabled.
fmt – A
format stringsuch as ‘{id}:{name}’ use. Default isTranslator.fmt.
- Returns:
- Raises:
ConnectionStatusError – If disconnected from the fetcher, i.e. not
online.
Examples
Using the returned
TranslationMapclass.>>> translation_map = translator.fetch() >>> translation_map TranslationMap('animals': 3 IDs, 'people': 2 IDs)
Convert to finished translations.
TranslationMap.to_translations()→{source: MagicDict}, where aMagicDictis similar to a regulardict[IdType, str]-type dict.
>>> people = translation_map.to_translations()["people"] >>> people {1999: '1999:Sofia', 1991: '1991:Richard'}
Warning
The
MagicDictclass is used internally and has a few important differences from the built-in type. Please refer to theMagicDictclass documentation for details.To convert to a
MagicDictto a regulardict, simply use the dict constructor:>>> dict(people) {1999: '1999:Sofia', 1991: '1991:Richard'}
Convert to raw translation data.
TranslationMap.to_pandas()→{source: DataFrame}TranslationMap.to_dicts()→{source: {placeholder: [values...]}}
>>> translation_map.to_dicts()["people"] {'id': [1999, 1991], 'name': ['Sofia', 'Richard']}