Utility Lenses

Here are some more useful lenses, though some are provided more as a demonstration of how to extend base lenses, so you might wish to adapt the ideas for your specific use.

class pylens.util_lenses.List(lens, delimiter_lens, **options)

Shortcut lens for delimited lists.

class pylens.util_lenses.NewLine(**options)

Matches a newline char or the end of text, so extends the Or lens.

class pylens.util_lenses.Word(body_chars, init_chars=None, min_count=1, max_count=None, negate=False, **options)

Useful for handling keywords of a specific char range.

class pylens.util_lenses.Whitespace(default=' ', optional=False, space_chars=' t', slash_continuation=False, indent_continuation=False, **options)

Whitespace helper lens, that knows how to handle (logically) continued lines with ‘n’ or that preclude an indent which are useful for certain config files.

class pylens.util_lenses.NullLens(type=None, name=None, default=None, **options)

When writing new lenses, particularly in a top-down fashion, this lens is useful for filling in lens branches that are yet to be completed.

Arguments:

  • type: The native type of this lens (e.g. int, str, some LensObject class). If set, this is a STORE lens, otherwise a NON-STORE lens.
  • name: For assigning a name to this lens for debugging purposes.
  • default: the default output of this lens if a NON-STORE lens.
  • options: arbitrary keyword options can be set on a lens to affect its functionality in an extendible way. For example, we might use this to specify how we align data items to their original position in the source string, perhaps by some label, their original position, or by some order in the native type.
class pylens.util_lenses.KeyValue(*args, **options)

Simply sets up the Group as an auto_list, which is useful when we just wish to store a value by a key.

class pylens.util_lenses.BlankLine(**options)

Matches a blank line (i.e. optional whitespace followed by NewLine().

class pylens.util_lenses.Keyword(additional_chars='_', **options)

A lens for matching a typical keyword.

class pylens.util_lenses.AutoGroup(lens, **options)

Sometimes it may be convenient to not explicitly set a type on an outer lens in order to extract one or more items from sub-lenses, so this lens allows an outer container to be set automatically, using auto_list such that a single item may be passed through the lens. If the enclosed lens has a type, then this lens simply becomes a transparent wrapper.

Note, this replaces __init__ of Group, which checks for a type.

class pylens.util_lenses.HashComment(**options)

A common hash comment.

Previous topic

Core Lenses

This Page