Changeset 749
- Timestamp:
- 08/24/05 10:14:54 (3 years ago)
- Files:
-
- nbdoc/trunk/notabene/notebook.py (modified) (3 diffs)
- nbdoc/trunk/notabene/testing/test_notebook.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
nbdoc/trunk/notabene/notebook.py
r748 r749 95 95 number=str(self.number)) 96 96 97 def slicerange(s): 98 #from http://inamidst.com/code/listdict.py for Log.__getitem 99 if (not isinstance(s, slice)) or (s.start == s.stop): 100 raise ValueError("Expected non-zero sized slice object") 101 return xrange(s.start, s.stop, s.step or 1) 102 97 103 class Log(object): 98 104 #one on the nbshell side there's IPythonLog … … 103 109 self.element = ET.SubElement(parent.root, 'ipython-log', id=logid) 104 110 self._cells = [] #it is not safe to manipulate this from outside 111 #'cause of removals, this may end up sparse - might make sense to use 112 #something like http://inamidst.com/code/listdict.py 105 113 106 114 def add(self, number): … … 123 131 raise RuntimeError, "unknown error when adding cell with number %d to log %s" % (number, self.id) 124 132 125 def __getitem__(self, number): 126 return self._cells[number] 127 #should None i.e. removed cells be handled specially already here? 133 def __getitem__(self, position): 134 if isinstance(position, slice): 135 if position.start != position.stop: 136 """Filters out None cells""" 137 return [self._cells[i] for i in slicerange(position) 138 if self._cells[i] is not None] 139 #retrieves _cells[i] twice but probably still pretty fast 140 #if this is bad or slow can be also removed and let users filter 141 return self._cells[position] 128 142 129 143 def __len__(self): 130 return len(self._cells) 144 return len(self._cells) #includes Nones i.e. is not the actual amount of cells. Notebook.add_cell uses this currently to check if addition is to the end of the list, so this can't be just changed to filter Nones out 131 145 132 146 def __iter__(self): nbdoc/trunk/notabene/testing/test_notebook.py
r746 r749 297 297 assert nb.get_last_cell() is not None 298 298 299 300 301 302 303 304 299 """ 300 16:16 < tzanko> I'm going to add additional rerunning fuctionality today and i 301 need a fast way to get all the cells with a number larger than 302 a given number. Probably the best way would be to implement 303 list-like functionality for the log where log[5:10] would give 304 me all the cells with 5<=cell.number<10 305 """ 306 for _ in range(10): 307 nb.add_cell() 308 log[3:8] #sanity of the tampered slicing not really checked (yet) 309 310 #filtering out Nones 311 nb.remove_cell(5) 312 for cell in log[3:8]: 313 assert cell is not None 314 315 316 317 318 319 320 321
