'Design pattern for transposing array slices/shapes

I'm working on a object oriented image processing module. Depending on whether my regions are vertical or horizontal I have to slice and pad them differently. I came up with Vertical and Horizontal classes that implement set_slice_to_value and get_shape methods to avoid if/elses:

class Vertical:

  template_zeros = "y"
  mask_pad = "x"
  axis = 0
  dim = 1

  def set_slice_to_value(array, start, end, value, axis):
    if axis == "y":
      array[start:end,:] = value
    else:
      array[:,start:end] = value
    return array

  def get_shape(dims):
    return (max(dims), min(dims))
self._orientation = "vertical" if image.shape[0] > image.shape[1] else "horizontal" # old
self._orientation = Vertical() if image.shape[0] > image.shape[1] else Horizontal() # new
if self._orientation == "vertical":
      mask[:,pad:-pad] = rolled
else: 
      mask[pad:-pad,:] = rolled # old 

mask = self._orientation.set_slice_to_value(mask, pad, -pad, rolled, self._orientation.mask_pad) # new
template_shape = [self._long_edge, self._frame_short]
template = np.ones(tuple(template_shape)) if self._orientation == "vertical" 
                                          else np.ones(tuple(template_shape.reverse())) # old

template = np.ones(self._orientation.get_shape([self._long_edge, self._frame_short])) # new

This still sounds a bit verbose though. Is there a cleaner way to do this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source