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

DOC: Update filter tutorial

parent 04ae295a
No related branches found
No related tags found
3 merge requests!159Release 0.16.o,!158F list filter,!157compare entities and parent/property lists
...@@ -87,46 +87,42 @@ entities. A short example: ...@@ -87,46 +87,42 @@ entities. A short example:
import linkahead as db import linkahead as db
# Setup a record with four properties # Setup a record with six properties
r = db.Record() r = db.Record()
p1 = db.Property(id=101, name="Property 1") p1_1 = db.Property(id=101, name="Property 1")
p2 = db.Property(id=102, name="Property 2") p1_2 = db.Property(name="Property 1")
p3_1 = db.Property(id=103, name="Property 3") p2_1 = db.Property(id=102, name="Property 2")
p3_2 = db.Property(id=103, name="Property 3") p2_2 = db.Property(id=102)
p4 = db.Property(name="Property") p2_3 = db.Property(id=102, name="Other Property")
p5 = db.Property(name="Property") p3 = db.Property(id=104, name="Other Property")
r.add_property(p1).add_property(p2).add_property(p3_1) r.add_property(p1_1).add_property(p1_2).add_property(p2_1)
r.add_property(p3_2).add_property(p4).add_property(p5) r.add_property(p2_2).add_property(p2_3).add_property(p3)
properties = r.properties properties = r.properties
# As r only has one property with id 101, this returns a list containing only p1 # As r only has one property with id 101, this returns a list containing only p1_1
properties.filter(pid=101) properties.filter(pid=101)
# Result: [p1] # Result: [p1_1]
# Filtering with name="Property" returns both p4 and p5, as they share their name # Filtering with name="Property 1" returns both p1_1 and p1_2, as they share their name
properties.filter(name="Property") properties.filter(name="Property 1")
# Result: [p4, p5] # Result: [p1_1, p1_2]
# Filtering with name="Property 1" and id=102 returns both p1 and p2, because # If both name and pid are given, matching is based only on pid for all entities that have an id
# any property matching either criterion is returned: properties.filter(pid="102", name="Other Property")
properties.filter(name="Property 1", pid="102") # Result: [p2_1, p2_2, p2_3]
# Result: [p1, p2]
# However, filtering with name="Property 1" and id=101 returns both p1_1 and p1_2, because
p6 = db.Property(name="Property 2") # p1_2 does not have an id and matches the name
r.add_property(p6) properties.filter(pid="101", name="Property 1")
# If we want to find properties matching one specific property, we can also filter using # Result: [p1_1, p1_2]
# the entity itself. In this case, only properties matching both name and id are returned,
# as long as both are set. # We can also filter using an entity, in which case the name and id of the entity are used:
properties.filter(p2) properties.filter(pid="102", name="Property 2") == properties.filter(p2_1)
# Result: [p2] # Result: True
# As p6 does not have an id yet, both candidates matching its name are returned
properties.filter(p6) # If we only need properties that match both id and name, we can set the parameter
# Result: [p2, p6] # conjunction to True:
# Similarly if we match using name and id parameters, all candidates matching either are returned properties.filter(pid="102", name="Property 2", conjunction=True)
properties.filter(name=p2.name, pid=p2.id) # Result: [p2_1]
# 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. The filter function of ParentList works analogously.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment