argsloader.units.utils¶
KeepUnit¶
keep¶
-
argsloader.units.utils.
keep
() → argsloader.units.utils.KeepUnit[source]¶ - Overview:
Simply keep the original input data.
See
KeepUnit
.
- Returns
A keep unit object.
- Examples::
>>> from argsloader.units import keep >>> u = keep() >>> u(1) 1 >>> u('this is str') 'this is str'
CheckUnit¶
check¶
-
argsloader.units.utils.
check
(unit) → argsloader.units.utils.CheckUnit[source]¶ - Overview:
Simply keep the input data, the
unit
is only used for checking.See
CheckUnit
.
- Parameters
unit – Unit used to check.
- Returns
A check unit object.
- Examples::
>>> from argsloader.units import check, is_type, add >>> u = check(is_type(int) >> add.by(2)) >>> u(2) 2 >>> u(10) 10 >>> u(2.0) TypeParseError: Value type not match - int expected but float found.
ValidityUnit¶
-
class
argsloader.units.utils.
ValidityUnit
(unit: argsloader.units.base.BaseUnit)[source]¶ - Overview:
Unit for getting validity of the given
unit
.
-
__init__
(unit: argsloader.units.base.BaseUnit)[source]¶ Constructor of
ValidityUnit
.- Parameters
unit – Unit for getting validity.
validity¶
-
argsloader.units.utils.
validity
(unit) → argsloader.units.utils.ValidityUnit[source]¶ - Overview:
Get the validity of the given
unit
. ReturnTrue
whenunit
is parsed success, otherwise returnFalse
.See
ValidityUnit
.
- Parameters
unit – Unit for getting validity.
- Returns
A validity unit object.
- Examples::
Simple usage
>>> from argsloader.units import validity, is_type >>> u = validity(is_type(int)) >>> u(10) True >>> u(10.0) False
Attribute-based usage
>>> u = is_type(int).validity # the same as validity(is_type(int)) >>> u(10) True >>> u(10.0) False
ErrorUnit¶
error¶
-
argsloader.units.utils.
error
(condition, errcls, *args) → argsloader.units.utils.ErrorUnit[source]¶ - Overview:
Raise error when the given
condition
is satisfied.
- Parameters
condition – Condition unit.
errcls – Error class.
args – Error arguments.
- Returns
A error unit object.
- Examples::
>>> from argsloader.units import error, is_type >>> u = error(is_type(int).validity, KeyError, 'a key error') >>> u(10.0) 10.0 >>> u(10) KeyParseError: 'a key error'
validate¶
-
argsloader.units.utils.
validate
(val, condition, errcls, *args)[source]¶ - Overview:
Raise error based on the validation of data.
- Parameters
val – Data calculation unit.
condition – Validation unit, the error will be raised if this
condition
is not satisfied.errcls – Error class.
args – Error arguments.
- Returns
A unit object which can do the validation.
- Examples::
>>> from argsloader.units import to_type, le, validate >>> u = validate(to_type(int), le.than(5).validity, KeyError, 'a key error') >>> u(4) 4 >>> u(5.2) 5.2 >>> u(6.0) KeyParseError: 'a key error'
fail¶
-
argsloader.units.utils.
fail
(errcls, *args) → argsloader.units.utils.ErrorUnit[source]¶ - Overview:
Raise error at any time.
- Parameters
errcls – Error class.
args – Error arguments.
- Returns
A unit object which can raise error.
- Examples::
>>> from argsloader.units import fail >>> u = fail(KeyError, 'a key error') >>> u(1) KeyParseError: 'a key error'
IfUnit¶
-
class
argsloader.units.utils.
IfUnit
(statements: List[Tuple[argsloader.units.base.BaseUnit, argsloader.units.base.BaseUnit]])[source]¶ - Overview:
Unit for if statement.
-
__init__
(statements: List[Tuple[argsloader.units.base.BaseUnit, argsloader.units.base.BaseUnit]])[source]¶ Constructor of
IfUnit
.- Parameters
statements – If statements.
_IfProxy¶
-
class
argsloader.units.utils.
_IfProxy
(statements)[source]¶ - Overview:
If proxy object, used to build
IfUnit
.
-
elif_
(cond, val) → argsloader.units.utils._IfProxy[source]¶ Add an
else-if
clause.- Parameters
cond – Condition unit.
val – Value unit.
- Returns
Another
_IfProxy
with this newelse-if
clause.
-
else_
(val) → argsloader.units.utils.IfUnit[source]¶ Add an
else
clause.- Parameters
val – Value unit.
- Returns
A completed
IfUnit
with thiselse
clause.
if_¶
-
argsloader.units.utils.
if_
(cond, val) → argsloader.units.utils._IfProxy[source]¶ - Overview:
If clause to determine the return value.
- Parameters
cond – Condition unit.
val – Value unit.
- Returns
An initial
_IfProxy
unit, a full if statement will be built based on this.
- Examples::
>>> from argsloader.units import if_, is_type >>> u = if_(is_type(int).validity, 'an int').elif_(is_type(float).validity, 'a float').else_('fxxk off') >>> u(1) 'an int' >>> u(1.0) 'a float' >>> u('1') 'fxxk off'