[ create a new paste ] login | about

Link: http://codepad.org/eH7LSyWf    [ raw code | fork ]

Python, pasted on Sep 1:
class Measurement(models.Model):
    """
    Metrics have many targets, Targets have many Metrics"
    """
    
    sum = models.FloatField(default=0.0)
    sumsq = models.FloatField(default=0.0)
    count = models.IntegerField(default = 0)
    min = models.FloatField(default=0.0)
    max = models.FloatField(default=0.0)

    content_type   = models.ForeignKey(ContentType)
    object_pk      = models.IntegerField('object ID')
    content_object = generic.GenericForeignKey(ct_field="content_type", fk_field="object_pk")


    def sample(self, sample):
        self.sum += sample
        self.sumsq += sample * sample
        if self.count == 0:
            self.min = sample
            self.max = sample
        else:
            if self.min > sample: self.min = sample
            if self.max < sample: self.max = sample

        self.count += 1.0

    def get_mean(self):
        try:
            return self.sum / self.count
        except ZeroDivisionError:
            return 0.0
    
    def get_sd(self):
        try:
            # (sqrt( ((s).sumsq - ( (s).sum * (s).sum / (s).count)) / ((s).count-1) ))
            return sqrt( (self.sumsq - ( self.sum * self.sum / self.count )) / (self.count - 1) )
        except ZeroDivisionError:
            return 0.0

    mean = property(get_mean)
    sd = property(get_sd)


Create a new paste based on this one


Comments: