A core principle of relational databases is that a database's schema, or the design of its tables, columns and other objects, is held within the database itself; this means we can retrieve the structure using ordinary SQL queries. In this post I will develop a simple module which uses the psycopg2 DB-API interface to retrieve the tables and columns of a PostgreSQL database.Continue reading
In a previous post I introduced the psycopg2 Python/PostgreSQL interface and used it to create a database, a few tables and a view. In this post I will demonstrate inserting, updating, deleting and selecting data using the database created in the previous post, as well as showing what happens if we try to violate database constraints.Continue reading
This article is the first in an ongoing series covering the use of the PostgreSQL RDBMS with Python. Python itself provides no database access functionality but it does provide the Python Database API 2.0 standard (or DB-API) which is implemented by various third-party modules. The module I will be using for this and subsequent posts is psycopg2, the most popular Python/PostgreSQL DB-API module.
In this post I will introduce psycopg2 with a simple program which creates a database and then adds a few tables and views.Continue reading
Pillow describes itself as "the friendly PIL fork", PIL being the now-defunct Python Imaging Library. Fortunately Pillow is still very much alive and provides comprehensive image editing functionality. You could in principle use it as the basis of a sort of lightweight Photoshop type application using perhaps Tkinter or PyQT, but its typical use case is for back-end processing, for example creating thumbnails and adding logos or watermarks to images uploaded to a website.
Despite its powerful and comprehensive abilities it is extremely easy to use and in this post I will introduce what to most users are likely to be its most useful features.Continue reading
Writing code to read or write text files can be tricky because the exact contents of a file cannot be viewed in a text editor if the file contains non-printable characters such as line feeds or carriage returns. This simple utility program will take a filename as a command line argument and print out its exact contents, including descriptions of any non-printable or whitespace characters.Continue reading
The Python language provides comprehensive support for complex numbers, with basic functionality being included with the core language and more advanced features included in the cmath module.
This article will demonstrate the basic operations such as arithmetic which does not require any additional modules. I will also go a bit further than that by writing code to carry out these operations without using the Python operators; of course you wouldn't do this in production code but it does provide an insight into how these operations and complex numbers in general work.Continue reading
Previous posts have included an SVG library, memoization of factorials and Taylor Polynomials. In this post I will bring these all together to plot various sine waves created using Taylor Polynomials.
Taylor Polynomials are used to approximate functions, in this case sine, to any level of accuracy. We can plot these levels to show how they become increasingly accurate, which is the subject of this post.Continue reading
The majority of data can easily be plotted on a graph with equal intervals on the axes, for example 1, 2, 3 or 100, 200, 300 etc.. Some data, typically that which increases or decreases exponentially, cannot comfortably be graphed on such a scale without squashing the data up so much at one end that it becomes incomprehensible. The solution to this problem is to use a logarithmic scale.Continue reading
In this post I will write a Python function to calculate sines using Taylor polynomials. It is impossible to calculate these directly but they can be approximated to any accuracy using this method, and I will show how a Taylor series converges to ever-increasing accuracy.Continue reading