@@ -642,118 +642,121 @@ def _extendLine(s, line, word, line_width, next_line_prefix, legacy):
642
642
line += word
643
643
return s , line
644
644
645
+ def _recursive_fmt (param , index , indent , curr_width ):
646
+ """
647
+ Helper function for _formatArray, to recursively print array elements.
645
648
646
- def _formatArray (a , format_function , line_width , next_line_prefix ,
647
- separator , edge_items , summary_insert , legacy ):
648
- """formatArray is designed for two modes of operation:
649
+ "param" are the values that are invariant under recursion, including
650
+ the array to be printed itself. index, indent and curr_width
651
+ are updated during recursion.
652
+ """
653
+ # unpack parameters
654
+ a , format_function , separator , edge_items , summary_insert , legacy = param
649
655
650
- 1. Full output
656
+ axis = len (index )
657
+ axes_left = a .ndim - axis
651
658
652
- 2. Summarized output
659
+ if axes_left == 0 :
660
+ return format_function (a [index ])
653
661
654
- """
655
- def recurser (index , hanging_indent , curr_width ):
656
- """
657
- By using this local function, we don't need to recurse with all the
658
- arguments. Since this function is not created recursively, the cost is
659
- not significant
660
- """
661
- axis = len (index )
662
- axes_left = a .ndim - axis
662
+ # when recursing, add a space to align with the [ added, and reduce the
663
+ # length of the line by 1
664
+ next_indent = indent + ' '
665
+ if legacy == '1.13' :
666
+ next_width = curr_width
667
+ else :
668
+ next_width = curr_width - len (']' )
663
669
664
- if axes_left == 0 :
665
- return format_function (a [index ])
670
+ a_len = a .shape [axis ]
671
+ show_summary = summary_insert and 2 * edge_items < a_len
672
+ if show_summary :
673
+ leading_items = edge_items
674
+ trailing_items = edge_items
675
+ else :
676
+ leading_items = 0
677
+ trailing_items = a_len
666
678
667
- # when recursing, add a space to align with the [ added, and reduce the
668
- # length of the line by 1
669
- next_hanging_indent = hanging_indent + ' '
670
- if legacy == '1.13' :
671
- next_width = curr_width
672
- else :
673
- next_width = curr_width - len (']' )
679
+ # stringify the array with the hanging indent on the first line too
680
+ s = ''
674
681
675
- a_len = a . shape [ axis ]
676
- show_summary = summary_insert and 2 * edge_items < a_len
677
- if show_summary :
678
- leading_items = edge_items
679
- trailing_items = edge_items
682
+ # last axis (rows) - wrap elements if they would not fit on one line
683
+ if axes_left == 1 :
684
+ # the length up until the beginning of the separator / bracket
685
+ if legacy == '1.13' :
686
+ elem_width = curr_width - len ( separator . rstrip ())
680
687
else :
681
- leading_items = 0
682
- trailing_items = a_len
688
+ elem_width = curr_width - max (len (separator .rstrip ()), len (']' ))
683
689
684
- # stringify the array with the hanging indent on the first line too
685
- s = ''
690
+ line = indent
691
+ for i in range (leading_items ):
692
+ word = _recursive_fmt (param , index + (i ,), next_indent , next_width )
693
+ s , line = _extendLine (s , line , word , elem_width , indent , legacy )
694
+ line += separator
686
695
687
- # last axis (rows) - wrap elements if they would not fit on one line
688
- if axes_left == 1 :
689
- # the length up until the beginning of the separator / bracket
696
+ if show_summary :
697
+ s , line = _extendLine (
698
+ s , line , summary_insert , elem_width , indent , legacy )
690
699
if legacy == '1.13' :
691
- elem_width = curr_width - len ( separator . rstrip ())
700
+ line += ", "
692
701
else :
693
- elem_width = curr_width - max (len (separator .rstrip ()), len (']' ))
694
-
695
- line = hanging_indent
696
- for i in range (leading_items ):
697
- word = recurser (index + (i ,), next_hanging_indent , next_width )
698
- s , line = _extendLine (
699
- s , line , word , elem_width , hanging_indent , legacy )
700
702
line += separator
701
703
702
- if show_summary :
703
- s , line = _extendLine (
704
- s , line , summary_insert , elem_width , hanging_indent , legacy )
705
- if legacy == '1.13' :
706
- line += ", "
707
- else :
708
- line += separator
709
-
710
- for i in range (trailing_items , 1 , - 1 ):
711
- word = recurser (index + (- i ,), next_hanging_indent , next_width )
712
- s , line = _extendLine (
713
- s , line , word , elem_width , hanging_indent , legacy )
714
- line += separator
704
+ for i in range (trailing_items , 1 , - 1 ):
705
+ word = _recursive_fmt (param , index + (- i ,), next_indent , next_width )
706
+ s , line = _extendLine (s , line , word , elem_width , indent , legacy )
707
+ line += separator
708
+
709
+ if legacy == '1.13' :
710
+ # width of the separator is not considered on 1.13
711
+ elem_width = curr_width
712
+ word = _recursive_fmt (param , index + (- 1 ,), next_indent , next_width )
713
+ s , line = _extendLine (
714
+ s , line , word , elem_width , indent , legacy )
715
+
716
+ s += line
717
+
718
+ # other axes - insert newlines between rows
719
+ else :
720
+ s = ''
721
+ line_sep = separator .rstrip () + '\n ' * (axes_left - 1 )
722
+
723
+ for i in range (leading_items ):
724
+ nested = _recursive_fmt (param , index + (i ,), next_indent , next_width )
725
+ s += indent + nested + line_sep
715
726
727
+ if show_summary :
716
728
if legacy == '1.13' :
717
- # width of the seperator is not considered on 1.13
718
- elem_width = curr_width
719
- word = recurser (index + (- 1 ,), next_hanging_indent , next_width )
720
- s , line = _extendLine (
721
- s , line , word , elem_width , hanging_indent , legacy )
729
+ # trailing space, fixed nbr of newlines, and fixed separator
730
+ s += indent + summary_insert + ", \n "
731
+ else :
732
+ s += indent + summary_insert + line_sep
722
733
723
- s += line
734
+ for i in range (trailing_items , 1 , - 1 ):
735
+ nested = _recursive_fmt (param , index + (- i ,), next_indent ,
736
+ next_width )
737
+ s += indent + nested + line_sep
724
738
725
- # other axes - insert newlines between rows
726
- else :
727
- s = ''
728
- line_sep = separator .rstrip () + '\n ' * (axes_left - 1 )
739
+ nested = _recursive_fmt (param , index + (- 1 ,), next_indent , next_width )
740
+ s += indent + nested
729
741
730
- for i in range ( leading_items ):
731
- nested = recurser ( index + ( i ,), next_hanging_indent , next_width )
732
- s += hanging_indent + nested + line_sep
742
+ # remove the hanging indent, and wrap in []
743
+ s = '[' + s [ len ( indent ):] + ']'
744
+ return s
733
745
734
- if show_summary :
735
- if legacy == '1.13' :
736
- # trailing space, fixed nbr of newlines, and fixed separator
737
- s += hanging_indent + summary_insert + ", \n "
738
- else :
739
- s += hanging_indent + summary_insert + line_sep
746
+ def _formatArray (a , format_function , line_width , next_line_prefix ,
747
+ separator , edge_items , summary_insert , legacy ):
748
+ """_formatArray is designed for two modes of operation:
740
749
741
- for i in range (trailing_items , 1 , - 1 ):
742
- nested = recurser (index + (- i ,), next_hanging_indent , next_width )
743
- s += hanging_indent + nested + line_sep
750
+ 1. Full output
744
751
745
- nested = recurser (index + (- 1 ,), next_hanging_indent , next_width )
746
- s += hanging_indent + nested
752
+ 2. Summarized output
747
753
748
- # remove the hanging indent, and wrap in []
749
- s = '[' + s [len (hanging_indent ):] + ']'
750
- return s
754
+ """
751
755
752
756
# invoke the recursive part with an initial index and prefix
753
- return recurser (
754
- index = (),
755
- hanging_indent = next_line_prefix ,
756
- curr_width = line_width )
757
+ param = a , format_function , separator , edge_items , summary_insert , legacy
758
+ return _recursive_fmt (param , index = (), indent = next_line_prefix ,
759
+ curr_width = line_width )
757
760
758
761
759
762
def _none_or_positive_arg (x , name ):
0 commit comments