1 3 package org.python.modules; 4 5 import org.python.core.*; 6 7 public class xreadlines { 8 private final static int CHUNKSIZE = 8192; 9 10 public static PyString __doc__xreadlines = new PyString( 11 "xreadlines(f)\n" + 12 "\n" + 13 "Return an xreadlines object for the file f." 14 ); 15 16 public static PyObject xreadlines$(PyObject file) { 17 return new XReadlineObj(file); 18 } 19 20 public static class XReadlineObj extends PyObject { 21 private PyObject file; 22 private PyObject lines = null; 23 private int lineslen = 0; 24 private int lineno = 0; 25 private int abslineno = 0; 26 27 public XReadlineObj(PyObject file) { 28 this.file = file; 29 } 30 31 public PyObject __iter__() { 32 return new PySequenceIter(this); 33 } 34 35 public PyObject __finditem__(PyObject idx) { 36 return __finditem__(((PyInteger)idx.__int__()).getValue()); 37 } 38 39 public PyObject __finditem__(int idx) { 40 if (idx != abslineno) { 41 throw Py.RuntimeError( 42 "xreadlines object accessed out of order"); 43 } 44 45 if (lineno >= lineslen) { 46 lines = file.invoke("readlines", Py.newInteger(CHUNKSIZE)); 47 lineno = 0; 48 lineslen = lines.__len__(); 49 } 50 abslineno++; 51 return lines.__finditem__(lineno++); 52 } 53 54 public String toString() { 55 return "<xreadlines object " + Py.idstr(this) + ">"; 56 } 57 58 } 59 } 60 | Popular Tags |