@@ -642,121 +642,118 @@ 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.
648
-
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
655
645
656
- axis = len (index )
657
- axes_left = a .ndim - axis
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:
658
649
659
- if axes_left == 0 :
660
- return format_function (a [index ])
650
+ 1. Full output
661
651
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 (']' )
652
+ 2. Summarized output
669
653
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
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
678
663
679
- # stringify the array with the hanging indent on the first line too
680
- s = ''
664
+ if axes_left == 0 :
665
+ return format_function ( a [ index ])
681
666
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
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 + ' '
685
670
if legacy == '1.13' :
686
- elem_width = curr_width - len ( separator . rstrip ())
671
+ next_width = curr_width
687
672
else :
688
- elem_width = curr_width - max (len (separator .rstrip ()), len (']' ))
689
-
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
673
+ next_width = curr_width - len (']' )
695
674
675
+ a_len = a .shape [axis ]
676
+ show_summary = summary_insert and 2 * edge_items < a_len
696
677
if show_summary :
697
- s , line = _extendLine (
698
- s , line , summary_insert , elem_width , indent , legacy )
699
- if legacy == '1.13' :
700
- line += ", "
701
- else :
702
- line += separator
703
-
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
678
+ leading_items = edge_items
679
+ trailing_items = edge_items
680
+ else :
681
+ leading_items = 0
682
+ trailing_items = a_len
708
683
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 )
684
+ # stringify the array with the hanging indent on the first line too
685
+ s = ''
715
686
716
- s += line
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
690
+ if legacy == '1.13' :
691
+ elem_width = curr_width - len (separator .rstrip ())
692
+ else :
693
+ elem_width = curr_width - max (len (separator .rstrip ()), len (']' ))
717
694
718
- # other axes - insert newlines between rows
719
- else :
720
- s = ''
721
- line_sep = separator .rstrip () + '\n ' * (axes_left - 1 )
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
+ line += separator
722
701
723
- for i in range (leading_items ):
724
- nested = _recursive_fmt (param , index + (i ,), next_indent , next_width )
725
- s += indent + nested + line_sep
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
726
715
727
- if show_summary :
728
716
if legacy == '1.13' :
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
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 )
733
722
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
723
+ s += line
738
724
739
- nested = _recursive_fmt (param , index + (- 1 ,), next_indent , next_width )
740
- s += indent + nested
725
+ # other axes - insert newlines between rows
726
+ else :
727
+ s = ''
728
+ line_sep = separator .rstrip () + '\n ' * (axes_left - 1 )
741
729
742
- # remove the hanging indent, and wrap in []
743
- s = '[' + s [ len ( indent ):] + ']'
744
- return s
730
+ for i in range ( leading_items ):
731
+ nested = recurser ( index + ( i ,), next_hanging_indent , next_width )
732
+ s += hanging_indent + nested + line_sep
745
733
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:
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
749
740
750
- 1. Full output
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
751
744
752
- 2. Summarized output
745
+ nested = recurser (index + (- 1 ,), next_hanging_indent , next_width )
746
+ s += hanging_indent + nested
753
747
754
- """
748
+ # remove the hanging indent, and wrap in []
749
+ s = '[' + s [len (hanging_indent ):] + ']'
750
+ return s
755
751
756
752
# invoke the recursive part with an initial index and prefix
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 )
753
+ return recurser (
754
+ index = (),
755
+ hanging_indent = next_line_prefix ,
756
+ curr_width = line_width )
760
757
761
758
762
759
def _none_or_positive_arg (x , name ):
0 commit comments