argsloader.units.common¶
IsUnit¶
is_¶
-
argsloader.units.common.
is_
(v) → argsloader.units.common.IsUnit[source]¶ - Overview:
Check if the given object is the same object as
v
.
- Parameters
v – Unit or value to be checked.
- Returns
A
IsUnit
object.
- Examples::
>>> from argsloader.units import is_ >>> u = is_(1) >>> u(1) # 1 is 1, int is immutable in python 1 >>> u(2) # 2 is 1 ValueParseError: Value expected to be 1(0x7fb7799546c0), but 2(0x7fb7799546e0) found. >>> u(None) ValueParseError: Value expected to be 1(0x7fb7799546c0), but None(0x7fb77990a110) found.
none¶
optional¶
-
argsloader.units.common.
optional
(u)[source]¶ - Overview:
Check if the given value is none or satisfy the given
u
.
- Parameters
u – Original unit.
- Examples::
>>> from argsloader.units import optional, is_type >>> u = optional(is_type(int)) >>> u(12) 12 >>> u(None) None >>> u(12.0) TypeParseError: Value type not match - int expected but float found. >>> u('abc') TypeParseError: Value type not match - int expected but str found.
YesNoUnit¶
yesno¶
-
argsloader.units.common.
yesno
(yes='yes', no='no') → argsloader.units.common.YesNoUnit[source]¶ - Overview:
Parse yes-or-no option.
- Parameters
yes – Yes string, default is
yes
.no – No string, default is
no
.
- Returns
A
YesNoUnit
object.
- Examples::
Simple Usage
>>> from argsloader.units import yesno >>> u = yesno() >>> u('yes') True >>> u('no') False >>> u(True) True >>> u(False) False >>> u(1) True >>> u(0) False >>> u('xxx') ValueParseError: Value expected to be 'yes' or 'no', but 'xxx' found.
Self-defined tags
>>> from argsloader.units import yesno >>> u = yesno('Accept', 'Decline') >>> u('accept') True >>> u('decline') False >>> u(True) True >>> u(False) False >>> u(1) True >>> u(0) False >>> u('yes') ValueParseError: Value expected to be 'Accept' or 'Decline', but 'yes' found.
Note
If you need to parse from multiple pairs of yes and no tags, you can simply chain them together with
|
operator.>>> from argsloader.units import yesno >>> u = yesno() | yesno('Accept', 'Decline') >>> u('yes') True >>> u('NO') False >>> u('accept') True >>> u('decline') False >>> u('xxx') ValueParseError: Value expected to be 'yes' or 'no', but 'xxx' found.