o
    ¹ií  ã                   @   s‚   d dl mZ d dlmZ d dlmZmZ g d¢ZG dd„ dƒZG dd„ deƒZ	G d	d
„ d
eƒZ
G dd„ deƒZG dd„ deƒZdS )é    )Úrandint)ÚRDF)ÚBNodeÚURIRef)Ú	ContainerÚBagÚSeqÚAltÚNoElementExceptionc                   @   s†   e Zd ZdZg d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d„ ZdS ) r   a{  A class for constructing RDF containers, as per https://www.w3.org/TR/rdf11-mt/#rdf-containers

    Basic usage, creating a ``Bag`` and adding to it::

        >>> from rdflib import Graph, BNode, Literal, Bag
        >>> g = Graph()
        >>> b = Bag(g, BNode(), [Literal("One"), Literal("Two"), Literal("Three")])
        >>> print(g.serialize(format="turtle"))
        @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
        <BLANKLINE>
        [] a rdf:Bag ;
            rdf:_1 "One" ;
            rdf:_2 "Two" ;
            rdf:_3 "Three" .
        <BLANKLINE>
        <BLANKLINE>

        >>> # print out an item using an index reference
        >>> print(b[2])
        Two

        >>> # add a new item
        >>> b.append(Literal("Hello")) # doctest: +ELLIPSIS
        <rdflib.container.Bag object at ...>
        >>> print(g.serialize(format="turtle"))
        @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
        <BLANKLINE>
        [] a rdf:Bag ;
            rdf:_1 "One" ;
            rdf:_2 "Two" ;
            rdf:_3 "Three" ;
            rdf:_4 "Hello" .
        <BLANKLINE>
        <BLANKLINE>

    r   c                 C   sH   || _ |ptƒ | _d| _|| _|  |¡ | j  | jtjt| j f¡ dS )z÷Creates a Container

        :param graph: a Graph instance
        :param uri: URI or Blank Node of the Container
        :param seq: the elements of the Container
        :param rtype: the type of Container, one of "Bag", "Seq" or "Alt"
        r   N)	Úgraphr   ÚuriÚ_lenÚ_rtypeÚappend_multipleÚaddr   Útype)Úselfr   r   ÚseqÚrtype© r   úK/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/rdflib/container.pyÚ__init__/   s   	
 zContainer.__init__c                 C   sD   g }t t| ƒƒD ]}| |d  }| |¡ qdd dd„ |D ƒ¡ S )Né   z( %s )ú c                 S   s   g | ]}|  ¡ ‘qS r   )Ún3)Ú.0Úar   r   r   Ú
