<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def to_dict(self, remove_nones=False):
 """Return a dict representation of the `DidlResource`. Args: remove_nones (bool, optional):
 Optionally remove dictionary elements when their value is `None`. Returns: dict: a dict representing the `DidlResource` """

        content = {
            'uri': self.uri,
            'protocol_info': self.protocol_info,
            'import_uri': self.import_uri,
            'size': self.size,
            'duration': self.duration,
            'bitrate': self.bitrate,
            'sample_frequency': self.sample_frequency,
            'bits_per_sample': self.bits_per_sample,
            'nr_audio_channels': self.nr_audio_channels,
            'resolution': self.resolution,
            'color_depth': self.color_depth,
            'protection': self.protection,
        }
        if remove_nones:
            # delete any elements that have a value of None to optimize size
            # of the returned structure
            nones = [k for k in content if content[k] is None]
            for k in nones:
                del content[k]
        return content