argsloader.units.string¶
TemplateUnit¶
-
class
argsloader.units.string.
TemplateUnit
(tmp, variables: Mapping[str, Any], safe: bool)[source]¶ - Overview:
Unit for templating string.
-
__init__
(tmp, variables: Mapping[str, Any], safe: bool)[source]¶ Constructor of
TemplateUnit
.- Parameters
tmp – Template string.
variables – Variable objects.
safe – Safe mode or not.
template¶
-
argsloader.units.string.
template
(_s_template, **vars_) → argsloader.units.string.TemplateUnit[source]¶ - Overview:
Create a string, based on a template and several variables.
- Parameters
_s_template – Template string.
vars – Variables to be used.
- Returns
A template based unit.
- Examples::
>>> from argsloader.units import keep, neg, mul, template >>> u = template( ... '${v} is original data,' ... '${v2} is doubled data,' ... '${v_} is negative data,' ... '${c} is const data', ... v=keep(), v2=mul.by(2), v_=neg(), c=-12 ... ) >>> u(4) '4 is original data,8 is doubled data,-4 is negative data,-12 is const data'
Note
When function
template()
is used, the safe mode is disabled in default. It means when one of the required variable’s data is not given, aKeyError
will be raised. For example,>>> u = template( ... '${v} is original data,' ... '${v2} is doubled data,' ... '${v_} is negative data,' ... '${c} is const data', ... v=keep(), v2=mul.by(2), v_=neg() # c is missing ... ) >>> u(4) KeyParseError: "Key 'c' which is required by template is not provided."
In the abovementioned case, if you need to just keep the missing string, you can enable the safe mode with
template.safe
. For example,>>> u = template.safe( ... '${v} is original data,' ... '${v2} is doubled data,' ... '${v_} is negative data,' ... '${c} is const data', ... v=keep(), v2=mul.by(2), v_=neg() # c is missing ... ) >>> u(4) '4 is original data,8 is doubled data,-4 is negative data,${c} is const data'
RegexpMatchUnit¶
-
class
argsloader.units.string.
RegexpMatchUnit
(r, fullmatch: bool = False)[source]¶ - Overview:
Unit for regular expression matching.
-
__getitem__
(struct_)[source]¶ - Parameters
struct – The expected structure.
- Returns
A
argsloader.units.operator.PipeUnit
which can combine the grouped data together.
-
__init__
(r, fullmatch: bool = False)[source]¶ Constructor of
RegexpMatchUnit
.- Parameters
r – Regular expression string or pattern.
fullmatch – Fully match required or not, default is
False
.
-
property
check
¶ - Returns
A
argsloader.units.utils.CheckUnit
which only check the match or not.
-
property
full
¶ - Returns
A
RegexpMatchUnit
with fully-match option enabled.
RegexpProxy¶
-
class
argsloader.units.string.
RegexpProxy
(r)[source]¶ - Overview:
Proxy class for regular expression units’ building.
Note
This class is only used for building unit, which means it can not be used like unit because of its incompleteness.
-
__init__
(r) → None[source]¶ Constructor of
RegexpProxy
.- Parameters
r – Regular expression string or pattern.
-
property
match
¶ - Returns
A
RegexpMatchUnit
with fully-match and check-only options disabled.
regexp¶
-
argsloader.units.string.
regexp
(r) → argsloader.units.string.RegexpProxy[source]¶ Overview:
- Parameters
r – Regular expression string or pattern. If string is given, it will be compiled inside.
- Returns
A
RegexpProxy
object.
- Examples::
Simple match
>>> from argsloader.units import regexp >>> u = regexp('([a-z0-9_]+)@([a-z0-9_\.]+)').match >>> u('mymail@gmail.com') {0: 'mymail@gmail.com', 1: 'mymail', 2: 'gmail.com'} >>> u('mymail@gmail.com here is other text') {0: 'mymail@gmail.com', 1: 'mymail', 2: 'gmail.com'} >>> u('mymail_gmail.com') ValueParseError: Regular expression '([a-z0-9_]+)@([a-z0-9_\.]+)' expected, but 'mymail_gmail.com' found which is not matched.
Check only
>>> u = regexp('([a-z0-9_]+)@([a-z0-9_\.]+)').match.check >>> u('mymail@gmail.com') 'mymail@gmail.com' >>> u('mymail@gmail.com here is other text') 'mymail@gmail.com here is other text' >>> u('mymail_gmail.com') ValueParseError: Regular expression '([a-z0-9_]+)@([a-z0-9_\.]+)' expected, but 'mymail_gmail.com' found which is not matched.
Fully match
>>> u = regexp('([a-z0-9_]+)@([a-z0-9_\.]+)').match.full >>> u('mymail@gmail.com') {0: 'mymail@gmail.com', 1: 'mymail', 2: 'gmail.com'} >>> u('mymail@gmail.com here is other text') ValueParseError: Regular expression '([a-z0-9_]+)@([a-z0-9_\.]+)' expected, but 'mymail@gmail.com here is other text' found which is not fully matched.
Fully match, check only
>>> u = regexp('([a-z0-9_]+)@([a-z0-9_\.]+)').match.full.check >>> u('mymail@gmail.com') 'mymail@gmail.com' >>> u('mymail@gmail.com here is other text') ValueParseError: Regular expression '([a-z0-9_]+)@([a-z0-9_\.]+)' expected, but 'mymail@gmail.com here is other text' found which is not fully matched.
Match with named group
>>> u = regexp('(?P<username>[a-z0-9_]+)@(?P<domain>[a-z0-9_\.]+)').match >>> u('mymail@gmail.com') {0: 'mymail@gmail.com', 1: 'mymail', 2: 'gmail.com', 'username': 'mymail', 'domain': 'gmail.com'} >>> u('mymail@gmail.com here is other text') {0: 'mymail@gmail.com', 1: 'mymail', 2: 'gmail.com', 'username': 'mymail', 'domain': 'gmail.com'} >>> u('mymail_gmail.com') ValueParseError: Regular expression '(?P<username>[a-z0-9_]+)@(?P<domain>[a-z0-9_\.]+)' expected, but 'mymail_gmail.com' found which is not matched.
Get data from matching result
>>> from argsloader.units import regexp >>> u = regexp('([a-z0-9_]+)@([a-z0-9_\.]+)').match.full[(0, {'u': 1, 'd': 2})] >>> u('mymail@gmail.com') ('mymail@gmail.com', {'u': 'mymail', 'd': 'gmail.com'}) >>> >>> u = regexp('(?P<username>[a-z0-9_]+)@(?P<domain>[a-z0-9_\.]+)').match[(0, ('username', 'domain'))] >>> u('mymail@gmail.com') ('mymail@gmail.com', ('mymail', 'gmail.com'))
Warning
The object which function
regexp()
returned is only aRegexpProxy
object, which is only used for building unit and can not be used like other functions.Note
The other functions like
re.find
orre.search
will be added later.