o
    ¹i#&  ã                   @  sl   d dl mZ d dlmZmZmZmZmZ d dlm	Z	 d dl
mZmZ er*d dlmZ dgZG dd„ dƒZdS )	é    )Úannotations)ÚTYPE_CHECKINGÚIterableÚIteratorÚListÚOptional)ÚRDF)ÚBNodeÚNode)ÚGraphÚ
Collectionc                   @  s”   e Zd ZdZg fd/dd	„Zd0dd„Zd1dd„Zd2dd„Zd3dd„Zd4dd„Z	d5dd„Z
d6dd „Zd7d"d#„Zd8d$d%„Zd9d&d'„Zd: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.term import Literal
    >>> from rdflib.graph import Graph
    >>> from pprint import pprint
    >>> listname = BNode()
    >>> g = Graph('Memory')
    >>> listItem1 = BNode()
    >>> listItem2 = BNode()
    >>> g.add((listname, RDF.first, Literal(1))) # doctest: +ELLIPSIS
    <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
    >>> g.add((listname, RDF.rest, listItem1)) # doctest: +ELLIPSIS
    <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
    >>> g.add((listItem1, RDF.first, Literal(2))) # doctest: +ELLIPSIS
    <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
    >>> g.add((listItem1, RDF.rest, listItem2)) # doctest: +ELLIPSIS
    <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
    >>> g.add((listItem2, RDF.rest, RDF.nil)) # doctest: +ELLIPSIS
    <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
    >>> g.add((listItem2, RDF.first, Literal(3))) # doctest: +ELLIPSIS
    <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
    >>> 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
    Úgraphr   Úurir
   Úseqú
List[Node]c                 C  s   || _ |ptƒ | _| |7 } d S ©N)r   r	   r   )Úselfr   r   r   © r   úL/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/rdflib/collection.pyÚ__init__6   s   zCollection.__init__ÚreturnÚstrc                 C  s   dd  dd„ | D ƒ¡ S )a  
        >>> from rdflib.term import Literal
        >>> from rdflib.graph import Graph
        >>> listname = BNode()
        >>> g = Graph('Memory')
        >>> listItem1 = BNode()
        >>> listItem2 = BNode()
        >>> g.add((listname, RDF.first, Literal(1))) # doctest: +ELLIPSIS
        <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
        >>> g.add((listname, RDF.rest, listItem1)) # doctest: +ELLIPSIS
        <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
        >>> g.add((listItem1, RDF.first, Literal(2))) # doctest: +ELLIPSIS
        <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
        >>> g.add((listItem1, RDF.rest, listItem2)) # doctest: +ELLIPSIS
        <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
        >>> g.add((listItem2, RDF.rest, RDF.nil)) # doctest: +ELLIPSIS
        <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
        >>> g.add((listItem2, RDF.first, Literal(3))) # doctest: +ELLIPSIS
        <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
        >>> 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 | ]}|  ¡ ‘qS r   )Ún3)Ú.0Úir   r   r   Ú
<listcomp>V   s    z!Collection.n3.<locals>.<listcomp>)Újoin©r   r   r   r   r   ;   s   zCollection.n3ÚindexÚintúOptional[Node]c                 C  sV   t |tƒsJ ‚| j}| j}d}||k r)|d7 }| |tj¡}|du r%	 |S ||k s|S )z+Gets the first, rest holding node at index.r   é   N)Ú
isinstancer    r   r   Úvaluer   Úrest)r   r   r   Ú	containerr   r   r   r   Ú_get_containerX   s   ûzCollection._get_containerc                 C  s   t t| j | j¡ƒƒS )zlength of items in collection.)ÚlenÚlistr   Úitemsr   r   r   r   r   Ú__len__e   s   zCollection.__len__Úitemc                 C  s’   | j }d}	 |tj|f| jv r|S t| j |tj¡ƒ}|d7 }|tjgkr.td|| j f ƒ‚|s7t	d| j  ƒ‚t
|ƒdksDJ d| j  ƒ‚|d }q)zM
        Returns the 0-based numerical index of the item in the list
        r   Tr"   z%s is not in %szMalformed RDF Collection: %s)r   r   Úfirstr   r)   Úobjectsr%   ÚnilÚ
