Skip to content
Snippets Groups Projects
Commit 67716dc3 authored by I. Nüske's avatar I. Nüske
Browse files

DOC: Update filter tutorial

parent 1b3f81a8
No related branches found
No related tags found
2 merge requests!159Release 0.16.o,!155Review filter lists and compare_entities
Pipeline #56992 passed with warnings
......@@ -91,39 +91,42 @@ entities. A short example:
r = db.Record()
p1 = db.Property(id=101, name="Property 1")
p2 = db.Property(id=102, name="Property 2")
p3 = db.Property(name="Property")
p3_1 = db.Property(id=103, name="Property 3")
p3_2 = db.Property(id=103, name="Property 3")
p4 = db.Property(name="Property")
r.add_property(p1).add_property(p2).add_property(p3).add_property(p4)
p5 = db.Property(name="Property")
r.add_property(p1).add_property(p2).add_property(p3_1)
r.add_property(p3_2).add_property(p4).add_property(p5)
properties = r.properties
# As r only has one property with id 101, this returns a list containing only p1
properties.filter(pid=101)
# Result: [p1]
# Filtering with name="Property" returns both p3 and p4, as they share their name
# Filtering with name="Property" returns both p4 and p5, as they share their name
properties.filter(name="Property")
# Result: [p3, p4]
# Result: [p4, p5]
# Filtering with name="Property 1" and id=102 returns both p1 and p2, because
# any property matching either criterion is returned:
properties.filter(name="Property 1", pid="102")
# Result: [p1, p2]
p6 = db.Property(name="Property 2")
r.add_property(p6)
# If we want to find properties matching one specific property, we can also filter using
# the entity itself. In this case, only properties matching both name and id are returned:
p5 = db.Property(name="Property 2")
r.add_property(p5)
properties.filter(p5)
# Result: [p5]
properties.filter(name=p5.name, pid=p5.id)
# Result: [p2, p5], because p2 and p5 share a name
properties.filter(p3)
# Result: [p3, p4], because p3 and p4 share both their name and id
# However, if you want to retrieve only instances of the property itself, this can be
# done using the parameter check_equality, which enforces that all entities in the returned
# list are equal to the parameter.
properties.filter(p3, check_equality=True)
# Result: [p3]
# the entity itself. In this case, only properties matching both name and id are returned,
# as long as both are set.
properties.filter(p2)
# Result: [p2]
# As p6 does not have an id yet, both candidates matching its name are returned
properties.filter(p6)
# Result: [p2, p6]
# Similarly if we match using name and id parameters, all candidates matching either are returned
properties.filter(name=p2.name, pid=p2.id)
# Result: [p2, p6], because p2 and p6 share a name
# And if both name and id match, there may also be several results when matching an entity
properties.filter(p3_1)
# Result: [p3_1, p3_2], because they share both their name and id
The filter function of ParentList works analogously.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment