gavo.votable.coding module

Common code for coding and decoding VOTable data.

gavo.votable.coding.buildCodec(source, env)[source]

returns a compiled function for source in env.

Source is the result of one of the makeXXX functions in this module, env typically the result of a getGlobals() on the codec module.

gavo.votable.coding.buildDecoder(tableDefinition, decoderModule)[source]
gavo.votable.coding.buildEncoder(tableDefinition, encoderModule)[source]
gavo.votable.coding.getNullvalue(field, validator, default=None)[source]

returns None or the nullvalue defined for field.

validator is a function that raises some exception if the nullvalue is inappropriate. It should do so in particular on everything that contains quotes and such; the nullvalues are included in source code and thus might be used to inject code if not validated.

gavo.votable.coding.getRowEncoderSource(tableDefinition, encoderModule)[source]

returns the source for a function encoding rows of tableDefition in the format implied encoderModule

tableDefinition is a VOTable.TABLE instance, encoderModule is one of the enc_whatever modules (this function needs getLinesFor and getPostamble from them).


returns code lines to validate an an array shape against a flat sequence in row.

This is used by the array decoders.

gavo.votable.coding.parseVOTableArraysizeEl(spec, fieldName)[source]

parses a single VOTable arraysize number to (flexible, length).

This will accept single numbers (returns False, number), number* (returns True, number) and just * (returns 0, number).

This is used to parse the last part of an n-d array spec. Everything before that must be an integer only.


expands flattens out any sub-sequences (lists or tuples) in seq recursively.

This is used by the array encoders.

gavo.votable.coding.trim(seq, arraysize, padder)[source]

returns seq with length arraysize.

arraysize is an int; you should just use field.getLength() when trimming VOTable arraysizes since the arraysize attribute is rather complex. Arraysize may be None for convenience; trim is a no-op then.

If seq is shorter, padder*missing will be appended, if it is longer, seq will be shortened from the end.

This is intended as a helper for array encoders.

gavo.votable.coding.trimString(val, arraysize, padChar=' ')[source]

returns val flattened and padded with padChar/cropped to length.

field is a V.FIELD or V.PARAM instance for which val should be prepared.

val can also be a sequence of strings (or nested more deeply). In that case, trimString will flatten the value(s), padding and cropping as necessary.

If val is None, then as many padChars will be returned as arraysize wants (which is 0 for variable-length fields).

trimString expects to deal with strings. It will ascii-decode bytes if it sees them, though.

For chars, arraysize None is equivalent to arraysize 1.

>>> trimString("abc", "4")
'abc '
>>> trimString(["abc", "de", "f"], "2x*")
'abdef '
>>> trimString([["abc", "cd", "e"], ["", "fgh", "i"]], "2x4x3")
'abcde     fgi           '
>>> trimString(None, "4x2", 'z')
>>> trimString(None, "4x2*", 'z')
>>> trimString("abc", None)
>>> trimString(b"abc", "5", "x")
gavo.votable.coding.unravelArray(arraysize, seq)[source]

turns a flat sequence into an n-dim array as specified by the votable arraysize spec arraysize.

arraysize is <int>{“x”<int>}*?|*.

No padding or cropping will take place. This means that the last row(s) may have improper sizes if seq is incompatible with arraysize.

>>> unravelArray("2x3", "012345")
['01', '23', '45']
>>> unravelArray("2x*", "012345")
['01', '23', '45']
>>> unravelArray("3x2x*", "012345012345")
[['012', '345'], ['012', '345']]