|
| 1 | +Code and Solutions Refresh |
| 2 | +########################## |
| 3 | + |
| 4 | +:date: 2026-11-03 08:20 |
| 5 | +:category: updates |
| 6 | + |
| 7 | +It's been 11 years, since **Doing Math With Python** was published. The world of software |
| 8 | +learning has changed drastically. I hope you are still continuing to learn using books, writing |
| 9 | +programs and the educators among you, i hope you are doing the same! We must persist. |
| 10 | + |
| 11 | +The book has also ended up being part of the `Anthropic copyright settlement <https://echorand.me/posts/dmwp-and-ai-notes/>`__. |
| 12 | +I think it's just a sign of the times and I have become part of the history it appears! |
| 13 | + |
| 14 | +Alright, so, I am going to be doing a bit of a refresh as needed for the various code in the book and solutions |
| 15 | +to ensure that they are all working with currently latest versions of the `Python` |
| 16 | +programming language and the third party packages that's used for the code: `sympy`, `matplotlib` |
| 17 | +and `matplotlib_venn`. |
| 18 | + |
| 19 | +This will take a while, so here's the software versions i will be working with: |
| 20 | + |
| 21 | +1. Python 3.14 |
| 22 | +2. sympy 1.14.0 |
| 23 | +3. matplotlib 3.11.0 |
| 24 | +4. matplotlib_venn 1.1.2 |
| 25 | + |
| 26 | +*This post is in progress* |
| 27 | + |
| 28 | +Chapter 2 |
| 29 | +---------------------- |
| 30 | + |
| 31 | +Programming challenges |
| 32 | +~~~~~~~~~~~~~~~~~~~~~~ |
| 33 | + |
| 34 | +2. How does the temperature vary during the day? |
| 35 | + |
| 36 | +One solution I suggested created the two lists with the data manually filled in from |
| 37 | +the google search results. |
| 38 | + |
| 39 | +The second solution, (file name: ``nyc_forecast_owm.py``) I proposed uses the `pyowm` |
| 40 | +package to download the data from open weather map. A reader wrote to me recently that |
| 41 | +it doesn't work any more. |
| 42 | + |
| 43 | +I took a look at the proposed solution and realized that the API itself has changed, which means |
| 44 | +the library `pyowm` had also changed significantly. Not unexpected, since it's been 11 years. |
| 45 | + |
| 46 | +So, this is the new proposed solution to use the real data: |
| 47 | + |
| 48 | +1. No need to use a third party package |
| 49 | +2. Download the CSV file from a new source |
| 50 | +3. Read the CSV file data to create the graph |
| 51 | + |
| 52 | +I suppose my original intention for asking the reader to use a third party package might have been |
| 53 | +an attempt to stretch the enterprising reader. However, I don't agree with that intention any more |
| 54 | +as demonstrating how to use a third party data provider isn't in the scope of the book. So, I am |
| 55 | +taking it out. |
| 56 | + |
| 57 | +I have created a new web application dedicated for this exercise so that you can download the data |
| 58 | +in CSV data format - something you learn in the book in Chapter 3 (next chapter). |
| 59 | + |
| 60 | +**Reading the CSV file requires the reader to refer to Chapter 3** |
| 61 | + |
| 62 | +Here's my proposed updated solution. |
| 63 | + |
| 64 | +First download the data by: |
| 65 | + |
| 66 | +1. Visit: https://doingmathwithpython.ai.studio/ and sign up (you don't provide email) |
| 67 | +2. Next sign in using the username and password |
| 68 | +3. By default New York is selected and default is also selected to 1 day |
| 69 | +4. On the top right you can change the temperature units |
| 70 | +5. Click on ``Download CSV data`` |
| 71 | + |
| 72 | + |
| 73 | +.. figure:: {filename}/images/download-forecast-data.png |
| 74 | + :align: left |
| 75 | + :alt: Download Forecast Data |
| 76 | + :scale: 80% |
| 77 | + |
| 78 | + |
| 79 | +A file will be downloaded which will have the data in the following format:: |
| 80 | + |
| 81 | + "Local Date & Time","Temperature" |
| 82 | + "2026-11-03 19:00","63.1" |
| 83 | + "2026-11-03 22:00","60.3" |
| 84 | + "2026-11-04 01:00","58.5" |
| 85 | + "2026-11-04 04:00","57.6" |
| 86 | + "2026-11-04 07:00","56.5" |
| 87 | + "2026-11-04 10:00","63.7" |
| 88 | + "2026-11-04 13:00","68.7" |
| 89 | + "2026-11-04 16:00","67.6" |
| 90 | + |
| 91 | +Rename the file to ``weather_New_York_1d_3h.csv`` and place in the same directory as the program. |
| 92 | + |
| 93 | +Here's the plan of what we will need to do: |
| 94 | + |
| 95 | +1. Ignore the first line (CSV header row) |
| 96 | +2. Read each row, creating one list for the time of the day and a second list for the temperature |
| 97 | +3. Create our graph |
| 98 | + |
| 99 | +You can find the updated solution `here <https://github.com/doingmathwithpython/code/blob/master/chapter2/solutions/nyc_forecast_real.py>`__. |
0 commit comments