argsloader.units.enum¶
EnumUnit¶
enum¶
-
argsloader.units.enum.
enum
(ecls: Type[enum.Enum]) → argsloader.units.enum.EnumUnit[source]¶ - Overview:
Get a unit for parsing enum.
- Parameters
ecls – Enum class, should be a subclass of
Enum
.- Returns
A unit for parsing enum.
- Examples::
Basic enum parsing
>>> from enum import Enum >>> from argsloader.units import enum >>> class Color(Enum): ... RED = 1 ... GREEN = 2 ... BLUE = 4 ... >>> u = enum(Color) >>> u(2) <Color.GREEN: 2> >>> u('red') <Color.RED: 1> >>> u('BLUE') <Color.BLUE: 4> >>> u(Color.GREEN) <Color.GREEN: 2> >>> u(3) ValueParseError: Value is expected to be a Color object, but 3 found actually.
Flag enum parsing
>>> from enum import Flag >>> from argsloader.units import enum >>> class Color(Flag): ... RED = 1 ... GREEN = 2 ... BLUE = 4 ... WHITE = RED | GREEN | BLUE ... >>> u = enum(Color) >>> u(2) <Color.GREEN: 2> >>> u('red') <Color.RED: 1> >>> u('White') <Color.WHITE: 7> >>> u(6) <Color.BLUE|GREEN: 6> >>> u('red, blue') <Color.BLUE|RED: 5> >>> u('green, 4') <Color.BLUE|GREEN: 6> >>> u('') <Color.0: 0> >>> u([1, Color.BLUE]) <Color.BLUE|RED: 5>
Note
Function
enum()
is used based onEnum
class, which you need to build an enum class before this. For further information, see the following pages:
[Deprecated] SChoiceUnit¶
-
class
argsloader.units.enum.
SChoiceUnit
(sch, case_sensitive: bool)[source]¶ - Overview:
Unit for parsing string-based enum value.
-
__init__
(sch, case_sensitive: bool)[source]¶ Constructor of class
SChoiceUnit
.- Parameters
sch – String choices.
case_sensitive – Case sensitive or not.
[Deprecated] schoice¶
-
argsloader.units.enum.
schoice
(sch, case_sensitive: bool = False) → argsloader.units.enum.SChoiceUnit[source]¶ - Overview:
Getting a string-based choice parser.
- Parameters
sch – String choices.
case_sensitive – Case sensitive or not.
- Returns
A unit for parsing string-based choices.
- Examples::
Simple choices
>>> from argsloader.units import schoice >>> u = schoice(['red', 'green', 'blue']) >>> u('red') 'red' >>> u('RED') 'red' >>> u('Green') 'green' >>> u('BlUe') 'blue' >>> u('Pink') ValueParseError: Value is expected to be within ('red', 'green', 'blue'), but 'pink' found actually.
Case sensitive choices
>>> from argsloader.units import schoice >>> u = schoice(['red', 'green', 'blue'], case_sensitive=True) >>> u('red') 'red' >>> u('RED') ValueParseError: Value is expected to be within ('red', 'green', 'blue'), but 'RED' found actually. >>> u('Green') ValueParseError: Value is expected to be within ('red', 'green', 'blue'), but 'Green' found actually. >>> u('Pink') ValueParseError: Value is expected to be within ('red', 'green', 'blue'), but 'Pink' found actually.
Warning
This function is deprecated and will be completely removed since
1.0.0
version. Please replace this to functionenum()
. For example, the followingschoice()
expression>>> from argsloader.units import schoice >>> u = schoice(['red', 'green', 'blue'])
can be replaced with complete-enum-based
enum()
expression, with a more reliable and conventional presentation mode, like the following code>>> from enum import Enum >>> from argsloader.units import enum >>> class Color(Enum): ... RED = 1 ... GREEN = 2 ... BLUE = 4 ... >>> u = enum(Color)
Deprecated since version 1.0.0: Function schoice is not recommended to be used, use function enum instead.