<commit_before># attributes.py - manages object attributes
# Copyright (C) 2005, 2006, 2007 <NAME> <EMAIL>
#
# This module is part of SQLAlchemy and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php

import weakref, threading
import UserDict
from sqlalchemy import util
from sqlalchemy.orm import interfaces, collections
from sqlalchemy.orm.mapper import class_mapper, identity_equal
from sqlalchemy import exceptions


PASSIVE_NORESULT = object()
ATTR_WAS_SET = object()
NO_VALUE = object()

class InstrumentedAttribute(interfaces.PropComparator):
    """public-facing instrumented attribute."""
    
    def __init__(self, impl, comparator=None):
        """Construct an InstrumentedAttribute.
        comparator
          a sql.Comparator to which class-level compare/math events will be sent
        """
        
        self.impl = impl
        self.comparator = comparator

    def __set__(self, obj, value):
        self.impl.set(obj._state, value, None)

    def __delete__(self, obj):
        self.impl.delete(obj._state)

    def __get__(self, obj, owner):
        if obj is None:
            return self
        return self.impl.get(obj._state)

    def get_history(self, obj, **kwargs):
        return self.impl.get_history(obj._state, **kwargs)
        
    def clause_element(self):
        return self.comparator.clause_element()

    def expression_element(self):
        return self.comparator.expression_element()

    def operate(self, op, *other, **kwargs):
        return op(self.comparator, *other, **kwargs)

    def reverse_operate(self, op, other, **kwargs):
        return op(other, self.comparator, **kwargs)

    def hasparent(self, instance, optimistic=False):
        return self.impl.hasparent(instance._state, optimistic=optimistic)

    property = property(lambda s: class_mapper(s.impl.class_).get_property(s.impl.key),
                        doc="the MapperProperty object associated with this attribute")
        
class AttributeImpl(object):
    """internal implementation for instrumented attributes."""
    
    def __init__(self, class_, manager, key, callable_, trackparent=False, extension=None, compare_function=None, mutable_scalars=False, **kwargs):
        """Construct an AttributeImpl.
        
            class_
              the class to be instrumented.
                
            manager
              AttributeManager managing this class
              
            key
              string name of the attribute
              
            callable_
              optional function which generates a callable based on a parent 
              instance, which produces the "default" values for a scalar or 
              collection attribute when it's first accessed, if not present already.
              
            trackparent
              if True, attempt to track if an instance has a parent attached to it 
              via this attribute
              
            extension
              an AttributeExtension object which will receive 
              set/delete/append/remove/etc. events 
              
            compare_function
              a function that compares two values which are normally assignable to this 
              attribute
              
            mutable_scalars
              if True, the values which are normally assignable to this attribute can mutate, 
              and need to be compared against a copy of their original contents in order to 
              detect changes on the parent instance
              
              
        """
        
        self.class_ = class_
        self.manager = manager
        self.key = key
        self.callable_ = callable_
        self.trackparent = trackparent
        self.mutable_scalars = mutable_scalars
        if mutable_scalars:
            class_._sa_has_mutable_scalars = True
        self.copy = None
        if compare_function is None:
            self.is_equal = lambda x,y: x == y
        else:
            self.is_equal = compare_function
        self.extensions = util.to_list(extension or [])
        
    def commit_to_state(self, state, value=NO_VALUE):
        """commit the object's current state to its 'committed' state."""
        
        if value is NO_VALUE:
            if self.key in state.dict:
                value = state.dict[self.key]
        if value is not NO_VALUE:
            state.committed_state[self.key] = self.copy(value)

    def hasparent(self, state, optimistic=False):
        """Return the boolean value of a `hasparent` flag attached to the given item.

        The `optimistic` flag determines what the default return value
        should be if no `hasparent` flag can be located.

        As this function is used to determine if an instance is an
        *orphan*, instances that were loaded from storage should be
        assumed to not be orphans, until a True/False value for this
        flag is set.

        An instance attribute that is loaded by a callable function
        will also not have a `hasparent` flag.
        """
<commit_msg>- Swap operator.eq for lambda x,y: x==y - Some docstring re-formatting<commit_after># attributes.py - manages object attributes
# Copyright (C) 2005, 2006, 2007 <NAME> <EMAIL>
#
# This module is part of SQLAlchemy and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php

