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: 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.

gavo.base.literals.getDefaultValueParsers()[source]

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.

gavo.base.literals.originalOrIdentity(soup)[source]

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

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

gavo.base.literals.parseBooleanLiteral(literal)[source]

returns a python boolean from some string.

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

gavo.base.literals.parseBytes(literal)[source]

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)

gavo.base.literals.parseCooPair(soup)[source]

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
gavo.base.literals.parseDefaultDate(literal: Optional[Union[str, date]]) Optional[date][source]

parseDefaultDatetime’s little sister.

gavo.base.literals.parseDefaultDatetime(literal: Optional[Union[str, datetime]]) Optional[datetime][source]

returns a datetime from string or passes through datetimes and Nones.

The function will try to parse a string in various ways; we will try not to drop formats from one minor version to the next.

gavo.base.literals.parseDefaultTime(literal: Optional[Union[str, time]]) Optional[time][source]

parseDefaultDatetime’s other little sister.

gavo.base.literals.parseFloat(literal)[source]

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 ")
5000000000.0
>>> parseFloat(None)
>>> parseFloat("  ")
>>> parseFloat("wobbadobba")
Traceback (most recent call last):
ValueError: could not convert string to float: 'wobbadobba'
gavo.base.literals.parseInt(literal)[source]

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

>>> parseInt("32")
32
>>> parseInt("")
>>> parseInt(None)
gavo.base.literals.parseSPoint(soup)[source]

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.

gavo.base.literals.parseSimpleSTCS(s)
gavo.base.literals.parseUnicode(literal)[source]