def iter(self): bt = BTree() for j in range(100): bt.add(j) assert list(bt) == list(bt.iterkeys()) assert list(bt.iteritems()) == list(zip(bt, bt.itervalues())) assert list(bt.iterkeys()) == list(bt.keys()) assert list(bt.itervalues()) == list(bt.values()) assert list(bt.iteritems()) == list(bt.items())
def test_iter(self): bt = BTree() for j in range(100): bt.add(j) if hasattr(bt, 'iterkeys'): assert list(bt) == list(bt.iterkeys()) assert list(bt.iteritems()) == list(zip(bt, bt.itervalues())) assert list(bt.iterkeys()) == list(bt.keys()) assert list(bt.itervalues()) == list(bt.values()) assert list(bt.iteritems()) == list(bt.items()) else: assert list(bt) == list(bt.keys()) assert list(bt.items()) == list(zip(bt, bt.values()))
class Document(Persistent): @property def handler(self): return _handler_index[id(self)] def __init__(self): self.notes = BTree() self.subscribers = PersistentList() self.notes[0] = Note(self, 0, {'desc': 'ROOT'}) def create_note(self, props={}, cls=Note, parent_id=0, id=None): if id is None: if self.notes: id = max(self.list_note_ids()) + 1 else: id = 0 if id in self.notes: raise ValueError('Note id "%s" already in use' % id) note = cls(self, id, props) self.notes[id] = note self.notes[parent_id]._children.append(note.id) return note def get_note(self, note_id): return self.notes[note_id] def list_note_ids(self): return self.notes.iterkeys() def list_notes(self): return self.notes.itervalues() def del_note(self, note_id): for child_note_id in self.get_note(note_id)._children: self.del_note(child_note_id) self._cleanup_child_links(note_id) del self.notes[note_id] def subscribe(self, subscriber): self.subscribers.append(subscriber) def _cleanup_child_links(self, note_id): for note in self.list_notes(): if note_id in note._children: note._children.remove(note_id) def dump_notes(self): def generate(): for note in self.list_notes(): note_data = { 'props': dict(note._props), 'children': list(note._children), } yield note.id, note_data return json.dumps(dict(generate())) # TODO: deprecate these methods def abort(self): self.handler.abort() def commit(self): self.handler.commit() def close(self): self.handler.close()