MCPcopy Create free account
hub / github.com/numpy/numpy / validate

Method validate

numpy/lib/_iotools.py:311–379  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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

Callers 3

__call__Method · 0.95
test_excludelistMethod · 0.95
test_case_sensitivityMethod · 0.80

Calls 4

stripMethod · 0.80
replaceMethod · 0.80
joinMethod · 0.45
getMethod · 0.45

Tested by 2

test_excludelistMethod · 0.76
test_case_sensitivityMethod · 0.64