o
    mi                     @   s   d dl Z 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 )    N)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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:

    ```python
    >>> 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 )zCreates a Container

        Args:
            graph: a Graph instance
            uri: URI or Blank Node of the Container
            seq: the elements of the Container
            rtype: the type of Container, one of "Bag", "Seq" or "Alt"
        r   N)	graphr   uri_len_rtypeappend_multipleaddr   type)selfr   r   seqZrtype r   J/home/kim/smarthome/.venv/lib/python3.10/site-packages/rdflib/container.py__init__2   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>L   s    z Container.n3.<locals>.<listcomp>)rangelenappendjoin)r   itemsivr   r   r   r   F   s
   zContainer.n3c                 C      | j S )z Returns the URI of the container)r   r   r   r   r   _get_containerN      zContainer._get_containerc                 C   r$   )zNumber of items in container)r   r%   r   r   r   __len__S   r'   zContainer.__len__c                 C   s   t jdtdd | jS )Nzardflib.container.Container.type_of_conatiner is deprecated. Use type_of_container method instead.   )
stacklevel)warningswarnDeprecationWarningr   r%   r   r   r   type_of_conatinerX   s   zContainer.type_of_conatinerc                 C   r$   N)r   r%   r   r   r   type_of_containera      zContainer.type_of_containerc                 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   Z
predicatesr   
ValueErrorr   strr   intreplace)r   itempredZli_indexr"   pr   r   r   indexd   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 keyr3   r   )
r&   
isinstancer7   r6   r   r   KeyErrorr   valuer   )r   keycelem_urir#   r   r   r   __getitem__q   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 valuer3   r   N)
r=   r7   r&   r6   r   r   r>   r   setr   )r   r@   r?   rA   rB   r   r   r   __setitem__   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   r3   Nr   )r=   r7   r   r>   r   r   r6   r   remover   r   r?   r   r   )r   r@   r   r2   rB   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   Tr3   N)r   r6   r   r   r   r   r?   )r   Zl_r2   r"   rB   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   Tr3   )r   r6   r   r   r   )r   r2   r"   rB   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 containerr3   r   )rI   r6   r   r   r   r   r   r   )r   r9   rI   rB   r2   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   r3   )rI   r   r   r6   r   r   r   r   )r   otherrI   r2   r9   rB   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   Tr3   Nr   )r   r   r6   r   r   rF   r   )r   r2   r   r"   rB   r   r   r   clear   s   
zContainer.clearN)__name__
__module____qualname____doc__r   r   r&   r(   r.   r0   r<   rC   rE   rH   r!   rI   r   r   rK   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)rL   rM   rN   rO   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   rP   )Nr	   rQ   rR   r   r   r   r      rS   zAlt.__init__c                 C   s.   t | dkr	t tdt | }| |}|S )Nr   r   )r   r
   r   rC   )r   r;   r9   r   r   r   anyone   s
   
z
Alt.anyoneN)rL   rM   rN   r   rU   r   r   r   r   r	          r	   c                   @   rT   )r   c                 C   rP   )Nr   rQ   rR   r   r   r   r      rS   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:Seqr3   )r=   r7   r   r5   r   r   r&   r6   r   r   r?   r   rF   r   r   )r   posr9   rG   r2   rB   r#   Zelem_uri_posr   r   r   add_at_position   s"   
zSeq.add_at_positionN)rL   rM   rN   r   rY   r   r   r   r   r      rV   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/   r[   r%   r   r   r   __str__  r1   zNoElementException.__str__N)rZ   )rL   rM   rN   r   r]   r   r   r   r   r
     s    
r
   )r+   randomr   Zrdflib.namespacer   Zrdflib.termr   r   __all__r   r   r	   r   	Exceptionr
   r   r   r   r   <module>   s     ^