Skip to main content

stanford-karel

Python 3.7+ PyPI version Build Status pre-commit.ci status GitHub license Downloads

This is a Python implementation of Karel for Stanford's CS 106A. This package is available on PyPI and allows you to run Karel programs without any additional setup!

Huge props to @nick-bowman for rewriting this project from scratch!

StanfordKarel now supports:

  • Pip-installable package means you can run Karel programs from anywhere!
  • Solution code no longer needed to grade assignments - instead, the output worlds are compared.
  • Karel in ASCII! Plus autograder support.
  • Improved autograding, testing, linting, and auto-formatting.
  • Exception trace makes suggestions if you misspell a command (e.g. turnLeft() -> turn_left()).

Usage

pip install stanfordkarel

Documentation

Follow the Karel tutorial on the Karel Reader!

Running Karel

First, ensure that StanfordKarel is correctly installed using pip. Any .py file can become a Karel program!

collect_newspaper_karel.py

from stanfordkarel import *


def main():
    """Karel code goes here!"""
    turn_left()
    move()
    turn_left()


if __name__ == "__main__":
    run_karel_program()

Save the file and run:

python collect_newspaper_karel.py

Karel Program

Available Commands

Karel Commands
move() right_is_clear() facing_east()
turn_left() right_is_blocked() not_facing_east()
put_beeper() beepers_present() facing_west()
pick_beeper() no_beepers_present() not_facing_west()
front_is_clear() beepers_in_bag() facing_south()
front_is_blocked() no_beepers_in_bag() not_facing_south()
left_is_clear() facing_north() paint_corner(color)
left_is_blocked() not_facing_north() corner_color_is(color)

Folder structure

You can set a default world by passing a world name to run_karel_program, e.g. run_karel_program("collect_newspaper_karel")

Worlds should be saved/loaded in a worlds/ folder in the same folder as the file being run.

  • assignment1/
    • worlds/ (additional worlds go here)
      • collect_newspaper_karel.w
      • collect_newspaper_karel_end.w
    • collect_newspaper_karel.py

Creating Worlds

If using the pip-installed version, create a Python file containing:

from stanfordkarel.world_editor import run_world_editor

if __name__ == "__main__":
    run_world_editor()

Then run python world_editor.py.

World Editor

Grading

./autograde runs the available tests using pytest in the tests/ folder and prints out any output differences in the world.

Functionality

The tests use the student's code and the expected world output to determine correctness. If the output is not the same, the test driver will print out an ASCII representation of the differences.

Autograder

Style

The autograde command also runs the builtin Karel Style Checker that performs linting automatically.

Development

Everything important is located in stanfordkarel/.

  • Python 3.5+ is required because of importlib.util.module_from_spec.
  • Python 3.6+ is required for f-strings.
  • Python 3.7+ is required for type annotations.
  • stanfordkarel/ is the exported package, which contains all of the available functions and commands for students to use.
  • karel_application.py is responsible for loading student code and displaying it to the screen.

Contributing

All issues and pull requests are much appreciated!

  • First, run pip install pre-commit and pre-commit install.
  • To see test coverage scripts and other auto-formatting tools, use pre-commit run.
  • To run all tests, run pytest.

Future Milestones

In the future, I hope to add:

  • Automatic student style checking
  • Ways of determining the student's strategy or approach from observing Karel movements
  • Autograde more worlds, broken down by assignment
  • Allow students to autograde their own work
  • Accessibility for visually-impaired students

Metadata

Release files for stanfordkarel 0.2.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for stanfordkarel 0.2.7
File Size Uploaded
stanfordkarel-0.2.7.tar.gz 43.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for stanfordkarel 0.2.7
File Interpreter ABI Platform
stanfordkarel-0.2.7-py3-none-any.whl Python 3 none any Details

Total release size: 95.2 kB

Release files / stanfordkarel-0.2.7.tar.gz

Download URL stanfordkarel-0.2.7.tar.gz
Size 43.3 kB
Tags Source
SHA-256 checksum
How to use checksums
54f22ac198ef1db7af8e9557f1830f8acc220e001d7fb511d82849d4236ee5f5
BLAKE2b-256 checksum
How to use checksums
c2fb820f2da1222036286bda997b74aa40fb4d0bedd414667b7d0392c7318d87
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.10.0

Release files / stanfordkarel-0.2.7-py3-none-any.whl

Download URL stanfordkarel-0.2.7-py3-none-any.whl
Size 51.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
840b0fc759484631e81e2131de6932703677213f90c196d1e88920b429f921d0
BLAKE2b-256 checksum
How to use checksums
0cee65510dbb7dbaccb9f22763092a4731ef55c74fa5e7f3ccf6a6b84aaa519b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.10.0

Release history Release notifications | RSS feed

This release

0.2.7 This release

2 release files

0.2.6

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page