Collect whitespace-separated fields from string list Allows quick awk-like usage of string lists. Example data (in var a, created by 'a = !ls -l'):: -rwxrwxrwx 1 ville None 18 Dec 14 2006 ChangeLog drwxrwxrwx+ 6 ville None 0 Oct 24 18:05 IPyth
(self, *fields)
| 165 | return SList([el for el in self if not pred(match_target(el))]) |
| 166 | |
| 167 | def fields(self, *fields): |
| 168 | """ Collect whitespace-separated fields from string list |
| 169 | |
| 170 | Allows quick awk-like usage of string lists. |
| 171 | |
| 172 | Example data (in var a, created by 'a = !ls -l'):: |
| 173 | |
| 174 | -rwxrwxrwx 1 ville None 18 Dec 14 2006 ChangeLog |
| 175 | drwxrwxrwx+ 6 ville None 0 Oct 24 18:05 IPython |
| 176 | |
| 177 | * ``a.fields(0)`` is ``['-rwxrwxrwx', 'drwxrwxrwx+']`` |
| 178 | * ``a.fields(1,0)`` is ``['1 -rwxrwxrwx', '6 drwxrwxrwx+']`` |
| 179 | (note the joining by space). |
| 180 | * ``a.fields(-1)`` is ``['ChangeLog', 'IPython']`` |
| 181 | |
| 182 | IndexErrors are ignored. |
| 183 | |
| 184 | Without args, fields() just split()'s the strings. |
| 185 | """ |
| 186 | if len(fields) == 0: |
| 187 | return [el.split() for el in self] |
| 188 | |
| 189 | res = SList() |
| 190 | for el in [f.split() for f in self]: |
| 191 | lineparts = [] |
| 192 | |
| 193 | for fd in fields: |
| 194 | try: |
| 195 | lineparts.append(el[fd]) |
| 196 | except IndexError: |
| 197 | pass |
| 198 | if lineparts: |
| 199 | res.append(" ".join(lineparts)) |
| 200 | |
| 201 | return res |
| 202 | |
| 203 | def sort(self,field= None, nums = False): |
| 204 | """ sort by specified fields (see fields()) |