templatest

Templates for testing with strings.

class templatest.BaseTemplate[source]

Bases: ABC

Abstract base class for string template and expected tests.

Override the template() abstract property method with a string that the test will be working with.

Override the expected() abstract property method with the expected result that the test will produce.

abstract property expected : str

Expected result.

property name : str

The name of the inherited class, parsed for test ID.

abstract property template : str

Template to test.

class templatest.Registered(*args: Template)[source]

Bases: MutableSequence[Template]

Mutable sequence of Template named tuples.

Parameters:
*args: Template

Instantiate with any number of Template objects.

filtergroup(*prefix: str) Registered[source]

Get new object excluding templates sharing a prefix.

Parameters:
*prefix: str

Common prefix(s) to registered subclasses.

Returns:

New Registered object containing Template objects that do not have the provided prefix in their names.

getbyname(name: str) Template | None[source]

Get a template by name if it exists.

Parameters:
name: str

Name assigned to template.

Returns:

Template object.

getgroup(*prefix: str) Registered[source]

Get new object containing templates sharing a prefix.

Parameters:
*prefix: str

Common prefix(s) to registered subclasses.

Returns:

New Registered object containing Template objects with common prefix to their names.

getids() tuple[str, ...][source]

Returns a tuple of all the names of the classes contained.

Returns:

A tuple of names of the classes within this sequence.

getindex(name: str) int | None[source]

Get the index of a template by name if it exists.

Parameters:
name: str

Name assigned to template.

Returns:

Index of the template if it exists, else None.

namedtuple templatest.Template(name: str, template: str, expected: str)[source]

Bases: NamedTuple

Contains registered BaseTemplate properties.

Fields:
  1.  name (str) – The name of the inherited class, parsed for test ID.

  2.  template (str) – Template to test.

  3.  expected (str) – Expected result.

templatest.exceptions

exception templatest.exceptions.NameConflictError(base_template: BaseTemplate, name: str)[source]

Bases: Exception

Raise if non-unique templatest.BaseTemplate name added.

Parameters:
base_template: BaseTemplate

templatest.BaseTemplate subclass which could not be registered.

name: str

Name of templatest.BaseTemplate subclass that is already registered.

templatest.templates

Registration and usage of template subclasses.

templatest.templates.register(base_template: type[BaseTemplate]) type[BaseTemplate][source]

Register templatest.BaseTemplate subclasses.

Decorate subclass definition to register.

Registered subclasses of templatest.BaseTemplate can be accessed through registered.

Parameters:
base_template: type[BaseTemplate]

templatest.BaseTemplate object.

Returns:

templatest.BaseTemplate object.

templatest.templates.registered = <Registered []>

Instantiated templatest.Registered object for accessing templatest.Template properties

Populated through registering templatest.BaseTemplate subclasses with register().

templatest.utils

Additional tools for working with string variables.

class templatest.utils.PosArgs(args: Any | None = None)[source]

Bases: List[str]

Get chain of strings as a list to represent positional arguments.

Example:
>>> from pathlib import Path
>>> from templatest.utils import PosArgs
>>> args = PosArgs('arg')
Traceback (most recent call last):
...
NotImplementedError: constructor cannot take args directly
>>> other_args = PosArgs()
>>> args = PosArgs(other_args)
Traceback (most recent call last):
...
NotImplementedError: constructor cannot take args directly
>>> args = PosArgs()
>>> args.path
['path']
>>> args("")
[]
>>> args("path")
['path']
>>> args.path.to
['path', 'to']
>>> args.path.to.another
['path', 'to', 'another']
>>> args.path.to.another.path
['path', 'to', 'another', 'path']
>>> args.path.to.another
['path', 'to', 'another']
>>> args.path.to
['path', 'to']
>>> args.path
['path']
>>> args.path.another
['path', 'another']
>>> args.path.another.new
['path', 'another', 'new']
>>> args.path.another.new.path
['path', 'another', 'new', 'path']
>>> args.src
['src']
>>> args.src.to
['src', 'to']
>>> args.src.to.another
['src', 'to', 'another']
>>> args.src.to.another.path
['src', 'to', 'another', 'path']
>>> args.src.to.another.path("")
['src', 'to', 'another', 'path']
>>> args.src.to.another.path("opt")
['src', 'to', 'another', 'path', 'opt']
>>> args.src.to.another.path("opt").another
['src', 'to', 'another', 'path', 'opt', 'another']
>>> path = Path("/tmp")
>>> args(path)
['/tmp']
Parameters:
args: Any | None = None

Args passed to child classes, not to be used to directly.

class templatest.utils.RandStrLenSeq(length: int)[source]

Bases: MutableStrSequence

Get random string of varying length.

The instantiated object will always return the same string as per the index. If index does not exist, strings will be generated up to the selected index.

Parameters:
length: int

Length of string.

Example:
>>> from templatest.utils import RandStrLenSeq
>>> LEN_3 = RandStrLenSeq(3)
>>> len(LEN_3)
0
>>> str_1 = LEN_3[0]
>>> type(str_1)
<class 'str'>
>>> len(str_1)
3
>>> len(LEN_3)
1
>>> str_2 = LEN_3[1]
>>> len(LEN_3)
2
>>> assert str_1 != str_2
class templatest.utils.VarPrefix(prefix: str, slug: str = '_')[source]

Bases: object

Get string with prefix.

Parameters:
prefix: str

String to prefix attribute with.

slug: str = '_'

Separator between words.

Example:
>>> from templatest.utils import VarPrefix
>>> flag = VarPrefix("--")
>>> slugify = VarPrefix("--", slug="-")
>>> flag.src
'--src'
>>> flag.dst
'--dst'
>>> flag.exclude
'--exclude'
>>> flag
<VarPrefix ['--src', '--dst', '--exclude']>
>>> flag.force_remove
'--force_remove'
>>> slugify.force_remove
'--force-remove'
class templatest.utils.VarSeq(name: str, suffix: object | None = None)[source]

Bases: MutableStrSequence

Get string as instantiated name with the index as the suffix.

The instantiated object will always return the same string as per the index. If index does not exist, strings will be generated up to the selected index.

Parameters:
name: str

Name of the string to return.

suffix: object | None = None

Separator between str and index.

Example:
>>> from templatest.utils import VarSeq
>>> CONST = VarSeq("CONST")
>>> CONST[0]
'CONST_0'
>>> CONST
<VarSeq ['CONST_0']>
>>> CONST[1]
'CONST_1'
>>> CONST
<VarSeq ['CONST_0', 'CONST_1']>
>>> CONST[4]
'CONST_4'
>>> CONST
<VarSeq ['CONST_0', 'CONST_1', 'CONST_2', 'CONST_3', 'CONST_4']>
>>> name = VarSeq("package-name", suffix="-")
>>> name[0]
'package-name-0'
class templatest.utils.VarSeqSuffix(name: str, suffix: str, separator: object | None = None)[source]

Bases: VarSeq

Get string as instantiated name with the index before suffix.

The instantiated object will always return the same string as per the index. If index does not exist, strings will be generated up to the selected index.

Parameters:
name: str

Name of the string to return.

suffix: str

Last string to return.

separator: object | None = None

Separator between str and index.

Example:
>>> from templatest.utils import VarSeqSuffix
>>> email = VarSeqSuffix("u", '@email.com')
>>> email[0]
'u_0@email.com'
>>> email
<VarSeqSuffix ['u_0@email.com']>
>>> email[2]
'u_2@email.com'
>>> email
<VarSeqSuffix ['u_0@email.com', 'u_1@email.com', 'u_2@email.com']>