<listcomp>H   s    z Container.n3.<locals>.<listcomp>)ÚrangeÚlenÚappendÚjoin)r   ÚitemsÚiÚvr   r   r   r   B   s
   zContainer.n3c                 C   ó   | j S )z Returns the URI of the container)r   ©r   r   r   r   Ú_get_containerJ   ó   zContainer._get_containerc                 C   r%   )zNumber of items in container)r   r&   r   r   r   Ú__len__O   r(   zContainer.__len__c                 C   r%   ©N)r   r&   r   r   r   Útype_of_conatinerT   ó   zContainer.type_of_conatinerc                 C   sV   | j  | j|¡}|std|df ƒ‚tttƒd ƒ}d}|D ]
}t| |d¡ƒ}q|S )z@Returns the 1-based numerical index of the item in the containerz%s is not in %sÚ	containerÚ_NÚ )	r   Ú
predicatesr   Ú
ValueErrorr   Ústrr   ÚintÚreplace)r   ÚitemÚpredÚli_indexr#   Úpr   r   r   ÚindexW   s   zContainer.indexc                 C   sh   |   ¡ }t|tƒsJ ‚ttƒd t|ƒ }|dks|t| ƒkr#t|ƒ‚| j |t	|ƒ¡}|r0|S t|ƒ‚)z*Returns item of the container at index keyr.   r   )
r'   Ú
isinstancer3   r2   r   r   ÚKeyErrorr   Úvaluer   )r   ÚkeyÚcÚelem_urir$   r   r   r   Ú__getitem__d   s   zContainer.__getitem__c                 C   s`   t |tƒsJ ‚|  ¡ }ttƒd t|ƒ }|dks|t| ƒkr#t|ƒ‚| j |t	|ƒ|f¡ dS )zJSets the item at index key or predicate rdf:_key of the container to valuer.   r   N)
r:   r3   r'   r2   r   r   r;   r   Úsetr   )r   r=   r<   r>   r?   r   r   r   Ú__setitem__s   s   zContainer.__setitem__c                 C   sð   t |tƒsJ ‚|dks|t| ƒkrt|ƒ‚| j}| j}ttƒd t|ƒ }| |t	|ƒdf¡ t
|d t| ƒd ƒD ]4}ttƒd t|ƒ }| |t	|ƒ¡}| |t	|ƒ|f¡ ttƒd t|d ƒ }| |t	|ƒ|f¡ q:|  jd8  _dS )z6Removing the item with index key or predicate rdf:_keyr   r.   Nr   )r:   r3   r   r;   r   r   r2   r   Úremover   r   r<   r   r   )r   r=   r   r-   r?   Újr$   r   r   r   Ú__delitem__   s   zContainer.__delitem__c                 C   sb   g }| j }d}	 ttƒd t|ƒ }|t|ƒdf| jv r-|d7 }| | j |t|ƒ¡¡ n	 |S q)z,Returns a list of all items in the containerr   Tr.   N)r   r2   r   r   r   r    r<   )r   Úl_r-   r#   r?   r   r   r   r"   “   s   øzContainer.itemsc                 C   sH   | j }d}	 ttƒd t|ƒ }|t|ƒd f| jv r|d7 }n|d S q)Nr   Tr.   )r   r2   r   r   r   )r   r-   r#   r?   r   r   r   Úend£   s   
úzContainer.endc                 C   sN   |   ¡ }ttƒd t|d ƒ }| j}| j |t|ƒ|f¡ |  jd7  _| S )z'Adding item to the end of the containerr.   r   )rG   r2   r   r   r   r   r   r   )r   r5   rG   r?   r-   r   r   r   r    °   s   zContainer.appendc                 C   s\   |   ¡ }| j}|D ]"}|d7 }|  jd7  _ttƒd t|ƒ }| j |t|ƒ|f¡ q	| S )zSAdding multiple elements to the container to the end which are in python list otherr   r.   )rG   r   r   r2   r   r   r   r   )r   ÚotherrG   r-   r5   r?   r   r   r   r   »   s   zContainer.append_multiplec                 C   sf   | j }| j}d}	 ttƒd t|ƒ }|t|ƒdf| jv r,| |t|ƒdf¡ |d7 }nnq	d| _| S )z(Removing all elements from the containerr   Tr.   Nr   )r   r   r2   r   r   rC   r   )r   r-   r   r#   r?   r   r   r   ÚclearÉ   s   
úzContainer.clearN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r'   r)   r+   r9   r@   rB   rE   r"   rG   r    r   rI   r   r   r   r   r   	   s     %r   c                   @   s   e Zd ZdZg fdd„ZdS )r   z5Unordered container (no preference order of elements)c                 C   ó   t  | |||d¡ d S )Nr   ©r   r   ©r   r   r   r   r   r   r   r   Ý   ó   zBag.__init__N)rJ   rK   rL   rM   r   r   r   r   r   r   Ú   s    r   c                   @   ó    e Zd Zg fdd„Zdd„ ZdS )r	   c                 C   rN   )Nr	   rO   rP   r   r   r   r   â   rQ   zAlt.__init__c                 C   s.   t | ƒdkr	tƒ ‚tdt | ƒƒ}|  |¡}|S )Nr   r   )r   r
   r   r@   )r   r8   r5   r   r   r   Úanyoneå   s
   
z
Alt.anyoneN)rJ   rK   rL   r   rS   r   r   r   r   r	   á   ó    r	   c                   @   rR   )r   c                 C   rN   )Nr   rO   rP   r   r   r   r   ï   rQ   zSeq.__init__c                 C   s  t |tƒsJ ‚|dks|t| ƒd krtdƒ‚|t| ƒd kr&|  |¡ | S tt| ƒ|d dƒD ];}|  ¡ }ttƒd t|ƒ }| j	 
|t|ƒ¡}| j	 |t|ƒ|f¡ ttƒd t|d ƒ }| j	 |t|ƒ|f¡ q0ttƒd t|ƒ }| j	 |t|ƒ|f¡ |  jd7  _| S )Nr   r   z1Invalid Position for inserting element in rdf:Seqéÿÿÿÿr.   )r:   r3   r   r1   r    r   r'   r2   r   r   r<   r   rC   r   r   )r   Úposr5   rD   r-   r?   r$   Úelem_uri_posr   r   r   Úadd_at_positionò   s"   
özSeq.add_at_positionN)rJ   rK   rL   r   rX   r   r   r   r   r   î   rT   r   c                   @   s   e Zd Zddd„Zdd„ ZdS )r
   úrdf:Alt Container is emptyc                 C   s
   || _ d S r*   ©Úmessage)r   r[   r   r   r   r     s   
zNoElementException.__init__c                 C   r%   r*   rZ   r&   r   r   r   Ú__str__  r,   zNoElementException.__str__N)rY   )rJ   rK   rL   r   r\   r   r   r   r   r
     s    
r
   N)Úrandomr   Úrdflib.namespacer   Úrdflib.termr   r   Ú__all__r   r   r	   r   Ú	Exceptionr
   r   r   r   r   Ú<module>   s     R