dgilland / pydash

The kitchen sink of Python utility libraries for doing "stuff" in a functional way. Based on the Lo-Dash Javascript library.

Home Page:http://pydash.readthedocs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Matches style callback should support collections of non-dicts

stevelacey opened this issue · comments

Maybe this is a design decision/limitation of other things already in pydash, but I expected this to work:

users = User.objects.filter(...)
pydash.find(users, dict(first_name="Steve"))
=> None

Like it does in fnc:

users = User.objects.filter(...)
fnc.find(dict(first_name="Steve"), users)
=> <User: Steve>

I see matches styles callback takes a dict, but it only seems to work on lists of dicts, aka it doesn't match on attributes/properties, like I expected, and as per fnc, it would be great if these had parity.

I don't really need fnc and I'd use pydash instead if this worked, the matches property style works but isn't a great way to write this, matches styles callback requires zero explanation to the uninitiated.

Do you have a minimal, self-contained example that demonstrates the behavior? I can then check to see what's happening under the hood to see how feasible it would be to change. Thanks!

Sure thing, you can exercise it with a namedtuple:

from collections import namedtuple
import pydash

User = namedtuple('User', ['first_name'])

users = [User('Steve')]

pydash.find(users, dict(first_name='Steve'))
=> None
from collections import namedtuple
import fnc

User = namedtuple('User', ['first_name'])

users = [User('Steve')]

fnc.find(dict(first_name='Steve'), users)
=> User(first_name='Steve')

@stevelacey I've pushed a potential fix for this to the develop branch. Can you install it for your application and let me know if it resolves your issue?

@dgilland yep works perfect 👌🏼