Steps to take to get a virtualenv with Python 3.3 working on Ubuntu 12.04:

I recently decided to do a bit of finals procrastination by working on a problem that’s been a mild nuisance for the past few months. My Google Contacts have had a problem with delineating first and last names, but that’s a story for another post. If you’re interested in the repo as it develops, check out https://github.com/andrewychoi/fix_contacts

What I had been having trouble with was the consistent need to enter a username and password for every time that I wanted to git push to the server. I went into the GitHub user menu and added an SSH key, but access to my accounts was still problematic.

The problem was that my project_directory/.git/config file still told me that origin was to be reached over HTTPS. Changing that to SSH solved the protocol access problem. For exactness, I changed the line

https://github.com/username/project.git

to

ssh://github.com/username/project.git

Now, however, I was greeted with a

Permission denied (publickey). fatal: The remote end hung up unexpectedly

At this point, I started searching the web for the solution to this problem. It turns out that I needed to SSH in as the git user. The fix was to change

ssh://github.com/username/project.git

to

ssh://git@github.com/username/project.git

Now everything works like a charm!

 > git pull Already up-to-date. > git push Everything up-to-date. 

I mentioned to a friend today that I’ve been making an effort to TeX the majority of my schoolwork, and I thought I’d share a couple of things that I memorized in the interest of saving time:

## \setcounter{secnumdepth}{-1}

I tend to use the \section{} keyword (function? delimiter?) a lot, especially when enumerating problem numbers, like so:

\section{Problem 3} \subsection{a.}

or

\section{3.4.2}

Using \setcounter{secnumdepth}{-1} helps me keep extraneous numbering to a minimum.

## \usepackage{fullpage}

Pretty self-explanatory, I use this to make sure I’m not wasting too much empty space.

## \usepackage{amsmath} and \begin{align}, \end{align}

I love the amsmath package and especially the align environment; it lets me get my equations in neat and tidy order. If you don’t want numbered equations, make sure to use \begin{align*} and \end{align*}.

I am, however, looking for a way of making sure that all columns in a three or more column structure are left-aligned. If you know how to do that, please let me know!

## \includegraphics[width=\textwidth]{filename.jpg}

The \includegraphics[]{} function comes in the graphicx package (N.B. the function name is \includegraphics but the package name is graphicx). The [width=\textwidth] section is a great little hack to keep your image aspect ratio constant while ensuring that all of the image is visible.

Any other great hacks out there?

I was getting an IntegrityError while working with the User model in django.contrib.auth.models, with a call for a check to see if the User model’s username was unique. I solved this by using a call to User.objects.create_user() instead of instantiating a new User() and manually setting all necessary attributes.

As a side note, if you’re working with the django.contrib.auth.forms UserCreationForm, be aware that there won’t be a password field in the returned form. There are instead password1 and password2 fields.

I was debuggigng this bit of code today, and ran into a silly error:
 # child_template.html: {% extends "base.html" %}

 

{% block tab_contents %} {{ block.super }} {% endblock %} 

 # base.html ... {% for i in 123|make_list %}

 {% if tab_index == i %} {% csrf_token %} {{ form.as_table }} {% endif %} 

{% endfor %} ... 

When you pass variables to a template, the type of the variable still does matter. It turns out that the for i in 123|make_list makes i a string variable. Thus, if you want to conditionally print something, you need to pass 'tab_index': '1' instead of 'tab_index': 1. It’s a subtle distinction, but the ‘==’ won’t work if the variables are of disparate types.

On a project that I’m working on currently, I have the following model definiton:

class Embassy(models.Model): name = models.CharField(max_length = 200) facebook_id = models.IntegerField(blank=True, null=True) twitter_id = models.IntegerField(blank=True, null=True) youtube_id = models.IntegerField(blank=True, null=True) 

I had initially run a python manage.py syncdb before changing the IntegerFields to allow null values. I thought that running a python manage.py sqlclear embassy would solve the problem, but it didn’t actually drop the table that it needed to. I had to go to python manage.py dbshell and run DROP TABLE embassy_embassy to get the table manually dropped.

Hopefully the wonderful people at Django will fix this bug soon!

Running across this bug is a bit odd, because I wasn’t aware of any assertions that I was making, but this was the offending code:

{% if display_type == 'ts' %} <script src="/static/highstock.js" type="text/javascript"> </script> {% else if display_type == 'avg' %} <script src="/static/highcharts.js" type="text/javascript"> </script> {% endif %}

The above code ended up causing an AssertionError: No exception supplied.

The idea was to do a conditional JavaScript import, but the problem actually ended up being in the Django template syntax.  The correction was to replace the “else if” with an “elif”.  Rookie mistake!

Corrected code:

{% if display_type == 'ts' %} <script src="/static/highstock.js" type="text/javascript"> </script> {% elif display_type == 'avg' %} <script src="/static/highcharts.js" type="text/javascript"> </script> {% endif %}