Create an Atom from a PyTables kind. Optional item size, shape and default value may be specified as the itemsize, shape and dflt arguments, respectively. Bear in mind that not all atoms support a default item size:: >>> Atom.from_kind('int', itemsize=2,
(
cls,
kind: str,
itemsize: int | None = None,
shape: Shape = (),
dflt: Any = None,
)
| 419 | |
| 420 | @classmethod |
| 421 | def from_kind( |
| 422 | cls, |
| 423 | kind: str, |
| 424 | itemsize: int | None = None, |
| 425 | shape: Shape = (), |
| 426 | dflt: Any = None, |
| 427 | ) -> Atom: |
| 428 | """Create an Atom from a PyTables kind. |
| 429 | |
| 430 | Optional item size, shape and default value may be |
| 431 | specified as the itemsize, shape and dflt |
| 432 | arguments, respectively. Bear in mind that not all atoms support |
| 433 | a default item size:: |
| 434 | |
| 435 | >>> Atom.from_kind('int', itemsize=2, shape=(2, 2)) |
| 436 | Int16Atom(shape=(2, 2), dflt=0) |
| 437 | >>> Atom.from_kind('int', shape=(2, 2)) |
| 438 | Int32Atom(shape=(2, 2), dflt=0) |
| 439 | >>> Atom.from_kind('int', shape=1) |
| 440 | Int32Atom(shape=(1,), dflt=0) |
| 441 | >>> Atom.from_kind('string', dflt=b'hello') |
| 442 | Traceback (most recent call last): |
| 443 | ... |
| 444 | ValueError: no default item size for kind ``string`` |
| 445 | >>> Atom.from_kind('Float') |
| 446 | Traceback (most recent call last): |
| 447 | ... |
| 448 | ValueError: unknown kind: 'Float' |
| 449 | |
| 450 | Moreover, some kinds with atypical constructor signatures |
| 451 | are not supported; you need to use the proper |
| 452 | constructor:: |
| 453 | |
| 454 | >>> Atom.from_kind('enum') #doctest: +ELLIPSIS |
| 455 | Traceback (most recent call last): |
| 456 | ... |
| 457 | ValueError: the ``enum`` kind is not supported... |
| 458 | |
| 459 | """ |
| 460 | kwargs: dict[str, Any] = {"shape": shape} |
| 461 | if kind not in atom_map: |
| 462 | raise ValueError(f"unknown kind: {kind!r}") |
| 463 | # This incompatibility detection may get out-of-date and is |
| 464 | # too hard-wired, but I couldn't come up with something |
| 465 | # smarter. -- Ivan (2007-02-08) |
| 466 | if kind in ["enum"]: |
| 467 | raise ValueError( |
| 468 | "the ``%s`` kind is not supported; " |
| 469 | "please use the appropriate constructor" % kind |
| 470 | ) |
| 471 | # If no `itemsize` is given, try to get the default type of the |
| 472 | # kind (which has a fixed item size). |
| 473 | if itemsize is None: |
| 474 | if kind not in deftype_from_kind: |
| 475 | raise ValueError("no default item size for kind ``%s``" % kind) |
| 476 | type_ = deftype_from_kind[kind] |
| 477 | kind, itemsize = split_type(type_) |
| 478 | kdata = atom_map[kind] |