Skip to content

Commit 7e3963b

Browse files
committed
use Sequence from collections` in StringMatrix
replaces `user_array.container`
1 parent a4c7e11 commit 7e3963b

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

pyproject.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ xfail_strict = true
8181
# convert warnings to errors, except for ImportWarnings in numpy (needed for pp38)
8282
# FIXME: eventually we should be able to remove the numpy exception
8383
# TODO: need to also move away from `user_array.container`: https://github.com/alexlancaster/pypop/issues/358
84-
filterwarnings = ["error", "default::ImportWarning", "ignore:.*Arlequin.*deprecated.*",
85-
"ignore:The numpy\\.lib\\.user_array\\.container class is deprecated:DeprecationWarning"]
84+
filterwarnings = ["error", "default::ImportWarning", "ignore:.*Arlequin.*deprecated.*"]
8685
log_cli_level = "INFO"
8786
testpaths = [
8887
"website/docs",

src/PyPop/utils.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@
4444
import shutil
4545
import stat
4646
import sys
47+
from collections.abc import Sequence
4748
from pathlib import Path
4849

4950
import numpy as np
5051
from numpy import asarray, take, zeros
51-
from numpy.lib.user_array import container
5252

5353
from PyPop import logger
5454

@@ -217,7 +217,7 @@ def tagContents(self, tagname, content, **kw): # noqa: D417
217217
self.closetag(tagname)
218218

219219

220-
class StringMatrix(container):
220+
class StringMatrix(Sequence):
221221
"""Matrix of strings and other metadata from input file to PyPop.
222222
223223
``StringMatrix`` is a subclass of NumPy's
@@ -264,8 +264,9 @@ def __init__(
264264
self.array = zeros(
265265
(self.rowCount, self.colCount * 2 + self.extraCount), dtype="O"
266266
)
267+
self.dtype = self.array.dtype
267268
self.shape = self.array.shape
268-
self._typecode = self.array.dtype # Numeric array.typecode()
269+
self._typecode = self.array.dtype
269270
self.name = str(self.__class__).split()[0]
270271

271272
def __repr__(self):
@@ -286,6 +287,17 @@ def __repr__(self):
286287
return (self.__class__.__name__) + repr(self.array)[len("array") :]
287288
return (self.__class__.__name__) + "(" + repr(self.array) + ")"
288289

290+
def __len__(self):
291+
"""Get number of rows (individuals) in the matrix.
292+
293+
This allows StringMatrix instances to be used with `len()`,
294+
iteration, and other Python sequence protocols.
295+
296+
Returns:
297+
int: number of rows in the matrix
298+
"""
299+
return self.array.shape[0]
300+
289301
def dump(self, locus=None, stream=sys.stdout):
290302
"""Write file to a stream in original format.
291303

0 commit comments

Comments
 (0)