1919 "repeat" ,
2020 "from_template" ,
2121 "coord" ,
22+ "set_print_limits" ,
2223]
2324
2425import abc
2526import numbers
27+ import textwrap
2628from collections .abc import Sequence
2729import numpy as np
2830from biotite .copyable import Copyable
@@ -42,6 +44,9 @@ class _AtomArrayBase(Copyable, metaclass=abc.ABCMeta):
4244 The amount of atoms in the structure.
4345 """
4446
47+ _max_models_printed = 10
48+ _max_atoms_printed = 1000
49+
4550 def __init__ (self , length ):
4651 """
4752 Create the annotation arrays
@@ -685,12 +690,11 @@ def __init__(self, length):
685690 def __repr__ (self ):
686691 """Represent AtomArray as a string for debugging."""
687692 atoms = ""
688- for i in range (0 , self .array_length ()):
689- if len (atoms ) == 0 :
690- atoms = "\n \t " + self .get_atom (i ).__repr__ ()
691- else :
692- atoms = atoms + ",\n \t " + self .get_atom (i ).__repr__ ()
693- return f"array([{ atoms } \n ])"
693+ for i in range (0 , min (self .array_length (), _AtomArrayBase ._max_atoms_printed )):
694+ atoms = textwrap .indent (self .get_atom (i ).__repr__ (), "\t " ) + ",\n "
695+ if self .array_length () > _AtomArrayBase ._max_atoms_printed :
696+ atoms = atoms + "\t ...,\n "
697+ return f"array([\n { atoms } ])"
694698
695699 @property
696700 def shape (self ):
@@ -833,7 +837,12 @@ def __str__(self):
833837
834838 Each line contains the attributes of one atom.
835839 """
836- return "\n " .join ([str (atom ) for atom in self ])
840+ string = "\n " .join (
841+ [str (atom ) for atom in self [: _AtomArrayBase ._max_atoms_printed ]]
842+ )
843+ if self .array_length () > _AtomArrayBase ._max_atoms_printed :
844+ string += "\n \t ..."
845+ return string
837846
838847 def __copy_create__ (self ):
839848 return AtomArray (self .array_length ())
@@ -940,12 +949,11 @@ def __init__(self, depth, length):
940949 def __repr__ (self ):
941950 """Represent AtomArrayStack as a string for debugging."""
942951 arrays = ""
943- for i in range (0 , self .stack_depth ()):
944- if len (arrays ) == 0 :
945- arrays = "\n \t " + self .get_array (i ).__repr__ ()
946- else :
947- arrays = arrays + ",\n \t " + self .get_array (i ).__repr__ ()
948- return f"stack([{ arrays } \n ])"
952+ for i in range (0 , min (self .stack_depth (), _AtomArrayBase ._max_models_printed )):
953+ arrays = textwrap .indent (self .get_array (i ).__repr__ (), "\t " ) + ",\n "
954+ if self .stack_depth () > _AtomArrayBase ._max_models_printed :
955+ arrays = arrays + "\t ...,\n "
956+ return f"stack([\n { arrays } ])"
949957
950958 def get_array (self , index ):
951959 """
@@ -1150,6 +1158,9 @@ def __str__(self):
11501158 """
11511159 string = ""
11521160 for i , array in enumerate (self ):
1161+ if i >= _AtomArrayBase ._max_models_printed :
1162+ string += "..." + "\n " + "\n "
1163+ break
11531164 string += "Model " + str (i + 1 ) + "\n "
11541165 string += str (array ) + "\n " + "\n "
11551166 return string
@@ -1560,3 +1571,21 @@ def coord(item):
15601571 return item .astype (np .float32 , copy = False )
15611572 else :
15621573 return np .array (item , dtype = np .float32 )
1574+
1575+
1576+ def set_print_limits (max_models = 10 , max_atoms = 1000 ):
1577+ """
1578+ Set the maximum number of models and atoms to print in the ``str()`` and ``repr()``
1579+ representations.
1580+
1581+ The remaining models/atoms are abbreviated by ellipses.
1582+
1583+ Parameters
1584+ ----------
1585+ max_models : int
1586+ The maximum number of models to print.
1587+ max_atoms : int
1588+ The maximum number of atoms to print.
1589+ """
1590+ _AtomArrayBase ._max_models_printed = max_models
1591+ _AtomArrayBase ._max_atoms_printed = max_atoms
0 commit comments