<commit_before>import sys
from collections import MutableMapping
from types import MethodType

from django.db.models.fields import FieldDoesNotExist
from django.utils.text import capfirst
from django.db.models.options import get_verbose_name

from mongoengine.fields import ReferenceField

def patch_document(function, instance):
    setattr(instance, function.__name__, MethodType(function, instance))

def create_verbose_name(name):
    name = get_verbose_name(name)
    name.replace('_', ' ')
    return name

class PkWrapper(object):
    def __init__(self, wrapped):
        self.obj = wrapped
    
    def __getattr__(self, attr):
        if attr in dir(self.obj):
            return getattr(self.obj, attr)
        raise AttributeError
        
    def __setattr__(self, attr, value):
        if attr != 'obj' and hasattr(self.obj, attr):
            setattr(self.obj, attr, value)
        super(PkWrapper, self).__setattr__(attr, value)    
        
class DocumentMetaWrapper(MutableMapping):
    """
    Used to store mongoengine's _meta dict to make the document admin
    as compatible as possible to django's meta class on models. 
    """
    pk = None
    pk_name = None
    _app_label = None
    module_name = None
    _verbose_name = None
    has_auto_field = False
    object_name = None
    proxy = []
    parents = {}
    many_to_many = []
    _field_cache = None
    document = None
    _meta = None
    concrete_model = None
    
    def __init__(self, document):
        super(DocumentMetaWrapper, self).__init__()
        
        self.document = document
        # used by Django to distinguish between abstract and concrete models<commit_msg>Support unbound function in patch_document<commit_after>import sys
from collections import MutableMapping
from types import MethodType

from django.db.models.fields import FieldDoesNotExist
from django.utils.text import capfirst
from django.db.models.options import get_verbose_name

from mongoengine.fields import ReferenceField

def patch_document(function, instance, bound=True):
    if bound:
        method = MethodType(function, instance)
    else:
        method = function
    setattr(instance, function.__name__, method)

def create_verbose_name(name):
    name = get_verbose_name(name)
    name.replace('_', ' ')
    return name

class PkWrapper(object):
    def __init__(self, wrapped):
        self.obj = wrapped
    
    def __getattr__(self, attr):
        if attr in dir(self.obj):
            return getattr(self.obj, attr)
        raise AttributeError
        
    def __setattr__(self, attr, value):
        if attr != 'obj' and hasattr(self.obj, attr):
            setattr(self.obj, attr, value)
        super(PkWrapper, self).__setattr__(attr, value)    
        
class DocumentMetaWrapper(MutableMapping):
    """
    Used to store mongoengine's _meta dict to make the document admin
    as compatible as possible to django's meta class on models. 
    """
    pk = None
    pk_name = None
    _app_label = None
    module_name = None
    _verbose_name = None
    has_auto_field = False
    object_name = None
    proxy = []
    parents = {}
    many_to_many = []
    _field_cache = None
    document = None
    _meta = None
    concrete_model = None
    concrete_managers = []
    
    def __init__(self, document):
        super(DocumentMetaWrapper, self).__init__()
        
        self.document = document
        # used by Django to distinguish between abstract and concrete models
{'fraction_non_alphanumeric': 0.07743235463442717, 'fraction_numerical': 0.0, 'mean_word_length': 2.8870246085011186, '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': '2339729', 'n_tokens_mistral': 984, 'n_tokens_neox': 972, 'n_words': 292}