Swampy – Green Tea Press (original) (raw)

Swampy is a suite of Python programs that support Think Python. It includes the following modules:

Code for Swampy is in this repository on GitHub. If you have any problems using it, you can check the issues posted there or create a new issue.

Installation

In order to use Swampy, you will need to install Python packages. The details depend on what operating system you are running and how Python is installed on your system.

If you don’t know how to install Python packages, you might find the following instructions frustrating. I suggest you start by reading this.

To use Swampy, you need Python 3 with tkinter. To see if you have tkinter, start Python and try to import it:

import tkinter

If you have tkinter, Python will not print an error message and you can go on to the next section.

Otherwise you will see something like this:

ImportError: No module named tkinter

In that case, you have to install tkinter. On Ubuntu, you can run

sudo apt install python3-tk

Or use the Package Manager to install “python3-tk”.

For other distributions, you can get more information from the Tkinter wiki.

To install Swampy, run

sudo pip install swampy

To see if that worked, start Python and try to import it:

import swampy

If you have Swampy, Python will not print an error message and you can go on to the next section.

Otherwise you will see something like this:

ImportError: No module named swampy

In that case, something went wrong with the installation. Once you have Swampy, you can try out the examples.

Swampy Examples

AmoebaWorld

To create AmoebaWorld, run the following code in Python

from swampy.AmoebaWorld import AmoebaWorld, Amoeba

create the World

world = AmoebaWorld(interactive=True)
world.set_end_time('2 * math.pi')
world.set_x_t('10 * math.cos(t)')
world.set_y_t('10 * math.sin(t)')

create the amoeba

amoeba = Amoeba()

wait for the user

world.mainloop()

A window should appear. Press the Run button. The results should look like this:

If you type different expressions in the x(t) and y(t) fields, you can make the Amoeba follow different paths.

TurtleWorld

To create TurtleWorld, run the following code in Python

from swampy.TurtleWorld import TurtleWorld, Turtle
#create the World
world = TurtleWorld(interactive=True)

create the Turtle

turtle = Turtle()

wait for the user

world.mainloop()

A window should appear. Press the Run File button. The results should look like this:

Modify the code in the entry field in the lower right, then press Run Code.

TurmiteWorld

To create TurmiteWorld, run the following code in Python

from swampy.TurmiteWorld import TurmiteWorld, Turmite

create the World

world = TurmiteWorld()

create the Turmite

turmite = Turmite(world)

wait for the user

world.mainloop()

A window should appear. Press the Run button. The results should look like this:

Modify the code in the entry field in the lower right, then press Run Code.

Lumpy

Lumpy is a tool for drawing diagrams that represent the state of a Python program. Try the following example:

from swampy.Lumpy import Lumpy

create a Lumpy object

lumpy = Lumpy()

capture reference state

lumpy.make_reference()

run the test code

x = [1, 2, 3]
y = x
z = list(x)

draw the current state (relative to the reference state)

lumpy.object_diagram()

A window should appear that looks like this:

This diagram shows that x and y refer to the same list and z refers to a copy. You can use Lumpy to draw an “object diagram” for most Python code. Lumpy can also draw class diagrams.

I hope you find Swampy useful.