ValueErrorÚ	Exceptionr(   )r   r,   Úlistnamer   Únewlinkr   r   r   r   i   s   ôzCollection.indexÚkeyc                 C  s6   |   |¡}|r| j |tj¡}|r|S t|ƒ‚t|ƒ‚)ÚTODO)r'   r   r$   r   r-   ÚKeyErrorÚ
IndexError)r   r4   ÚcÚvr   r   r   Ú__getitem__}   s   
zCollection.__getitem__r$   ÚNonec                 C  s.   |   |¡}|r| j |tj|f¡ dS t|ƒ‚)r5   N)r'   r   Úsetr   r-   r7   )r   r4   r$   r8   r   r   r   Ú__setitem__‰   s   
zCollection.__setitem__c                 C  sÎ   | |  | j }|  |¡}|sJ ‚t| ƒdkr|dkrdS |t| ƒd kr@|  |d ¡}| j  |tjtjf¡ | |ddf¡ dS |  |d ¡}|  |d ¡}|rR|sTJ ‚| |ddf¡ | |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)) # doctest: +ELLIPSIS
        <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
        >>> g.add((a, RDF.rest, b)) # doctest: +ELLIPSIS
        <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
        >>> g.add((b, RDF.first, RDFS.label)) # doctest: +ELLIPSIS
        <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
        >>> g.add((b, RDF.rest, c)) # doctest: +ELLIPSIS
        <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
        >>> g.add((c, RDF.first, RDFS.comment)) # doctest: +ELLIPSIS
        <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
        >>> g.add((c, RDF.rest, RDF.nil)) # doctest: +ELLIPSIS
        <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
        >>> 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<   r   r%   r/   Úremove)r   r4   r   ÚcurrentÚ	priorlinkÚnextÚpriorr   r   r   Ú__delitem__‘   s   +
zCollection.__delitem__úIterator[Node]c                 C  s   | j  | j¡S )z"Iterator over items in Collections)r   r*   r   r   r   r   r   Ú__iter__Ï   s   zCollection.__iter__c                 C  s4   | j }	 | j |tj¡}|d u s|tjkr|S |}qr   )r   r   r$   r   r%   r/   )r   r&   r%   r   r   r   Ú_endÓ   s   ûzCollection._endc                 C  sf   |   ¡ }|tjdf| jv rtƒ }| j |tj|f¡ |}| j |tj|f¡ | j |tjtjf¡ | S )a‰  
        >>> from rdflib.term import Literal
        >>> 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)	rF   r   r-   r   r	   r<   r%   Úaddr/   )r   r,   ÚendÚnoder   r   r   ÚappendÝ   s   zCollection.appendÚotherúIterable[Node]c                 C  s„   |   ¡ }| j |tjd f¡ |D ]$}|tjd f| jv r*tƒ }| j |tj|f¡ |}| j |tj|f¡ q| j |tjtjf¡ | S r   )	rF   r   r>   r   r%   r-   r	   rG   r/   )r   rK   rH   r,   Únxtr   r   r   Ú__iadd__ö   s   zCollection.__iadd__c                 C  sN   | j }| j}|r%| |tj¡}| |tjd f¡ | |tjd f¡ |}|s| S r   )r   r   r$   r   r%   r>   r-   )r   r&   r   r%   r   r   r   Úclear  s   üzCollection.clearN)r   r   r   r
   r   r   )r   r   )r   r    r   r!   )r   r    )r,   r
   r   r    )r4   r    r   r
   )r4   r    r$   r
   r   r;   )r4   r    r   r;   )r   rD   )r   r
   )r,   r
   r   r   )rK   rL   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r'   r+   r   r:   r=   rC   rE   rF   rJ   rN   rO   r   r   r   r   r      s    '







>



N)Ú
__future__r   Útypingr   r   r   r   r   Úrdflib.namespacer   Úrdflib.termr	   r
   Úrdflib.graphr   Ú__all__r   r   r   r   r   Ú<module>   s    