Python – Modifying a List of Dictionaries Using Map

Assume we have a list of python dictionaries, and all of the dictionaries have a common key, let’s call it ‘numeric_value’

list = [{'numeric_value' : 0}, {'numeric_value' : 3}, {'numeric_value' : 5}, {'numeric_value' : 2}]

Now, we want to add a field to each of the dictionaries, let’s call it ‘prime’. We want it to be True if ‘numeric_value’ is a prime number, and False otherwise.

Assume that we have already implemented a simple function ‘is_prime(number)’ which checks whether a number is prime or not.

The simple approach:

for idx,item in enumerate(list):
    list[idx]['Prime'] = is_prime(item['numeric_value'])

Which works, but it’s not attractive enough :D

This is how I implement it when I’m bored:

list = map(lambda x: dict(x.items() + [('prime', is_prime(x['numeric_value'])]), list)

Note: I’m NOT saying that you should do it this way :)

Check If A Number is Prime Using A Regular Expression

The implementation is using Python, but can easily be ported to other languages:

The idea is to “flatten” the number  (e.g. 3 becomes “111” and   5 becomes “11111”), and then try to group the 1’s into multiple equal groups. If we succeed to group, the number isn’t prime, otherwise it’s prime (with the exception of the number 1).

It works because the so-called “regular expression”, isn’t actually regular.


import re

def is_prime(n):
    return re.match(r'^1?$|^(11+?)\1+$', "1" * n) == None;

The first part of the regexp “^1?$” matches a string with an optional “1″, that is, it matches the empty string, and the string “1″, which are the “flat” representations of 0 and 1 respectively. This is because Zero and One are special cases, and are not prime.

The second part tries to match a string of “start of line”, then two 1’s or more ^(11+?), then it tries to find one or more times the previous match (\1+). then end of line $.
i.e. try to divide the 1’s into two or more groups of two, if failed, try two or more groups of three, etc… in general: try to divide the sticks into two or more groups of “two or more”. Which is exactly the definition of a non-prime number (a.k.a composite number).
So, if the regexp succeeds to match the pattern, the number is not prime. If it fails, the number is prime.

Note about ^(11+?):
This is almost the same as ^(11+) but, with one difference: using (+?) instead of (+) means “match minimally”, as opposed to “match greedily”.
For example, consider the string “aaaa” and the regexp ‘(a+)\1+’ the regexp will match the string, the value of \1 will be “aa”, since the a+ pattern matched the longest string which allows the regexp to match the string.
The regexp ‘(a+?)\1+’ will also match the string, but the value of \1 will be “a”, because this time (a+?) looked for the minimal match which will allow the regexp to match the string.
In our case, the use of (+?) instead of (+) does not affect correctness. The only difference is related to performance, because it tries small groups first, rather than big groups first.

A Python Tool to export Read It Later list to Instapaper

Needed to move my “Read It Later” reading list to Instapaper so I wrote this script.

To use it, you need: Python, Read it Later username, password and API key, Instapaper username and password.

Notice that if the reading list is long, it will take some time to finish, since each item issues a HTTP request to Instapaper.

Sorry about the typos in the code. Too lazy to fix.

Get the script at malloc.co public repo in GitHub.

Please let me know if you face problems.

 

PLY – Python Lex-Yacc

PLY is a python implementation of lex & yacc. Allowing you to implement a lexer, interpreter or compiler in python.

If you have used lex/yacc (or flex/bison) before, it is very straightforward to use.

Official Page

Official Documentation

An example of implementing a calculator with PLY. You can start here if you want to get going quickly.

I used it and it is (combined with the power of python) really powerful.