What I learned from writing three Python books in a year

Image for post
Image for post
Photo by Eugenio Mazzone on Unsplash

Over the past year or so, I have written three e-books on various topics in Python programming. Although I certainly don’t consider myself to be an expert on self-publishing, I have learned a bit in this time, so I will share it here. I hope you might find it useful.

This article covers four stages:

  • Choosing and researching the topic.
  • Writing the manuscript.
  • Making it available for sale.
  • Generating sales.

I have found the whole process takes me about 3 months, if I put most of my hobby time into it. …

Process images in 3 lines of Python

Image for post
Image for post

Pillow is a versatile and popular Python imaging library. The ImageEnhance module is a particularly useful module within Pillow. It can be used to adjust colour, contrast, brightness and sharpness in a few simple lines of code.

_Enhance interface

Each image enhance function is implemented as an object that implements the _Enhance interface. The interface provides a single method:


For example, the ImageEnhance.Brightness class controls the brightness of an image, It has an enhance method that it inherits from _Enhance.

factor is a number that controls the effect. For example, for Brightness a factor of less than 1.0 makes the image darker (and a value of 0.0 makes the image completely black). A factor of greater than 1.0 makes the image brighter. A factor of exactly 1.0 …

Easy animation with generativepy

Image for post
Image for post
Photo by Gensa Hub on Unsplash

generativepy makes it very easy to create animated GIFs. This can be done with a simple script, and usually results in a fairly well optimised GIf file.

The gif module makes use of the gifsicle application. This can be downloaded for free, and must be installed on your system in order for the gif module to work.

GIF images

GIF (Graphics Interchange Format) is a bitmap image format that was developed in the 1980s for transferring images over networks. …

Deriving new functions with functools

Image for post
Image for post
Photo by Matthew Henry on Unsplash

Functional programming is a paradigm in which functions form the fundamental building blocks.

It should be no surprise that functional programming includes several techniques for deriving new functions from existing functions. This is analogous to object oriented programming, where classes are the building block, and we have various ways to derive new classes from existing ones (inheritance and composition, for example). In both cases, the aim is the same — to reuse existing code (following the DRY principle, don’t repeat yourself).

Partial application is one such technique.

What is partial application?

Here is a function that returns the value of x, clamped to the range a to b. That is, it returns the value of x unless x is less than a (in which case a is returned), or x is greater than b (in which case b is…

There are various ways of testing software, and unit testing is a vital part of the mix.

Image for post
Image for post
Photo by Jeswin Thomas on Unsplash

It is, of course, vital to test software. There are various ways to test software, including:

  • Testing that the individual software components of the system work correctly. This can include anything from testing an entire sub-system, or testing individual modules.
  • Testing the whole system — this can take the form of integration testing, end-to-end testing, and functional testing, etc, that each emphasises different aspects of system testing.
  • More specialised forms of testing — for example, acceptance tests are often used as part of a development contract to prove that the software is fit for purpose, performance testing proves that the software has the capacity (including processing power, memory and disk space and so on) to do its job, and penetration testing to check the system is secure against hackers. …

Simple implementations of the List and Maybe monads.

Image for post
Image for post
Photo by marc liu on Unsplash

In the article on the Failure monad we looked at a simple definition of what a monad is:

A monad is a design pattern that allows us to add a context to data values, and also allows us to easily compose existing functions so that they execute in a context aware manner.

In this article we will look at two other common monads:

  • List monads
  • Maybe monads

List monads

A List monad allows us to process lists of values, without cluttering our code with looping constructs.

How a List monad is used

As with the Failure monad, our List monad should do three main…

A simple implementation of the Failure monad.

Image for post
Image for post

Image by Safijc

Monads have a reputation for being difficult to understand, but in this article we will look at a simple Python implementation of a simple monad. This will hopefully give some insight into what monads are all about.

What is a monad

In functional programming, we use functions as our primary building block, and function composition as one of the basic ways to structure our code. By function composition we mean functions that call other functions, like this:

y = str(neg(int(x)))

This composed function takes a string, converts it to an integer, negates it, and converts it back to a string. …

Converting individual markdown pages to html.

In a previous article we looked at what static sites are, and how they work.

Now we will look at how to convert a single markdown file into an HTML file.

The conversion process

This diagram from the previous article shows the basic process for converting a set of markdown files into the required HTML files for a complete website:

Image for post
Image for post

This time we will look in more detail at what is involved in converting a single page of markdown into the corresponding HTML file:

List comprehensions are great at what they do. But maps are something rather different, and they have their place too.

List comprehensions

A list comprehension is a clever little Python construct that can build a list from another list (or other iterable). For example:

k = [x + 3 for x in s]

This code takes an iterable sand adds 3 to each element, returning the result as a list. So if swas [1, 2, 3]the result would be [4, 5, 6].

We could do this with a the map function:

k = list(map(lambda x: x + 3, s))

This is obviously longer and uglier than the list comprehension. That is hardly surprising because this example is exactly what list comprehensions are for. …

Static site generators, how and why to create your own.

Before looking at how to build a static website using Python, we will look at exactly what a static site is and what the advantages are.

And, since there are already a number of free static site generators, why would you want to write your own?

Static websites

A static website simply consists of a collection of HTML files on a webserver. When you access a particular URL, the server just sends the file to your browser, as is, unchanged.

My site, PythonInformer.com is a static website. When you access this page:



Martin McBride

I am a software developer with over 30 years experience in Java, Python and C++. I write for pythoninformer.com.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store