• Stars
    star
    4,934
  • Rank 8,427 (Top 0.2 %)
  • Language
    Python
  • License
    MIT License
  • Created over 14 years ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Python Koans - Learn Python through TDD

Python Koans

https://travis-ci.org/gregmalcolm/python_koans.png?branch=master https://img.shields.io/badge/Gitpod-ready--to--code-blue?logo=gitpod

One click installation:

or

Python Koans is a port of Edgecase's "Ruby Koans" which can be found at http://rubykoans.com/.

https://user-images.githubusercontent.com/2614930/28401740-ec6214b2-6cd0-11e7-8afd-30ed3102bfd6.png

Python Koans is an interactive tutorial for learning the Python programming language by making tests pass.

Most tests are fixed by filling the missing parts of assert functions. Eg:

self.assertEqual(__, 1+2)

which can be fixed by replacing the __ part with the appropriate code:

self.assertEqual(3, 1+2)

Occasionally you will encounter some failing tests that are already filled out. In these cases you will need to finish implementing some code to progress. For example, there is an exercise for writing some code that will tell you if a triangle is equilateral, isosceles or scalene.

As well as being a great way to learn some Python, it is also a good way to get a taste of Test Driven Development (TDD).

Downloading Python Koans

Python Koans is available on GitHub:

You can clone with Git or download the source as a zip/gz/bz2.

Installing Python Koans

Aside from downloading or checking out the latest version of Python Koans, you need to install the Python interpreter.

At this time of writing, we support Python 3. The policy is to try to keep current with the latest production version.

You should be able to work with newer Python versions, but older ones will likely give you problems.

You can download Python from here:

After installing Python make sure the folder containing the python executable is in the system path. In other words, you need to be able to run Python from a command console. It will either be python3 or for Windows it will be python.exe.

If you have problems, this may help:

Windows users may also want to update the line in the batch file run.bat to set the python path:

SET PYTHON_PATH=C:\Python39

Getting Started

Jake Hebbert has created a couple of screencasts available here:

https://www.youtube.com/watch?v=e2WXgXEjbHY&list=PL5Up_u-XkWgNcunP_UrTJG_3EXgbK2BQJ&index=1

Or if you prefer to read:

From a *nix terminal or Windows command prompt run:

.. code-block:: sh
python contemplate_koans.py

or:

python3 contemplate_koans.py

In my case I'm using Python 3 with Windows, so I fire up my command shell (cmd.exe) and run this:

https://user-images.githubusercontent.com/2614930/28401747-f723ff00-6cd0-11e7-9b9a-a6993b753cf6.png

Apparently a test failed:

AssertionError: False is not True

It also tells me exactly where the problem is, it's an assert on line 12 of .\\koans\\about_asserts.py. This one is easy, just change False to True to make the test pass.

Sooner or later you will likely encounter tests where you are not sure what the expected value should be. For example:

class Dog:
    pass

def test_objects_are_objects(self):
    fido = self.Dog()
    self.assertEqual(__, isinstance(fido, object))

This is where the Python Command Line can come in handy. In this case I can fire up the command line, recreate the scenario and run queries:

https://user-images.githubusercontent.com/2614930/28401750-f9dcb296-6cd0-11e7-98eb-c20318eada33.png

Sniffer Support

Sniffer allows you to run the tests continuously. If you modify any files files in the koans directory, it will rerun the tests.

To set this up, you need to install sniffer:

python3 -m pip install sniffer

You should also run one of these libraries depending on your system. This will automatically trigger sniffer when a file changes, otherwise sniffer will have to poll to see if the files have changed.

On Linux:

python3 -m pip install pyinotify

On Windows:

python3 -m pip install pywin32

Also available here:

https://github.com/mhammond/pywin32/releases

On macOS:

python3 -m pip install MacFSEvents

Once it is set up, you just run:

sniffer

Just modify one of the koans files and you'll see that the tests are triggered automatically. Sniffer is controlled by scent.py.

