penman.constant

Functions for working with constant values.

When a PENMAN string is parsed to a tree or a graph, constant values are left as strings or, if the value is missing, as None. Penman nevertheless recognizes four datatypes commonly used in PENMAN data: integers, floats, strings, and symbols. A fifth type, called a “null” value, is used when an attribute is missing its target, but aside from robustness measures it is not a supported datatype.

Enumerated Datatypes

penman.constant.SYMBOL = <Type.SYMBOL: 'Symbol'>

Symbol constants (e.g., (... :polarity -))

penman.constant.STRING = <Type.STRING: 'String'>

String constants (e.g., (... :op1 "Kim"))

penman.constant.INTEGER = <Type.INTEGER: 'Integer'>

Integer constants (e.g., (... :value 12))

penman.constant.FLOAT = <Type.FLOAT: 'Float'>

Float constants (e.g., (... :value 1.2))

penman.constant.NULL = <Type.NULL: 'Null'>

Empty values (e.g., (... :ARG1 ))

Module Functions

penman.constant.type(constant_string)[source]

Return the type of constant encoded by constant_string.

Examples

>>> from penman import constant
>>> constant.type('-')
<Type.SYMBOL: 'Symbol'>
>>> constant.type('"foo"')
<Type.STRING: 'String'>
>>> constant.type('1')
<Type.INTEGER: 'Integer'>
>>> constant.type('1.2')
<Type.FLOAT: 'Float'>
>>> constant.type('')
<Type.NULL: 'Null'>
penman.constant.evaluate(constant_string)[source]

Evaluate and return constant_string.

If constant_string is None or an empty symbol (''), this function returns None, while an empty string constant ('""') returns an empty str object (''). Otherwise, symbols are returned unchanged while strings get quotes removed and escape sequences are unescaped. Note that this means it is impossible to recover the original type of strings and symbols once they have been evaluated. For integer and float constants, this function returns the equivalent Python int or float objects.

Examples

>>> from penman import constant
>>> constant.evaluate('-')
'-'
>>> constant.evaluate('"foo"')
'foo'
>>> constant.evaluate('1')
1
>>> constant.evaluate('1.2')
1.2
>>> constant.evaluate('') is None
True
penman.constant.quote(constant)[source]

Return constant as a quoted string.

If constant is None, this function returns an empty string constant ('""'). All other types are cast to a string and quoted.

Examples

>>> from penman import constant
>>> constant.quote(None)
'""'
>>> constant.quote('')
'""'
>>> constant.quote('foo')
'"foo"'
>>> constant.quote('"foo"')
'"\\"foo\\""'
>>> constant.quote(1)
'"1"'
>>> constant.quote(1.5)
'"1.5"'