<commit_before>__author__ = 'calvin'

from copy import deepcopy

from eventdispatcher import Property
from weakref import ref


class WeakRefProperty(Property):
    """
    Property that stores it's values as weak references in order to facilitate garbage collection.
    """

    def __init__(self, default_value, **additionals):
        self.instances = {}
        try:
            self.default_value = ref(default_value)
        except TypeError:
            self.default_value = None
        try:
            self.value = ref(deepcopy(default_value))
        except TypeError:
            self.value = None
        self._additionals = additionals

    def __get__(self, obj, objtype=None):
        value = obj.event_dispatcher_properties[self.name]['value']
        if value:
            return value()
        else:
            return value

    def __set__(self, obj, value):
        super(WeakRefProperty, self).__set__(obj, ref(value))

    def register(self, instance, property_name, default_value):
        wr = None if default_value is None else ref(default_value)
        super(WeakRefProperty, self).register(instance, property_name, wr)<commit_msg>fixed issue in WeakRefProperty setter<commit_after>__author__ = 'calvin'

from copy import deepcopy

from eventdispatcher import Property
from weakref import ref


class WeakRefProperty(Property):
    """
    Property that stores it's values as weak references in order to facilitate garbage collection.
    """

    def __init__(self, default_value, **additionals):
        self.instances = {}
        try:
            self.default_value = ref(default_value)
        except TypeError:
            self.default_value = None
        try:
            self.value = ref(deepcopy(default_value))
        except TypeError:
            self.value = None
        self._additionals = additionals

    def __get__(self, obj, objtype=None):
        value = obj.event_dispatcher_properties[self.name]['value']
        if value:
            return value()
        else:
            return value

    def __set__(self, obj, value):
        wr = ref(value) if value is not None else None
        if wr != obj.event_dispatcher_properties[self.name]['value']:
            prop = obj.event_dispatcher_properties[self.name]
            prop['value'] = wr
            for callback in prop['callbacks']:
                if callback(obj, value):
                    break

    def register(self, instance, property_name, default_value):
        wr = None if default_value is None else ref(default_value)
        super(WeakRefProperty, self).register(instance, property_name, wr)
{'fraction_non_alphanumeric': 0.08403361344537816, 'fraction_numerical': 0.0, 'mean_word_length': 2.534412955465587, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2867850', 'n_tokens_mistral': 709, 'n_tokens_neox': 683, 'n_words': 200}