Skip to main content

IPython custom cell magic to test Python and R code snippets typed into notebook cells.

Project description

README

IPython custom cell magic to test code snippets typed into notebook cells.

Installation

This package depends on IPython and RPy2, you may need to install them first.

Install from PyPI

As of 2022-07-01, tmagic is available at https://pypi.org/project/tmagic/. Install it as usual: pip3 install tmagic.

Install from BitBucket

To install the package for yourself as user:

pip install git+ssh://git@bitbucket.org/interquadrat/testmagic.git

You will need to have the appropriate credentials to get this working.

Install from a local repository

To get around BitBucket authentication issues, the trick is to install from the local Git repository.

Assuming that this local repo belongs to the user "teacher" that also has sudo rights, then install the package for all users like this:

sudo -H pip3 install git+file:///home/teacher/PROJECTS/training/testmagic

where you may need to modify the path to the local repo.

Preparation

Import the ExerMagic class and create an instance of it. This instance stores exercise name / expected value pairs, which can be passed either to the ExerMagic() constructor or to the register_tests() method, or be read from a JSON file.

In the example below we set up an exercise called "answer" so that the correct value generated at the end should be 42:

from tmagic import ExerMagic
magic = ExerMagic({"answer":42})

Python exercises

Start a notebook cell with %%pyexer <testname> where <testname> is the name of the test that you registered with the ExerMagic object ("answer" in the above example). Then add one or more Python statements to the cell. The last statement must evaluate to the "expected value" (42) registered with the "answer" test.

The following exercise will fail:

%%pyexer answer
a = 6
b = 8
a*b

When executing this cell, the output will be:

Test failed :-(
48

This, however, will succeed:

%%pyexer answer
a = 6
b = 7
a*b

producing the output:

Test passed :-)
42

R exercises

The R programming language exercises can be set up with the %%rexer cell magic. These cells "see" the results of previous notebook cells run with Rpy2's %%R magic. The above example in R would look like this:

%%rexer answer
a <- 6
b <- 7
a*b

The response would be:

Test passed :-)
array([42.0,])

due to the peculiarities of the R<->Python variable mapping.

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

tmagic-0.2.0.tar.gz (11.6 kB view details)

Uploaded Source

Built Distribution

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

tmagic-0.2.0-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

Details for the file tmagic-0.2.0.tar.gz.

File metadata

  • Download URL: tmagic-0.2.0.tar.gz
  • Upload date:
  • Size: 11.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.2

File hashes

Hashes for tmagic-0.2.0.tar.gz
Algorithm Hash digest
SHA256 d532c510f7d4d6f3fa0d1dbb88b2aa0e5eeadc6aae4d0b39d43db92711bd420f
MD5 af50a42dfc09fb19c447fb27137e2571
BLAKE2b-256 08c68482651ceb04e41a34215976f57e05b943b572c623e07fdee7b2432e3801

See more details on using hashes here.

File details

Details for the file tmagic-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: tmagic-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 12.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.2

File hashes

Hashes for tmagic-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ee3be848b300d002775bfc712d1fb7ab0b280d352aefbb55adb5f3120a39e91d
MD5 bc8da7203ad84af1c0f248666bea6f89
BLAKE2b-256 5024d07d6c6c60e634f6d6c0ed31cae1f0e69ac7947f6d808002fff6f3075fae

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