import operator, weakref, threading
import UserDict
from sqlalchemy import util
from sqlalchemy.orm import interfaces, collections
from sqlalchemy.orm.mapper import class_mapper, identity_equal
from sqlalchemy import exceptions


PASSIVE_NORESULT = object()
ATTR_WAS_SET = object()
NO_VALUE = object()

class InstrumentedAttribute(interfaces.PropComparator):
    """public-facing instrumented attribute."""
    
    def __init__(self, impl, comparator=None):
        """Construct an InstrumentedAttribute.
        comparator
          a sql.Comparator to which class-level compare/math events will be sent
        """
        
        self.impl = impl
        self.comparator = comparator

    def __set__(self, obj, value):
        self.impl.set(obj._state, value, None)

    def __delete__(self, obj):
        self.impl.delete(obj._state)

    def __get__(self, obj, owner):
        if obj is None:
            return self
        return self.impl.get(obj._state)

    def get_history(self, obj, **kwargs):
        return self.impl.get_history(obj._state, **kwargs)
        
    def clause_element(self):
        return self.comparator.clause_element()

    def expression_element(self):
        return self.comparator.expression_element()

    def operate(self, op, *other, **kwargs):
        return op(self.comparator, *other, **kwargs)

    def reverse_operate(self, op, other, **kwargs):
        return op(other, self.comparator, **kwargs)

    def hasparent(self, instance, optimistic=False):
        return self.impl.hasparent(instance._state, optimistic=optimistic)

    property = property(lambda s: class_mapper(s.impl.class_).get_property(s.impl.key),
                        doc="the MapperProperty object associated with this attribute")


class AttributeImpl(object):
    """internal implementation for instrumented attributes."""

    def __init__(self, class_, manager, key, callable_, trackparent=False, extension=None, compare_function=None, mutable_scalars=False, **kwargs):
        """Construct an AttributeImpl.

        class_
          the class to be instrumented.

        manager
          AttributeManager managing this class

        key
          string name of the attribute

        callable_
          optional function which generates a callable based on a parent
          instance, which produces the "default" values for a scalar or
          collection attribute when it's first accessed, if not present
          already.

        trackparent
          if True, attempt to track if an instance has a parent attached
          to it via this attribute.

        extension
          an AttributeExtension object which will receive
          set/delete/append/remove/etc. events.

        compare_function
          a function that compares two values which are normally
          assignable to this attribute.

        mutable_scalars
          if True, the values which are normally assignable to this
          attribute can mutate, and need to be compared against a copy of
          their original contents in order to detect changes on the parent
          instance.
        """

        self.class_ = class_
        self.manager = manager
        self.key = key
        self.callable_ = callable_
        self.trackparent = trackparent
        self.mutable_scalars = mutable_scalars
        if mutable_scalars:
            class_._sa_has_mutable_scalars = True
        self.copy = None
        if compare_function is None:
            self.is_equal = operator.eq
        else:
            self.is_equal = compare_function
        self.extensions = util.to_list(extension or [])

    def commit_to_state(self, state, value=NO_VALUE):
        """Commits the object's current state to its 'committed' state."""

        if value is NO_VALUE:
            if self.key in state.dict:
                value = state.dict[self.key]
        if value is not NO_VALUE:
            state.committed_state[self.key] = self.copy(value)

    def hasparent(self, state, optimistic=False):
        """Return the boolean value of a `hasparent` flag attached to the given item.

        The `optimistic` flag determines what the default return value
        should be if no `hasparent` flag can be located.

        As this function is used to determine if an instance is an
        *orphan*, instances that were loaded from storage should be
        assumed to not be orphans, until a True/False value for this
        flag is set.

        An instance attribute that is loaded by a callable function
        will also not have a `hasparent` flag.
        """

{'fraction_non_alphanumeric': 0.0702247191011236, 'fraction_numerical': 0.002407704654895666, 'mean_word_length': 2.3252168112074716, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6591049', 'n_tokens_mistral': 2599, 'n_tokens_neox': 2514, 'n_words': 909}