4444import shutil
4545import stat
4646import sys
47+ from collections .abc import Sequence
4748from pathlib import Path
4849
4950import numpy as np
5051from numpy import asarray , take , zeros
51- from numpy .lib .user_array import container
5252
5353from 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