'Trim the state down to something that can be pickled.
Uses the fact that the instance variable _dynamic_attrs holds attrs that
will be wiped and restored on pickle serialization.'

def __getstate__(self):

    state_dict = copy.copy(self.__dict__)
    for dynamic_attr in self._dynamic_attrs:
        del state_dict[dynamic_attr]
    del state_dict['_dynamic_attrs']
    return state_dict
