Git Product home page Git Product logo

pythonwhat's Introduction

pythonwhat

Build Status PyPI version Documentation Status FOSSA Status

Verify Python code submissions and auto-generate meaningful feedback messages. Originally developed for Python exercises on DataCamp for so-called Submission Correctness Tests, but can also be used independently.

Installation

# latest stable version from PyPi
pip install pythonwhat

# latest development version from GitHub
pip install git+https://github.com/datacamp/pythonwhat

Demo

To experiment locally, you can use setup_state() and write SCTs interactively. The code throws an error when the underlying checks fail.

# make all checking functions available
from pythonwhat.test_exercise import prep_context
_, ctxt = prep_context()
globals().update(ctxt)

# initialize state with student and solution submission
from pythonwhat.test_exercise import setup_state
setup_state(stu_code = "x = 5", sol_code = "x = 4")

Ex().check_object('x')
# No error: x is defined in both student and solution process

Ex().check_object('x').has_equal_value()
# TestFail: Did you correctly define the variable `x`? Expected `4`, but got `5`.

# Debugging state
Ex()._state               # access state object
dir(Ex()._state)          # list all elements available in the state object
Ex()._state.student_code  # access student_code of state object

To learn how to include an SCT in a DataCamp course, visit https://instructor-support.datacamp.com.

Run tests

pyenv local 3.9.6
pip3.9 install -r requirements.txt
pip3.9 install -e .
pytest

Contributing

Bugs? Questions? Suggestions? Create an issue, or contact us!

License

FOSSA Status

pythonwhat's People

Contributors

beccarobins avatar bogdanfloris avatar ewaldbervoets avatar filipsch avatar fossabot avatar hermansje avatar jamesdanieloreilly avatar jayaddison avatar machow avatar nunorafaelrocha avatar timsangster avatar vvnkr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pythonwhat's Issues

Test if function arguments are there

It's not possible to test whether arguments in a function are used, without checking whether they have the correct value (evaluated or un-evaluated)

`test_with()`

test_with() works for some context managers, but it doesn't close properly in the SCT for others.

Scratch pad for pythonwhat

to demonstrate an issue that wasn't totally related to my course, I needed to create a branch and write an SCT there.

It would be great to have a place where we could write SCTs to demonstrate issues

Specify arguments explicitly inside `test_function()`

@hugobowne @vincentvankrunkelsven
Currently, test_function() tests all arguments when you don't specify the args argument.
The more I'm looking at it, however, it seems to be clearer, more straightforward to right and more intuitive to have SCT authors explicitly say which arguments and which keywords they want to check.
What do you think?

For backwords compatibilty, we can go through the SCTs that are live today, make the keywords and args explicit, put those live, and then rewrite test_function().

Correct submission throwing "Incorrect submission" -- block data June 23

in the following exercise, when the actual solution is submitted, it is NOT accepted AND an”Incorrect submission” is thrown (screenshot below):

https://campus.datacamp.com/courses/1167/2627?ex=13

The same occurs in here and a few more:

https://campus.datacamp.com/courses/1167/2627?ex=15

It's weird because there is not a problem in the following exercise and the SCT looks the same:

https://campus.datacamp.com/courses/1167/2627?ex=12

I think this is the SCT in question:

# Test: call to np.genfromtxt() and 'data' variable
test_correct(
    lambda: test_object("data"),
    lambda: test_function("numpy.genfromtxt")
)

screenshot 2016-06-08 12 34 29

All of this is currently on @franciscastro 's branch: https://github.com/datacamp/courses-importing-data-in-python/tree/review-francis

IPython magic shell commands

With IPython, you call execute Shell commands by prefacing them with !, e.g. ! ls for directory contents.

I use this in my course and Hamel will use it in his Cleaning Data in Python course.

We will need some test functions for this.

Testing user-defined generator functions: block beta testing 08/17

SCT needed for user-defined generator functions such as

# Create a list of strings
lannister = ['cersei', 'jaime', 'tywin', 'tyrion', 'joffrey']

# Define generator function get_lengths
def get_lengths(input_list):
    """Generator function that yields the
    length of the strings in input_list."""

    # Yield the length of a string
    for person in input_list:
        yield len(person)

