Object to validate a list of strings to use as field names. The strings are stripped of any non alphanumeric character, and spaces are replaced by '_'. During instantiation, the user can define a list of names to exclude, as well as a list of invalid characters. Names in the ex
| 227 | |
| 228 | |
| 229 | class NameValidator: |
| 230 | """ |
| 231 | Object to validate a list of strings to use as field names. |
| 232 | |
| 233 | The strings are stripped of any non alphanumeric character, and spaces |
| 234 | are replaced by '_'. During instantiation, the user can define a list |
| 235 | of names to exclude, as well as a list of invalid characters. Names in |
| 236 | the exclusion list are appended a '_' character. |
| 237 | |
| 238 | Once an instance has been created, it can be called with a list of |
| 239 | names, and a list of valid names will be created. The `__call__` |
| 240 | method accepts an optional keyword "default" that sets the default name |
| 241 | in case of ambiguity. By default this is 'f', so that names will |
| 242 | default to `f0`, `f1`, etc. |
| 243 | |
| 244 | Parameters |
| 245 | ---------- |
| 246 | excludelist : sequence, optional |
| 247 | A list of names to exclude. This list is appended to the default |
| 248 | list ['return', 'file', 'print']. Excluded names are appended an |
| 249 | underscore: for example, `file` becomes `file_` if supplied. |
| 250 | deletechars : str, optional |
| 251 | A string combining invalid characters that must be deleted from the |
| 252 | names. |
| 253 | case_sensitive : {True, False, 'upper', 'lower'}, optional |
| 254 | * If True, field names are case-sensitive. |
| 255 | * If False or 'upper', field names are converted to upper case. |
| 256 | * If 'lower', field names are converted to lower case. |
| 257 | |
| 258 | The default value is True. |
| 259 | replace_space : '_', optional |
| 260 | Character(s) used in replacement of white spaces. |
| 261 | |
| 262 | Notes |
| 263 | ----- |
| 264 | Calling an instance of `NameValidator` is the same as calling its |
| 265 | method `validate`. |
| 266 | |
| 267 | Examples |
| 268 | -------- |
| 269 | >>> import numpy as np |
| 270 | >>> validator = np.lib._iotools.NameValidator() |
| 271 | >>> validator(['file', 'field2', 'with space', 'CaSe']) |
| 272 | ('file_', 'field2', 'with_space', 'CaSe') |
| 273 | |
| 274 | >>> validator = np.lib._iotools.NameValidator(excludelist=['excl'], |
| 275 | ... deletechars='q', |
| 276 | ... case_sensitive=False) |
| 277 | >>> validator(['excl', 'field2', 'no_q', 'with space', 'CaSe']) |
| 278 | ('EXCL', 'FIELD2', 'NO_Q', 'WITH_SPACE', 'CASE') |
| 279 | |
| 280 | """ |
| 281 | |
| 282 | defaultexcludelist = 'return', 'file', 'print' |
| 283 | defaultdeletechars = frozenset(r"""~!@#$%^&*()-=+~\|]}[{';: /?.>,<""") |
| 284 | |
| 285 | def __init__(self, excludelist=None, deletechars=None, |
| 286 | case_sensitive=None, replace_space='_'): |
no outgoing calls
searching dependent graphs…