I had posted about a recursive maze solver earlier. This is an iterative solution to that problem.
Tag Archives: Python
Accessing a MSSQL Database on a Mac
This was… not straightforward. There’s a couple of Python modules out there for this. I ended up using pyodbc
and sqlalchemy
. I needed to edit some files in /usr/local/etc
, and then symlink them to /etc
.
Access Elements in a Django JSONField List
I set up my Django project to use MySQL, so that I could use a list in a JSONField. Little did I know that accessing elements of that list by their index would be another problem entirely. There are two confounding things. First, in Jinja2, you can access a for-loop index with the loop.index
or loop.index0
keywords, but in a Django project, you need to use forloop.counter
or forloop.counter0`. The trailing zero on those keywords specify a zero-indexed counter rather than a one-indexed counter. The second thing is that you need to provide a custom template tag in order to cleanly access elements in a JSONField list by their index.
Set Up MySQL for a Django Application
I wanted to store a variable list of things in a Django Model. One way to do that is to create another Model, another way is to use a JSON field in your current Django Model. Unfortunately, the default SQLite3 database does not support JSON fields, and you will need to set up a MySQL database.
Using SASS with Django
A few months ago I worked on a Rails application and got acquainted with SASS. I found that I really prefer using SASS over CSS to the extent that it’s worth the overhead of learning another tool’s quirks. Here, I’ll describe what worked for me using SASS with Django.
Using the State Machine Compiler with Python
Today I learned about a state machine compiler and code generator. It provides a small DSL that you can use to describe a state machine and the transitions between the states, which will be compiled to create a number of classes, and then all you have to do is provide the code for the actions. Here, actions, states, and transitions are defined terms that are described in the documentation for the smc tool.
Continue reading Using the State Machine Compiler with Python
The Recursive Maze Problem
Earlier this year, when I was looking for work, I got the same recursive maze problem three interviews in a row. Recursion is cute and clever, but you generally want to use iterative solutions in production.
Very Very Verbose Cosine Similarity
This material was a teaching aid for a crash course I gave at work about cosine similarity. Cosine similarity is a blunt instrument used to compare two sets of text. If two the two texts have high numbers of common words, then the texts are assumed to be similar. The ultimate goal is to plug two texts into a function and get an easy to understand number out that describes how similar the texts are, and cosine similarity is one way to skin that cat.
Please note, there are plenty of other very fast implementations for cosine similarity, but this one was written for educational purposes.
How Many Trials Before the Nth Success
TLDR: the negative binomial counts the number of trials needed before the Nth success.
I had this problem where we were considering running some very expensive tests that had a known success rate, and we wanted to know, given the success rate and the cost, whether we should run them at all. To make things more interesting, we were only interested in a set number of successes, and we could stop all testing after the first successes. My initial thought was to use the binomial distribution, but the binomial doesn’t “cut off” after a set number of successes. It turns out that we needed to use a version of the negative binomial distribution.
Setting Up Sphinx Documentation
It took me a while to get Sphinx documentation set up correctly. Since it is highly configurable, it is highly easy to not configure correctly. In this guide I’ll assume that you’re using a Python virtual environment, and that you’ve placed the source code that you want to document in a directory called src/
. I’ll walk through installing and configuring what you need to create documentation from inline comments using the Google or NumPy style, and create API documentation for a Flask server. I’ll be extra-explicit about what directory I’m in when I make calls that make assumptions about the working directory.