parameters: (self, geounits, combine)
return_statement: return changed
docstring: Update the stats for this district incrementally. This method
          iterates over all the computed characteristics and adds or removes
          the characteristic values for the specific geounits only.
  
          Parameters:
              geounits -- The Geounits to add or remove to this districts
                  ComputedCharacteristic value.
              combine -- The aggregate value computed should be added or
                  removed from the ComputedCharacteristicValue
  
          Returns:
              True if the stats for this district have changed.
docstring_summary: Update the stats for this district incrementally. This method
          iterates over all the computed characteristics and adds or removes
          the characteristic values for the specific geounits only.
docstring_tokens: Update the stats for this district incrementally . This method iterates over all the computed characteristics and adds or removes the characteristic values for the specific geounits only .
function: def delta_stats(self, geounits, combine):
          """
          Update the stats for this district incrementally. This method
          iterates over all the computed characteristics and adds or removes
          the characteristic values for the specific geounits only.
  
          Parameters:
              geounits -- The Geounits to add or remove to this districts
                  ComputedCharacteristic value.
              combine -- The aggregate value computed should be added or
                  removed from the ComputedCharacteristicValue
  
          Returns:
              True if the stats for this district have changed.
          """
          # Get the subjects that don't rely on others first - that will save us
          # from computing characteristics for denominators twice
          all_subjects = Subject.objects.order_by(
              '-percentage_denominator').all()
          changed = False
  
          # For all subjects
          for subject in all_subjects:
              # Aggregate all Geounits Characteristic values
              aggregate = Characteristic.objects.filter(
                  geounit__in=geounits, subject__exact=subject).aggregate(
                      Sum('number'))['number__sum']
              # If there are aggregate values for the subject and geounits.
              if not aggregate is None:
                  # Get the pre-computed values
                  defaults = {'number': Decimal('0000.00000000')}
                  computed, created = ComputedCharacteristic.objects.get_or_create(
                      subject=subject, district=self, defaults=defaults)
  
                  if combine:
                      # Add the aggregate to the computed value
                      computed.number += aggregate
                  else:
                      # Subtract the aggregate from the computed value
                      computed.number -= aggregate
  
                  # If this subject is viewable as a percentage, do the math
                  # using the already calculated value for the denominator
                  if subject.percentage_denominator:
                      denominator = ComputedCharacteristic.objects.get(
                          subject=subject.percentage_denominator, district=self)
                      if denominator:
                          if denominator.number > 0:
                              computed.percentage = computed.number / denominator.number
                          else:
                              computed.percentage = '0000.00000000'
  
                  # If there are aggregate values for the subject & geounits.
                  computed.save()
  
                  changed = True
  
          return changed
function_tokens: def delta_stats ( self , geounits , combine ) : # Get the subjects that don't rely on others first - that will save us # from computing characteristics for denominators twice all_subjects = Subject . objects . order_by ( '-percentage_denominator' ) . all ( ) changed = False # For all subjects for subject in all_subjects : # Aggregate all Geounits Characteristic values aggregate = Characteristic . objects . filter ( geounit__in = geounits , subject__exact = subject ) . aggregate ( Sum ( 'number' ) ) [ 'number__sum' ] # If there are aggregate values for the subject and geounits. if not aggregate is None : # Get the pre-computed values defaults = { 'number' : Decimal ( '0000.00000000' ) } computed , created = ComputedCharacteristic . objects . get_or_create ( subject = subject , district = self , defaults = defaults ) if combine : # Add the aggregate to the computed value computed . number += aggregate else : # Subtract the aggregate from the computed value computed . number -= aggregate # If this subject is viewable as a percentage, do the math # using the already calculated value for the denominator if subject . percentage_denominator : denominator = ComputedCharacteristic . objects . get ( subject = subject . percentage_denominator , district = self ) if denominator : if denominator . number > 0 : computed . percentage = computed . number / denominator . number else : computed . percentage = '0000.00000000' # If there are aggregate values for the subject & geounits. computed . save ( ) changed = True return changed