3
di                  @   s  d dl mZ d dl mZ d dl mZ d dlmZ d dlmZ d dlmZ dgZ	G dd de
Zd	d
 Zedkre  d dlmZ e Zeee Zeed ksteee ededededgZeedksted edksted ed= eeedededgks(teey
ed= W n$ ek
rV Z zW Y ddZ[X nX ejed eee xeD ]Zee qxW ed= ej  eed kstdS )    )absolute_import)division)print_function)RDF)BNode)Literal
Collectionc               @   s|   e Zd ZdZg fddZdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd ZdS )r   a  
    See "Emulating container types":
    https://docs.python.org/reference/datamodel.html#emulating-container-types

    >>> from rdflib.graph import Graph
    >>> from pprint import pprint
    >>> listName = BNode()
    >>> g = Graph('IOMemory')
    >>> listItem1 = BNode()
    >>> listItem2 = BNode()
    >>> g.add((listName, RDF.first, Literal(1)))
    >>> g.add((listName, RDF.rest, listItem1))
    >>> g.add((listItem1, RDF.first, Literal(2)))
    >>> g.add((listItem1, RDF.rest, listItem2))
    >>> g.add((listItem2, RDF.rest, RDF.nil))
    >>> g.add((listItem2, RDF.first, Literal(3)))
    >>> c = Collection(g,listName)
    >>> pprint([term.n3() for term in c])
    [u'"1"^^<http://www.w3.org/2001/XMLSchema#integer>',
     u'"2"^^<http://www.w3.org/2001/XMLSchema#integer>',
     u'"3"^^<http://www.w3.org/2001/XMLSchema#integer>']

    >>> Literal(1) in c
    True
    >>> len(c)
    3
    >>> c._get_container(1) == listItem1
    True
    >>> c.index(Literal(2)) == 1
    True
    c             C   s   || _ |pt | _| |7 } d S )N)graphr   uri)selfr	   r
   seq r   3/tmp/pip-build-7vycvbft/rdflib/rdflib/collection.py__init__.   s    zCollection.__init__c             C   s   ddj dd | D  S )a  
        >>> from rdflib.graph import Graph
        >>> listName = BNode()
        >>> g = Graph('IOMemory')
        >>> listItem1 = BNode()
        >>> listItem2 = BNode()
        >>> g.add((listName, RDF.first, Literal(1)))
        >>> g.add((listName, RDF.rest, listItem1))
        >>> g.add((listItem1, RDF.first, Literal(2)))
        >>> g.add((listItem1, RDF.rest, listItem2))
        >>> g.add((listItem2, RDF.rest, RDF.nil))
        >>> g.add((listItem2, RDF.first, Literal(3)))
        >>> c = Collection(g, listName)
        >>> print(c.n3()) #doctest: +NORMALIZE_WHITESPACE
        ( "1"^^<http://www.w3.org/2001/XMLSchema#integer>
          "2"^^<http://www.w3.org/2001/XMLSchema#integer>
          "3"^^<http://www.w3.org/2001/XMLSchema#integer> )
        z( %s ) c             S   s   g | ]}|j  qS r   )n3).0ir   r   r   
<listcomp>F   s    z!Collection.n3.<locals>.<listcomp>)join)r   r   r   r   r   3   s    zCollection.n3c             C   sP   t |tst| j}| j}d}x,||k rJ|d7 }|j|tj}|dkr P q W |S )z+Gets the first, rest holding node at index.r      N)
isinstanceintAssertionErrorr	   r
   valuer   rest)r   indexr	   	containerr   r   r   r   _get_containerH   s    
zCollection._get_containerc             C   s   t t| jj| jS )zlength of items in collection.)lenlistr	   itemsr
   )r   r   r   r   __len__U   s    zCollection.__len__c             C   s   | j }d}x|tj|f| jkr"|S t| jj|tj}|d7 }|tjgkr^td|| j f q|srt	d| j  qt
|dkstd| j  |d }qW dS )zM
        Returns the 0-based numerical index of the item in the list
        r   r   z%s is not in %szMalformed RDF Collection: %sN)r
   r   firstr	   r    objectsr   nil
ValueError	Exceptionr   r   )r   itemZlistNamer   ZnewLinkr   r   r   r   Y   s    zCollection.indexc             C   s<   | j |}|r0| jj|tj}|r&|S t|nt|dS )TODON)r   r	   r   r   r#   KeyError
IndexError)r   keycvr   r   r   __getitem__n   s    

zCollection.__getitem__c             C   s0   | j |}|r$| jj|tj|f nt|dS )r)   N)r   r	   setr   r#   r+   )r   r,   r   r-   r   r   r   __setitem__z   s    
zCollection.__setitem__c             C   s   | |  | j }| j|}|s tt| dkr6|dkr6n|t| d kr|| j|d }| j j|tjtjf |j|ddf nJ| j|d }| j|d }|r|st|j|ddf |j|tj|f dS )a  
        >>> from rdflib.namespace import RDF, RDFS
        >>> from rdflib import Graph
        >>> from pprint import pformat
        >>> g = Graph()
        >>> a = BNode('foo')
        >>> b = BNode('bar')
        >>> c = BNode('baz')
        >>> g.add((a, RDF.first, RDF.type))
        >>> g.add((a, RDF.rest, b))
        >>> g.add((b, RDF.first, RDFS.label))
        >>> g.add((b, RDF.rest, c))
        >>> g.add((c, RDF.first, RDFS.comment))
        >>> g.add((c, RDF.rest, RDF.nil))
        >>> len(g)
        6
        >>> def listAncestry(node, graph):
        ...   for i in graph.subjects(RDF.rest, node):
        ...     yield i
        >>> [str(node.n3())
        ...   for node in g.transitiveClosure(listAncestry, RDF.nil)]
        ['_:baz', '_:bar', '_:foo']
        >>> lst = Collection(g, a)
        >>> len(lst)
        3
        >>> b == lst._get_container(1)
        True
        >>> c == lst._get_container(2)
        True
        >>> del lst[1]
        >>> len(lst)
        2
        >>> len(g)
        4

        r   r   N)	r	   r   r   r   r0   r   r   r%   remove)r   r,   r	   currentZ	priorLinknextZpriorr   r   r   __delitem__   s    %
zCollection.__delitem__c             C   s   | j j| jS )z"Iterator over items in Collections)r	   r!   r
   )r   r   r   r   __iter__   s    zCollection.__iter__c             C   s:   | j }x.| jj|tj}|d ks*|tjkr.|S |}qW d S )N)r
   r	   r   r   r   r%   )r   r   r   r   r   r   _end   s    zCollection._endc             C   sf   | j  }|tjdf| jkr8t }| jj|tj|f |}| jj|tj|f | jj|tjtjf dS )a]  
        >>> from rdflib.graph import Graph
        >>> listName = BNode()
        >>> g = Graph()
        >>> c = Collection(g,listName,[Literal(1),Literal(2)])
        >>> links = [
        ...     list(g.subjects(object=i, predicate=RDF.first))[0] for i in c]
        >>> len([i for i in links if (i, RDF.rest, RDF.nil) in g])
        1

        N)	r7   r   r#   r	   r   r0   r   addr%   )r   r(   endnoder   r   r   append   s    zCollection.appendc             C   s   | j  }| jj|tjd f xP|D ]H}|tjd f| jkrVt }| jj|tj|f |}| jj|tj|f q"W | jj|tjtjf d S )N)	r7   r	   r2   r   r   r#   r   r8   r%   )r   otherr9   r(   Znxtr   r   r   __iadd__   s    
zCollection.__iadd__c             C   sP   | j }| j}x>|rJ|j|tj}|j|tjd f |j|tjd f |}qW d S )N)r
   r	   r   r   r   r2   r#   )r   r   r	   r   r   r   r   clear   s    zCollection.clearN)__name__
__module____qualname____doc__r   r   r   r"   r   r/   r1   r5   r6   r7   r;   r=   r>   r   r   r   r   r      s   7
c              C   s   dd l } | j  d S )Nr   )doctesttestmod)rC   r   r   r   test   s    rE   __main__)Graph1234   r   i  N5   )
__future__r   r   r   Zrdflib.namespacer   Zrdflib.termr   r   __all__objectr   rE   r?   ZrdflibrG   gr-   r   r   r    r+   r   r;   printr>   r   r   r   r   <module>   s@    k
$,

