Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support searching with regular expressions #238

Closed
lyz-code opened this issue Apr 6, 2021 · 11 comments · Fixed by #239
Closed

Support searching with regular expressions #238

lyz-code opened this issue Apr 6, 2021 · 11 comments · Fixed by #239

Comments

@lyz-code
Copy link
Contributor

lyz-code commented Apr 6, 2021

I'd like to use regular expressions to find elements of a dictionary with grep.

(Pdb) entity_attributes
{'alone': {'id_': 'alone', 'name': 'Timothy', 'last_name': 'Jackson', 'country': 'Korea', 'rating': 5491}, 'film': {'id_': 'film', 'name': 'David', 'last_name': 'Howell', 'country': 'Moldova', 'rating': 9731}, 'thought': {'id_': 'thought'
, 'name': 'Jason', 'last_name': 'Roberts', 'country': 'Western Sahara', 'rating': 5484}}
(Pdb) entity_attributes | grep('Da')
{'matched_values': ["root['film']['name']"]}
(Pdb) entity_attributes | grep('Da.*')
{}

If you give me some guidance on how to do it, I can make a PR

@seperman
Copy link
Owner

seperman commented Apr 7, 2021

Hi @lyz-code
Thanks a lot for the PR. I left a comment. We can continue discussing in the PR. :)

@lyz-code
Copy link
Contributor Author

Hi @seperman, I've added a new PR to close this issue, so you can review it when you have time

@seperman
Copy link
Owner

seperman commented Apr 19, 2021 via email

@lyz-code
Copy link
Contributor Author

It will greatly help me in my use case. I'm developing repository-orm a library to act as an abstraction over persistent storage, allowing the user to decouple the model layer from the data layer.

In one of the implementations I use grep to search on the attributes of pydantic objects. The library is already being used by other programs that need to do regular expression searches on numbers of these object attributes, but pydantic correctly saves them as integers, so they don't show up in the results.

If we don't implement it, I will need to manually go through each attribute of each object stored, and if it's an int, convert it to string and see if the regular expression matches. Basically rewrite part of DeepSearch, so I'll be glad if you accepted the change.

I can see that having a different outcome whether use_regexp is enabled is not desirable, so if you want, I can implement that also for the regular use. I think it's safe to assume that if a user is searching for '1234', 1234 should also show up. If you don't agree maybe we can use a strict_checking flag?

Thanks anyway for reviewing it :)

@seperman
Copy link
Owner

seperman commented Apr 21, 2021 via email

@lyz-code
Copy link
Contributor Author

Hi, don't worry about the delay, that's the thing with open source, sometimes you have more time, sometimes you have less.

I'll implement the strict_checking flag these days. Regarding your question, my first thought is that if strict_checking == False and use_regexp == False then 1123456 should not match as I would expect only to match 1234, but if you use use_regexp == True then yes, as you're not searching for the exact 1234 content but strings that match that regular expression. How does that sound?

@seperman
Copy link
Owner

seperman commented Apr 22, 2021 via email

@lyz-code
Copy link
Contributor Author

I've made the changes, they are ready for review in the PR.

@lyz-code
Copy link
Contributor Author

lyz-code commented Apr 27, 2021

Thanks for merging the PR, should we close then this issue when #245 is merged?

@seperman
Copy link
Owner

seperman commented Apr 27, 2021 via email

@seperman
Copy link
Owner

Hi @lyz-code
Thanks for contributing to DeepDiff. Your feature is released: https://zepworks.com/deepdiff/current/#new-in-deepdiff-5-3-0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants