Archive

Archive for August, 2009

Fix indentation on entire file

August 11th, 2009 1 comment

Sometimes, a file is just not indented right (usually happens with HTML files), and is totally unmaintable. Then I find myself opening the file in emacs and manually going through each line and tabbing them to the appropriate indentation. This is really just dumb, and I should’ve looked this up a long time ago, but there’s a built in command in emacs to handle this. All you do is select the text you want to indent — ctrl-x h for the entire buffer — and then do M-x “indent-region”. That’s all. Nifty little trick. I’m going to have to mess around with the other indent commands and see what they do. You can also use the shortcut C-M-\ instead of M-x “indent-region”.

Categories: Emacs Tags:

Firefox Keywords

August 10th, 2009 No comments

I remember reading about the new features of Firefox 3 before it came out many moons ago and how the new bookmarking features would blow our minds. Well, blow my mind, it did. The upgrade actually did such a great job, I forgot about one of the features they really stressed before the release: keywords. The claim was that you could map your bookmarks to your own preset strings and then call up the bookmarks by entering the string. I thought this was an interesting idea, but seemed a bit much. Anyways, when Firefox 3 actually did come out, they included this great feature where the link bar would do a full text search against your link history. This feature made bookmarking things almost useless, since it was probably quicker — and more natural — to type in some text as you remembered it into the link bar, and what you wanted would usually just come up. Pretty amazing stuff.

So now, why did I bring up the keywords if everything’s so great in FF3-land? because they actually add even more utility to this setup. I setup keywords for the things I least want to have to go through a menu for (and for which I probably shouldn’t have to use my mouse): a link to my repository, the company wiki, and our bug reporting tool. I added little keywords for those three things, and really like how simple and easy they are to reach now. This is one of those things that I’m sure you knew about, but it’s so simple to setup, and is actually a great little thing to have in place for those web tools you keep going to over and over. Especially with the advent of web tools taking the place of where real GUIs used to live.

For the sake of clarity, here’s one example of how I use the keywords. I have my bug tracking software bookmark set to the keyword ‘bugs’. All I do is press ‘ctrl-l’ type in ‘bugs’ and it shoots to my bug tracker.

Categories: Firefox Tags:

crontab shell

August 10th, 2009 1 comment

I wrote a Python script that I want to run every five minutes through a crontab. The script ran fine and linked with my local libraries until I ran it through the crontab and it couldn’t find my local libraries. After a bit of thought, I realized that the crontab was not running through the same shell environment as I expected it to. Apparently the shell is set in /etc/crontab file and there it was set to bash. That in turn was calling the wrong version of Python, and that was why my local Python scripts weren’t being found.

There are four different ways around this:
1) Modify /etc/crontab to hit the right shell; in my case (first line modified):
/etc/crontab

SHELL=/bin/tcsh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly

crontab -e

*/5 * * * * python /my/script.py

2) Add the shell value to the top of the custom crontab (crontab -e); in my case:

SHELL=/bin/tcsh

*/5 * * * * python /my/script.py

3) Run the actual command through the tcsh as so:

*/5 * * * * tcsh -c "python /my/script.py"

4) Directly request the right version of Python:

*/5 * * * * /tools/bin/python /my/script.py
Categories: Linux, Python Tags:

Emacs in shell

August 7th, 2009 2 comments

Most people don’t know this, but you can also run emacs from within your shell (like vi). There are some moments where it just doesn’t make sense to load the emacs GUI, and it’s just nice to know that we don’t have to use vi in those circumstances. All you need to do is add the “-nw” tag when running emacs to load it up in the shell. I also add the “-Q” so that there’s no fancy color schemes introduced.

emacs -nw -Q

The most common usecase for doing this is when you’re committing something from the shell and want to add a comment. Usually the default is vi in these circumstances, but it doesn’t really have to be…

Categories: Emacs Tags:

os module gotya

August 4th, 2009 No comments

Apparently documentation is important sometimes… I’m writing this Python script that’s run in both Linux and Windows. My development happens on a Linux box, so until I move onto testing my scripts on Windows, I don’t realize the discrepancies. I wanted to get the username of the user, and there is this command in the os module called getlogin() which worked perfectly on my Linux box. In Windows it raises an exception. I checked the documentation and it says that getlogin() is only supported in UNIX, go figure… The way it recommends to get the username is from the os environment variable dictionary. The documentation tells you to use the “LOGNAME” key. I’ve found that “LOGNAME” does not work in Windows, but “USERNAME” works in both…

import os
os.getenv("USERNAME")

Anyways, tread lightly and always look at the documentation, even when something seems to be named so appropriately in such a mainstream language/module. One of the things that I really love about Python is that it usually does what you expect it to do. I guess nothing’s perfect though, and it pays to always be on your toes.

Categories: Python Tags: