<commit_before>from django.db.models import Sum, Count
from django.db.models.query import QuerySet
from django.utils import timezone

from .models import BankTransaction, RemoteDocdataPayment, RemoteDocdataPayout, BankTransactionCategory
from .enum import BANK_ACCOUNTS

from decimal import Decimal


def add_up(first, second):
    """
    Addition of two objects.

    Distinguish addition of dictionaries and numbers.

    return None when both are None
    if one of the two is None, return the other one which is not None

    if both are dictionaries, add them up with the custom mydict.__add__

    if both are convertable to a Decimal, return the addition of the numbers

    in other cases, return 'first' if it is equal to 'second' without caring about the type
    and when they are not equal, return None because we dont know how to handle that
    """
    if not first:
        return second
    elif not second:
        return first
    else:
        if isinstance(first, mydict) and isinstance(second, mydict):
            return first + second
        elif isinstance(first, mylist) and isinstance(second, mylist):
            return first.add(second)
        elif isinstance(first, QuerySet) and isinstance(second, QuerySet):
            return None # first | second
        else:
            try:
                result = Decimal(first) + Decimal(second)
                return result
            except:
                if first == second:  # if both are some Class like a DjangoModel
                    return first
                else:
                    # both are NOT None, they are different and are not a dict and not numeric
                    print 'Could not handle addition of {} and {}'.format(type(first), type(second))
                    return None


class mydict(dict):

    def __add__(self, dict2):
        if self == mydict():
            return dict2

        keys = self.keys()
        assert set(keys) == set(dict2.keys())
<commit_msg>when adding numbers convert to string then to Decimal<commit_after>from django.db.models import Sum, Count
from django.db.models.query import QuerySet
from django.utils import timezone

from .models import BankTransaction, RemoteDocdataPayment, RemoteDocdataPayout, BankTransactionCategory
from .enum import BANK_ACCOUNTS

from decimal import Decimal


def add_up(first, second):
    """
    Addition of two objects.

    Distinguish addition of dictionaries, list, querysets and numbers.

    return None when both are None
    if one of the two is None, return the other one which is not None

    if both are dictionaries, add them up with the custom mydict.__add__

    if both are convertable to a Decimal, return the addition of the numbers

    in other cases, return 'first' if it is equal to 'second' without caring about the type
    and when they are not equal, return None because we don't know how to handle that
    """
    if not first:
        return second
    elif not second:
        return first
    else:
        if isinstance(first, mydict) and isinstance(second, mydict):
            return first + second
        elif isinstance(first, mylist) and isinstance(second, mylist):
            return first.add(second)
        elif isinstance(first, QuerySet) and isinstance(second, QuerySet):
            return None # first | second
        else:
            try:
                result = Decimal(str(first)) + Decimal(str(second))
                return result
            except:
                if first == second:  # if both are some Class like a DjangoModel
                    return first
                else:
                    # both are NOT None, they are different and are not a dict and not numeric
                    print 'Could not handle addition of {} and {}'.format(type(first), type(second))
                    return None


class mydict(dict):

    def __add__(self, dict2):
        if self == mydict():
            return dict2

        keys = self.keys()
        assert set(keys) == set(dict2.keys())

{'fraction_non_alphanumeric': 0.062344759066070544, 'fraction_numerical': 0.0014903129657228018, 'mean_word_length': 2.2927228127555193, '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': '5976775', 'n_tokens_mistral': 1027, 'n_tokens_neox': 978, 'n_words': 448}