You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Collections: New keywords for matching list items using glob/regexp patterns optionally case/space sensitively #1724
Originally submitted to Google Code by Marcin.Koperski on 2 Jun 2014
It would nice to have option to find item which containing specific subset of string with option to be cases insensitive or add option to use wild cards in value
List Should Contain Value Containing Value
[ list_ | value | msg=None |caseSensitive=True]
${list} Create List Value1
List Should Contain Value ${list} Value List doesnt have item
List Should Contain Value ${list} value List doesnt have item True
I would rather enhance the current List Should Contain Value. It could easily get new case_insensitive=False argument to make matching case-insensitive. Specifying should matching be exact or should patterns be used there are two possibilities:
Add separate pattern=False or match=False argument. A problem here is that there could be need for both glob style patterns (foo*) and regexps (foo\d+). We could either add separate match=False and regexp=False arguments or add just single match_style='exact' argument with possible values like exact, match, and regexp.
Add matching style to the value with a prefix like match=foo* or regexp=foo\d+.
Are you interested in implementing this or sponsoring someone else to get this? Notice that for consistency reasons, same functionality should be added also to most other should keywords.
I can implement this. I suggest adding three arguments: case_insensitive (boolean, default=False), glob (string, default=None), and regex (string, default=None)
If glob or regex have a value, then use glob or regex matching as appropriate. If case_insensitive is True, do exact (but case insensitive) matching. If all three arguments are left at their default value, do exact case sensitive matching.
Glob and regex matching can be implemented using the glob and re packages from the standard library, and case insensitive matching can be implemented by converting all strings to lowercase. One question is, what should be done with non-string list values? Should they be converted to strings during the search? Should they simply be ignored?
@guykisel Great that you are interested to implement this!
I agree adding case_insensitive=False is a good idea but I'm not sure about adding separate arguments for regexp and glob. Two other alternatives:
Add separate argument like match_type='exact' that can get values exact (default), regexp or glob:
| List Should Contain Value | ${list} | foo | case_insensitive=yes |
| List Should Contain Value | ${list} | f* | case_insensitive=yes | match_type=glob |
| List Should Contain Value | ${list} | f.* | case_insensitive=yes | match_type=regexp |
Make it possible to add regexp= or glob= prefix for the current value argument:
| List Should Contain Value | ${list} | foo | case_insensitive=yes |
| List Should Contain Value | ${list} | glob=f* | case_insensitive=yes |
| List Should Contain Value | ${list} | regexp=f.* | case_insensitive=yes |
I kind of like the last alternative best. What about others?
For 2) I would suggest also allowing (but not requiring) an explicit match=, as in:
| List Should Contain Value | ${list} | foo | case_insensitive=yes |
| List Should Contain Value | ${list} | glob=f* | case_insensitive=yes |
| List Should Contain Value | ${list} | regexp=f.* | case_insensitive=yes |
| List Should Contain Value | ${list} | match=foo | case_insensitive=yes |
I think this matching logic could be implemented in a util function, and then it could be easily applied to any keyword that does matching on iterables.
Keywords to use this logic in:
BuiltIn.Should Contain
BuiltIn.Should Not Contain
BuiltIn.Should Contain X Times
BuiltIn.Get Count
Collections.List Should Contain Value
Collections.List Should Not Contain Value
Collections.Dictionary Should Contain Key
Collections.Dictionary Should Not Contain Key
Collections.Dictionary Should Contain Item
Collections.Dictionary Should Not Contain Item
Collections.Dictionary Should Contain Value
Collections.Dictionary Should Not Contain Value
This will result in some redundant keywords, since some of the libraries already have keywords for matching patterns or regexps.
@pekkaklarck I've implemented this functionality in Collections and I'm fairly happy with it, but there are still the BuiltIn keywords. I have a few options:
Don't implement this logic for BuiltIn keywords
Move the logic for this functionality into robot.utils, then import it in Collections and BuiltIn
Import Collections in BuiltIn
Move the logic to BuiltIn and import BuiltIn in Collections
It looks like the BuiltIn.Should Match keyword (and the other similar keywords) already support some pattern matching. I could do a backwards-incompatible rewrite of these keywords with the new matching pattern functionality, but I'm not sure if this is desirable.
After discussing this issue with @guykisel on IRC and thinking about it a bit more, I think it's better to add new keywords for finding/counting matches from lists. It has two main benefits:
Less work than updating all keywords that operate with lists in one way or other. In a comment above @guykisel lists altogether 12 keywords that could get this support.
No risk for breaking backwards compatibility.
The keywords that could be added:
Should Contain Match
Should Not Contain Match
Get Matches
Get Match Count
All of them should take arguments list, pattern, case_insensitive=False. Pattern should be a glob pattern by default, and support also regexps by using regexp= prefix. Also glob= prefix should be supported. It would allow having a glob pattern starting with regexp= text (e.g. glob=regexp=*).
I'm not certain should these new keywords be added to BuiltIn or to Collections. The former has related Should (Not) Contain and Get Count keywords (although they work also with strings) but the latter has more list related keywords. I don't want to make BuiltIn huge so perhaps Collections is better.
Sounds good! I'd also consider enhancing Matcher to accept regexp patterns too. Ought to be easy to implement with a help of few new unit tests and ought to also ease further implementation considerably.
changed the title [-]Collections: Support case-insensitive matching and pattern matching[/-][+]Collections: New keywords for matching list items using glob/regexp patterns optionally case/space sensitively[/+]on Jul 31, 2014
Activity
spooning commentedon Jun 30, 2014
I would rather enhance the current List Should Contain Value. It could easily get new
case_insensitive=Falseargument to make matching case-insensitive. Specifying should matching be exact or should patterns be used there are two possibilities:Add separate
pattern=Falseormatch=Falseargument. A problem here is that there could be need for both glob style patterns (foo*) and regexps (foo\d+). We could either add separatematch=Falseandregexp=Falsearguments or add just singlematch_style='exact'argument with possible values likeexact,match, andregexp.Add matching style to the value with a prefix like
match=foo*orregexp=foo\d+.Are you interested in implementing this or sponsoring someone else to get this? Notice that for consistency reasons, same functionality should be added also to most other
shouldkeywords.spooning commentedon Jun 30, 2014
I think first solution is better, since it would more compatible friendly.
Unfortunately, I don't think I can help with development,but I can help to test this functionality or add some tests.
spooning commentedon Jun 30, 2014
Case-insensitive matching could be also used with keywords like Remote Duplicates, Remove Values From Lists.
guykisel commentedon Jul 3, 2014
I can implement this. I suggest adding three arguments: case_insensitive (boolean, default=False), glob (string, default=None), and regex (string, default=None)
If glob or regex have a value, then use glob or regex matching as appropriate. If case_insensitive is True, do exact (but case insensitive) matching. If all three arguments are left at their default value, do exact case sensitive matching.
Glob and regex matching can be implemented using the glob and re packages from the standard library, and case insensitive matching can be implemented by converting all strings to lowercase. One question is, what should be done with non-string list values? Should they be converted to strings during the search? Should they simply be ignored?
pekkaklarck commentedon Jul 3, 2014
@guykisel Great that you are interested to implement this!
I agree adding
case_insensitive=Falseis a good idea but I'm not sure about adding separate arguments for regexp and glob. Two other alternatives:match_type='exact'that can get valuesexact(default),regexporglob:regexp=orglob=prefix for the currentvalueargument:I kind of like the last alternative best. What about others?
pekkaklarck commentedon Jul 3, 2014
To which keywords would you like to implement this support? It would at least potentially be useful with many.
guykisel commentedon Jul 3, 2014
For 2) I would suggest also allowing (but not requiring) an explicit match=, as in:
I think this matching logic could be implemented in a util function, and then it could be easily applied to any keyword that does matching on iterables.
Keywords to use this logic in:
BuiltIn.Should Contain
BuiltIn.Should Not Contain
BuiltIn.Should Contain X Times
BuiltIn.Get Count
Collections.List Should Contain Value
Collections.List Should Not Contain Value
Collections.Dictionary Should Contain Key
Collections.Dictionary Should Not Contain Key
Collections.Dictionary Should Contain Item
Collections.Dictionary Should Not Contain Item
Collections.Dictionary Should Contain Value
Collections.Dictionary Should Not Contain Value
This will result in some redundant keywords, since some of the libraries already have keywords for matching patterns or regexps.
Initial commit for robotframework#1724
guykisel commentedon Jul 10, 2014
@pekkaklarck I've implemented this functionality in Collections and I'm fairly happy with it, but there are still the BuiltIn keywords. I have a few options:
What do you think?
guykisel commentedon Jul 10, 2014
It looks like the BuiltIn.Should Match keyword (and the other similar keywords) already support some pattern matching. I could do a backwards-incompatible rewrite of these keywords with the new matching pattern functionality, but I'm not sure if this is desirable.
pekkaklarck commentedon Jul 12, 2014
After discussing this issue with @guykisel on IRC and thinking about it a bit more, I think it's better to add new keywords for finding/counting matches from lists. It has two main benefits:
The keywords that could be added:
All of them should take arguments
list, pattern, case_insensitive=False. Pattern should be a glob pattern by default, and support also regexps by usingregexp=prefix. Alsoglob=prefix should be supported. It would allow having a glob pattern starting withregexp=text (e.g.glob=regexp=*).I'm not certain should these new keywords be added to BuiltIn or to Collections. The former has related
Should (Not) ContainandGet Countkeywords (although they work also with strings) but the latter has more list related keywords. I don't want to make BuiltIn huge so perhaps Collections is better.guykisel commentedon Jul 14, 2014
@pekkaklarck that all sounds good to me. I'll add them to Collections for now, but it's easy to move the keywords if we decide otherwise later.
I'll use Matcher's glob parser as opposed to using Python's built in glob functions, for consistency with other RF matching/contains keywords.
pekkaklarck commentedon Jul 14, 2014
Sounds good! I'd also consider enhancing Matcher to accept regexp patterns too. Ought to be easy to implement with a help of few new unit tests and ought to also ease further implementation considerably.
Case insensitive and pattern matching
Case insensitive and pattern matching
New pattern matching keywords in Collections
pekkaklarck commentedon Jul 31, 2014
@guykisel has been working with a great pull request so this is soon implemented. Will be included in the forthcoming 2.8.6 release.
[-]Collections: Support case-insensitive matching and pattern matching[/-][+]Collections: New keywords for matching list items using glob/regexp patterns optionally case/space sensitively[/+]pekkaklarck commentedon Jul 31, 2014
This was implemented by pull request #1756 by @guykisel.
The newly added keywords are: