Skip to content

Commit 267bc83

Browse files
authored
Merge pull request #275 from tcmitchell/8-read-write
Speed up document loading
2 parents 5350e24 + 5f1a4dc commit 267bc83

2 files changed

Lines changed: 14 additions & 32 deletions

File tree

sbol2/document.py

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -471,13 +471,6 @@ def parse_all(self):
471471
# Instantiate all objects with an RDF type
472472
for s, _, o in self.graph.triples((None, rdflib.RDF.type, None)):
473473
self.parse_objects_inner(s, o)
474-
# Find everything in the triple store
475-
all_query = "PREFIX : <http://example.org/ns#> " \
476-
"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> " \
477-
"PREFIX sbol: <http://sbols.org/v2#> " \
478-
"SELECT ?s ?p ?o " \
479-
"{ ?s ?p ?o }"
480-
all_results = self.graph.query(all_query)
481474
# Find the graph base uri. This is the location of the sbol
482475
# file, and begins with the "file://" scheme. Any URI in the
483476
# file without a scheme will appear relative to this URI, after
@@ -488,18 +481,16 @@ def parse_all(self):
488481
pos = graphBaseURIStr.rfind('/')
489482
if pos != -1:
490483
pos += 1
491-
rdf_type = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
492-
for result in all_results:
484+
rdf_type = rdflib.RDF.type
485+
for result_s, result_p, result_o in self.graph:
493486
# Look for properties
494-
if str(result.p) != rdf_type:
495-
obj = result.o
496-
lval = str(obj)
497-
if isinstance(result.o, URIRef) and pos != -1:
498-
if lval[:pos] == graphBaseURIStr:
487+
if result_p != rdf_type:
488+
obj = result_o
489+
if isinstance(result_o, URIRef) and pos != -1:
490+
if obj[:pos] == graphBaseURIStr:
499491
# This was a URI without a scheme. Remove URI base
500-
lval = lval[pos:]
501-
obj = URIRef(lval)
502-
self.parse_properties_inner(result.s, result.p, obj)
492+
obj = URIRef(obj[pos:])
493+
self.parse_properties_inner(result_s, result_p, obj)
503494

504495
# Remove objects from SBOLObjects if they are not TopLevel AND
505496
# they have a parent object.
@@ -541,8 +532,7 @@ def parse_objects_inner(self, subject, obj):
541532
values.clear()
542533
new_obj.identity = subject
543534
# Update document
544-
identity_uri = rdflib.URIRef(new_obj.identity)
545-
self.SBOLObjects[identity_uri] = new_obj
535+
self.SBOLObjects[new_obj.identity] = new_obj
546536
new_obj.doc = self
547537
# For now, set the parent to the Document.
548538
# This may get overwritten later for child objects.
@@ -557,8 +547,7 @@ def parse_objects_inner(self, subject, obj):
557547
new_obj = SBOLObject()
558548
new_obj.identity = subject
559549
new_obj.rdf_type = obj
560-
identity_uri = rdflib.URIRef(new_obj.identity)
561-
self.SBOLObjects[identity_uri] = new_obj
550+
self.SBOLObjects[new_obj.identity] = new_obj
562551
new_obj.doc = self
563552

564553
def parse_properties_inner(self, subject, predicate, obj):
@@ -584,11 +573,7 @@ def parse_properties_inner(self, subject, predicate, obj):
584573
# del self.SBOLObjects[obj]
585574
else:
586575
# Extension data
587-
if predicate not in parent.properties:
588-
parent.properties[predicate] = []
589-
parent.properties[predicate].append(obj)
590-
else:
591-
parent.properties[predicate].append(obj)
576+
parent.properties[predicate] = [obj]
592577
else:
593578
msg = 'Subject {} ({}) not found in my SBOLObjects'
594579
msg = msg.format(subject, type(subject))

sbol2/object.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -426,14 +426,11 @@ def __getattribute__(self, name):
426426
# Call the default method
427427
result = object.__getattribute__(self, name)
428428
if isinstance(result, OwnedObject):
429-
sbol_property = object.__getattribute__(self, name)
430-
if sbol_property.upper_bound == 1:
431-
if len(sbol_property):
432-
result = sbol_property[0]
429+
if result.getUpperBound() == '1':
430+
if result:
431+
result = result[0]
433432
else:
434433
result = None
435-
else:
436-
result = sbol_property
437434
elif isinstance(result, Property):
438435
# Else if attribute is any other kind of Property besides
439436
# OwnedObject, convert so the Property attributes are

0 commit comments

Comments
 (0)