https://github.com/scardine/doid
Generic object container with filter/order DSL inspired by Django and SQLAlchemy
https://github.com/scardine/doid
dsl filtering ordering
Last synced: 3 months ago
JSON representation
Generic object container with filter/order DSL inspired by Django and SQLAlchemy
- Host: GitHub
- URL: https://github.com/scardine/doid
- Owner: scardine
- License: mit
- Created: 2018-09-26T06:17:07.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-09-26T12:19:05.000Z (over 7 years ago)
- Last Synced: 2025-10-30T09:09:43.942Z (6 months ago)
- Topics: dsl, filtering, ordering
- Language: Python
- Homepage:
- Size: 11.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README

> **doid** (plural doids)
>
> 1. (zoology) Any member of the Doidae, a family of moths.
# DOID - Django ORM inspired DSL
> “Talent borrows. Genius Steals!” - Oscar Wilde.
DOID is a generic DSL for filtering and sorting inspired by projects like
the Django ORM and SQLAlchemy. It is a generic container with `filter` and `order_by`
methods much like the result set managers from Django. This should be useful
if you are fond of Django's ORM idioms and would like to use the same patterns
on objects that are not Django Models like objects from API responses.
## Filter Interface
A filter object is a callable that receives an object and returns True or False.
Filters can be combined using the bitwise operators: `&`, `|` and `~`. For example:
filter4 = filter1 | (filter2 & filter3)
You can turn any callable into a filter using a decorator:
from doid.filter import doid_filter
@doid_filter
def milenials(value):
return value.born.year > 2000
Filter should never mutate the value they receive.
## Sample data
It is easier to explain using examples. The containers are totally agnostic and
take any dataclasses-style object, so lets generate some fake data:
>>> data =[
['Adrian Mathews', 'Wilsonview', datetime.date(1944, 9, 5), 'PM'],
['Amanda Kaufman', 'East Franklin', datetime.date(1972, 8, 6), 'AM'],
['Benjamin Mcconnell', 'Wilsonview', datetime.date(1928, 8, 8), 'AM'],
['Carolyn Wilcox', 'Lake Benjaminbury', datetime.date(1944, 4, 21), 'PM'],
['Christina White', 'Angelamouth', datetime.date(1963, 10, 22), 'PM'],
['David Berry', 'Lake Benjaminbury', datetime.date(1950, 2, 10), 'PM'],
['Jacob Johnson', 'Wilsonview', datetime.date(1950, 5, 8), 'AM'],
['Jasmine Sanchez', 'Port Janefort', datetime.date(2008, 5, 13), 'AM'],
['John Robinson', 'Angelamouth', datetime.date(1945, 7, 16), 'PM'],
['Kenneth Hernandez', 'Port Janefort', datetime.date(1978, 10, 23), 'PM'],
['Monica Conley', 'East Franklin', datetime.date(1918, 9, 27), 'AM'],
['Paula Melendez', 'East Franklin', datetime.date(2017, 5, 21), 'AM'],
['Robin Harris', 'Angelamouth', datetime.date(1976, 2, 9), 'PM'],
['Sheri Kerr', 'East Franklin', datetime.date(1904, 1, 3), 'AM'],
['Shirley Gray', 'Lake Benjaminbury', datetime.date(1996, 8, 2), 'AM'],
['Stacy Weaver', 'East Franklin', datetime.date(1931, 10, 31), 'PM'],
['Tiffany Sullivan DVM', 'Wilsonview', datetime.date(1909, 1, 7), 'PM'],
['Tracy Norman', 'Wilsonview', datetime.date(1932, 5, 2), 'PM'],
['William Johnson', 'Angelamouth', datetime.date(1950, 3, 27), 'PM'],
['Xavier Harris', 'East Franklin', datetime.date(1903, 10, 5), 'AM']
]
>>> class GenericObject(object):
def __init__(self, **kwargs):
for k, v in kwargs.items():
setattr(self, k, v)
def __repr__(self):
return "<" + ", ".join(f"{k}={v}" for k, v in self.__dict__.items()) + ">"
>>> from doid.container import ListContainer
>>> results = ListContainer(
GenericObject(name=name, city=city, born=born, ampm=ampm) for
name, city, born, ampm in data
)
>>> results
[,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
]
## Attribute getter protocol
The filter protocol folows a attribute-getter protocol much like one used by Django.
We can filter by any attribute using `.filter(name=value)`:
>>> results.filter(city="East Franklin")
[,
,
,
,
,
]
We can access nested attributes replacing the `.` by double underscores:
>>> results.filter(born__month=5)
[,
,
,
]
Some operators ara available using the same names from the `operator` module, for example
you can append `__gt` to represent the `>` operator:
>>> from doid.filter import Q
>>> milenials = Q(born__year__gt=1980)
>>> results.filter(milenials)
[,
,
]
We can filter using regular expressions by appending `__match`:
>>> results.filter(name__match='^S')
[,
,
]
Like in the Django ORM, methods can be chained:
>>> results.filter(name__match='^S').filter(milenials)
[]
This is the same as:
>>> results.filter(name__match='^S', milenials)
[]
Again like in Django, we can express OR filters using Q objects:
>>> results.filter(Q(name__match='^S') | milenials)
[,
,
,
,
]
The `order_by` method accepts strings following the same protocol (keyword
arguments will be passed directly to the sort method):
>>> results.order_by('born__year')[:5]
[,
,
,
,
,
>>> results.order_by('born__year', reverse=True)[:5]
[,
,
,
,
]
## Other ideas
This is pretty much a work in progress. Some ideas are:
1. Implement getters that works also for dicts and lists
1. Implement getters that fail gracefully it the attribute/key does not exist
1. Create other container types like ordered sets