<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def layer(self, queryset, stylename=None):
 """Returns a map Layer. Arguments: queryset -- QuerySet for Layer Keyword args: stylename -- str name of style to apply """

        cls = RasterLayer if hasattr(queryset, 'image') else VectorLayer
        layer = cls(queryset, style=stylename)
        try:
            style = self.map.find_style(layer.stylename)
        except KeyError:
            self.map.append_style(layer.stylename, layer.style())
        layer.styles.append(layer.stylename)
        self.map.layers.append(layer._layer)
        return layer