Commits

ZyX_I committed b1919c3

Added AccNum(list) support

Comments (0)

Files changed (1)

     return r
 
 class AccNum(float):
-    flt=float
     __slots__=("num", "acc")
 
     def __new__(cls, f, acc=None):
-        return super(cls, cls).__new__(cls, f)
+        try:
+            l=list(f)
+            return super(cls, cls).__new__(cls, sum(l)/len(l))
+        except TypeError:
+            return super(cls, cls).__new__(cls, f)
 
     def __init__(self, f, acc=None):
         if isinstance(f, self.__class__):
                 uselist=1
             except TypeError:
                 uselist=0
+            acc=float(acc) if acc is not None else 0.0
             if uselist:
-                raise NotImplementedError
+                n=len(l)
+                ax=sum(l)/n
+                self.num=float(ax)
+                self.acc=float(sqrt(acc+sum([(x-ax)**2 for x in l])/(n*(n-1))))
             else:
-                self.num=self.flt(f)
-                self.acc=self.flt(acc) if acc is not None else self.flt(0)
+                self.num=float(f)
+                self.acc=acc
                 if self.acc<0:
                     raise ValueError("Accuracy value cannot be negative")