Split a PyTables type into a PyTables kind and an item size. Returns a tuple of (kind, itemsize). If no item size is present in the type (in the form of a precision), the returned item size is None:: >>> split_type('int32') ('int', 4) >>> split_type('string')
(type_: str)
| 40 | |
| 41 | |
| 42 | def split_type(type_: str) -> tuple[str, int | None]: |
| 43 | """Split a PyTables type into a PyTables kind and an item size. |
| 44 | |
| 45 | Returns a tuple of (kind, itemsize). If no item size is present in the type |
| 46 | (in the form of a precision), the returned item size is None:: |
| 47 | |
| 48 | >>> split_type('int32') |
| 49 | ('int', 4) |
| 50 | >>> split_type('string') |
| 51 | ('string', None) |
| 52 | >>> split_type('int20') |
| 53 | Traceback (most recent call last): |
| 54 | ... |
| 55 | ValueError: precision must be a multiple of 8: 20 |
| 56 | >>> split_type('foo bar') |
| 57 | Traceback (most recent call last): |
| 58 | ... |
| 59 | ValueError: malformed type: 'foo bar' |
| 60 | |
| 61 | """ |
| 62 | match = _type_re.match(type_) |
| 63 | if not match: |
| 64 | raise ValueError("malformed type: %r" % type_) |
| 65 | kind, precision = match.groups() |
| 66 | itemsize = None |
| 67 | if precision: |
| 68 | precision = int(precision) |
| 69 | itemsize, remainder = divmod(precision, 8) |
| 70 | if remainder: # 0 could be a valid item size |
| 71 | raise ValueError( |
| 72 | "precision must be a multiple of 8: %d" % precision |
| 73 | ) |
| 74 | return (kind, itemsize) |
| 75 | |
| 76 | |
| 77 | def _invalid_itemsize_error( |
no outgoing calls
no test coverage detected