##// END OF EJS Templates
SList.grep() supports optional 'field' argument
Ville M. Vainio -
Show More
@@ -1052,25 +1052,40 b' class SList(list):'
1052
1052
1053 p = paths = property(get_paths)
1053 p = paths = property(get_paths)
1054
1054
1055 def grep(self, pattern, prune = False):
1055 def grep(self, pattern, prune = False, field = None):
1056 """ Return all strings matching 'pattern' (a regex or callable)
1056 """ Return all strings matching 'pattern' (a regex or callable)
1057
1057
1058 This is case-insensitive. If prune is true, return all items
1058 This is case-insensitive. If prune is true, return all items
1059 NOT matching the pattern.
1059 NOT matching the pattern.
1060
1060
1061 If field is specified, the match must occur in the specified
1062 whitespace-separated field.
1063
1061 Examples::
1064 Examples::
1062
1065
1063 a.grep( lambda x: x.startswith('C') )
1066 a.grep( lambda x: x.startswith('C') )
1064 a.grep('Cha.*log', prune=1)
1067 a.grep('Cha.*log', prune=1)
1068 a.grep('chm', field=-1)
1065 """
1069 """
1070
1071 def match_target(s):
1072 if field is None:
1073 return s
1074 parts = s.split()
1075 try:
1076 tgt = parts[field]
1077 return tgt
1078 except IndexError:
1079 return ""
1080
1066 if isinstance(pattern, basestring):
1081 if isinstance(pattern, basestring):
1067 pred = lambda x : re.search(pattern, x, re.IGNORECASE)
1082 pred = lambda x : re.search(pattern, x, re.IGNORECASE)
1068 else:
1083 else:
1069 pred = pattern
1084 pred = pattern
1070 if not prune:
1085 if not prune:
1071 return SList([el for el in self if pred(el)])
1086 return SList([el for el in self if pred(match_target(el))])
1072 else:
1087 else:
1073 return SList([el for el in self if not pred(el)])
1088 return SList([el for el in self if not pred(match_target(el))])
1074 def fields(self, *fields):
1089 def fields(self, *fields):
1075 """ Collect whitespace-separated fields from string list
1090 """ Collect whitespace-separated fields from string list
1076
1091
@@ -1083,6 +1098,7 b' class SList(list):'
1083 a.fields(0) is ['-rwxrwxrwx', 'drwxrwxrwx+']
1098 a.fields(0) is ['-rwxrwxrwx', 'drwxrwxrwx+']
1084 a.fields(1,0) is ['1 -rwxrwxrwx', '6 drwxrwxrwx+']
1099 a.fields(1,0) is ['1 -rwxrwxrwx', '6 drwxrwxrwx+']
1085 (note the joining by space).
1100 (note the joining by space).
1101 a.fields(-1) is ['ChangeLog', 'IPython']
1086
1102
1087 IndexErrors are ignored.
1103 IndexErrors are ignored.
1088
1104
@@ -1,3 +1,7 b''
1 2008-04-15 Ville Vainio <vivainio@gmail.com>
2
3 * genutils.py: SList.grep supports 'field' argument
4
1 2008-04-09 Ville Vainio <vivainio@gmail.com>
5 2008-04-09 Ville Vainio <vivainio@gmail.com>
2
6
3 * deep_reload.py: do not crash on from __future__ import
7 * deep_reload.py: do not crash on from __future__ import
General Comments 0
You need to be logged in to leave comments. Login now