def _insert_thousands_sep(digits, spec, min_width=1):
 sep = spec['thousands_sep']
 grouping = spec['grouping']
 groups = []
 for l in _group_lengths(grouping):
  if (l <= 0):
   raise ValueError('group length should be positive')
  l = min(max(len(digits), min_width, 1), l)
  groups.append((('0' * (l - len(digits))) + digits[(- l):]))
  digits = digits[:(- l)]
  min_width -= l
  if ((not digits) and (min_width <= 0)):
   break
  min_width -= len(sep)
 else:
  l = max(len(digits), min_width, 1)
  groups.append((('0' * (l - len(digits))) + digits[(- l):]))
 return sep.join(reversed(groups))

def _insert_thousands_sep ( digits , spec , min_width = 1 ) : sep = spec [ 'thousands_sep' ] grouping = spec [ 'grouping' ] groups = [ ] for l in _group_lengths ( grouping ) : if ( l <= 0 ) : raise ValueError ( 'group length should be positive' ) l = min ( max ( len ( digits ) , min_width , 1 ) , l ) groups . append ( ( ( '0' * ( l - len ( digits ) ) ) + digits [ ( - l ) : ] ) ) digits = digits [ : ( - l ) ] min_width -= l if ( ( not digits ) and ( min_width <= 0 ) ) : break min_width -= len ( sep ) else : l = max ( len ( digits ) , min_width , 1 ) groups . append ( ( ( '0' * ( l - len ( digits ) ) ) + digits [ ( - l ) : ] ) ) return sep . join ( reversed ( groups ) )
insert thousands separators into a digit string .