High on my list of neglected tech. projects is the Testament books to prisoners database web application.Â This is the database program that projects like the Midwest Pages to Prisoners Project use to track packages sent and returned and books requested in the hopes avoiding delays in delivering books to incarcerated people and to provide metrics that grant providers like.
The reason for duplicate records is that both the person writing to request books and the volunteer may list their name and/or DOC# inconsistently.Â For instance, the state may store the DOC# in their database as A-123456 but the incarcerated person may write it as A123456 A-123-456 or just 123456.Â Volunteers who don’t know about this and aren’t careful may not check beforehand for an existing record.
This is probably preventable through more sophisticated validation, but we still need a way to find duplicates in the existing records.Â As this application is written in the Django framework, I want to try to use the Django API to find matches.
At first thought, it seems like I will have to iterate through each inmate record and check if there is a duplicate record.Â This seems pretty slow, but I can’t think of a better way to do this.Â At this point, there aren’t so many records that this approach will fail, but it would be nice to do something slicker.
The other problem is how to match a duplicate.Â One approach might be to build a regexp for the DOC# (for instance, match either the first character or omit it, allow dashes or spaces between all characters, …) and then use the iregexp field lookup to try to find matches. One challenge with this is that the current Testament codebase is using Django 0.97 (I think) and iregexp is only available starting in 1.0.Â Maybe it’s time we updated our code anyway.
There is also the Python difflib module that can compute deltas between strings.Â However, it seems like this would slow things down even further because you would have to load each inmate object and then use difflib to compare the DOC#s.Â I assume that the previous approach would be faster because the regexp matching happens at the database level.