'Class template with methods already defined

I have two methods inside a class (mask_im and save_im) that I must include in several other classes in separate modules. How can I create a class with those two methods without copying and paste 10 lines of code, or at least reduce it to two lines of code? I want to have like a class template or something that I can reuse easily.

class MaskFromImages:
    def __init__(self, im_to_mask, m_rules, val_assign=None):
        im_mem = ReadMap(im_to_mask, mem=True)
        self.reference = im_mem.reference
        
        if im_mem.no_data is None:
            self.out_no_data = 255
        else:
            self.out_no_data = im_mem.no_data
        
        out_array = im_mem.array
        out_mem = im_mem.osgeodata
        
        for i in m_rules:
            out_array = MaskCls(out_mem, i[0], i[1], val_assign).array
            out_mem = save_map(out_array, "", im_mem.d_type, self.reference, format_out="MEM")
        out_array = out_array.astype("int16")
        self.array = out_array
    
    def mask_im(self, mask_aoi, mask_aoi_vl, replace_im=None, reverse=False):
        map_mem = save_map(self.array, "", gdal.GDT_UInt16, self.reference, format_out="MEM")
        im_masked = MaskAOI(map_mem, mask_aoi, mask_aoi_vl, self.out_no_data,
                            replace_im=replace_im, reverse=reverse)
        return im_masked
    
    def save_im(self, output, format_out="GTiff"):
        ds_out = save_map(self.array, output, gdal.GDT_UInt16, self.reference, out_no_data=self.out_no_data,
                          format_out=format_out)
        if format_out == 'MEM':
            return ds_out



Solution 1:[1]

This is the way I solved it using inheritance:

class Template:
 def __init__(self):
    self.array = None
    self.reference = None
    self.out_no_data = None
    self.data_type = None
    
 def mask(self, mask_aoi, mask_aoi_vl, replace_im=None, reverse=False):
    map_mem = save_map(self.array, "", self.data_type, self.reference, format_out="MEM")
    im_masked = MaskAOI(map_mem, mask_aoi, mask_aoi_vl, self.out_no_data,
                        replace_im=replace_im, reverse=reverse)
    return im_masked

 def save(self, output, format_out="GTiff"):
    ds_out = save_map(self.array, output, self.data_type, self.reference, out_no_data=self.out_no_data,
                      format_out=format_out)
    if format_out == 'MEM':
        return ds_out


class MergeClasses(Template):
 def __init__(self, input_data, m_classes, other_cls_val=None):
    class_map1 = ReadMap(input_data)
    self.reference = class_map1.reference
    class_map1_array = class_map1.array
    
    if class_map1.no_data is None:
        self.out_no_data = 255
    else:
        self.out_no_data = class_map1.no_data
    
    array_class = merge_cls(class_map1_array, m_classes, other_cls_val)
    
    class_map_f = array_class.astype("int16")
    self.array = class_map_f
    self.data_type = gdal.GDT_UInt16

Sources

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

Source: Stack Overflow

Solution Source
Solution 1