gavo.base.literals module

Functions taking strings and returning python values.

All of them accept None and return None for Nullvalue processing.

All of them leave values alone if they already have the right type.

This is usually used in conjunction with base.typesystems.ToPythonCodeConverter.

class gavo.base.literals.NumericRange(lower=None, upper=None, bounds='[)', empty=False)[source]

Bases: psycopg2._range.Range

A Range suitable to pass Python numeric types to a PostgreSQL range.

PostgreSQL types :sql:`int4range`, :sql:`int8range`, :sql:`numrange` are casted into !NumericRange instances.


returns a dict containing all exported names from this module.

This is useful with typesystems.ToPythonCodeConverter; see rscdef.column.Parameter for an example.

This is always the same dict; thus, if you change it, copy it first.


returns soup.original or soup if there is no original attribute.

This is for cooperation with BinaryItem coming in from the web into ContextGrammars.


returns a python boolean from some string.

Boolean literals are strings like True, false, on, Off, yes, No in some capitalization.


returns bytes from a literal.

This will interpret hex and octal byte escapes, and it’ll support lists of integer-like things; not sure if that’s actually more harmful than good. But then people can always override the default behaviour. >>> parseBytes(“abc”) b’abc’ >>> parseBytes(r”xab000”) b’xabx00’ >>> parseBytes([123, 231, 23]) b’{xe7x17’ >>> parseBytes([10002]) Traceback (most recent call last): ValueError: bytes must be in range(0, 256)


returns a pair of RA, DEC floats if they can be made out in soup or raises a value error.

No range checking is done (yet), i.e., as long as two numbers can be made out, the function is happy.

>>> parseCooPair("23 12")
(23.0, 12.0)
>>> parseCooPair("23.5,-12.25")
(23.5, -12.25)
>>> parseCooPair("3.75 -12.125")
(3.75, -12.125)
>>> parseCooPair("3 25,-12 30")
(51.25, -12.5)
>>> ["{:.9f}".format(v) for v in parseCooPair("12 15 30.5 +52 18 27.5")]
['183.877083333', '52.307638889']
>>> parseCooPair("3.39 -12 39")
Traceback (most recent call last):
ValueError: Invalid time with sepChar None: '3.39'
>>> parseCooPair("12 15 30.5 +52 18 27.5e")
Traceback (most recent call last):
ValueError: 12 15 30.5 +52 18 27.5e has no discernible position in it
>>> parseCooPair("QSO2230+44.3")
Traceback (most recent call last):
ValueError: QSO2230+44.3 has no discernible position in it

returns a float from a literal, or None if literal is None or an empty string.

Temporarily, this includes a hack to work around a bug in psycopg2.

>>> parseFloat("   5e9 ")
>>> parseFloat(None)
>>> parseFloat("  ")
>>> parseFloat("wobbadobba")
Traceback (most recent call last):
ValueError: could not convert string to float: 'wobbadobba'

returns an int from a literal, or None if literal is None or an empty string.

>>> parseInt("32")
>>> parseInt("")
>>> parseInt(None)

returns an SPoint for a coordinate pair.

The coordinate pair can be formatted in a variety of ways; see the `function parseCooPair`_. Input is always in degrees.