I have an object in my ontology (obj_foo
) that i want to filter. I have an array all_ui
and want to check if the object property foo_array
contains any value in the array all_ui
. then, i want to apply all_filters
to obj_foo
but the filter()
method does not exist on the object.
here is my current approach:
filter_condition = [obj_foo.foo_array.contains(ui) for ui in all_ui]
all_filters = filter_condition[0]
for condition in filter_condition[1:]:
all_filters |= condition
filtered_obj_set = obj_foo.filter(all_filters)
error:
AttributeError: type object 'obj_foo' has no attribute 'filter'.
Hi @meehirbhalla,
Here’s an alternative approach:
-
Use a lambda function or custom logic to iterate through the obj_foo and filter based on your condition.
-
Apply logical conditions programmatically since the filter() method is not available.
Initialization:
(I don’t have access to a similar object type right now)
# Example class definition for obj_foo
class FooObject:
def __init__(self, name, foo_array):
self.name = name
self.foo_array = foo_array
def __repr__(self):
return f'FooObject(name={self.name}, foo_array={self.foo_array})'
# Initialize obj_foo instances
obj_foo1 = FooObject("Foo1", [1, 2, 3])
obj_foo2 = FooObject("Foo2", [4, 5, 6])
obj_foo3 = FooObject("Foo3", [7, 8, 9])
# List of all obj_foo instances (just for example)
obj_foo_list = [obj_foo1, obj_foo2, obj_foo3]
# Initialize the array of UI values we want to check against foo_array
all_ui = [2, 5, 10]
Revised code, for which I assumed you want any instance based on a single hit:
“[…] and want to check if the object property foo_array contains any value in the array all_ui”
# Filter the obj_foo instances based on the
# condition that foo_array contains any value from all_ui
filtered_obj_set = []
for obj_foo in obj_foo_list:
# Check if any element in all_ui is present in obj_foo.foo_array
filter_condition = any(ui in obj_foo.foo_array for ui in all_ui)
if filter_condition:
# If condition is true, add obj_foo to filtered_obj_set
filtered_obj_set.append(obj_foo)
# Output the filtered object set
print("Filtered Object Set:", filtered_obj_set)
This should print, Foo1 and Foo2 objects.
Hope this works,
1 Like