Author Archives: coen

A simple plot with Python and Bokeh

Python is a simple but powerful language, and comes with a wealth of libraries. The chart above took just 9 lines of Python. All the hard work is done by the Bokeh library. It shows the chart in your browser, where you can zoom in and move around the chart.

Here is the annotated code. You can find the raw code at the end of this post or at the GitHub repository

Before installing Bokeh, to keep your Python version(s) clean, you may want to set up a virtual environment first

To install Bokeh: pip install bokeh

1. from bokeh.plotting import figure, show
Import part of bokeh, so we can create and show a figure

2. import math
We’ll use the math module to generate the points on the charts

3. x_values = range(0, 720)
The x axis contains the numbers from 0 to 719 (Python stops just before 720)

4. y_values = [math.sin(math.radians(x)) for x in x_values]
For each of the x values, the y value is sine of x. Python’s sin function expects the angle in radians rather than degrees. math.radians converts from degrees to radians.

We use something called ‘list comprehension’ here, to build up the list of y axis values. It creates a new list, which consists of the sine of each x (converted from degrees to radians) in the original list.

5. p = figure(title=’10 Sine waves’, x_axis_label=’x (degrees)’, y_axis_label=’y = sin(x)’, plot_width=850, plot_height=350)
Create an empty Bokeh figure, and set the title, labels, width and height

6. for i in range(10):
We’re drawing the same sine curve 10 times, at 10%, 20%, … 100% of the full height

For i is 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, do the following:

7.     factor = 1 – i/10
Calculate the scaling factor, as (1 – 0/10) = 0, (1 – 1/10) = 0.9, (2 – 2/10) = 0.8, 0.7, … 0.1

8.     p.line(x_values, [y * factor for y in y_values])
Add a line to the figure, using the original list of x_values, but scale down the y_values by the current factor

9. show(p)
Ask Bokeh to show the result in your browser

1. from bokeh.plotting import figure, show
2. import math
3. x_values = range(0, 720)
4. y_values = [math.sin(math.radians(x)) for x in x_values]
5. p = figure(title='10 Sine waves', x_axis_label='x (degrees)', y_axis_label='y = sin(x)', plot_width=850, plot_height=350)
6. for i in range(10):
7.     factor = 1 - i/10
8.     p.line(x_values, [y * factor for y in y_values])
9. show(p)

Use Python to update a spreadsheet

How would you like to grab a share price daily and store it in a spreadsheet? Or add a new column to dozens of spreadsheets – automatically?

Python is a simple but powerful language, and comes with a wealth of libraries. Its openpyxl library lets you easily open a spreadsheet and make some changes.

Here is an example which adds a new column (“Next age”) to all spreadsheets in the source_folder. The left side of the image above shows an original spreadsheet. The Python script opens this, adds a new column (Next age), then saves it to the target_folder. The right side of the image shows the result

Here is the annotated code. You can find the raw code at the GitHub repository

Before installing openpyxl, to keep your Python version(s) clean, you may want to set up a virtual environment first

To install openpyxl: pip install openpyxl

1. import openpyxl
2. import os
3. for name in os.listdir('source_files'):
4.     workbook = openpyxl.load_workbook(filename='source_files/' + name)
5.     sheet = workbook['Sheet1']
6.     sheet['C1'].value = 'Next age'
7.     for row in range(2, 100):
8.         if sheet[f'B{row}'].value:
9.             sheet[f'C{row}'].value = sheet[f'B{row}'].value + 1
10.     workbook.save(filename='target_files/' + name)

1. import openpyxl
Load the openpyxl library.

2. import os
Load the os library. We will use this list the files in a folder

3. for name in os.listdir(‘source_files’):
For each file in our ‘source_files’ folder. Note that this includes all files, regardless of whether it is a spreadsheet or not

4.     workbook = openpyxl.load_workbook(filename=’source_files/’ + name)
Open the workbook

5.     sheet = workbook[‘Sheet1’]
Take the worksheet called ‘Sheet1’

6.     sheet[‘C1’].value = ‘Next age’
Enter something in cell C1

7.     for row in range(2, 100):
For rows 2 – 99 (Python stops just before reaching 100), do the following:

8.          if sheet[f’B{row}’].value:
If cell B2, B3, B4, etc is not empty, do the following:

9.               sheet[f’C{row}’].value = sheet[f’B{row}’].value + 1
Take the age from column B, add one to it and store in the cell to the right, i.e. in column C

10. workbook.save(filename=’target_files/’ + name)
Save the updated workbook to the target_files folder, using the same name

1/2 + 1/3 = 1/6

Fractions in Python

When you ask your spreadsheet to calculate 1/2 + 1/3 you get something like this:
This is obviously an approximation. The 3’s after the decimal point repeat indefinitely.

The correct answer is:

  • 1/2 = 3/6
  • 1/3 = 2/6
  • 1/2 + 1/3 = 3/6 + 2/6 = 5/6

Python is a simple but powerful language, and comes with a wealth of libraries. Its Fractions library gives you the correct answer in a couple of lines

Here is the annotated code. You can find the raw code at the GitHub repository

1. from fractions import Fraction
Load the Fractions library

2. half = Fraction(‘1/2’)
3. third = Fraction(‘1/3’)
Create the two fractions

4. total = half + third
Add them up

5. print(half, ‘+’, third, ‘=’, total)
Show the result.
The more modern way is to use an “f-string”, which was introduced in Python 3.6, December 2016. This is often more readable, but not here. It would look like this:
print(f'{half} + {third} = {total}’)

Sample chart

Retrieve and display a data set

(First part of the “Practical Python in 10 lines or less” series)

Python is a simple but powerful language, and comes with a wealth of libraries. The chart above took just 10 lines of Python. All the hard work is done by the Pandas and MatPlotLib libraries.

The code

import pandas, matplotlib
data = pandas.read_csv('http://www.compassmentis.com/wp-content/uploads/2019/04/cereal.csv')
data = data.set_index('name')
data = data.calories.sort_values()[-10:]
ax = data.plot(kind='barh')
ax.set_xlabel('Calories per serving')
ax.set_ylabel('Cereal')
ax.set_title('Top 10 cereals by calories')
matplotlib.pyplot.subplots_adjust(left=0.45)
matplotlib.pyplot.show()

How it works

You will need Python and the Pandas and MatPlotLib libraries. See the installation instructions

Get started

1. import pandas, matplotlib
Grab the libraries we need to load, clean up and display the data.
The recommended approach (PEP 8) is to have two import statements on separate lines. To leave enough lines to make the chart look good, in this example I have combined them.

2. data = pandas.read_csv(‘http://www.compassmentis.com/wp-content/uploads/2019/04/cereal.csv’)
Load the csv data from a website. This gives us a pandas DataFrame, a two dimensional datastructure similar to a page in a spreadsheet.
I downloaded the data from https://www.kaggle.com/crawford/80-cereals/version/2, under Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0) [https://creativecommons.org/licenses/by-sa/3.0/]

3. data = data.set_index(‘name’)
Set the row names (index) to the ‘name’ column. When we plot the data this becomes the data labels.

4. data = data.calories.sort_values()[-10:]
Take the ‘calories’ column, sort it and limit to the last 10 values. This gives us the 10 cereals with the highest calories per serving

5. ax = data.plot(kind=’barh’)
Plot the data as a horizontal bar chartax.set_xlabel(‘Calories per serving’)

6. ax.set_ylabel(‘Cereal’)
7. ax.set_title(‘Top 10 cereals by calories’)
8. ax.set_xlabel(‘Area in millions square kilometers’)

Set the label for the x and y axes and the title

9. matplotlib.pyplot.subplots_adjust(left=0.45)
Set the left margin (from the left of the image to the left of the chart area) to 45% to give enough space for the cereal names.

10. matplotlib.pyplot.show()
Show the chart

Getting started with Python for Scientific Computing

So you’d like to do some data analysis or other scientific computer with Python. How do you start?

The Anaconda distribution

A Python ‘distribution’ is a bundle of Python goodies, typically Python itself, a set of Python libraries and possibly an integrated development environment.

Anaconda is a Python distribution specifically for data science. It includes the most popular data science and machine learning Python packages, Jupyter for quick exploratory data analysis and Spyder for creating and running Python scripts.

For more information and to install Anaconda go to the Anaconda Distribution page

Jupyter Notebook

A Jupyter notebook lets you try out different Python commands and create a story which shows your steps and the results. For instance:

Once you have installed Anaconda, or otherwise installed Jupyter:

  1. Open a Terminal or Command Prompt
  2. jupyter notebook
  3. Jupyter will open in your browser
  4. Click on the ‘New’ button (right hand side), and select ‘Python 3’
  5. Start typing
  6. To execute a cell, hit Ctrl-Enter
  7. Jupyter automatically saves the notebook. Click on the title (top left hand corner, next to Jupyter logo) to give it a sensible name

Getting started with Python

So you’d like to give Python a go. How do you start?

(If you are going to be using Python for Scientific Computing, including Data Analysis, have a look at this article instead)

Installing Python

Make sure you install Python 3, which is the modern version of Python. There is also a legacy version of Python, Python 2.7, but this is being phased out and should not be used for new projects.

You can find installation files for Windows and Mac OSX at https://www.python.org/downloads/. When you start the installation on Windows there will be an option to add Python to the system path. I recommend you select this option, as it makes it easier to run your Python scripts. I have not tried this on Mac OSX; it may have the same option.

For Linux you can use your software package manager, such as aptitude, yum or zypper to install ‘python3’. This will give you Python 3

Running Python – REPL/Console

For trying out some simple Python commands you can use the Python Console. This is also called the REPL (Read, Execute, Print Loop). To start the Python Console, just run Python. This will give you something like this:

Have a little play with this. For instance:

When you are done, press ^Z (Windows) or ^D (Mac OSX and Linux). Or enter ‘exit()’

Running Python – IDLE editor

The console is great for quick experiments. For anything more permanent it is better to create a script, a text file which contains Python code. When you installed Python it came with IDLE, a very simple integrated development environment.

Start IDLE from your operating system’s menu. You will see something like this:

Now select File, New File. Enter some Python commands, like:

Hit ‘F5’ to run the program. You will be prompted to save the file first, so give it a name and save it. You will see the result of your script in the original (shell) window:

Running a Python script from the command line

Say you’ve written a Python script, or someone else has given you a script. How do you run it?

  1. Start a Terminal or (as Windows calls it) a Command Prompt.
  2. Use the ‘cd <path to folder>’ command to go to the folder which contains the script
  3. Enter: ‘python <scriptname>.py’. For instance: ‘python test.py’

Other editors

IDLE is great for getting you started quickly, but for any serious Python development I suggest you use a professional text editor or IDE (Integrated Development Environment). Both a text editor and an IDE let you create and edit text files. An IDE can also run, debug, test and more. For instance:

  • PyCharm. My favourite IDE. It gives you so much power to write, run, debug and test your scripts, I don’t know where to start. Just check it out at …. Start with the free Community edition.
  • Visual Studio Code. I hear good things about this IDE, and it recently became more popular than PyCharm, so it must be doing something right.
  • Sublime Text. An excellent text editor

Data Analysis with Python

Python is a very popular tool for data extraction, clean up, analysis and visualisation. I’ve recently done some work in this area, and would love to do some more. I particularly enjoy using my maths background and creating pretty, clear and helpful visualisations

  • Short client project, analysing sensor data. I took readings from two accelerometers and rotated the readings to get the relative movement between them. Using NumPy, Pandas and MatplotLib, I created a number of different charts, looking for a correlation between the equipment’s setting and the movement. Unfortunately the sensors aren’t sensitive enough to return usable information. Whilst not the outcome they were hoping for, the client told me “You’ve been really helpful and I’ve learned a lot”
  • At PyCon UK (Cardiff, September 2018) I attended 14 data analysis sessions. It was fascinating to see the range of tools and applications in Python data analytics. At a Bristol PyData MeetUp I summarised the sessions in a 5 minute lightening talk. This made me pay extra attention and keep useful notes during the conference
  • Short client project, researching best way to import a large data set, followed by implementation. The client regularly accesses large datasets using a folder hierarchy\to structure that data. They were looking to replace this with a professional database, i.e. PostgreSQL. I analysed their requirements, researched the different storage methods in PostgreSQL, reported my findings and created an import script.

Django Rest Framework API Microservice

I recently completed a small project for Zenstores. They simplify the shipping process for ecommerce sites. Their online service lets online businesses use multiple shipping companies for deliveries.

Each shipping companies offers a different own API, for booking shipments, etc. My client uses a separate microservice for each shipping company. These microservices listen to requests from the main system and translate them to the shipping company’s standard.

My client asked me to use Django Rest Framework to create a microservice which supports a new shipping company. DRF is a popular and powerful library to create RESTful APIs using Django.

The supplier provided me with a sandbox API and extensive documentation. The documentation was somewhat incomplete and out of date. Fortunately their support contact was very helpful all along.

I used Test Driven Design for complex functions where I understood the functionality well. For the rest I used a more experimental approach and added unit tests afterwards. Testing coverage was over 90{d34bf16ac7b745ad0d2811187511ec8954163ba9b5dbe9639d7e21cc4b3adbdb}.

The client has integrated the microservice within their system and the first test shipments have gone through.

Teaching Python

Recently Learning Tree, a well-respected training company, invited me to teach Python for them. Last week I delivered my first course for them, their Advanced Python course

A room full of people, nearly 500 slides, about 10 step-by-step practical exercises and four days to make sure every left with a better understanding of Python

Even though I’ve been programming in Python for 6 years, I still don’t know it all. The language itself is constantly growing, there are 150,000+ open source Python packages, and only so many bytes of storage in my brain. In preparation I read through the slides, and looked up anything which i wasn’t fully clear on myself. I was pleasantly surprised by how much I do know

And, on the flip side, I added some of my own experiences whilst delivering the slides, adding some depth and flavour to the course

I made sure to regularly check the delegates’ understanding, and to fine tune my delivery. I’ve yet to receive a compilation of the feedback but, as far as I can tell, everyone made good progress and enjoyed it.

 

How I’m learning French

Or, how to learn without studying

Introduction

I love learning, but I don’t like studying. Take for instance learning a foreign language. There are many ways to do this, including “studying”: studying the grammar, rote learning words and reading literature. There is nothing wrong with studying, if that works for you. It just isn’t for me.

Instead I am learning French a bit like I learned my first and languages (Dutch and English) – by a lot of natural exposure and use in my daily life, not as a separate activity. I have added a French “flavour” to many of my day to day activities

Music

Listen to French music. Particularly with the internet, you should be able to find some music you like. I attribute my love of French music to a French musical we were shown on video in secondary school (Michael Fugain et le Big Bazar). I got the album and have played it over and over again. Little by little I’m picking out (and learning) more and more words

I often listen to music whilst I’m working. Some days I’ll hear three or more hours of French music. I’ve collected quite a few French CDs and downloads, listen to a French online radio station (e.g. Chante France) or to French musicians on Spotify or YouTube

For a while I even collected (as downloads and as a playlist) French version of songs I already knew in Dutch or English. Because I already know the lyrics, it is easier to make sense of the French lyrics

There is a great website LyricsTranslate where people submit song lyrics in the original language and others translate them. So you can find many French song lyrics with an English translation. It also has YouTube videos, so you listen to the French words and try to read along with the French or English lyrics

Also on YouTube, you can find many French songs with French and English subtitles. Listen to the French lyrics and read the subtitles

Movies

Watch movies with French audio. I love French movies. Many have a different pace, a bit slower and more thoughtful, than a Hollywood super hero blockbuster. This also makes it a bit easier to hear and understand the dialogue. My favourite French director, with lots of French dialogue, is Eric Rohmer.

When in France I look out for second hand DVDs, especially movies that I really want to watch. It shouldn’t become a chore. Ideally they should have subtitles. Some streaming services (e.g. Netflix) let you choose the subtitles and audio language for some of movies and programmes.

I  watch the following

  • French movies with English subtitles – as I read the English subtitles I try to hear how you say it in French
  • French movies with French subtitles – I find it easier to understand written French than spoken French, so this way I can more or less follow the story whilst practicing my listening skills
  • French movies without any subtitles – I still miss a lot whilst doing this, but it is good practice from time to time. And I may watch the movie a second time, with subtitles, to see what I’ve missed or misunderstood
  • English movies with French subtitles. Many of my favourite movies are in English. Ilisten to the English and see how the French say the same thing
  • For something really multinational I watch Ultimate Beastmaster on Netflix. Athletes from 6 different countries compete on an obstacle course. Each country has its own commentators. With French subtitles I get the US and UK commentators speaking in English with French subtitles, French commentators speaking in French with no subtitles, and some other languages I don’t understand but with French subtitles

Reading

Read French. I love reading – but it has to be something I’m interested in. Reading a boring French children’s book just to learn French doesn’t do it for me

Looking up words as i read doesn’t excite me either. It kills the joy of reading for me. Sometimes I get curious and look up a few words

How do you read interesting French when you just get started

  • Follow some French people or groups on Facebook, like Topito, or the Facebook page of a French town you’re visiting on holiday. If, like me, you spend too much time on Facebook, at least you’ll start picking up some French words
  • Switch your computer and/or mobile phone to French. But write down how you did it, so you can switch back later. There are many different settings you can change: your browser (so Google will return French websites), your operating system (so things like “open” and “save” will be in French), your Facebook, Twitter, etc, settings, so your “wall” becomes your “mur” (French for “wall”) etc. Or your phone, and your GPS directions may now be in French – maybe not as helpful but quite fun, in particular when the French lady starts mis-pronouncing the English road names
  • Read the French version of some of your favourite books. For instance, I’ve read The Lord of the Rings many times, and I know the story well. This helped when I started reading it in French. I don’t have to worry about losing the plot, and can just skip over words I don’t know or sentences I don’t understand
  • Try out different books. If you can’t get into a certain book, just put it aside and try another one. Again, second hand book shops and market stalls (in France) are very good for this. I’ve bought books for 1 euro. I’ve got over 50 unread books, which gives me plenty of choice
  • Or try your local library. Many libraries have a foreign literature section
  • Comic books are good too. The pictures help you to understand the story

Podcasts

Listen to podcasts. I’m a great fan of podcasts. I’ll listen to them whilst out running, doing the dishes and other chores, going off to sleep, doing some finger exercises on the guitar, and even whilst flossing my teeth. Here are some recommendations

  • Coffee break French. I started with this one. They have an archive with four seasons, from absolute beginners to advanced, so pick your level
  • Learn French by Podcast. Their lessons pack a lot in a short podcast. They cover many practical topics (e.g. how to talk about yourself). 195 podcasts (and still going), some of them very topical (politics, science, society)
  • Journal en Français facile. The (French) news in easy French. 10 minutes daily news

More

And a few more ideas

  • Visit the country
  • Immerse yourself in the culture
  • Make French friends, stay in touch on Facebook or whatever you use
  • If you play a musical instrument or sing, learn some French songs. I’ve even taken some French+guitar lessons with Cécile, a French singer/songwriter whose songs I really enjoy
  • Here in Bristol we’ve got some French singing workshops, which I’ve found very enjoyable – particularly because, as I’ve already mentioned, I love French songs
  • Find your local French Meetup groups