summaryrefslogtreecommitdiff
path: root/rdflib
diff options
context:
space:
mode:
authorkushagr08 <35035965+kushagr08@users.noreply.github.com>2020-04-08 11:44:45 +0530
committerGitHub <noreply@github.com>2020-04-08 11:44:45 +0530
commitb4bf25acedc6605ecb7ca092d08923e3faed85b5 (patch)
treed27dd979ff64af0a31bbda2818ee9b2419fc3842 /rdflib
parent4a9c0c1b19a50107098b4cb01b84c55830f904db (diff)
downloadrdflib-b4bf25acedc6605ecb7ca092d08923e3faed85b5.tar.gz
update as per comment 1 of pull request
update as per comment 1 of pull request
Diffstat (limited to 'rdflib')
-rw-r--r--rdflib/container.py103
1 files changed, 54 insertions, 49 deletions
diff --git a/rdflib/container.py b/rdflib/container.py
index d62f90da..62496dc4 100644
--- a/rdflib/container.py
+++ b/rdflib/container.py
@@ -18,24 +18,24 @@ class Container(object):
self.graph = graph
self.uri = uri or BNode()
- self.len=0
- self.type=typee
+ self._len=0
+ self._type=typee# Bag or Seq or Alt
- self.__iadd__(seq)
+ self.append_multiple(seq)
pred_uri='http://www.w3.org/1999/02/22-rdf-syntax-ns#type'
- container_uri='http://www.w3.org/1999/02/22-rdf-syntax-ns#'+str(self.type)
+ container_uri='http://www.w3.org/1999/02/22-rdf-syntax-ns#'+str(self._type)
container = self.uri
- self.graph.add((container, URIRef(pred_uri), URIRef(container_uri)))
+ self.graph.add((container, URIRef(pred_uri), URIRef(container_uri)))#adding triple corresponding to container type
def n3(self):
items=[]
- for i in range(self.__len__()):
+ for i in range(len(self)):
- v=self.__getitem__(i+1)
+ v=self[i+1]
items.append(v)
@@ -48,13 +48,17 @@ class Container(object):
return container
def __len__(self):
- """length of items in container."""
+ """number of items in container."""
- return self.len
+ return self._len
+
+ def type_of_conatiner(self):
+ '''returns container type- bag or seq or alt'''
+ return self._type
def index(self, item):
"""
- Returns the 1-based numerical index of the item in the container or the key
+ Returns the 1-based numerical index of the item in the container
"""
@@ -76,7 +80,7 @@ class Container(object):
assert isinstance(key,int)
elem_uri='http://www.w3.org/1999/02/22-rdf-syntax-ns#_'+str(key)
- if (key<=0) or (key>self.__len__()):
+ if (key<=0) or (key>len(self)):
raise KeyError(key)
v = self.graph.value(c, URIRef(elem_uri))
if v:
@@ -92,7 +96,7 @@ class Container(object):
c = self._get_container()
elem_uri='http://www.w3.org/1999/02/22-rdf-syntax-ns#_'+str(key)
- if key<=0 or key>self.__len__():
+ if key<=0 or key>len(self):
raise KeyError(key)
self.graph.set((c, URIRef(elem_uri), value))
@@ -103,21 +107,21 @@ class Container(object):
'''removing the item with index key or predicate rdf:_key'''
assert isinstance(key,int)
- if key<=0 or key>self.__len__():
+ if key<=0 or key>len(self):
raise KeyError(key)
graph = self.graph
container = self.uri
elem_uri='http://www.w3.org/1999/02/22-rdf-syntax-ns#_'+str(key)
graph.remove((container,URIRef(elem_uri), None))
- for j in range(key+1,self.__len__()+1):
+ for j in range(key+1,len(self)+1):
elem_uri='http://www.w3.org/1999/02/22-rdf-syntax-ns#_'+str(j)
v=graph.value(container,URIRef(elem_uri))
graph.remove((container,URIRef(elem_uri),v))
elem_uri='http://www.w3.org/1999/02/22-rdf-syntax-ns#_'+str(j-1)
graph.add((container,URIRef(elem_uri),v))
- self.len-=1
+ self._len-=1
def items(self):
"""returns a list of all items in the container"""
@@ -136,8 +140,8 @@ class Container(object):
'''
'''
- def _end(self):#
- # find end of container
+ def end(self):#
+ # find end index (1-based) of container
container = self.uri
i=1
while True:
@@ -152,25 +156,25 @@ class Container(object):
''' adding item to the end of the container'''
- end = self._end()
+ end = self.end()
elem_uri='http://www.w3.org/1999/02/22-rdf-syntax-ns#_'+str(end+1)
container = self.uri
self.graph.add((container, URIRef(elem_uri), item))
- self.len+=1
+ self._len+=1
- def __iadd__(self, other):
+ def append_multiple(self, other):
- '''adding multiple elements in the container to the end'''
+ '''adding multiple elements to the container to the end which are in python list other'''
- end = self._end() #it should return the last index
+ end = self.end() #it should return the last index
container = self.uri
for item in other:
end+=1
- self.len+=1
+ self._len+=1
elem_uri='http://www.w3.org/1999/02/22-rdf-syntax-ns#_'+str(end)
self.graph.add((container, URIRef(elem_uri), item))
@@ -189,11 +193,12 @@ class Container(object):
i+=1
else:
break
- self.len=0
+ self._len=0
class Bag(Container):
+ '''unordered container (no preference order of elements)'''
def __init__(self, graph, uri, seq=[]):
Container.__init__(self,graph,uri,seq,"Bag")
@@ -201,12 +206,12 @@ class Alt(Container):
def __init__(self, graph, uri, seq=[]):
Container.__init__(self,graph,uri,seq,"Alt")
- '''returns any one item of the Alt Container randomly'''
+ '''returns any one item of the Alt Container randomly, this functionality added since semantic meaning of ALt is anyone of the elements of the container'''
def anyone(self):
- if (self.__len__==0):
+ if (len(self)==0):
raise NoElementException()
else:
- p=randint(1,self.__len__())
+ p=randint(1,len(self))
item=self.__getitem__(p)
return item
@@ -214,16 +219,17 @@ class Seq(Container):
def __init__(self, graph, uri, seq=[]):
Container.__init__(self,graph,uri,seq,"Seq")
- '''adds the item at position pos, valid values are 1 to len+1'''
+ '''adds the item at position pos, valid values are 1 to len+1, this functionality added since sematic meaning of seq is that elements are arranged in decreasing order of preference'''
+ '''user can add the element at the position according to the preference he wants to give to the newly added element '''
def add_at_position(self,pos,item):
assert isinstance(pos,int)
- if pos<=0 or pos>(self.__len__()+1):
+ if pos<=0 or pos>(len(self)+1):
raise ValueError("Invalid Position for inserting element in rdf:seq")
- if (pos==(self.__len__()+1)):
+ if (pos==(len(self)+1)):
self.append(item)
else:
- for j in range(self.__len__(),pos-1,-1):
+ for j in range(len(self),pos-1,-1):
container=self._get_container()
elem_uri='http://www.w3.org/1999/02/22-rdf-syntax-ns#_'+str(j)
v=self.graph.value(container,URIRef(elem_uri))
@@ -232,7 +238,7 @@ class Seq(Container):
self.graph.add((container,URIRef(elem_uri),v))
elem_uri_pos='http://www.w3.org/1999/02/22-rdf-syntax-ns#_'+str(pos)
self.graph.add((container,URIRef(elem_uri_pos),item))
- self.len+=1
+ self._len+=1
@@ -260,7 +266,7 @@ if __name__ == "__main__":
c = Bag(g, BNode())
- assert c.__len__() == 0
+ assert len(c) == 0
cc = Bag(
g, BNode(), [Literal("1"), Literal("2"), Literal("3"), Literal("4")])
@@ -269,35 +275,34 @@ if __name__ == "__main__":
from pprint import pprint
pprint (cc.n3())
- assert cc.__len__() == 4
+ assert len(cc) == 4
cc.append(Literal("5"))
- cc.__delitem__(2)
- assert cc.__len__()==4
+ del cc[2]
+
+ assert len(cc)==4
assert cc.index(Literal("5"))==4
- assert cc.__getitem__(2) == Literal("3")
+ assert cc[2] == Literal("3")
print (cc.items())
- cc.__setitem__(2,Literal("9"))
- assert cc.__getitem__(2) == Literal("9")
+ cc[2]=Literal("9")
+ assert cc[2] == Literal("9")
from pprint import pprint
pprint (cc.n3())
cc.clear()
- assert cc.__len__()==0
- cc.__iadd__([Literal("80"),Literal("90")])
- assert cc.__getitem__(1) == Literal("80")
- assert cc.__getitem__(2) == Literal("90")
- assert cc.__len__()==2
- assert cc._end()==2
+ assert len(cc)==0
+ cc.append_multiple([Literal("80"),Literal("90")])
+ assert cc[1] == Literal("80")
+ assert cc[2] == Literal("90")
+ assert len(cc)==2
+ assert cc.end()==2
cc = Alt(
g, BNode(), [Literal("1"), Literal("2"), Literal("3"), Literal("4")])
assert cc.anyone() in [Literal("1"), Literal("2"), Literal("3"), Literal("4")]
cc = Seq(
g, BNode(), [Literal("1"), Literal("2"), Literal("3"), Literal("4")])
cc.add_at_position(3,Literal("60"))
- assert cc.__len__()==5
+ assert len(cc)==5
assert cc.index(Literal("60"))==3
assert cc.index(Literal("3"))==4
assert cc.index(Literal("4"))==5
- print ("All tests passed")
-
- \ No newline at end of file
+ print ("All tests passed")