from decimal import Decimal


class FixedWidthWriter():

    def __init__(self, fd, fields, line_ending='linux'):
        self.fd = fd
        self.fields = fields
        if line_ending == 'linux':
            self.line_ending = '\n'
        elif line_ending == 'windows':
            self.line_ending = '\r\n'
        else:
            raise ValueError('Only windows or linux line endings supported')

    def writerow(self, rowdict):
        _row = []
        for field in self.fields:
            try:
                _key, _width, _options = field
            except ValueError:
                _key, _width = field
                _options = {}
            _value = rowdict[_key]
            _decimal_spaces = _options.get('decimal_spaces', 0)
            if _decimal_spaces:
                _value = unicode(Decimal(_value)
                                 .quantize(Decimal(10)**-_decimal_spaces))
            _part = '{0: {1}{2}}' \
                .format(_value, _options.get('direction', '<'), _width)
            _row.append(_part)
        _row = ''.join(_row)
        self.fd.write(_row + self.line_ending)

    def writerows(self, rowdicts):
        for rowdict in rowdicts:
            self.writerow(rowdict)

from decimal import Decimal


class FixedWidthWriter():

    def __init__(self, fd, fields, line_ending='linux'):
        self.fd = fd
        self.fields = fields
        if line_ending == 'linux':
            self.line_ending = '\n'
        elif line_ending == 'windows':
            self.line_ending = '\r\n'
        else:
            raise ValueError('Only windows or linux line endings supported')

    def writerow(self, rowdict):
        row = []
        for field in self.fields:
            try:
                key, width, options = field
            except ValueError:
                key, width = field
                options = {}
            value = rowdict[key]
            decimal_spaces = options.get('decimal_spaces', 0)
            if decimal_spaces:
                value = unicode(Decimal(value)
                                .quantize(Decimal(10)**-decimal_spaces))
            part = '{0: {1}{2}}' \
                .format(value, options.get('direction', '<'), width)
            row.append(part)
        row = ''.join(row)
        self.fd.write(row + self.line_ending)

    def writerows(self, rowdicts):
        for rowdict in rowdicts:
            self.writerow(rowdict)

Remove leading underscores from variables.
Remove leading underscores from variables.

  from decimal import Decimal
  
  
  class FixedWidthWriter():
  
      def __init__(self, fd, fields, line_ending='linux'):
          self.fd = fd
          self.fields = fields
          if line_ending == 'linux':
              self.line_ending = '\n'
          elif line_ending == 'windows':
              self.line_ending = '\r\n'
          else:
              raise ValueError('Only windows or linux line endings supported')
  
      def writerow(self, rowdict):
-         _row = []
+         row = []
          for field in self.fields:
              try:
-                 _key, _width, _options = field
+                 key, width, options = field
              except ValueError:
-                 _key, _width = field
+                 key, width = field
-                 _options = {}
+                 options = {}
-             _value = rowdict[_key]
+             value = rowdict[key]
-             _decimal_spaces = _options.get('decimal_spaces', 0)
+             decimal_spaces = options.get('decimal_spaces', 0)
-             if _decimal_spaces:
+             if decimal_spaces:
-                 _value = unicode(Decimal(_value)
+                 value = unicode(Decimal(value)
-                                  .quantize(Decimal(10)**-_decimal_spaces))
+                                 .quantize(Decimal(10)**-decimal_spaces))
-             _part = '{0: {1}{2}}' \
+             part = '{0: {1}{2}}' \
-                 .format(_value, _options.get('direction', '<'), _width)
+                 .format(value, options.get('direction', '<'), width)
-             _row.append(_part)
+             row.append(part)
-         _row = ''.join(_row)
+         row = ''.join(row)
-         self.fd.write(_row + self.line_ending)
+         self.fd.write(row + self.line_ending)
  
      def writerows(self, rowdicts):
          for rowdict in rowdicts:
              self.writerow(rowdict)
  
Remove leading underscores from variables.
## Code Before:
from decimal import Decimal


class FixedWidthWriter():

    def __init__(self, fd, fields, line_ending='linux'):
        self.fd = fd
        self.fields = fields
        if line_ending == 'linux':
            self.line_ending = '\n'
        elif line_ending == 'windows':
            self.line_ending = '\r\n'
        else:
            raise ValueError('Only windows or linux line endings supported')

    def writerow(self, rowdict):
        _row = []
        for field in self.fields:
            try:
                _key, _width, _options = field
            except ValueError:
                _key, _width = field
                _options = {}
            _value = rowdict[_key]
            _decimal_spaces = _options.get('decimal_spaces', 0)
            if _decimal_spaces:
                _value = unicode(Decimal(_value)
                                 .quantize(Decimal(10)**-_decimal_spaces))
            _part = '{0: {1}{2}}' \
                .format(_value, _options.get('direction', '<'), _width)
            _row.append(_part)
        _row = ''.join(_row)
        self.fd.write(_row + self.line_ending)

    def writerows(self, rowdicts):
        for rowdict in rowdicts:
            self.writerow(rowdict)

## Instruction:
Remove leading underscores from variables.
## Code After:
from decimal import Decimal


class FixedWidthWriter():

    def __init__(self, fd, fields, line_ending='linux'):
        self.fd = fd
        self.fields = fields
        if line_ending == 'linux':
            self.line_ending = '\n'
        elif line_ending == 'windows':
            self.line_ending = '\r\n'
        else:
            raise ValueError('Only windows or linux line endings supported')

    def writerow(self, rowdict):
        row = []
        for field in self.fields:
            try:
                key, width, options = field
            except ValueError:
                key, width = field
                options = {}
            value = rowdict[key]
            decimal_spaces = options.get('decimal_spaces', 0)
            if decimal_spaces:
                value = unicode(Decimal(value)
                                .quantize(Decimal(10)**-decimal_spaces))
            part = '{0: {1}{2}}' \
                .format(value, options.get('direction', '<'), width)
            row.append(part)
        row = ''.join(row)
        self.fd.write(row + self.line_ending)

    def writerows(self, rowdicts):
        for rowdict in rowdicts:
            self.writerow(rowdict)
