Skip to content

Instantly share code, notes, and snippets.

View GroupAndCountDict.html
Python How To: Group and Count with Dictionaries
In this post I want to have a look at the different possible solutions to two rather simple and closely related problems. How to group objects into a dictionary, or count them. It something that at least I have found I do every now an then in various forms. I want to start from the simplest good solution, and work towards better and faster solutions.
So the problem that we want to solve is that we have some items that we want to group according to some criteria. So in python that is going to be turning a list or some other iterable into a dictionary of lists. We are also going to want some function to create the value we group by, and for the sake of simplicity we will use <code>len()</code> since that is the simplest built in function that gives a key we might group on. In your own code, you would replace that with some logic that gives you the key or tag that you want to group things by.
codefisher / DecoratorsAndContentManagers.html
Last active Jul 4, 2018
Python decorators and context managers
View DecoratorsAndContentManagers.html
Two weeks ago I wrote a post called <a href="">Python: tips, tricks and idioms</a> where I went though a whole lot of features of python. Now however I want to narrow down on just a few, and look at them in more depth. The first is decorators, which I did not cover at all, and the second is context managers which I only gave one example of.
There is a reason that I put them together; they both have the same goal. They can both help separate what your trying to do (the "business logic") from some extra code added for clean up or performance etc. (the "administrative logic"). So basically it helps package away in a reusable way code that really we don't care too much about.
View PythonTipsTricksAndIdioms.html
My programming language of preference is python for the simple reason that I feel I write better code faster with it then I do with other languages. However also has a lot of nice tricks and idioms to do things well. And partly as a reminder to myself to use them, and partly because I thought this might be of general interest I have put together this collection of some of my favourite idioms. I am also putting this on <a href=""></a> so that anyone that wants to contribute there own things can, and I will try and keep this post up to date.
A fairly common thing to do is loop over a list while also keeping track of what index we are up to. Now we could use a <code>count</code> variable, but python gives us a nicer syntax for this with the <code>enumerate()</code> function.
<script src=""></script>
View customizable.jsm
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cu = Components.utils;
this.EXPORTED_SYMBOLS = ["customizableUI"];
* This is a reimplementation of parts of resource:///modules/CustomizableUI.jsm
* It is used for those applications that don't have it available.
View add_resource.js
function createResource(resourceName, uriPath) {
let resource ="resource").QueryInterface(Ci.nsIResProtocolHandler);
var fileuri =, null, null);
resource.setSubstitution(resourceName, fileuri);
// in startup()
createResource('toolbar-buttons', 'chrome://toolbar-buttons/content/resources/');
// in shutdown()
You can’t perform that action at this time.