codequery
A library to make it easy to write code queries, asserts on code facts and design tests for Python code. It provides a simple wrapper object with an easiear, more readable API to write queries on ast nodes as well as the tokenizer standard libraries.
BEWARE This is just a proof of concept and should be used with care in any professional context. My personal and primary use for this package is to check students code in the context of an introductory programming course. While automated tests do provide good feedback and guidance wrt to functional aspects of the code (whether it does what it is expected to do), they do not provide any feedback wrt to how the code has been written (and, in particular, wrt the way it was NOT expected to be). I use
codequeryto write easy to read and understand design tests that the students use to restrict the solution space, while still using a conventional testing framework and tooling (namely, pytest and unittests).
how to install
pip3 install codequery
how to use
TBD. However, the following examples should make it clear for simple cases. Soon I will add here a minimal documentation for each query method available in the API.
examples
The test below is used in a programming problem in which the
student is asked to implement an in-place bubble sort. It
prevents the module under test from: i) importing any modules;
ii) using the sort() method and the sorted() function (python
built-in functions); and iii) using methods that change lists in
structural way, to assure the algorithm is in-place (this forces
the programmer to manipulate the list as if it was a simple
array, namely, using only item assignments). The calls() method
receives a function (or class) name and returns a boolean stating
whether that function is called or not from that module.
from codequery import Module
mod = Module(filename="answer.py")
def test_is_inplace():
assert not mod.imported
assert not mod.calls("sorted")
assert not mod.calls(".sort")
assert not mod.calls(".append")
assert not mod.calls(".pop")
assert not mod.calls(".insert")
The test below could be used to complement a query-sort
implementation, to require that a recursive solution be written.
Observe the select() function being used. It receives either an
ast node type (or a string that matches the type name) and returns
all the elements in the tree rooted at the given node (in this
case, the whole module mod). I'll assume the assert line is
self-explanatory (the call() is the same in the example above).
def test_is_recursive():
functions = mod.select("FunctionDef")
assert any(f.calls(f) for f in functions)
The next test checks whether the module implements the python main
pattern and that it has a main() function, called from it.
def test_uses_main_section():
mainsect = next(e for e in mod.select("If") if e.get("left") == "__name__")
mainsect.calls("main")
In fact, as selecting the main section is such a common pattern,
codequeryhas a helper method that makes this code become simpler and more readable. The first line in the example above could be:mainsect = mod.select("main_section").
scripts
Two scripts that use codequery to analyze code scripts are also
provided: code-outline and code-profile, which are used to
create code outlines (a listing of the most relevant node
elements that give the code its outline structure) and code
profiles (a vector containing an ordered counting of the most
important elements in the code under analysis).
Release files for codequery 0.1.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| codequery-0.1.4.tar.gz | 17.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| codequery-0.1.4-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 36.4 kB
Release files / codequery-0.1.4.tar.gz
| Download URL | codequery-0.1.4.tar.gz |
|---|---|
| Size | 17.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
81d409cad1f216bca8a9cd10e5c6dec7c666dcfaf1afce6dd7088e98d54799a2
|
|
BLAKE2b-256 checksum How to use checksums |
6f629999b65d1dcab849081b957561a77bfb274d366f73ffe3b20739b4c7fef0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.10.5
|
Release files / codequery-0.1.4-py3-none-any.whl
| Download URL | codequery-0.1.4-py3-none-any.whl |
|---|---|
| Size | 18.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
42a14849360e4d3370f9d76e4276f04165221826d295cf12d35bd6983def4a0c
|
|
BLAKE2b-256 checksum How to use checksums |
10914b872fe3e50342ce90f738127f1641d7d0af00a103796bd18a329ef58093
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.10.5
|