Ronny Pfannschmidt avatar Ronny Pfannschmidt committed ab350da

resuffle

Comments (0)

Files changed (1)

         if initializer is not missing:
             self.extend(initializer)
 
-    @property
-    def itemsize(self):
-        return ffi.sizeof(self.__data)/len(self.__data)
-
     def _realloc(self, new_size):
         assert new_size > self.__len
         new = alloc(self.typecode, new_size)
         self.__data = new
         self.__capacity = new_size
 
+    @property
+    def itemsize(self):
+        return ffi.sizeof(self.__data)/len(self.__data)
+
     def __len__(self):
         return self.__len
 
+    def __getitem__(self, index):
+        if index > self.__len:
+            raise IndexError(index)
+        return self.__data[index]
+
+    def __eq__(self, other):
+        if len(self) != len(other):
+            return False
+        return all(a == b for a, b in zip(self, other))
+
     def append(self, item):
         #XXX: checksize
         if self.__len == self.__capacity:
     def extend(self, items):
         if isinstance(items, array) and items.typecode!=self.typecode:
             raise TypeError
+        if isinstance(items, str):
+            raise TypeError
         #XXX: speedups
         for item in items:
             self.append(item)
 
+    def fromlist(self, items):
+        old = self.__len
+        try:
+            self.extend(items)
+        except:
+            self.__len = old
+            raise
 
+    def tolist(self):
+        return list(self)
+
+    def _tobuffer(self):
+        ffi.buffer(self.__data, self.itemsize * self.__len)
+
+    def tostring(self):
+        return str(self._tobuffer())
+
+    def fromstring(self, data):
+        #XXX: expensive
+        if len(data) % self.itemsize:
+            raise ValueError('string size not aligned with itemsize')
+        count = len(data)/self.itemsize -1 
+        char = ffi.new('char[]', data)
+        items = ffi.cast(typecodes[self.typecode].replace('[]', '*'), char)
+        for i in range(count):
+            self.append(items[i])
+
+    def fromfile(self, fp, nbytes=-1):
+        if not isinstance(fp, file):
+            raise TypeError('arg must be open file')
+        self.fromstring(fp.read(nbytes))
+
+
+
+    def tofile(self, fp):
+        if not isinstance(fp, file):
+            raise TypeError('arg must be open file')
+        fp.write(self._tobuffer())
 
     def __add__(self, other):
         new = array(self.typecode, self)
         for i in range(other):
             copy.extend(self)
 
-    def __iter__(self):
-        for i in range(self.__len):
-            yield self.__data[i]
 
-    def __eq__(self, other):
-        if len(self) != len(other):
-            return False
-        return all(a == b for a, b in zip(self, other))
-
-
-    def tostring(self):
-        return str(ffi.buffer(self.__data, self.itemsize*self.__len))
-
-    def fromstring(self, data):
-        #XXX: expensive
-        count = len(data)/self.itemsize
-        temp = alloc(self.typecode, count)
-        ffi.buffer(temp)[:] = data
-        for i in range(count):
-            self.append(temp[i])
-
-
-    def tolist(self):
-        return list(self)
 
     def __repr__(self):
         if self.__len:
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.