Computer Programming

Yes, a bit specific, but I need to store some links!

Django error: Reverse for 'some-name' with arguments '(1,)' and keyword arguments '{}' not found. 0 pattern(s) tried: []

A nice feature of the URL matching rules is that even if you have named arguments in the url(), like so:

 url(r'^items/(?P<pk>[0-9]+)/$', ItemDetailView.as_view(), name='item_detail'),

the system will accept a positional parameter, like so:

{% url 'item_detail' item.id %}

I was thinking that it was going to be particular about named parameters, and I was searching for information about passing named parameters to url, but it wasn't necessary. Once again, misdirected by an error message.

A CSV Reader for UTF-8 Files

I was using the Places to CSV plugin to dump my website visit logs, and needed to ingest this data. The problem was, the fields were in UTF-8... but when I tried to decode each line from str to unicode, the csv library wouldn't do its magic. The fix was to let the csv.reader read in the data as str (no encoding, just bytes), and then decode each field as utf-8, returning a unicode string. These unicode strings were returned as a list, just like csv.reader does.

Django URL Namespaces and Templates... kind of not pretty

So, I shouldn't admit it, but I'm a Django beginner. (I've done a lot of other frameworks, but Django is new to me.) It has a really nice feature to write URLs for you; you don't code URLs into the pages, but use a name to find a URL's associated pattern, and generate based on the pattern. It's really nice.

Python Operator Overloading

I was reading up on Django F() and Q(). I didn't know Python had operator overloading. They call it magic methods.

Magic Methods on Rafe Kettler

There's a pretty good tutorial at Treehouse.

But... a few memories of arguments about operator overloading surfaced, so I had to think about why Java rejected adding the feature to Java.

Are Doctests in Django 1.8 Failing to Run?

I don't know what's up, but I ran some old examples, and it looks like doctests aren't running when I do the "manage.py test" (or the one with a settings file).

Django Database Testing Unmanaged Tables with Migrations

The answer is here: Models inside tests - Django 1.7 issue.

The problem is that you have some tables that are not managed by Django. (They get called "legacy", but in my case, they weren't legacy but data imported from another system.)

CORS, Angular JS, and Parse.com, together, didn't let me login twice.

Things were going well with a re-architecting and re-factoring of a service to use Angular's awesome $request, and Django REST Frameworks' awesome ModelViewSet generics. As usual, when things are chugging along, you come across a weird bug that just sucks you in for a while. The bug I hit today involved CORS, AngularJS, and Parse (we're using Parse for part of our backend).

The symptom was that, if I logged in once, then logged out, I could not log in again. I could reach the server, but it wouldn't let me do the exact same thing I'd done just 30 seconds before.

Should Javascript Code Blocks be Delimited with Parentheses?

This is some some random thought I had.

I'm annoyed that I often need to terminate with this "})".

It would be easier, in some editors, to terminte with "))", because the editor will show you the matching parentheses.

Let's see:

function z(x)(return x+x;)
applying(a, function(x)(return x+x;));
var z = generate_function(function(x)(return x+y;), y);
if (x==y) (return x+y;) else (return false;)
for(var i=0;i<x.length;i++) (document.write(i))

Maybe it would work.

Adding Custom Fields to a Database Table

I was watching and OpenERP/Odoo video, and the demo showed custom fields being added.

My first thought was, "uh, I hope it's not implemented as a generalized database system written using a database as its platform."

I guess I could go read the code.

Then I started to think about using a NoSQL database to achieve that flexibility. NoSQL is schemaless, so being able to add fields is simple and not ugly. The tradeoff is that it's not relational - a huge tradeoff.

Hating on HATEOAS a little bit, thinking about it a lot

I've been thinking about how to enable HATEOAS on this web app, and it is a mind-bender. Django isn't quite up to it - but it does have a key feature of named links and URL generation that seems like a requirement for HATEOAS. To do HATEOAS, links need to be elevated to a higher status in the system.

For one, on the web, links are very different from the way software internals are organized. In software, and also in URLs, we have hierarchies: paths to files, paths to APIs, hierarchies of objects, and nested data structures.

Python Cheatsheet

When I shift languages, I sometimes make cheatsheets to speed up the transition to the new keywords and syntax. There's only so much shelf space in the brain. This is one I made for Python.

Framework Debate

Why Django Sucks is a really good thread.

I generally don't *like* big frameworks, but that's more my aesthetic and cranky side's opinion. My practical side often prevails. I've never really regretted using big frameworks (or big libraries) for real projects. Getting a bunch of features, fully integrated, saves a lot of time.

Recreate a Dropped Table in Django Migrations

This is a somewhat embarrassing story, but one that's common enough that you can find it online: I dropped a table, and needed to recreate it, and I'm using those (grr) Django migrations.

The right way out of this mistake is to recover from a backup. But let's suppose I didn't have a recent backup of my development database... because I didn't.

Suddenly, a LINK's REL is important.

I'm one of those HTML losers who doesn't use the REL attribute unless is is LINK REL="stylesheet" HREF="...", or if it's provided in some template and I just paste it. (You can also use it for linking RSS and Atom feeds, but I rely on the software to do that for me.)

Syndicate content