Tag: Tips

gawk script for updating your models.py

Last evening I updated my django source to the latest SVN and found a surprise. The latest set of backwards incompatible changes were affecting my project. Specifically the new ModelAdmin moves were making things hard for me. Given that I have a few applications in django, I decided to write a script that would help speed up the process. What I ended up with was a simple gawk script that processes the models.py file and outputs the code for new ModelAdmin classes. I still have to manually remove the original admin classes because I didn’t want this to mess up things, but not having to look through the models and copy-pasting should save you some time.

For instance, if your models.py has three models-Tag, TaggedItem and NodeGroup-with Admin classes defined then this script will print out…

$ awk -f admin_model.awk models.py
class NodeGroupAdmin(admin.ModelAdmin):
                date_hierarchy = 'created_on'
                list_per_page = 25
                search_fields = ('title', )
                list_display_links = ('title',)
                list_display = ('id', 'title', 'created_on', 'updated_on', 'published', 'group_type',)
                list_filter = ('published','group_type',)
class TaggedItemAdmin(admin.ModelAdmin):
                pass
class TagAdmin(admin.ModelAdmin):
                pass
admin.site.register(Tag, TagAdmin)
admin.site.register(TaggedItem, TaggedItemAdmin)
admin.site.register(NodeGroup, NodeGroupAdmin)
$

I have added the usage instructions at the top of the script, and here is the script.

screen is awesome!

Screenshot showing two windows sharing the same screen session

Linux/Unix has so many cool utilities for the command line and very often I find new things about the tools that I am already using. I have recently upgraded to dual screens at home (19” and 22” combo) and needed a command line window to be open on each screen. I have been using screen to have multiple sessions and keep my taskbar uncluttered1, but the multiple windows put an interesting spin on my screen usage. I wanted to be able to switch to any session in either window without having to detach and then reattach in a different window. So, I started reading the screen manual and noticed the -x command line argument. Guess what it does? Attach to a session which is already attached elsewhere (multi-display mode). Exactly what I was looking for!

By the way if you are looking a hosting service, make sure to checkout WebFaction. They have excellent packages and support, and have screen installed on their servers.

1Additionally, with screen you don’t have to open multiple SSH session if you are connected remotely.

Mapping Caps Lock to Backspace in Linux

One of the first things that I do after installing the operating system is remap the Caps Lock key to the Backspace key. This is because I have rarely seen any need for the Caps Lock key, while the Backspace is much more useful. You can remap this in Windows using a very nice program called SharpKeys. In Linux there are several ways to accomplish this. You can change the underlying keyboard layout, or if you do not use the terminal directly and instead use the command line interface only through X then you can accomplish this very easily by using xmodmap.

Basically there are two steps.

remove lock = Caps_Lock
keysym Caps_Lock = BackSpace

Save the above code to a file (~/bin/map_caps_lock_to_backspace.txt
, may I suggest), and then execute xmodmap map_caps_lock_to_backspace.txt. Try it out, assuming you are in an X session, now the Caps Lock key will act as Backspace!

There you have it. Now, to do this every time that X starts, you can add this to your favorite window manager (XFCE > Settings > Autostarted Application).