Monday, February 6, 2012

How to get ack to search .txt files

By default, ack (aka ack-grep) doesn't search through text files. To get it to do this, add
--type=text
to the command. If you know you'll always want to search text files, you can create a .ackrc file in your home directory with the same command in it:

--type=text

Ack just adds anything from this file to the command.

Thursday, February 2, 2012

Determine Image Size from Command Line

To get information from the command line about an image, do this:
$ identify filename.png

As an example:
$ identify button.png
button.png PNG 127x34 127x34+0+0 8-bit DirectClass 3.5KiB 0.000u 0:00.000

This shows, among other things, the interpreted filetype, dimensions (width x height), and file size.  You can get way more information by adding -verbose to the command.

Learn more.

Disabling Postgres Pagination

If you query your PostgreSQL DB and the results are too long to fit on one page, the default postgres behavior is to paginate the results with something like `more` or `less`. Sometimes you don't want this. Here's how to disable pagination:

To disable pagination temporarily when you in the postgres command line:
=# \pset pager
This will toggle pagination, so you can use it again to go back to the normal mode. (The "=#" is just the prompt; type the stuff following that)

To disable it when you start up psql:
$ psql -U admin db_name --pset pager=off

And finally, if you always want it to be off, you can add this to your `.bashrc` file:
alias psql='psql --pset pager=off'

Wednesday, January 4, 2012

How to prevent mailto in email client

Recently I was designing an email that gets sent to customers. In it I had an email address, but it wasn't meant to be used to send an actual email, just to copy and paste. Gmail automatically converted it to a mailto link, which made it clickable and thus harder to select the text. Here's what I did to force it back to text.

The problem:

Original HTML of email that I'm sending:

<p>Please add blah@yadda.com to your address book.</p>

HTML of email that I receive:

<p>Please add <a href="mailto:blah@yadda.com" target="_blank">blah@yadda.com</a> to your address book.</p>

What I see:

Please add blah@yadda.com to your address book.

Solution:

<p>Please add blah<span>@</span>yadda<span>.</span>com to your address book.</p>

What I see now:

Please add blah@yadda.com to your address book.

Unfortunately, this will only work for HTML emails, not plaintext emails.

Tuesday, December 20, 2011

POST with a list in django

In your template, you may want to do a post with a list of values, like this:
$.post('{% url affiliate.views.manage_access %}', {
    id_list: [1,2,3],
  });
You can't just grab the value from the post like you normally would, since it will only grab one of the items.  So this won't work:
def manage_access(request):
    id_list = request.POST['id_list']
You must instead do this:
def manage_access(request):
    id_list = request.POST.getlist('id_list[]')

Wednesday, August 10, 2011

Django template tag escaping

In django, template tags look like this for a variable:
{{ variable_name }}
Sometimes you want to print out "{{" or "}}" without django trying to interpret it. Here's an example of how to escape the the template tags:
{% templatetag openvariable %} blah {% templatetag closevariable %}

Wednesday, August 3, 2011

Quick regex match example in python

This is an example of matching the "blahblah" that is between "this" and the first "end" (i.e. it's not greedy).

>>> s = 'try to match thisblahblahend and not that end or else...'
>>> r = re.compile('this(?P.+?)end')
>>> m = r.search(s)
>>> m.group('my_match')
'blahblah'