{"id":260,"date":"2019-06-21T15:09:38","date_gmt":"2019-06-21T15:09:38","guid":{"rendered":"http:\/\/greenteapress.com\/wp\/?page_id=260"},"modified":"2019-06-21T15:09:38","modified_gmt":"2019-06-21T15:09:38","slug":"swampy","status":"publish","type":"page","link":"https:\/\/greenteapress.com\/wp\/swampy\/","title":{"rendered":"Swampy"},"content":{"rendered":"\n<p>Swampy is a suite of Python programs that support <em>Think Python<\/em>. It includes the following modules:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>AmoebaWorld: a fun way for beginning programmers to practice writing Python expressions.<\/li><li>TurtleWorld: an implementation of turtle graphics used in Think Python (first edition)<\/li><li>TurmiteWorld: an implementation of Langton&#8217;s ant.<\/li><li>Lumpy: a program that generates UML object and class diagrams from a Python program.<\/li><\/ul>\n\n\n\n<p>Code for Swampy is in this <a href=\"https:\/\/github.com\/AllenDowney\/Swampy\">repository on GitHub<\/a>.  If you have any problems using it, you can check the issues posted there or <a href=\"https:\/\/github.com\/AllenDowney\/Swampy\/issues\">create a new issue<\/a>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Installation<\/h4>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>If you don&#8217;t know how to install Python packages, you might find the following instructions frustrating.  I suggest you start by <a href=\"https:\/\/packaging.python.org\/tutorials\/installing-packages\/\">reading this<\/a>.<\/p>\n\n\n\n<p>To use Swampy, you need Python 3 with tkinter.  To see if you have tkinter, start Python and try to import it:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">>>> import tkinter<\/pre>\n\n\n\n<p>If you have tkinter, Python will not print an error message and you can go on to the next section.<\/p>\n\n\n\n<p>Otherwise you will see something like this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ImportError: No module named tkinter <\/pre>\n\n\n\n<p>In that case, you have to install tkinter. On Ubuntu, you can run<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo apt install python3-tk <\/pre>\n\n\n\n<p>Or use the Package Manager to install &#8220;python3-tk&#8221;.<\/p>\n\n\n\n<p>For other distributions, you can get more information from the\u00a0<a href=\"http:\/\/wiki.python.org\/moin\/TkInter\">Tkinter wiki<\/a>.<\/p>\n\n\n\n<p>To install Swampy, run<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo pip install swampy<\/pre>\n\n\n\n<p>To see if that worked, start Python and try to import it:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">>>> import swampy<\/pre>\n\n\n\n<p style=\"text-align:left\">If you have Swampy, Python will not print an error message and you can go on to the next section.<\/p>\n\n\n\n<p>Otherwise you will see something like this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ImportError: No module named swampy <\/pre>\n\n\n\n<p>In that case, something went wrong with the installation.  Once you have Swampy, you can try out the examples.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Swampy Examples<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">AmoebaWorld<\/h4>\n\n\n\n<p>To create AmoebaWorld, run the following code in Python<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from swampy.AmoebaWorld import AmoebaWorld, Amoeba<br># create the World<br>world = AmoebaWorld(interactive=True)<br>world.set_end_time('2 * math.pi')<br>world.set_x_t('10 * math.cos(t)')<br>world.set_y_t('10 * math.sin(t)')<br># create the amoeba<br>amoeba = Amoeba()<br># wait for the user<br>world.mainloop()<\/pre>\n\n\n\n<p>A window should appear.  Press the Run button.  The results should look like this:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"408\" height=\"553\" src=\"https:\/\/greenteapress.com\/wp\/wp-content\/uploads\/2019\/06\/Screenshot-at-2019-06-21-10-44-22.png\" alt=\"\" class=\"wp-image-261\" srcset=\"https:\/\/greenteapress.com\/wp\/wp-content\/uploads\/2019\/06\/Screenshot-at-2019-06-21-10-44-22.png 408w, https:\/\/greenteapress.com\/wp\/wp-content\/uploads\/2019\/06\/Screenshot-at-2019-06-21-10-44-22-350x474.png 350w, https:\/\/greenteapress.com\/wp\/wp-content\/uploads\/2019\/06\/Screenshot-at-2019-06-21-10-44-22-221x300.png 221w\" sizes=\"auto, (max-width: 408px) 100vw, 408px\" \/><\/figure>\n\n\n\n<p>If you type different expressions in the x(t) and y(t) fields, you can make the Amoeba follow different paths.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">TurtleWorld<\/h4>\n\n\n\n<p>To create TurtleWorld, run the following code in Python<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from swampy.TurtleWorld import TurtleWorld, Turtle<br>#create the World<br>world = TurtleWorld(interactive=True)<br># create the Turtle<br>turtle = Turtle()<br># wait for the user<br>world.mainloop()<\/pre>\n\n\n\n<p>A window should appear.  Press the Run File button.  The results should look like this:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"589\" height=\"431\" src=\"https:\/\/greenteapress.com\/wp\/wp-content\/uploads\/2019\/06\/Screenshot-at-2019-06-21-10-49-12.png\" alt=\"\" class=\"wp-image-262\" srcset=\"https:\/\/greenteapress.com\/wp\/wp-content\/uploads\/2019\/06\/Screenshot-at-2019-06-21-10-49-12.png 589w, https:\/\/greenteapress.com\/wp\/wp-content\/uploads\/2019\/06\/Screenshot-at-2019-06-21-10-49-12-350x256.png 350w, https:\/\/greenteapress.com\/wp\/wp-content\/uploads\/2019\/06\/Screenshot-at-2019-06-21-10-49-12-300x220.png 300w\" sizes=\"auto, (max-width: 589px) 100vw, 589px\" \/><\/figure>\n\n\n\n<p>Modify the code in the entry field in the lower right, then press Run Code.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">TurmiteWorld<\/h4>\n\n\n\n<p>To create TurmiteWorld, run the following code in Python<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from swampy.TurmiteWorld import TurmiteWorld, Turmite<br># create the World<br>world = TurmiteWorld()<br># create the Turmite<br>turmite = Turmite(world)<br># wait for the user<br>world.mainloop()<\/pre>\n\n\n\n<p>A window should appear.  Press the Run button.  The results should look like this:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"894\" height=\"631\" src=\"https:\/\/greenteapress.com\/wp\/wp-content\/uploads\/2019\/06\/Screenshot-at-2019-06-21-10-57-56.png\" alt=\"\" class=\"wp-image-264\" srcset=\"https:\/\/greenteapress.com\/wp\/wp-content\/uploads\/2019\/06\/Screenshot-at-2019-06-21-10-57-56.png 894w, https:\/\/greenteapress.com\/wp\/wp-content\/uploads\/2019\/06\/Screenshot-at-2019-06-21-10-57-56-350x247.png 350w, https:\/\/greenteapress.com\/wp\/wp-content\/uploads\/2019\/06\/Screenshot-at-2019-06-21-10-57-56-300x212.png 300w, https:\/\/greenteapress.com\/wp\/wp-content\/uploads\/2019\/06\/Screenshot-at-2019-06-21-10-57-56-768x542.png 768w\" sizes=\"auto, (max-width: 894px) 100vw, 894px\" \/><\/figure>\n\n\n\n<p>Modify the code in the entry field in the lower right, then press Run Code.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Lumpy<\/h4>\n\n\n\n<p>Lumpy is a tool for drawing diagrams that represent the state of a Python program.  Try the following example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from swampy.Lumpy import Lumpy<br># create a Lumpy object<br>lumpy = Lumpy()<br># capture reference state<br>lumpy.make_reference()<br># run the test code<br>x = [1, 2, 3]<br>y = x<br>z = list(x)<br># draw the current state (relative to the reference state)<br>lumpy.object_diagram()<\/pre>\n\n\n\n<p>A window should appear that looks like this:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"602\" height=\"582\" src=\"https:\/\/greenteapress.com\/wp\/wp-content\/uploads\/2019\/06\/Screenshot-at-2019-06-21-11-02-58.png\" alt=\"\" class=\"wp-image-265\" srcset=\"https:\/\/greenteapress.com\/wp\/wp-content\/uploads\/2019\/06\/Screenshot-at-2019-06-21-11-02-58.png 602w, https:\/\/greenteapress.com\/wp\/wp-content\/uploads\/2019\/06\/Screenshot-at-2019-06-21-11-02-58-350x338.png 350w, https:\/\/greenteapress.com\/wp\/wp-content\/uploads\/2019\/06\/Screenshot-at-2019-06-21-11-02-58-300x290.png 300w\" sizes=\"auto, (max-width: 602px) 100vw, 602px\" \/><\/figure>\n\n\n\n<p>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 &#8220;object diagram&#8221; for most Python code. Lumpy can also draw class diagrams.<\/p>\n\n\n\n<p>I hope you find Swampy useful.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Swampy is a suite of Python programs that support Think Python. It includes the following modules: AmoebaWorld: a fun way for beginning programmers to practice writing Python expressions. TurtleWorld: an implementation of turtle graphics used in Think Python (first edition) TurmiteWorld: an implementation of Langton&#8217;s ant. Lumpy: a program that generates UML object and class &hellip; <a href=\"https:\/\/greenteapress.com\/wp\/swampy\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Swampy<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-260","page","type-page","status-publish","hentry"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/greenteapress.com\/wp\/wp-json\/wp\/v2\/pages\/260","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/greenteapress.com\/wp\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/greenteapress.com\/wp\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/greenteapress.com\/wp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/greenteapress.com\/wp\/wp-json\/wp\/v2\/comments?post=260"}],"version-history":[{"count":2,"href":"https:\/\/greenteapress.com\/wp\/wp-json\/wp\/v2\/pages\/260\/revisions"}],"predecessor-version":[{"id":266,"href":"https:\/\/greenteapress.com\/wp\/wp-json\/wp\/v2\/pages\/260\/revisions\/266"}],"wp:attachment":[{"href":"https:\/\/greenteapress.com\/wp\/wp-json\/wp\/v2\/media?parent=260"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}