Skip to content

Commit a59357a

Browse files
committed
Remove numpy dependency; implement shape inference
Drop numpy from pyproject and remove its import from constant.py. Replace numpy-based shape detection with a new Constant._infer_shape method that handles objects with a .shape attribute as well as nested Python lists/tuples (including empty lists), returning None for scalar values. This eliminates the runtime/dependency on numpy while preserving array-shape inference for constants.
1 parent d5ffff2 commit a59357a

2 files changed

Lines changed: 17 additions & 10 deletions

File tree

‎pyproject.toml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ readme = "README.md"
99
requires-python = ">=3.8.0"
1010
dependencies = [
1111
"minizinc",
12-
"numpy",
1312
]
1413
classifiers = [
1514
"Programming Language :: Python :: 3",

‎src/pymzm/constant.py‎

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
from .variable import *
44
from .misc import *
55

6-
import numpy as np
7-
86

97
class Constant(Expression):
108
def __init__(self, name: str, value, vtype=Variable.VTYPE_INTEGER):
@@ -22,14 +20,24 @@ def __init__(self, name: str, value, vtype=Variable.VTYPE_INTEGER):
2220
if (not self.is_enum_type and self.vtype not in [Variable.VTYPE_INTEGER, Variable.VTYPE_BOOL, Variable.VTYPE_FLOAT, Variable.VTYPE_STRING]):
2321
raise Exception("Invalid vtype for constant. Supported scalar types are int, bool, float, string, and enum type names")
2422

25-
arr = np.array(value)
26-
if (arr.shape):
27-
# Is nd array
28-
self.shape = arr.shape
23+
self.shape = self._infer_shape(value)
2924

30-
else:
31-
# is single value
32-
self.shape = None
25+
@staticmethod
26+
def _infer_shape(value):
27+
if (hasattr(value, "shape")):
28+
shape = tuple(int(d) for d in value.shape)
29+
return shape if len(shape) > 0 else None
30+
31+
def _shape_of(v):
32+
if (isinstance(v, (list, tuple))):
33+
if (len(v) == 0):
34+
return (0,)
35+
first_shape = _shape_of(v[0])
36+
return (len(v), *first_shape)
37+
return ()
38+
39+
shape = _shape_of(value)
40+
return shape if len(shape) > 0 else None
3341

3442
def __getitem__(self, other: Expression):
3543
# TODO boolean expression

0 commit comments

Comments
 (0)