User-defined functions already good so maybe just yield necessary?

testing lambda functions: block beta testing 08/17

can we test the definition and use of lambda function?

E.g.

# Define echo_word as a lambda function: echo_word
echo_word = (lambda word1, echo: word1 * echo)

and

# Create a list of strings: spells
spells = ['protego', 'accio', 'expecto patronum', 'legilimens']

# Use map() to apply a lambda function over spells: shout_spells
shout_spells = map(lambda item: item + '!!!', spells)

Make test_function robust to args vs keywords

Here, the solution code is
file = open('moby_dick.txt', 'r') and the SCT is
test_function("open", incorrect_msg="Pass the correct arguments toopen().")

The SCT accepts the solution code but does NOT accept
file = open('moby_dick.txt', mode='r') so there is something funky happening with keyword arguments.

If I change solution code file = open('moby_dick.txt', mode='r') , then the other one isn't accepted.

The only way I can get it to work is making solution code file = open('moby_dick.txt', mode='r') and using the SCT

test_correct(
    lambda: test_object("file", do_eval=False, undefined_msg="You don't have to change any of the predefined code."),
    lambda: test_function(
                "open",
                not_called_msg="You don't have to change any of the predefined code.",
                incorrect_msg="Did you pass the correct arguments to `open()`?"
            )
)

This SCT doesn't seem to work for the keyword arg if solution is positional.

My workaround seems to be working but we should figure out in detail the keyword/positional business in SCTs.

test_correct not working on sql_achemy function

https://campus.datacamp.com/courses/1167/2629?ex=4

SCT is currently this:

# Test: call to create_engine() and 'engine' variable
test_correct(
    lambda: test_object("engine"),
    lambda: test_function("sqlalchemy.create_engine")
)

see screenshot for error:

screenshot 2016-06-14 14 05 05

could it be a ploblem with importing like this?

from sqlalchemy import create_engine

this is a standard import so a workaround would be great.

this issue will block beta testing. please all feel free to suggest workarounds!

Use exception handling to improve flow

Each time a function like test_function() runs a test, it gets back a result. If this result is False, the function will be exited, with not result. This works, but it requires you to explicitly do this after each test. It would be better if asesrtions fail with a certain Error, so that it can be handled with exception handling. (SCTException).

test_student_typed not working

I have tried to reproduce this

*** =sct

test_student_typed(/sum\s*\(\s*range\s*\(/, not_typed_msg="You didn't use `range()` inside `sum()`.")
success_msg("Great job!")

from this page and it doesn't work.

See here and here.

I tried by putting quotations marks around the regex and still doesn't work

Move tests to inside package

According to these docs, the tests folder should be inside the package folder. That's also easier for the docker to test; currently the tests have to be fetched separately.

Style police

Make sure all files and tests use 4-space indentation

multi-arg custom feedback not working

hi :-)

I am able to replicate the round example with multi-args here

However, even in MY simplest cases, I cannot seem to get the multi-arg SCTs to work :-(

  • This exercise with open (the SCT responds 'first arg wrong!' when 1st correct and 2nd wrong): here is the branch
  • This exercise wrt np.loadtxt (solution code now not accepted: 'list index out of range'): here is the branch

Test methods and chained methods

Let's say that the solution code has

tables = connection.execute(stmt).fetchall()

How would we test that the correct methods are applied in the correct order? Can test_function do this?

In our sqlalchemy course, this will be necessary in more than 50% of the exercises.

Note: my intern Vincent is currently working on these SCTs and has tried test_student_typed but it isn't quite working. See this issue. any suggestions?

Syntax Error Diagnostics and Suggestions

If the student submits code that contains a syntax error, the student will get to see rather cryptic Python interpreter information, that is not necessarily helpful for the student.

Similar to testwhat, we'll want a tool that wraps around syntax error, providing more diagnostics.
This is a tough nut to crack.

Testing comprehensions: block beta testing 08/17

We'll need to test comprehensions (list/dict/set); e.g. for

doctor = ['house', 'cuddy', 'chase', 'thirteen', 'wilson']

we will need an SCT for

x = [doc[0] for doc in doctor]

Also conditional comps such as

x = [doc[0] for doc in doctor if doc[1] == 'h']

and nested comps! ;-)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.