Skip to main content

Queries and code facts checking support for python source code

Project description

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 codequery to 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, codequery has 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").

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

codequery-0.1.2.tar.gz (15.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

codequery-0.1.2-py3-none-any.whl (14.6 kB view details)

Uploaded Python 3

File details

Details for the file codequery-0.1.2.tar.gz.

File metadata

  • Download URL: codequery-0.1.2.tar.gz
  • Upload date:
  • Size: 15.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for codequery-0.1.2.tar.gz
Algorithm Hash digest
SHA256 b3ebb2a554c89b1c5572507a3ab7bee93195018ddb4f87bd1c686b9a785a7f4e
MD5 563c29f01cca363ef5514803055984e9
BLAKE2b-256 74412ae0b16e0a14e4c3e46b27b9bc6db86bef0d2bc010d90c7963e3f992e5f8

See more details on using hashes here.

File details

Details for the file codequery-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: codequery-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 14.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for codequery-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1e44d7f84da2462e3cd9c0f7485a8977c4300d04516cbd05ea1c42c6e15d98c4
MD5 e8c97b3eb5e0c77dd4941c56a7f2a3b1
BLAKE2b-256 f66fec3a27beafc06a03d1d9a00331458de9c946dbc8d9c4394116520ed5d936

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page