<commit_before>from django.db import models
from django.db.models.query import QuerySet
from djangofeeds.utils import truncate_field_data
import sys

DEFAULT_POST_LIMIT = 5


def update_with_dict(obj, fields):
    set_value = lambda (name, val): setattr(obj, name, val)
    map(set_value, fields.items())
    obj.save()
    return obj


class ExtendedQuerySet(QuerySet):

    def update_or_create(self, **kwargs):
        try:
            obj, created = self.get_or_create(**kwargs)
        except self.model.MultipleObjectsReturned:
            fields = dict(kwargs.pop("default", {}))
            fields.update(kwargs)
            sys.stderr.write("djfeedsMultipleObjectsReturned: %s" % (
                str(kwargs)))
            self.filter(**fields).delete()
            obj, created = self.get_or_create(**kwargs)

        if not created:
            fields = dict(kwargs.pop("defaults", {}))
            fields.update(kwargs)
            update_with_dict(obj, fields)

        return obj


class ExtendedManager(models.Manager):

    def get_query_set(self):
        return ExtendedQuerySet(self.model)

    def update_or_create(self, **kwargs):
        return self.get_query_set().update_or_create(**kwargs)


FeedManager = ExtendedManager
CategoryManager = ExtendedManager
EnclosureManager = ExtendedManager


class PostManager(ExtendedManager):
    """Manager class for Posts"""

    def all_by_order(self, limit=DEFAULT_POST_LIMIT):
        ordering = self.model._meta.ordering<commit_msg>Fix typo default -> defaults and copy kwargs<commit_after>from django.db import models
from django.db.models.query import QuerySet
from djangofeeds.utils import truncate_field_data
import sys

DEFAULT_POST_LIMIT = 5


def update_with_dict(obj, fields):
    set_value = lambda (name, val): setattr(obj, name, val)
    map(set_value, fields.items())
    obj.save()
    return obj


class ExtendedQuerySet(QuerySet):

    def update_or_create(self, **kwargs):
        try:
            obj, created = self.get_or_create(**kwargs)
        except self.model.MultipleObjectsReturned:
            # Make copy of kwargs so the pop below will work.
            kwargs_copy = dict(kwargs)
            defaults = kwargs_copy.pop("defaults", {}))
            fields.update(kwargs_copy)
            sys.stderr.write("djfeedsMultipleObjectsReturned: %s" % (
                str(kwargs)))
            self.filter(**fields).delete()
            obj, created = self.get_or_create(**kwargs_copy)

        if not created:
            fields = dict(kwargs.pop("defaults", {}))
            fields.update(kwargs)
            update_with_dict(obj, fields)

        return obj


class ExtendedManager(models.Manager):

    def get_query_set(self):
        return ExtendedQuerySet(self.model)

    def update_or_create(self, **kwargs):
        return self.get_query_set().update_or_create(**kwargs)


FeedManager = ExtendedManager
CategoryManager = ExtendedManager
EnclosureManager = ExtendedManager


class PostManager(ExtendedManager):
    """Manager class for Posts"""

    def all_by_order(self, limit=DEFAULT_POST_LIMIT):
        ordering = self.model._meta.ordering
{'fraction_non_alphanumeric': 0.10111287758346582, 'fraction_numerical': 0.0006359300476947536, 'mean_word_length': 3.0751295336787563, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '3064326', 'n_tokens_mistral': 927, 'n_tokens_neox': 873, 'n_words': 197}