Simple Python Tricks #5

Tags

, ,

Last time we looked at dealing with files in Python, looked into filename handling, and left off after creating a couple of base classes to support general file operations.

This time we’ll extend those classes into some useful file utility classes for handling data from different types of files (for instance, binary, plain text, line-oriented text, and any type of structured file).

Continue reading

Simple Python Tricks #4

Tags

, ,

The last two posts looked at Python list comprehensions. [See Simple Tricks #2 and Simple Tricks #3] This time we look at file handling, one of the most common tasks programmers deal with, especially with script languages such as Python.

Python’s native file object, created with the built in open function, is simple and easy, but here are some tricks that make file access even simpler and easier.

Continue reading

Simple Python Tricks #3

Tags

, ,

In the last post we looked at Python list comprehensions, an inline alternative to for-next loops. List comprehensions are a powerful technique for creating and processing lists.

In this post, we’ll look at more list comprehensions, including some real-life examples, and look at three other kinds of Python comprehensions: sets, dictionaries, and generators.

Continue reading

Simple Python Tricks #2

Tags

, ,

Considering that it has been six months since the last post, my attempt to get a post flow going with “Simple Python Tricks” left something to be desired. Like success. Undaunted, I’m drawing from the well again.

This time to discuss Python comprehensions, a feature that is powerful and expressive, but which is rather unique to Python. Newcomers may not, at first, even realize they exist.

Continue reading

Python Descriptors, part 2

Tags

, , ,

Python has the useful notion of descriptor objects, which give object attributes control over how they are accessed. Descriptors enable calculated-on-the-fly attribute values and can prevent or control modification of data values.

The previous post covered the basics. In this post, I’ll dig deeper into Python descriptors with some more involved examples. This post assumes the basics covered in the previous post.

Continue reading

Python Descriptors, part 1

Tags

, , ,

Python has the useful notion of descriptor objects as well as the built-in property() function to make using them in the most common cases — read-only and calculated instance attributes — quite easy.

In this post I’ll explore Python descriptors with lots of examples demonstrating how to use them. Descriptors are an important aspect in understanding Python and using it effectively.

Continue reading

Why 1s and 0s?

Tags

, , , ,

Quite some time ago, I posted about this on my main blog, but it has occurred to me that, firstly, it might be time for an update, and secondly, that a post about why computers use 1s and 0s is better suited here on my programming blog. (Especially as this is the 101st post!)

It turns out there is a very good reason computers use 1s and 0s, and while it is possible to use other numbers, the 1s and 0s are all that is needed.

Continue reading

Musical Scale Modes Table

Tags

, ,

Mathematician and educator John Baez has an excellent series of blog posts about music theory. The seventh concerns generating scales by using notes separated by fifths. Shifting the start point generates the seven major scale modes. Shifting the root key generates those seven modes in the twelve keys (a total of 7×12=84 scales).

John asked if any of his readers would be interested in creating that table of all 84 rows. It sounded like — and turned out to be — a fun exercise. This post explores in detail the Python solution I came up with.

Continue reading

Calculating the Number e (in Python)

Tags

, , ,

It has been almost a year since my last post here. I haven’t been idle code-wise, but I have been too distracted by Real Life to do any blogging here. My publication rate is way down on my main blog, too.

To get me posting here more, I think I need more of a focus on casually documenting my own little projects rather than Code Wise articles. Those take more work than I seem willing to put in these days. With that in mind, I have some trivial Python fun to share…

Continue reading