Skip to content

Commit 61b6e56

Browse files
committed
Use .items() comparison
1 parent 20f3e5c commit 61b6e56

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

src/posit/connect/_api.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -262,17 +262,19 @@ def find_by(self, **conditions: Any) -> T | None:
262262
The first record matching the conditions, or `None` if no match is found.
263263
"""
264264
results = self.fetch()
265-
for item in results:
266-
if obj_has_attrs(item, conditions):
267-
return item
268-
return None
269-
270-
271-
def obj_has_attrs(obj: ReadOnlyDict, attrs: dict[str, Any]) -> bool:
272-
for attr_key, attr_val in attrs.items():
273-
if not hasattr(obj, attr_key):
274-
return False
275-
obj_val = getattr(obj, attr_key)
276-
if obj_val != attr_val:
277-
return False
278-
return True
265+
266+
conditions_items = conditions.items()
267+
268+
# Get the first item of the generator that matches the conditions
269+
# If no item is found, return None
270+
return next(
271+
(
272+
# Return result
273+
result
274+
# Iterate through `results` generator
275+
for result in results
276+
# If all `conditions`'s key/values are found in `result`'s key/values...
277+
if result.items() >= conditions_items
278+
),
279+
None,
280+
)

0 commit comments

Comments
 (0)