<s> from rdflib . namespace import RDF <EOL> from rdflib . term import BNode <EOL> from rdflib . term import Literal <EOL> from rdflib . graph import Graph <EOL> from rdflib . py3compat import format_doctest_out <EOL> __all__ = [ '<STR_LIT>' ] <EOL> class Collection ( object ) : <EOL> __doc__ = format_doctest_out ( """<STR_LIT>""" ) <EOL> def __init__ ( self , graph , uri , seq = [ ] ) : <EOL> self . graph = graph <EOL> self . uri = uri or BNode ( ) <EOL> for item in seq : <EOL> self . append ( item ) <EOL> def n3 ( self ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT>" % ( '<STR_LIT:U+0020>' . join ( [ i . n3 ( ) for i in self ] ) ) <EOL> def _get_container ( self , index ) : <EOL> """<STR_LIT>""" <EOL> assert isinstance ( index , int ) <EOL> graph = self . graph <EOL> container = self . uri <EOL> i = <NUM_LIT:0> <EOL> while i < index : <EOL> i += <NUM_LIT:1> <EOL> container = graph . value ( container , RDF . rest ) <EOL> if container is None : <EOL> break <EOL> return container <EOL> def __len__ ( self ) : <EOL> """<STR_LIT>""" <EOL> count = <NUM_LIT:0> <EOL> links = set ( ) <EOL> for item in self . graph . items ( self . uri ) : <EOL> assert item not in links , "<STR_LIT>" + "<STR_LIT>" % item <EOL> links . add ( item ) <EOL> count += <NUM_LIT:1> <EOL> return count <EOL> def index ( self , item ) : <EOL> """<STR_LIT>""" <EOL> listName = self . uri <EOL> index = <NUM_LIT:0> <EOL> while True : <EOL> if ( listName , RDF . first , item ) in self . graph : <EOL> return index <EOL> else : <EOL> newLink = list ( self . graph . objects ( listName , RDF . rest ) ) <EOL> index += <NUM_LIT:1> <EOL> if newLink == [ RDF . nil ] : <EOL> raise ValueError ( "<STR_LIT>" % ( item , self . uri ) ) <EOL> elif not newLink : <EOL> raise Exception ( "<STR_LIT>" % self . uri ) <EOL> else : <EOL> assert len ( newLink ) == <NUM_LIT:1> , "<STR_LIT>" % self . uri <EOL> listName = newLink [ <NUM_LIT:0> ] <EOL> def __getitem__ ( self , key ) : <EOL> """<STR_LIT>""" <EOL> c = self . _get_container ( key ) <EOL> if c : <EOL> v = self . graph . value ( c , RDF . first ) <EOL> if v : <EOL> return v <EOL> else : <EOL> raise KeyError ( key ) <EOL> else : <EOL> raise IndexError ( key ) <EOL> def __setitem__ ( self , key , value ) : <EOL> """<STR_LIT>""" <EOL> c = self . _get_container ( key ) <EOL> if c : <EOL> self . graph . set ( ( c , RDF . first , value ) ) <EOL> else : <EOL> raise IndexError ( key ) <EOL> def __delitem__ ( self , key ) : <EOL> """<STR_LIT>""" <EOL> self [ key ] <EOL> graph = self . graph <EOL> current = self . _get_container ( key ) <EOL> assert current <EOL> if len ( self ) == <NUM_LIT:1> and key > <NUM_LIT:0> : <EOL> pass <EOL> elif key == len ( self ) - <NUM_LIT:1> : <EOL> priorLink = self . _get_container ( key - <NUM_LIT:1> ) <EOL> self . graph . set ( ( priorLink , RDF . rest , RDF . nil ) ) <EOL> graph . remove ( ( current , None , None ) ) <EOL> else : <EOL> next = self . _get_container ( key + <NUM_LIT:1> ) <EOL> prior = self . _get_container ( key - <NUM_LIT:1> ) <EOL> assert next and prior <EOL> graph . remove ( ( current , None , None ) ) <EOL> graph . set ( ( prior , RDF . rest , next ) ) <EOL> def __iter__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . graph . items ( self . uri ) <EOL> def append ( self , item ) : <EOL> """<STR_LIT>""" <EOL> container = self . uri <EOL> graph = self . graph <EOL> rest = graph . value ( container , RDF . rest ) <EOL> while rest : <EOL> if rest == RDF . nil : <EOL> node = BNode ( ) <EOL> graph . set ( ( container , RDF . rest , node ) ) <EOL> container = node <EOL> break <EOL> else : <EOL> if container != self . uri : <EOL> rest = graph . value ( rest , RDF . rest ) <EOL> if not rest == RDF . nil : <EOL> container = rest <EOL> graph . add ( ( container , RDF . first , item ) ) <EOL> graph . add ( ( container , RDF . rest , RDF . nil ) ) <EOL> def clear ( self ) : <EOL> container = self . uri <EOL> graph = self . graph <EOL> while container : <EOL> rest = graph . value ( container , RDF . rest ) <EOL> graph . remove ( ( container , RDF . first , None ) ) <EOL> graph . remove ( ( container , RDF . rest , None ) ) <EOL> container = rest <EOL> def test ( ) : <EOL> import doctest <EOL> doctest . testmod ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> test ( ) <EOL> g = Graph ( ) <EOL> c = Collection ( g , BNode ( ) ) <EOL> assert len ( c ) == <NUM_LIT:0> <EOL> c = Collection ( <EOL> g , BNode ( ) , [ Literal ( "<STR_LIT:1>" ) , Literal ( "<STR_LIT:2>" ) , Literal ( "<STR_LIT:3>" ) , Literal ( "<STR_LIT:4>" ) ] ) <EOL> assert len ( c ) == <NUM_LIT:4> <EOL> assert c [ <NUM_LIT:1> ] == Literal ( "<STR_LIT:2>" ) , c [ <NUM_LIT:1> ] <EOL> del c [ <NUM_LIT:1> ] <EOL> assert list ( c ) == [ Literal ( "<STR_LIT:1>" ) , Literal ( "<STR_LIT:3>" ) , Literal ( "<STR_LIT:4>" ) ] , list ( c ) <EOL> try : <EOL> del c [ <NUM_LIT> ] <EOL> except IndexError , i : <EOL> pass <EOL> c . append ( Literal ( "<STR_LIT:5>" ) ) <EOL> print ( list ( c ) ) <EOL> for i in c : <EOL> print ( i ) <EOL> del c [ <NUM_LIT:3> ] <EOL> c . clear ( ) <EOL> assert len ( c ) == <NUM_LIT:0> </s>
