Add user definable functions aggregates and collations #155
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What does this do and why?
This adds new functionality allowing users to define and use custom aggregations, functions, and collations without requiring that those functions be included in the base
q
distribution by configuringq
to find and register those using setuptools' 'Entry Points' system (available by default with all Python installations newer than 2.7.9).Creating Custom Aggregates, Functions, or Collations
Given that I have a concrete example available for only one of the three possibilities below, I've leaned toward verbosity; apologies for the repetitiveness:
Creating a Custom Aggregate
To define a custom aggregate (example repository: https://github.com/coddingtonbear/q-stdev), just add a group
q_aggregates
to your project'ssetup.py
:The class referenced should meet the requirements defined by Sqlite3's documentation for
create_aggregate
. The argument count will be inferred by inspecting the referenced function's signature usinginspect
.After defining your custom aggregate, you can use it in any query as a normal aggregation:
Creating a Custom Function
To define a custom function, add a group
q_functions
to your project'ssetup.py
:The function referenced should meet the requirements defined by Sqlite3's documentation for
create_function
. The argument count will be inferred by inspecting the referenced function's signature usinginspect
.After defining your custom function, you can use it in any query as a normal function:
Creating a Custom Collation
To define a custom function, add a group
q_collations
to your project'ssetup.py
:The function referenced should meet the requirements defined by Sqlite3's documentation for
create_collation
.After defining your custom collation, you can use it in any query as a normal collation:
Installing Custom Aggregates, Functions, or Collations
Just install the package providing the custom aggregation using whatever method you normally use for installing third-party packages. This might be just running
pip install q-stdev
, or it might be runningpython setup.py install
orpip install .
from a package directory.I think this'll be helpful for people like me who might have specific needs of running specialized calculations via
q
without needing to submit a patch upstream.Cheers, and thank you for creating such a useful library that I use almost every single day.