modernize type annotations and improve handling of int scalars
This commit is contained in:
parent
f12a1c6421
commit
15af9078f0
@ -1,7 +1,8 @@
|
|||||||
"""
|
"""
|
||||||
Functionality for encoding/decoding basic datatypes
|
Functionality for encoding/decoding basic datatypes
|
||||||
"""
|
"""
|
||||||
from typing import Sequence, IO
|
from typing import IO
|
||||||
|
from collections.abc import Sequence
|
||||||
import struct
|
import struct
|
||||||
import logging
|
import logging
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
Functionality for reading/writing elements (geometry, text labels,
|
Functionality for reading/writing elements (geometry, text labels,
|
||||||
structure references) and associated properties.
|
structure references) and associated properties.
|
||||||
"""
|
"""
|
||||||
from typing import Optional, IO, TypeVar, Type, Union
|
from typing import IO, TypeVar
|
||||||
from abc import ABCMeta, abstractmethod
|
from abc import ABCMeta, abstractmethod
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
|
|
||||||
@ -78,7 +78,7 @@ class Element(metaclass=ABCMeta):
|
|||||||
"""
|
"""
|
||||||
@classmethod
|
@classmethod
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def read(cls: Type[E], stream: IO[bytes]) -> E:
|
def read(cls: type[E], stream: IO[bytes]) -> E:
|
||||||
"""
|
"""
|
||||||
Read from a stream to construct this object.
|
Read from a stream to construct this object.
|
||||||
Consumes up to (and including) the ENDEL record.
|
Consumes up to (and including) the ENDEL record.
|
||||||
@ -151,7 +151,7 @@ class Reference(Element):
|
|||||||
""" Properties associated with this reference. """
|
""" Properties associated with this reference. """
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def read(cls: Type[R], stream: IO[bytes]) -> R:
|
def read(cls: type[R], stream: IO[bytes]) -> R:
|
||||||
invert_y = False
|
invert_y = False
|
||||||
mag = 1
|
mag = 1
|
||||||
angle_deg = 0
|
angle_deg = 0
|
||||||
@ -233,7 +233,7 @@ class Boundary(Element):
|
|||||||
""" Properties for the element. """
|
""" Properties for the element. """
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def read(cls: Type[B], stream: IO[bytes]) -> B:
|
def read(cls: type[B], stream: IO[bytes]) -> B:
|
||||||
layer = LAYER.skip_and_read(stream)[0]
|
layer = LAYER.skip_and_read(stream)[0]
|
||||||
dtype = DATATYPE.read(stream)[0]
|
dtype = DATATYPE.read(stream)[0]
|
||||||
xy = XY.read(stream).reshape(-1, 2)
|
xy = XY.read(stream).reshape(-1, 2)
|
||||||
@ -279,7 +279,7 @@ class Path(Element):
|
|||||||
""" Properties for the element. """
|
""" Properties for the element. """
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def read(cls: Type[P], stream: IO[bytes]) -> P:
|
def read(cls: type[P], stream: IO[bytes]) -> P:
|
||||||
path_type = 0
|
path_type = 0
|
||||||
width = 0
|
width = 0
|
||||||
bgn_ext = 0
|
bgn_ext = 0
|
||||||
@ -344,7 +344,7 @@ class Box(Element):
|
|||||||
""" Properties for the element. """
|
""" Properties for the element. """
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def read(cls: Type[X], stream: IO[bytes]) -> X:
|
def read(cls: type[X], stream: IO[bytes]) -> X:
|
||||||
layer = LAYER.skip_and_read(stream)[0]
|
layer = LAYER.skip_and_read(stream)[0]
|
||||||
dtype = BOXTYPE.read(stream)[0]
|
dtype = BOXTYPE.read(stream)[0]
|
||||||
xy = XY.read(stream).reshape(-1, 2)
|
xy = XY.read(stream).reshape(-1, 2)
|
||||||
@ -378,7 +378,7 @@ class Node(Element):
|
|||||||
""" Properties for the element. """
|
""" Properties for the element. """
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def read(cls: Type[N], stream: IO[bytes]) -> N:
|
def read(cls: type[N], stream: IO[bytes]) -> N:
|
||||||
layer = LAYER.skip_and_read(stream)[0]
|
layer = LAYER.skip_and_read(stream)[0]
|
||||||
dtype = NODETYPE.read(stream)[0]
|
dtype = NODETYPE.read(stream)[0]
|
||||||
xy = XY.read(stream).reshape(-1, 2)
|
xy = XY.read(stream).reshape(-1, 2)
|
||||||
@ -438,7 +438,7 @@ class Text(Element):
|
|||||||
""" Properties for the element. """
|
""" Properties for the element. """
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def read(cls: Type[T], stream: IO[bytes]) -> T:
|
def read(cls: type[T], stream: IO[bytes]) -> T:
|
||||||
path_type = 0
|
path_type = 0
|
||||||
presentation = 0
|
presentation = 0
|
||||||
invert_y = False
|
invert_y = False
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
"""
|
"""
|
||||||
File-level read/write functionality.
|
File-level read/write functionality.
|
||||||
"""
|
"""
|
||||||
from typing import IO, TypeVar, Type, MutableMapping
|
from typing import IO, Self, TYPE_CHECKING
|
||||||
import io
|
import io
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
@ -15,8 +15,8 @@ from .records import BGNSTR, STRNAME, ENDSTR, SNAME, COLROW, ENDEL
|
|||||||
from .records import BOX, BOUNDARY, NODE, PATH, TEXT, SREF, AREF
|
from .records import BOX, BOUNDARY, NODE, PATH, TEXT, SREF, AREF
|
||||||
from .elements import Element, Reference, Text, Box, Boundary, Path, Node
|
from .elements import Element, Reference, Text, Box, Boundary, Path, Node
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
FH = TypeVar('FH', bound='FileHeader')
|
from collections.abc import MutableMapping
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
@ -45,7 +45,7 @@ class FileHeader:
|
|||||||
""" Last-accessed time """
|
""" Last-accessed time """
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def read(cls: Type[FH], stream: IO[bytes]) -> FH:
|
def read(cls: type[Self], stream: IO[bytes]) -> Self:
|
||||||
"""
|
"""
|
||||||
Read and construct a header from the provided stream.
|
Read and construct a header from the provided stream.
|
||||||
|
|
||||||
|
@ -1,7 +1,10 @@
|
|||||||
"""
|
"""
|
||||||
Record type and tag definitions
|
Record type and tag definitions
|
||||||
"""
|
"""
|
||||||
from typing import Sequence
|
from typing import Self
|
||||||
|
from collections.abc import Sequence, Sized
|
||||||
|
import numpy
|
||||||
|
from numpy.typing import NDArray
|
||||||
|
|
||||||
from .record import NoDataRecord, BitArrayRecord, Int2Record, Int4Record, Real8Record
|
from .record import NoDataRecord, BitArrayRecord, Int2Record, Int4Record, Real8Record
|
||||||
from .record import ASCIIRecord, DateTimeRecord
|
from .record import ASCIIRecord, DateTimeRecord
|
||||||
@ -169,7 +172,7 @@ class GENERATIONS(Int2Record):
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def check_data(cls: type[Self], data: NDArray[numpy.integer] | Sequence[int] | int) -> None:
|
def check_data(cls: type[Self], data: NDArray[numpy.integer] | Sequence[int] | int) -> None:
|
||||||
if len(data) != 1:
|
if not isinstance(data, Sized) or len(data) != 1:
|
||||||
raise Exception(f'Expected exactly one integer, got {data}')
|
raise Exception(f'Expected exactly one integer, got {data}')
|
||||||
|
|
||||||
|
|
||||||
@ -267,7 +270,7 @@ class FORMAT(Int2Record):
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def check_data(cls: type[Self], data: NDArray[numpy.integer] | Sequence[int] | int) -> None:
|
def check_data(cls: type[Self], data: NDArray[numpy.integer] | Sequence[int] | int) -> None:
|
||||||
if len(data) != 1:
|
if not isinstance(data, Sized) or len(data) != 1:
|
||||||
raise Exception(f'Expected exactly one integer, got {data}')
|
raise Exception(f'Expected exactly one integer, got {data}')
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user