Getting the Most From the Koans

Quoting the Ruby Koans instructions:

"In test-driven development the mantra has always been, red, green, refactor. Write a failing test and run it (red), make the test pass (green), then refactor it (that is look at the code and see if you can make it any better). In this case you will need to run the koan and see it fail (red), make the test pass (green), then take a moment and reflect upon the test to see what it is teaching you and improve the code to better communicate its intent (refactor)."

Finding More Koan Projects

There are number of other great Koan projects out there for various languages and frameworks. Most of them can be found in GitHub. Also there is a little koans activity on Bitbucket.

Translations

Translations are always welcome! Feel free to add one to this README if you happen to work on one:

https://github.com/mswell/python_koans_br

Acknowledgments

Thanks go to Jim Weirich and Joe O'Brien for the original Ruby Koans that the Python Koans is based on! Also the Ruby Koans in turn borrows from Metakoans so thanks also go to Ara Howard for that!

Also thanks to everyone who has contributed to Python Koans! I got a great headstart by taking over a code base initiated by the combined Mikes of FPIP. So here's a little plug for their very cool Python podcast:

A big thanks also to Mike Pirnat @pirnat and Kevin Chase @kjc have pitched in as co-maintainers at various times

More Repositories

1

wacky-wandas-wicked-weapons-frontend

Simple js based storefront demo
JavaScript
14
star
2

unix_for_programmers_demo

Preparation for a Unix for Programmers talk
Python
8
star
3

guard-jasmine-coffeescript

Implemented instructions for Jasmine with Coffeescript through guard-coffeescript
Ruby
6
star
4

ed-materializer

Material prospecting API for use with Elite: Dangerous second party tooling
Ruby
5
star
5

elite-engineers-data

Elite Engineers JSON data store
JavaScript
3
star
6

ed-materializer-frontend

Web Frontend for ed-materializer api
JavaScript
3
star
7

elite-engineers

Prototype frontend app for managing Elite:Dangerous inventory
JavaScript
3
star
8

Webodoro-old

A simple web based tool for recording Pomodoro records
Ruby
2
star
9

ember-rails-movies-tutorial

Training material for ember training exercises
Ruby
2
star
10

choose-your-own-misadventure

A choose your adventure app
JavaScript
2
star
11

ember-beginner-workshop

Ember Workshop for Beginners
JavaScript
2
star
12

KataBankOCR

Working through Kata Bank OCR kata (C++ flavor)
C++
2
star
13

crb_member_site

Rails app for ColumbusRB member management and bragging.
Ruby
2
star
14

Javascript-ATM-Kata

This is a fancy little kata for building a fully functional ATM machine in javascript!
1
star
15

rails4demo

New Stuff in Rails 4 sandbox. Just so I have something to work with during a talk
Ruby
1
star
16

poker-kata

javascript kata
JavaScript
1
star
17

Barnacle

Rewrite of Elite:Dangerous BGS tooling
C#
1
star
18

gmconfig

My personal unix configurations
Shell
1
star
19

ember-sensei-cards-frontend

Learn Ember by building a Movies Database app, guided by agile like Sensei Cards
JavaScript
1
star
20

webodoro

Web based Pomodoro time management
Ruby
1
star
21

etech-presentations

slides and jottings for etech conference ruby presentaions
1
star
22

coffeescript_presentation

Coffeescript Presentation materials
JavaScript
1
star
23

IronPoker

IronRuby and Cucumber demo code
C#
1
star
24

chirp

Following @kitanata's Django tutorial at PyOhio 2012
Python
1
star
25

gmvim

Greg's VIM config
Vim Script
1
star
26

randori-20090615

Columbus.rb Randori-Fishbowl from 20090615
1
star
27

ember-sensei-cards-api

Database backend API for Ember Sensei Cards for cards and movies
JavaScript
1
star
28

making-movies

Exercise for building a nice frontend on top of a basic rails app
Ruby
1
star