import struct
from steel.fields import Field

__all__ = ['Integer']


class Integer(Field):
    "An integer represented as a sequence and bytes"

    # These map a number of bytes to a struct format code
    size_formats = {
        1: 'B',  # char
        2: 'H',  # short
        4: 'L',  # long
        8: 'Q',  # long long
    }

    def __init__(self, *args, endianness='<', **kwargs):
        super(Integer, self).__init__(*args, **kwargs)
        self.format_code = endianness + self.size_formats[self.size]

    def encode(self, value):
        return struct.pack(self.format_code, value)

    def decode(self, value):
        # The index on the end is because unpack always returns a tuple
        return struct.unpack(self.format_code, value)[0]

import struct
from steel.fields import Field

__all__ = ['Integer']


class Integer(Field):
    "An integer represented as a sequence and bytes"

    # These map a number of bytes to a struct format code
    size_formats = {
        1: 'B',  # char
        2: 'H',  # short
        4: 'L',  # long
        8: 'Q',  # long long
    }

    def __init__(self, *args, endianness='<', **kwargs):
        super(Integer, self).__init__(*args, **kwargs)
        self.format_code = endianness + self.size_formats[self.size]

    def encode(self, value):
        try:
            return struct.pack(self.format_code, value)
        except struct.error as e:
            raise ValueError(*e.args)

    def decode(self, value):
        # The index on the end is because unpack always returns a tuple
        try:
            return struct.unpack(self.format_code, value)[0]
        except struct.error as e:
            raise ValueError(*e.args)

Raise ValueError instead of struct.error
Raise ValueError instead of struct.error

The protocol for field encoding and decoding is to raise ValueError, so this is a necessary translation.

  import struct
  from steel.fields import Field
  
  __all__ = ['Integer']
  
  
  class Integer(Field):
      "An integer represented as a sequence and bytes"
  
      # These map a number of bytes to a struct format code
      size_formats = {
          1: 'B',  # char
          2: 'H',  # short
          4: 'L',  # long
          8: 'Q',  # long long
      }
  
      def __init__(self, *args, endianness='<', **kwargs):
          super(Integer, self).__init__(*args, **kwargs)
          self.format_code = endianness + self.size_formats[self.size]
  
      def encode(self, value):
+         try:
-         return struct.pack(self.format_code, value)
+             return struct.pack(self.format_code, value)
+         except struct.error as e:
+             raise ValueError(*e.args)
  
      def decode(self, value):
          # The index on the end is because unpack always returns a tuple
+         try:
-         return struct.unpack(self.format_code, value)[0]
+             return struct.unpack(self.format_code, value)[0]
+         except struct.error as e:
+             raise ValueError(*e.args)
  
Raise ValueError instead of struct.error
## Code Before:
import struct
from steel.fields import Field

__all__ = ['Integer']


class Integer(Field):
    "An integer represented as a sequence and bytes"

    # These map a number of bytes to a struct format code
    size_formats = {
        1: 'B',  # char
        2: 'H',  # short
        4: 'L',  # long
        8: 'Q',  # long long
    }

    def __init__(self, *args, endianness='<', **kwargs):
        super(Integer, self).__init__(*args, **kwargs)
        self.format_code = endianness + self.size_formats[self.size]

    def encode(self, value):
        return struct.pack(self.format_code, value)

    def decode(self, value):
        # The index on the end is because unpack always returns a tuple
        return struct.unpack(self.format_code, value)[0]

## Instruction:
Raise ValueError instead of struct.error
## Code After:
import struct
from steel.fields import Field

__all__ = ['Integer']


class Integer(Field):
    "An integer represented as a sequence and bytes"

    # These map a number of bytes to a struct format code
    size_formats = {
        1: 'B',  # char
        2: 'H',  # short
        4: 'L',  # long
        8: 'Q',  # long long
    }

    def __init__(self, *args, endianness='<', **kwargs):
        super(Integer, self).__init__(*args, **kwargs)
        self.format_code = endianness + self.size_formats[self.size]

    def encode(self, value):
        try:
            return struct.pack(self.format_code, value)
        except struct.error as e:
            raise ValueError(*e.args)

    def decode(self, value):
        # The index on the end is because unpack always returns a tuple
        try:
            return struct.unpack(self.format_code, value)[0]
        except struct.error as e:
            raise ValueError(*e.args)
