Changeset 749

Show
Ignore:
Timestamp:
08/24/05 10:14:54 (3 years ago)
Author:
antont
Message:

Log getitem to filter out Nones. the of the tampered slicing not really checked (yet) but seems ok

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • nbdoc/trunk/notabene/notebook.py

    r748 r749  
    9595                                 number=str(self.number)) 
    9696 
     97def 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 
    97103class Log(object): 
    98104    #one on the nbshell side there's IPythonLog 
     
    103109        self.element = ET.SubElement(parent.root, 'ipython-log', id=logid) 
    104110        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 
    105113 
    106114    def add(self, number): 
     
    123131                    raise RuntimeError, "unknown error when adding cell with number %d to log %s" % (number, self.id) 
    124132 
    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] 
    128142 
    129143    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 
    131145 
    132146    def __iter__(self): 
  • nbdoc/trunk/notabene/testing/test_notebook.py

    r746 r749  
    297297    assert nb.get_last_cell() is not None 
    298298 
    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