Validate a list of strings as field names for a structured array. Parameters ---------- names : sequence of str Strings to be validated. defaultfmt : str, optional Default format string, used if validating a given string r
(self, names, defaultfmt="f%i", nbfields=None)
| 310 | self.replace_space = replace_space |
| 311 | |
| 312 | def validate(self, names, defaultfmt="f%i", nbfields=None): |
| 313 | """ |
| 314 | Validate a list of strings as field names for a structured array. |
| 315 | |
| 316 | Parameters |
| 317 | ---------- |
| 318 | names : sequence of str |
| 319 | Strings to be validated. |
| 320 | defaultfmt : str, optional |
| 321 | Default format string, used if validating a given string |
| 322 | reduces its length to zero. |
| 323 | nbfields : integer, optional |
| 324 | Final number of validated names, used to expand or shrink the |
| 325 | initial list of names. |
| 326 | |
| 327 | Returns |
| 328 | ------- |
| 329 | validatednames : list of str |
| 330 | The list of validated field names. |
| 331 | |
| 332 | Notes |
| 333 | ----- |
| 334 | A `NameValidator` instance can be called directly, which is the |
| 335 | same as calling `validate`. For examples, see `NameValidator`. |
| 336 | |
| 337 | """ |
| 338 | # Initial checks .............. |
| 339 | if (names is None): |
| 340 | if (nbfields is None): |
| 341 | return None |
| 342 | names = [] |
| 343 | if isinstance(names, str): |
| 344 | names = [names, ] |
| 345 | if nbfields is not None: |
| 346 | nbnames = len(names) |
| 347 | if (nbnames < nbfields): |
| 348 | names = list(names) + [''] * (nbfields - nbnames) |
| 349 | elif (nbnames > nbfields): |
| 350 | names = names[:nbfields] |
| 351 | # Set some shortcuts ........... |
| 352 | deletechars = self.deletechars |
| 353 | excludelist = self.excludelist |
| 354 | case_converter = self.case_converter |
| 355 | replace_space = self.replace_space |
| 356 | # Initializes some variables ... |
| 357 | validatednames = [] |
| 358 | seen = {} |
| 359 | nbempty = 0 |
| 360 | |
| 361 | for item in names: |
| 362 | item = case_converter(item).strip() |
| 363 | if replace_space: |
| 364 | item = item.replace(' ', replace_space) |
| 365 | item = ''.join([c for c in item if c not in deletechars]) |
| 366 | if item == '': |
| 367 | item = defaultfmt % nbempty |
| 368 | while item in names: |
| 369 | nbempty += 1 |