A few weeks ago, I had trouble accessing aliases in bash scripts. It turns out that aliases are not expanded in scripts, only when the shell i interactive. However, we can get around this by using
expand_aliases at the top o our script.
shopt -s expand_aliases
This technique is known by several names: ports and adapters, hexagonal architecture, layered architecture, onion model, or (most boringly) dependency injection. The main idea is that you separate your business logic from your storage and from your presentation etc. so that you can easily swap out any single piece without refactoring all of your code. I originally read about this on Robert Martin’s site.
Here, I present a simple notes app using dependency injection for the storage and output. Right now I’m using TinyDB for storage, and presenting output to the terminal as a formatted string, or as JSON.
The first two abstract classes
Output_Adapter define the general form what a database or output mechanism should have or provide. Next, we subclass these adapters into concrete classes that can pull data from an actual database, or present output in different ways. At the end of it all, when we instantiate the Notebook class, we pass if the database and output adapters that it will use in order to do its job. At this point, all of its dependencies have been provided (or injected) and it is free to focus on business logic, like managing permissions, spam filtering, or whatever.
- Allow templates to access the “get_url” function
- Create an endpoint to serve your own static pages yourself
I had posted about a recursive maze solver earlier. This is an iterative solution to that problem.
This was… not straightforward. There’s a couple of Python modules out there for this. I ended up using
sqlalchemy. I needed to edit some files in
/usr/local/etc, and then symlink them to
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.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.
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.
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.
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.
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.