Python
Converters, options, and errors
Options are given to each conversion, and checked strictly. Two objects can be made once and reused, and they do different things:
- Prepared options fix how the text is written.
prepare_options()checks and resolves options once, preset included, and every conversion takes the result in their place. - A converter fixes which characters a conversion knows.
create_converter()can bind a preset, whose style then applies to each call and whose standard’s characters it holds the text to; it can also add characters, or restrict them to a list or by exclusion. Its methods convert, with options given at each call.
| Prepared options | A converter | |
|---|---|---|
| Made by | prepare_options() |
create_converter() |
| It fixes | How the text is written | Which characters are converted |
| Use it for | Options of one’s own, over many calls | A standard’s characters, or characters of one’s own |
| It raises | When made | When made, and at a call: for another preset, or a character it rejects |
The two work together: a converter takes options at each call, prepared or not. With both a converter and options of one’s own, prepare the options with the converter’s preset, as Prepared options in a converter shows.
Options are checked
Section titled “Options are checked”A misspelled option never falls back silently to a default. An unknown key,
value, or preset raises an OptionError, a ValueError that names the option
(path), what it received, what it accepts, and the current spelling of a key
or value an earlier release knew; its message also names the option a key was
likely meant for, such as transliteration.long_vowels for transliteration.long-vowels.
A key written in camelCase, as the TypeScript library spells it, is
refused with its snake_case spelling. An unknown format raises a plain ValueError,
and an option group of the wrong type a TypeError. This matters most when
options come from a configuration file or a form:
import json
from greek_conversion import OptionError, convert
options = json.loads('{ "transliteration": { "eta": "ē" } }')try: convert("λόγος", "greek", "transliteration", options)except OptionError as error: problem = error
str(problem)# → Invalid value for transliteration.eta: "ē"; expected one of "e", "i".problem.path # → transliteration.etaproblem.accepted # → ('e', 'i')FORMATS and CONVERSION_OPTION_VALUES list everything that is accepted, which
is convenient for building a user interface:
from greek_conversion import CONVERSION_OPTION_VALUES, FORMATS
FORMATS # → ('greek', 'beta-code', 'transliteration')CONVERSION_OPTION_VALUES["transliteration"]["eta"] # → ['e', 'i']Prepare options once
Section titled “Prepare options once”prepare_options() checks and resolves options once, with the same errors,
and returns prepared options, which every conversion function takes in
their place:
from greek_conversion import convert, prepare_options
options = prepare_options(transliteration={"chi": "kh"})words = ["χάος", "ψυχή"][convert(word, "greek", "transliteration", options) for word in words]# → ['kháos', 'psykhḗ']The defaults and each preset named are resolved once anyway. Other options
are checked and resolved at each call, which a word takes about twice as
long to convert: prepare them where many short texts are converted, or
where options come from a configuration file or a form, to check them once.
Prepared options cannot be changed, nor be put in other options, as
source or otherwise, nor be given with keywords: each raises a
TypeError. A converter takes them too, as
Prepared options in a converter shows.
Create a converter
Section titled “Create a converter”create_converter() makes a converter from its configuration, which is not
options: preset, the preset it binds; out_of_scope_behavior, what it does
with the characters outside its scope; aliases and characters, the
spellings and characters it adds; repertoire and exclude, the characters
it keeps to or leaves out. Options given there raise an OptionError
(“Unknown converter option: transliteration”), and prepared options a
TypeError.
A converter has the conversion functions as methods, which take options as
the functions do: convert(), reencode(), convert_detailed(),
convert_greek_passages(), convert_greek_passages_detailed(), and the
_many forms of the first three. In the
Greek passages of a text,
its own Greek spellings, those of its aliases in Greek and the Greek forms of
its characters, are Greek, whatever characters they are made of.
Bind a preset
Section titled “Bind a preset”A preset plays one part in options, and two in a converter:
- Named in options, as
{"preset": "sbl-general"}, a preset is a style: a set of options, which the defaults complete and the options given with it refine. Every conversion function takes it so, andprepare_options()too: prepared options hold a preset’s style, never its scope. - Bound by a converter, as the
presetof its configuration, it is that style and a scope: the characters that its standard provides for, as the preset repertoire audit records them. The converter preserves and reports the others, or rejects them. The options of a call refine the bound preset’s; another preset raises aValueError.
The SBL Handbook makes no provision for the digamma. Named, the preset writes it as the library does; bound, the converter keeps it in Greek and says where:
from greek_conversion import convert, create_converter
convert("ὁ ϝάναξ", "greek", "transliteration", preset="sbl-general")# → ho wanax
sbl = create_converter(preset="sbl-general")result = sbl.convert_detailed("ὁ ϝάναξ", "greek", "transliteration")result.output # → ho ϝanaxresult.diagnostics[0].code # → out-of-scope-characterresult.diagnostics[0].offset # → 2With out_of_scope_behavior="reject", the conversion raises instead, and the
error’s diagnostics list each rejected occurrence; here, koppa, for which
ISO 843 defines no transliteration:
from greek_conversion import create_converter
iso = create_converter( preset="iso-843-type-1", out_of_scope_behavior="reject",)iso.convert("ϟ", "greek", "transliteration")# raises CharacterScopeError: Conversion rejected: 1 unsupported character.source is the exception to the bound preset: it describes the input, is
resolved on its own, and may name any preset, so that source={} reads the
library’s default style, not the bound preset’s. Name a preset for its style
alone: convert() given its name costs less than a converter. Bind it where
the text must keep to its standard’s characters. See
character extensions and repertoires.
Add, restrict, or exclude characters
Section titled “Add, restrict, or exclude characters”A converter can teach new spellings of a letter, aliases, or characters of
its own, characters; keep to a list of characters, repertoire; or leave
some out, exclude:
from greek_conversion import create_converter
converter = create_converter( aliases=[ { "letter": "theta", "format": "transliteration", "spellings": ["þ"], }, ], exclude=["stigma", "koppa", "sampi"],)
converter.convert("þeos", "transliteration", "greek") # → θεοςAn alias inherits every rule of its built-in letter. A new character definition
maps directly between formats and keeps its case, but does not join the
contextual rules for diphthongs, breathings, or numerals. Excluded characters
are kept as the source writes them in their word. Of the options, only the
transformations of the whole text (text) still apply to them:
from greek_conversion import create_converter
no_stigma = create_converter(exclude=["stigma"])
no_stigma.convert( "λόγος ϛ", "greek", "transliteration", text={"letter_case": "uppercase"},)# → LÓGOS ϚSee character extensions and repertoires for the definitions of aliases and characters, and the identifiers of the characters.
Prepared options in a converter
Section titled “Prepared options in a converter”A converter takes options at each call, as the functions do, and prepared
options among them. Prepared options are already resolved: the values of a
preset, or of the defaults, merged with the options given. Which values were
given is lost, so they cannot be resolved again on another preset: a
converter takes options prepared with its preset, or any when it binds none,
and raises a ValueError for others. With a converter and options of one’s
own, prepare the options once, with the converter’s preset, and give them to
each call:
from greek_conversion import create_converter, prepare_options
sbl = create_converter(preset="sbl-general", out_of_scope_behavior="reject")capitals = prepare_options( preset="sbl-general", text={"letter_case": "uppercase"},)[ sbl.convert(word, "greek", "transliteration", capitals) for word in ["λόγος", "ψυχή"]]# → ['LOGOS', 'PSYCHĒ']Options prepared without the preset would raise: “Converter is bound to preset sbl-general; received options prepared without a preset.”