MCPcopy Index your code
hub / github.com/numpy/numpy / StringConverter

Class StringConverter

numpy/lib/_iotools.py:452–821  ·  view source on GitHub ↗

Factory class for function transforming a string into another object (int, float). After initialization, an instance can be called to transform a string into another object. If the string is recognized as representing a missing value, a default value is returned. Attribute

Source from the content-addressed store, hash-verified

450
451
452class StringConverter:
453 """
454 Factory class for function transforming a string into another object
455 (int, float).
456
457 After initialization, an instance can be called to transform a string
458 into another object. If the string is recognized as representing a
459 missing value, a default value is returned.
460
461 Attributes
462 ----------
463 func : function
464 Function used for the conversion.
465 default : any
466 Default value to return when the input corresponds to a missing
467 value.
468 type : type
469 Type of the output.
470 _status : int
471 Integer representing the order of the conversion.
472 _mapper : sequence of tuples
473 Sequence of tuples (dtype, function, default value) to evaluate in
474 order.
475 _locked : bool
476 Holds `locked` parameter.
477
478 Parameters
479 ----------
480 dtype_or_func : {None, dtype, function}, optional
481 If a `dtype`, specifies the input data type, used to define a basic
482 function and a default value for missing data. For example, when
483 `dtype` is float, the `func` attribute is set to `float` and the
484 default value to `np.nan`. If a function, this function is used to
485 convert a string to another object. In this case, it is recommended
486 to give an associated default value as input.
487 default : any, optional
488 Value to return by default, that is, when the string to be
489 converted is flagged as missing. If not given, `StringConverter`
490 tries to supply a reasonable default value.
491 missing_values : {None, sequence of str}, optional
492 ``None`` or sequence of strings indicating a missing value. If ``None``
493 then missing values are indicated by empty entries. The default is
494 ``None``.
495 locked : bool, optional
496 Whether the StringConverter should be locked to prevent automatic
497 upgrade or not. Default is False.
498
499 """
500 _mapper = [(nx.bool, str2bool, False), # noqa: RUF012
501 (nx.int_, int, -1),]
502
503 # On 32-bit systems, we need to make sure that we explicitly include
504 # nx.int64 since ns.int_ is nx.int32.
505 if nx.dtype(nx.int_).itemsize < nx.dtype(nx.int64).itemsize:
506 _mapper.append((nx.int64, int, -1))
507
508 _mapper.extend([(nx.float64, float, nx.nan),
509 (nx.complex128, complex, nx.nan + 0j),

Callers 11

test_creationMethod · 0.90
test_upgradeMethod · 0.90
test_missingMethod · 0.90
test_upgrademapperMethod · 0.90
test_string_to_objectMethod · 0.90
test_keep_defaultMethod · 0.90
test_int64_dtypeMethod · 0.90
test_uint64_dtypeMethod · 0.90
genfromtxtFunction · 0.85

Calls 1

dtypeMethod · 0.45

Tested by 10

test_creationMethod · 0.72
test_upgradeMethod · 0.72
test_missingMethod · 0.72
test_upgrademapperMethod · 0.72
test_string_to_objectMethod · 0.72
test_keep_defaultMethod · 0.72
test_int64_dtypeMethod · 0.72
test_uint64_dtypeMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…