Skip to content

Commit

Permalink
Use numbers.Real instead of (int, float) (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
loganbvh committed Mar 10, 2024
1 parent deee6ae commit 01125dd
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 13 deletions.
7 changes: 4 additions & 3 deletions superscreen/device/device.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import numbers
import os
from collections import defaultdict
from contextlib import contextmanager, nullcontext
Expand Down Expand Up @@ -281,7 +282,7 @@ def scale(
if not (
isinstance(origin, tuple)
and len(origin) == 2
and all(isinstance(val, (int, float)) for val in origin)
and all(isinstance(val, numbers.Real) for val in origin)
):
raise TypeError("Origin must be a tuple of floats (x, y).")
self._warn_if_mesh_exist("scale()")
Expand All @@ -304,7 +305,7 @@ def rotate(self, degrees: float, origin: Tuple[float, float] = (0, 0)) -> "Devic
if not (
isinstance(origin, tuple)
and len(origin) == 2
and all(isinstance(val, (int, float)) for val in origin)
and all(isinstance(val, numbers.Real) for val in origin)
):
raise TypeError("Origin must be a tuple of floats (x, y).")
self._warn_if_mesh_exist("rotate()")
Expand Down Expand Up @@ -364,7 +365,7 @@ def translate(
return device

@contextmanager
def translation(self, dx: float, dy: float, dz: float = 0) -> None:
def translation(self, dx: float, dy: float, dz: float = 0):
"""A context manager that temporarily translates a device in-place,
then returns it to its original position.
Expand Down
7 changes: 4 additions & 3 deletions superscreen/device/layer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import numbers
from copy import deepcopy
from typing import Optional, Union

Expand Down Expand Up @@ -73,13 +74,13 @@ def Lambda(self, value: Union[float, Parameter]) -> None:

def __repr__(self) -> str:
Lambda = self.Lambda
if isinstance(Lambda, (int, float)):
if isinstance(Lambda, numbers.Real):
Lambda = f"{Lambda:.3f}"
d = self.thickness
if isinstance(d, (int, float)):
if isinstance(d, numbers.Real):
d = f"{d:.3f}"
london = self.london_lambda
if isinstance(london, (int, float)):
if isinstance(london, numbers.Real):
london = f"{london:.3f}"
return (
f"{self.__class__.__name__}({self.name!r}, Lambda={Lambda}, "
Expand Down
7 changes: 4 additions & 3 deletions superscreen/parameter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import inspect
import numbers
import operator
from typing import Callable, Optional, Union

Expand Down Expand Up @@ -239,7 +240,7 @@ def __init__(
f"Right must be a number, Parameter, or CompositeParameter, "
f"not {type(right)!r}."
)
if isinstance(left, (int, float)) and isinstance(right, (int, float)):
if isinstance(left, numbers.Real) and isinstance(right, numbers.Real):
raise TypeError(
"Either left or right must be a Parameter or CompositeParameter."
)
Expand All @@ -261,11 +262,11 @@ def __call__(
y: Union[int, float, np.ndarray],
z: Optional[Union[int, float, np.ndarray]] = None,
) -> Union[int, float, np.ndarray]:
if isinstance(self.left, (int, float)):
if isinstance(self.left, numbers.Real):
left_val = self.left
else:
left_val = self.left(x, y, z)
if isinstance(self.right, (int, float)):
if isinstance(self.right, numbers.Real):
right_val = self.right
else:
right_val = self.right(x, y, z)
Expand Down
7 changes: 4 additions & 3 deletions superscreen/solver/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import numbers
from dataclasses import dataclass
from typing import Dict, List, Optional, Tuple, Union

Expand Down Expand Up @@ -247,7 +248,7 @@ def make_film_info(
london_lambda = layer.london_lambda
d = layer.thickness
Lambda = layer.Lambda
if isinstance(london_lambda, (int, float)) and london_lambda <= d:
if isinstance(london_lambda, numbers.Real) and london_lambda <= d:
length_units = device.ureg(device.length_units).units
logger.info(
f"Layer {name!r}: The film thickness, d = {d:.4f} {length_units:~P},"
Expand All @@ -257,12 +258,12 @@ def make_film_info(
f" {length_units:~P}. The assumption that the current density is nearly"
f" constant over the thickness of the film may not be valid."
)
if isinstance(Lambda, (int, float)):
if isinstance(Lambda, numbers.Real):
Lambda = Constant(Lambda)
Lambda = Lambda(mesh.sites[:, 0], mesh.sites[:, 1]).astype(dtype, copy=False)
Lambda = Lambda[:, np.newaxis]
if london_lambda is not None:
if isinstance(london_lambda, (int, float)):
if isinstance(london_lambda, numbers.Real):
london_lambda = Constant(london_lambda)
london_lambda = london_lambda(mesh.sites[:, 0], mesh.sites[:, 1])
london_lambda = london_lambda.astype(dtype, copy=False)[:, np.newaxis]
Expand Down
2 changes: 1 addition & 1 deletion superscreen/version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version_info__ = (0, 10, 5)
__version_info__ = (0, 10, 6)
__version__ = ".".join(map(str, __version_info__))

0 comments on commit 01125dd

Please sign in to comment.