Builtins Parser Implementations¶
Parser implementations for (mostly) builtin types.
Classes¶
- class disnake_compass.impl.parser.builtins.NoneParser(*, strict=True)[source]¶
-
Parser implementation for
None.Mainly relevant for
Optional[…] parsers.- Parameters:
strict (bool) –
Whether this parser should be set to
strictmode.To prevent unforeseen bugs, this defaults to
True.
Attributes¶
Methods¶
- await dumps(argument, /)[source]¶
Dump
Noneinto a string.If
strictis set toTrue, this will fail if the providedargumentisn’t exactlyNone. Otherwise, this parser will always returnNone.- Parameters:
argument (
None) – The value that is to be dumped.- Raises:
ValueError: – The parser is in strict mode, and the provided
argumentwas notNone.
- await loads(argument, /)[source]¶
Load
Nonefrom a string.If
strictis set toTrue, this will fail if the providedargumentisn’t the empty string (""). Otherwise, this parser will always returnNone.- Parameters:
argument (
str) – The string that is to be converted toNone.- Raises:
ValueError: – The parser is in strict mode, and the provided
argumentwas not the empty string.
- class disnake_compass.impl.parser.builtins.FloatParser[source]¶
-
Parser implementation for
floats.Methods¶
- class disnake_compass.impl.parser.builtins.IntParser(signed=True, base=36)[source]¶
-
Parser implementation for
ints.- Parameters:
Attributes¶
Methods¶
- await dumps(argument, /)[source]¶
Dump an integer into a string.
- Parameters:
argument (
int) – The value that is to be dumped.
- class disnake_compass.impl.parser.builtins.BoolParser(trues=NOTHING, falses=NOTHING)[source]¶
-
Parser type with support for bools.
This parser type can be supplied with a collection of strings for the values that should be considered true and false. By default,
"true", "t", "yes", "y", "1"are consideredTrue, while"false", "f", "no", "n", "0"are consideredFalse. Note that this is case-insensitive.- Parameters:
trues (
Collection[str]) – Values that should be consideredTrueby this parser.falses (
Collection[str]) – Values that should be consideredFalseby this parser.
Attributes¶
-
falses:
Collection[str]¶ A collection of values that should be considered
Falseby this parser.
-
trues:
Collection[str]¶ A collection of values that should be considered
Trueby this parser.
Methods¶
- class disnake_compass.impl.parser.builtins.StringParser[source]¶
-
Parser type with support for strings.
Both loads and dumps are essentially no-ops.
Methods¶
- class disnake_compass.impl.parser.builtins.TupleParser(*inner_parsers, sep=', ', tuple_cls=<class 'tuple'>)[source]¶
Bases:
Parser[_TupleT]Parser type with support for
tuples.The benefit of a tuple parser is fixed-length checks and the ability to set multiple types. For example, a
Tuple[str, int, bool]parser will actually return a tuple with astr,int, andboolinside.- Parameters:
*inner_parsers (parser_api.Parser[typing.Any]) –
The parsers to use to parse the items inside the tuple.
Defaults to a single string parser, i.e. a one-element tuple containing exactly one string.
sep (str) –
The separator to use.
Defaults to “,”.
Attributes¶
- inner_parsers: tuple[parser_api.Parser[typing.Any], ...]¶
The parsers to use to parse the items inside the tuple.
These define the inner types and the allowed number of items in the in the tuple.
- sep: str¶
The separator to use.
Can be any string, though a single character is recommended.
Warning
Ensure that this does not match
ComponentManager.sepon the component manager that corresponds to this parser’s component.
- tuple_cls: type[_TupleT]¶
The tuple type to use.
This mainly exists to support NamedTuples.
Methods¶
- classmethod default(type_, /)[source]¶
Return the default implementation of this parser type.
By default, this will just create the parser class with no arguments, but this can be overwritten on child classes for customised behaviour.
- Parameters:
target_type – The exact type that this parser should be created for
- Returns:
The default parser instance for this parser type.
- Return type:
- await dumps(argument, /)[source]¶
Dump a tuple into a string.
- Parameters:
argument (
_TupleT) – The value that is to be dumped.- Raises:
RuntimeError – The length of the
argumenttuple does not match the number of inner parsers.
- class disnake_compass.impl.parser.builtins.CollectionParser(inner_parser=None, *, collection_type=None, sep=',')[source]¶
Bases:
Parser[_CollectionT]Parser type with support for
typing.Collections.This supports types such as
list,set, etc.; but also abstract types such astyping.Collectionitself.Note
This parser does not support
tuples.- Parameters:
inner_parser (parser_api.Parser[typing.Any] | None) –
The parser to use to parse the items inside the collection.
This defines the inner type for the collection.
Defaults to a
StringParser.collection_type (type[_CollectionT] | None) –
The type of collection to use. This does not specify the inner type.
Defaults to
list.sep (str) –
The separator to use.
Can be any string, though a single character is recommended.
Defaults to
",".
Attributes¶
- collection_type: type[_CollectionT]¶
The collection type for this parser.
This is the type that holds the items, e.g. a
list.Note
This has to support being instantiated from an iterable as
collection_type(iterable_of_values); e.g.set([1,2,3])
- inner_parser: parser_api.Parser[typing.Any]¶
The parser to use to parse the items inside the collection.
Note that, unlike a
TupleParser, a collection parser requires all items to be of the same type.
- sep: str¶
The separator to use.
Can be any string, though a single character is recommended.
Warning
Ensure that this does not match
ComponentManager.sepon the component manager that corresponds to this parser’s component.
Methods¶
- classmethod default(type_, /)[source]¶
Return the default implementation of this parser type.
By default, this will just create the parser class with no arguments, but this can be overwritten on child classes for customised behaviour.
- Parameters:
target_type – The exact type that this parser should be created for
- Returns:
The default parser instance for this parser type.
- Return type:
- class disnake_compass.impl.parser.builtins.UnionParser(*inner_parsers)[source]¶
Bases:
Parser[_T],Generic[_T]Parser type with support for
Unions.The provided parsers are sequentially tried until one passes. If none work, an exception is raised instead.
Important
Unlike
NoneParser,strictfor this class is set via the underlying none parser, if any. Note that this class does proxy it through thestrictproperty, which supports both getting and setting; but only if this parser isoptional.- Parameters:
*inner_parsers (parser_api.Parser[typing.Any] | None) –
The parsers with which to sequentially try to parse the argument.
Nonecan be provided as one of the parameters to make it optional; this will automatically add a strictNoneParser.
Attributes¶
- inner_parsers: typing.Sequence[parser_api.Parser[typing.Any]]¶
The parsers with which to sequentially try to parse the argument.
- optional: bool¶
Whether this parser is optional.
- property strict: bool[source]¶
Whether this parser is strict.
This is only meaningful when the parser is optional, in which case it directly reflects
NoneParser.strict.
Methods¶
- classmethod default(type_, /)[source]¶
Return the default implementation of this parser type.
By default, this will just create the parser class with no arguments, but this can be overwritten on child classes for customised behaviour.
- Parameters:
target_type – The exact type that this parser should be created for
- Returns:
The default parser instance for this parser type.
- Return type:
- await dumps(argument, /)[source]¶
Dump a union of types into a string.
- Parameters:
argument (
_T) –The value that is to be dumped.
This finds a parser that is registered for the type of the provided
argument.- Raises:
RuntimeError – None of the
inner_parserssucceeded to dump theargument.
- await loads(argument, /)[source]¶
Load a union of types from a string.
If
optionalisTrueandstrictisFalse, this returnsNoneif all parsers fail. Otherwise, an exception is raised.- Parameters:
argument (
str) –The string that is to be converted into one of the types in this union.
Each inner parser is tried in the order they are provided, and the first to load the
argumentsuccessfully short-circuits and returns.- Raises:
RuntimeError – None of the
inner_parserssucceeded to load theargument.