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
.
All posts by cjohnson318
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.
Getting CSRF to work with Rails and Angular
I had trouble posting to my Rails server from an Angular controller until I found this SO post. In /app/controllers/application_controller.rb
I added the following code.
class ApplicationController < ActionController::Base protect_from_forgery with: :exception after_action :set_csrf_cookie_for_ng def set_csrf_cookie_for_ng cookies['XSRF-TOKEN'] = form_authenticity_token if protect_against_forgery? end protected # In Rails 4.2 and above def verified_request? super || valid_authenticity_token?(session, request.headers['X-XSRF-TOKEN']) end end
Getting Started with Watercolor
In this post, I’ll discuss options for building a watercolor kit, and provide some introductory reading material.
My Background
I started working in watercolor in 2016. I heard the quote by Annie Dillard, “How we spend our days is, of course, how we spend our lives.” and thought that I should make an effort to carve out more time for art, before I don’t have any time left to carve. I decided to work more in watercolor because, for me, it strikes a balance between range of expression, time, and space. Also, although I’ve done art my whole life, I’ve tended to shy away from color because of its difficulty and expense. I enjoy watercolor because it is challenging, affordable, environmentally safe, and easy to clean up.
Functional Digression at Work
One of my coworkers asked how to filter a list, but return the “pass” and “fail” items in separate lists. For example, filter
takes some data and a condition, and returns a list of the items that pass the condition, but what if we returned two lists? One of my other coworkers came up with a neat solution using reduce
, and I wrote something using generics. Here’s the operation I’m calling “fork”, implemented in Swift3.
import Foundation func fork<T>(_ data: [T], completion: (T)->Bool) -> ([T],[T]) { var pass = [T]() var fail = [T]() for item in data { completion(item) ? pass.append(item) : fail.append(item) } return (pass,fail) } var (pass, fail) = fork([0,1,2,3,4]) { $0 % 2 == 0 }
Deterministic Finite Automata in Swift
I’ve been reading Understanding Computation by Tom Suart, and it reminds me of my first CS class in grad school where I learned about and struggled to implement finite automata. I think one major lesson from that experience was to forget about the pictures of circles and loops on the blackboard, and figure out the bare minimum needed to represent the idea. In the case of finite automata, the bare minimum was that there was a state, and a set of rules that modified the state; each rule was composed of a current state, an input, and a next state. I decided to implement a simple finite automata in Swift using a list of Int
s as states, and a single Int
as input.