https://github.com/mkalioby/leopards Skip to content Navigation Menu Toggle navigation Sign in * Product + GitHub Copilot Write better code with AI + Security Find and fix vulnerabilities + Actions Automate any workflow + Codespaces Instant dev environments + Issues Plan and track work + Code Review Manage code changes + Discussions Collaborate outside of code + Code Search Find more, search less Explore + All features + Documentation + GitHub Skills + Blog * Solutions By company size + Enterprises + Small and medium teams + Startups By use case + DevSecOps + DevOps + CI/CD + View all use cases By industry + Healthcare + Financial services + Manufacturing + Government + View all industries View all solutions * Resources Topics + AI + DevOps + Security + Software Development + View all Explore + Learning Pathways + White papers, Ebooks, Webinars + Customer Stories + Partners * Open Source + GitHub Sponsors Fund open source developers + The ReadME Project GitHub community articles Repositories + Topics + Trending + Collections * Enterprise + Enterprise platform AI-powered developer platform Available add-ons + Advanced Security Enterprise-grade security features + GitHub Copilot Enterprise-grade AI features + Premium Support Enterprise-grade 24/7 support * Pricing Search or jump to... Search code, repositories, users, issues, pull requests... Search [ ] Clear Search syntax tips Provide feedback We read every piece of feedback, and take your input very seriously. [ ] [ ] Include my email address so I can be contacted Cancel Submit feedback Saved searches Use saved searches to filter your results more quickly Name [ ] Query [ ] To see all available qualifiers, see our documentation. Cancel Create saved search Sign in Sign up Reseting focus You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. Dismiss alert {{ message }} mkalioby / leopards Public * Notifications You must be signed in to change notification settings * Fork 1 * Star 132 Query your python lists License MIT license 132 stars 1 fork Branches Tags Activity Star Notifications You must be signed in to change notification settings * Code * Issues 0 * Pull requests 0 * Actions * Projects 0 * Security * Insights Additional navigation options * Code * Issues * Pull requests * Actions * Projects * Security * Insights mkalioby/leopards main BranchesTags [ ] Go to file Code Folders and files Name Name Last commit Last commit message date Latest commit History 28 Commits .github/workflows .github/ workflows leopards leopards tests tests .gitignore .gitignore LICENSE LICENSE README.md README.md setup.py setup.py View all files Repository files navigation * README * MIT license Leopards PyPI version Python Versions Coverage build status Leopards is a way to query list of dictionaries or objects as if you are filtering in DBMS. You can get dicts/objects that are matched by OR, AND or NOT or all of them. As you can see in the comparison they are much faster than Pandas. Installation pip install leopards Usage from leopards import Q l = [{"name":"John","age":"16"}, {"name":"Mike","age":"19"},{"name":"Sarah","age":"21"}] filtered= Q(l,{'name__contains':"k", "age__lt":20}) print(list(filtered)) output [{'name': 'Mike', 'age': '19'}] The above filtration can be written as from leopards import Q l = [{"name": "John", "age": "16"}, {"name": "Mike", "age": "19"}, {"name": "Sarah", "age": "21"}] filtered = Q(l, name__contains="k", age__lt=20) Notes: 1. Q returns an iterator which can be converted to a list by calling list. 2. Even though, age was str in the dict, as the value of in the query dict was int, Leopards converted the value in dict automatically to match the query data type. This behaviour can be stopped by passing False to convert_types parameter. Supported filters * eq: equals and this default filter * gt: greater than. * gte: greater than or equal. * lt: less than * lte: less than or equal * in: the value in a list of a tuple. + e.g. age__in=[10,20,30] * contains: contains a substring as in the example. * icontains: case-insensitive contains. * startswith: checks if a value starts with a query strings. * istartswith: case-insensitive startswith. * endswith: checks if a value ends with a query strings. * iendswith: case-insensitive endswith. * isnull: checks if the value matches any of NULL_VALUES which are ('', '.', None, "None", "null", "NULL") + e.g. filter__isnull=True or filter__isnull=False For eq,gt,gte,lt,lte, in, contains, icontains, startswith,istartswith, endswith and iendswith, you can add a n to negate the results. e.g nin which is equivalent to not in Advanced examples This section will cover the use of OR, AND and NOT Usage of OR OR or __or__ takes a list of dictionaries to evaluate and returns with the first True. from leopards import Q l = [{"name": "John", "age": "16"}, {"name": "Mike", "age": "19"}, {"name": "Sarah", "age": "21"}] filtered = Q(l, {"OR": [{"name__contains": "k"}, {"age__gte": 21}]}) print(list(filtered)) output [{'name': 'Mike', 'age': '19'}, {'name': 'Sarah', 'age': '21'}] Usage of NOT NOT or __not__ takes a dict for query run. from leopards import Q l = [{"name": "John", "age": "16"}, {"name": "Mike", "age": "19"}, {"name": "Sarah", "age": "21"}] filtered = Q(l, {"age__gt": 15, "NOT": {"age__eq": 19}}) print(list(filtered)) output [{'name': 'John', 'age': '16'}, {'name': 'Sarah', 'age': '21'}] Usage of AND AND or __and__ takes a list of dict for query run, returns with the first False. from leopards import Q l = [{"name": "John", "age": "16"}, {"name": "Mike", "age": "19"}, {"name": "Sarah", "age": "21"}] filtered = Q(l, {"__and__": [{"age__gte": 15}, {"age__lt": 21}]}) print(list(filtered)) output [{'name': 'John', 'age': '16'}, {'name': 'Mike', 'age': '19'}] Aggregating Data You can run the following aggregations * Count * Max * Min * Sum * Avg Count Find the count of certain aggregated column l = [{"name": "John", "age": "16"}, {"name": "Mike", "age": "19"}, {"name": "Sarah", "age": "21"},{"name":"John","age":"19"}] from leopards import Count count = Count(l,['age']) output [{"age":"16","count":1},{"age":"19","count":2}, {"age":"21","count":1}] Max Find the Max value for a certain column in certain aggregated columns l = [{"name": "John", "age": "16"}, {"name": "Mike", "age": "19"}, {"name": "Sarah", "age": "21"},{"name":"Joh","age":"19"}] from leopards import Max count = Max(l,"age",['name'],dtype=int) output [{'name': 'John', 'age': '19'}, {'name': 'Mike', 'age': '19'}, {'name': 'Sarah', 'age': '21'}] Notes: * If you don't pass the aggregation columns, the maximum will be found across dataset. * You can pass the datatype of the column to convert it on the fly while evaluating l = [{"name": "John", "age": "16"}, {"name": "Mike", "age": "19"}, {"name": "Sarah", "age": "21"},{"name":"Joh","age":"19"}] from leopards import Max m = Max(l,"age",dtype=int) output [{'age': 21}] Min Find the Max value for a certain column in certain aggregated columns l = [{"name": "John", "age": "16"}, {"name": "Mike", "age": "19"}, {"name": "Sarah", "age": "21"},{"name":"Joh","age":"19"}] from leopards import Min m = Min(l,"age",['name']) output [{'name': 'John', 'age': '16'}, {'name': 'Mike', 'age': '19'}, {'name': 'Sarah', 'age': '21'}] Note: * If you don't pass the aggregation columns, the min will be found across dataset. * You can pass the datatype of the column to convert it on the fly while evaluating Sum and Avg Like Min and Max but only works with integers and floats. Comparison with Pandas This is done on Python 3.8 running on Ubuntu 22.04 on i7 11th generation and 32 GB of RAM. Comparison Pandas Leopards Package Size 29.8 MB 7.5 KB (Lower is better) import Time (Worst) 146 ms 1.05 ms (Lower is better) load 10k CSV lines 0.295s 0.138s (Lower is better) ^[1] get first matched record 0.310s 0.017s (Lower is better) print all filtered records (10/10k) 0.310s 0.137s (Lower is better) filter by integers 0.316s 0.138s (Lower is better) ^[1] This was loading the whole csv in memory which was for sake of fair comparison. Nevertheless, Leopards can work with DictReader as an iterable which executes in 0.014s, then it handles line by line. Thanks for Asma Tahir for Pandas stats. Tutorials * Work on CSV Files with Leopards About Query your python lists Resources Readme License MIT license Activity Stars 132 stars Watchers 3 watching Forks 1 fork Report repository Releases No releases published Packages 0 No packages published Languages * Python 100.0% Footer (c) 2024 GitHub, Inc. Footer navigation * Terms * Privacy * Security * Status * Docs * Contact * Manage cookies * Do not share my personal information You can't perform that action at this time.