xotl.tools.values.simple – Simple or internal coercers

Simple or internal coercers.

With coercers defined in this module, many of the xotl.tools.string utilities could be deprecated.

In Python 3, all arrays, not only those containing valid byte or unicode chars, are buffers.

xotl.tools.values.simple.ascii_coerce(arg)[source]

Coerce to string containing only ASCII characters.

Convert all non-ascii to valid characters using unicode ‘NFKC’ normalization.

xotl.tools.values.simple.ascii_set_coerce(arg)[source]

Coerce to string with only ASCII characters removing repetitions.

Convert all non-ascii to valid characters using unicode ‘NFKC’ normalization.

xotl.tools.values.simple.bytes_coerce(arg)[source]

Encode an unicode string (or any object) returning a bytes buffer.

Uses the defined encoding system value.

In Python 2.x bytes coincide with str type, in Python 3 str uses unicode and str is different to bytes.

There are differences if you want to obtain a buffer in Python 2.x and Python 3; for example, the following code obtain different results:

>>> ba = bytes([65, 66, 67])

In Python 2.x is obtained the string "[65, 66, 67]" and in Python 3 b"ABC". This function normalize these differences.

Name is used in named objects, see name_coerce() for more information.

See str_coerce() to coerce to standard string type, bytes in Python 2.x and unicode (str) in Python 3.

Always returns the bytes type.

New in version 1.7.0.

xotl.tools.values.simple.chars_coerce(arg)[source]

Convert to unicode characters.

If arg is an integer between 0 and 0x10ffff is converted assuming it as ordinal unicode code, else is converted with unicode_coerce().

xotl.tools.values.simple.collection(arg=nil, avoid=(), force=False, base=None, name=None)[source]

Coercer for logic collections.

Inner coercer returns the same argument if it is a strict iterable. In Python, strings are normally iterables, but never in our logic. So:

>>> collection('abc') is nil
True

This function could directly check an argument if it isn’t nil, or returns a coercer using extra parameters:

Parameters:
  • avoid

    a type or tuple of extra types to ignore as valid collections; for example:

    >>> collection(avoid=dict)({}) is nil
    True
    >>> collection()({}) is nil
    False
    
  • force

    if main argument is not a valid collection, it is are wrapped inner a list:

    >>> collection(avoid=(dict,), force=True)({}) == [{}]
    True
    
  • base – if not None, must be the base to check instead of Iterable.
  • name – decorate inner coercer with that function name.
xotl.tools.values.simple.decode_coerce(arg)[source]

Decode objects implementing the buffer protocol.

xotl.tools.values.simple.encode_coerce(arg)[source]

Encode string objects.

xotl.tools.values.simple.force_collection_coerce(arg)

Return the same argument if it is a strict iterable. Strings and (<class ‘collections.abc.Mapping’>,) are not considered valid iterables in this case. A non iterable argument is wrapped in a list.

xotl.tools.values.simple.force_iterable_coerce(arg)

Return the same argument if it is a strict iterable. Strings are not considered valid iterables in this case. A non iterable argument is wrapped in a list.

xotl.tools.values.simple.force_sequence_coerce(arg)

Return the same argument if it is a strict iterable. Strings and (<class ‘collections.abc.Mapping’>,) are not considered valid iterables in this case. A non iterable argument is wrapped in a list.

xotl.tools.values.simple.isnot(value)[source]

Create a coercer that returns arg if arg is not value.

xotl.tools.values.simple.iterable_coerce(arg)[source]

Return the same argument if it is an iterable.

xotl.tools.values.simple.logic_collection_coerce(arg)

Return the same argument if it is a strict iterable. Strings and (<class ‘collections.abc.Mapping’>,) are not considered valid iterables in this case.

xotl.tools.values.simple.logic_iterable_coerce(arg)

Return the same argument if it is a strict iterable. Strings are not considered valid iterables in this case.

xotl.tools.values.simple.logic_sequence_coerce(arg)

Return the same argument if it is a strict iterable. Strings and (<class ‘collections.abc.Mapping’>,) are not considered valid iterables in this case.

xotl.tools.values.simple.lower_ascii_coerce(arg)[source]

Coerce to string containing only lower-case ASCII characters.

Convert all non-ascii to valid characters using unicode ‘NFKC’ normalization.

xotl.tools.values.simple.lower_ascii_set_coerce(arg)[source]

Coerce to string with only lower-case ASCII chars removing repetitions.

Convert all non-ascii to valid characters using unicode ‘NFKC’ normalization.

xotl.tools.values.simple.name_coerce(arg)[source]

If arg is a named object, return its name, else nil.

Object names are always of str type, other types are considered invalid.

Generator objects has the special __name__ attribute, but they are ignored and considered invalid.

xotl.tools.values.simple.not_false(default)[source]

Create a coercer that returns default if arg is considered false.

See not_false_coercer() for more information on values considered false.

xotl.tools.values.simple.not_false_coercer(arg)[source]

Validate that arg is not a false value.

Python convention for values considered True or False is not used here, our false values are only None or any false instance of xotl.tools.symbols.boolean (of course including False itself).

xotl.tools.values.simple.str_coerce(arg)[source]

Coerce to standard string type.

bytes in Python 2.x and unicode (str) in Python 3.

New in version 1.7.0.

Deprecated since version 2.0.6.

xotl.tools.values.simple.strict_string_coerce(arg)[source]

Coerce to string only if argument is a valid string type.

class xotl.tools.values.simple.text[source]

Return a nice text representation of one object.

text(obj=’’) -> text

text(bytes_or_buffer[, encoding[, errors]]) -> text

Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object text representation.

Parameters:
  • encoding – defaults to sys.getdefaultencoding().
  • errors – defaults to ‘strict’.

Method join is improved, in order to receive any collection of objects, as variable number of arguments or as one iterable.

chr_join(variable_number_args or iterable) → text[source]

Return a text which is the concatenation of the objects (converted to text) in argument items. The separator between elements is S.

Difference with join() is that integers between 0 and 0x10ffff are converted to characters as unicode ordinal.

join(variable_number_args or iterable) → text[source]

Return a text which is the concatenation of the objects (converted to text) in argument items. The separator between elements is S.

See chr_join() for other vertion of this functionality.

xotl.tools.values.simple.unicode_coerce(arg)[source]

Decode a buffer or any object returning unicode text.

Uses the defined encoding system value.

In Python 2.x unicode has a special type different to str but in Python 3 coincide with str type.

Name is used in named objects, see name_coerce() for more information.

See str_coerce() to coerce to standard string type, bytes in Python 2.x and unicode (str) in Python 3.

New in version 1.7.0.