cycle_xrandr.py : dual and single displays on Ubuntu

July 10, 2008 at 06:24 AM | categories: python, linux | View Comments

Linux power management has gotten good recently. My last two laptops (HP dv9000 and Macbook Pro) have supported hardware suspend mode out of the box. I'm impressed!

Being that I hardly ever turn off the laptop (I just suspend now) I'm left with an annoyance: I use two displays at work and one everywhere else. In Ubuntu, I was easily able to setup dual and single modes such that when I boot up, Xorg detects how many displays are connected appropriately. But now that I don't turn off the machine, I want to be able to just plug in another monitor and go. I don't want to have to reboot the computer. I don't even want to have to log out. I want to plug the monitor in, leave all my apps running and in the state they are in.

Xrandr does this and it works great. But what if I'm in dual display mode but I only actually have one monitor connected? Can I get to an open terminal to use xrandr? Easily? Probably not.

I wrote the following python script to take care of this. It detects all of the modes that xrandr knows about and cycles to the next size listed. I then bind this script to a custom keyboard shortcut so I don't have to type or even see anything on the screen:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

__author__ = "Ryan McGuire (ryan@enigmacurry.com)"
__date__   = "Thu Jul 10 15:27:18 2008"

"""Cycle through all screen resolutions detected by xrandr"""

from subprocess import Popen, PIPE
import re

size_re = re.compile("^   ([0-9]*x[0-9]*)\W*[0-9]*\.[0-9]*(\*)?")

def list_sizes():
    """List all sizes detected by xrandr,
    ordered by the next resolution to cycle to"""
    p1 = Popen(['xrandr'], stdout=PIPE)
    sizes = []
    current_size_index = 0
    for line in  p1.communicate()[0].split("\n"):
        m = size_re.match(line)
        if m:
            sizes.append(m.group(1))
            if m.group(2) == "*":
                current_size_index = len(sizes) - 1
    return sizes[current_size_index+1:] + sizes[:current_size_index+1]

if __name__ == '__main__':
    Popen(['xrandr','-s',list_sizes()[0]])
Read and Post Comments

Emacs as a powerful Python IDE

May 09, 2008 at 03:27 PM | categories: python, enigma curry, emacs | View Comments

Update 01/2009: this post is still valid, but see updated installation instructions here.

Last night at the Python user group I gave a short demo on using Emacs as a Python editor/IDE. My macbook pro refused to display on the projector so I thought my demo was going to be a 'no go'. Thankfully, sontek allowed me to use his Linux laptop. I hurriedly copied over my emacs environment, installed a few packages and was able to present after all. I think the demo went fairly well even though I think it was a bit hurried and I forgot to cover a few things, I think I was pretty nervous at the same time because of the fact that the mac didn't work and got me flustered. Oh well, I think people enjoyed it.

My Emacs Environment

Below are the Emacs features most applicable to Python development:

  • Rope and Ropemacs Rope is a general (non-emacs specific) Python IDE library. It has awesome support for multiple refactoring methods and code introspection. Inside Emacs, this gives us:
    • Full (working!!) code-completion support of modules, classes, methods etc. (M-/ and M-?)
    • Instant documentation for element under the cursor (C-c d)
    • Jump to module/class/method definition of element under the cursor (C-c g). This works for any Python code it finds in your PYTHONPATH, including things from the stdlib.
    • Refactoring of code (like rename -- C-c r r)
    • List all occurences of of a name in your entire project
    • and More.
  • YASnippet YASnippet is a snippet tool like TextMate. You can expand user defined keywords into whole blocks of predefined code. This is especially useful for the usual boilerplate that would go into a python file like
    #!/usr/bin/env python
    and
    if __name__ == '__main__':

    Granted, Python doesn't require much boilerplate, and therefore this package is much more suited to languages like Java, but I bring it up because I think its cool and if you get into the habit of using it, then a few keystrokes saved here and there will add up over time.

  • Subversion support with psvn.el Psvn is a comprehensive subversion client for Emacs. It integrates well with ediff mode so you can use it to check changes between versions. It does all of the other boring subversion stuff well too.
  • Ido-mode for buffer switching and file opening. Emacs, to the uninitiated, can be confusing because by default there is only one view into a single file at a time. How does one get to another file? Instead of cluttering the interface with GUIness and making the user click somewhere (and thereby forcing the user to waste their time by moving their hand off of the keyboard), Emacs gives powerful ways to switch between files. Ido-mode is one of these useful ways -- it makes a list of open files starting with the most frequently visted files and widdles this list down as you type part of the filename. You can have dozens of files open and only be a few keystrokes away from any one of them.

A lot of people, for whatever reason, don't feel that Emacs is an IDE at all. I don't personally care what you define it as -- the fact remains -- Emacs is a powerful Python environment and despite being over 32 years old has proven to be just as modern as any IDE today, and remains THE most configurable editor (operating system?) ever.

I've tarred up my Emacs environment for general consumption. Instructions:

  • Install Pymacs
  • Install Rope and Ropemacs
  • BTW, those three packages should be the only packages other than Emacs you'll need. Everything else is self contained.
  • Extract the tarball to your home directory. This creates a directory called ryan-emacs-env.
  • Rename "ryan-emacs-env" to ".emacs.d"
  • Symlink my dot-emacs file to your .emacs. Run "ln -s .emacs.d/dot-emacs .emacs".
  • If you also want to do Java development run "tar xfvz jde-2.3.5.1.tar.gz". I leave it tarred because you don't need to pollute your environment if you're not going to use Java. (Also for whatever reason, jde doesn't like to be stuck in my subversion repository so I just leave it tarred up and untar on every machine I check it out on.)

Extra tips:

  • Put your .emacs.d directory under version control. Never rely on your distros emacs packages, install all future elisp files yourself in your .emacs.d file and commit to your repository often. This way you've got an environment that is easily transportable and synchronizable across multiple machines. This is the major reason why my emacs environment was so fast to trasnfer from my macbook pro to sontek's laptop during the demo.
  • Speaking of sontek, he brought up an excellent point in #utahpython the other day, he's not going to be using my emacs environment except for reference, instead he's starting with a clean slate. This is by far the best and most prudent thing to do. My emacs environment is a culmination of several years of plugging in and deleting various packages and writing various snippets of elisp. Your needs are always going to be different than mine and you are also going to be better off by educating yourself along the way by creating your own.

Some more fun Emacs evangelism:

Read and Post Comments

New Slacker Screenlet released

April 04, 2008 at 10:43 PM | categories: python, linux | View Comments

Check it out: I updated my Slacker Screenlet today.

New in this version:

  • Bug fixed where the screenlet-manager would respawn infinitely
  • Integrated LastSlacker support

You can read more about the original version in my previous post.

LastSlacker is a way to scrobble what you listen to on Last.FM. This lets you share a log of what you listen to with friends. I've been meaning to add this feature for a long time now, until today it's been in a sorta working state for months! It feels good to have finally gotten this project pretty much finished.

Read and Post Comments

Automated Time Lapse Photography in Python

February 14, 2008 at 11:32 PM | categories: python | View Comments

Last night at the Utah Python User Group meeting, John Harrison had an awesome game he has made called Marshie Attacks. It combines PyGame, PySight, a projector and lasers. You point the laser at Marshie and the isight camera detects if the laser is on target, and if so, he blows up. Very cool stuff indeed.

His game depends on the Macintosh PySight library for video capture, but is otherwise cross platform. I decided to take a stab at getting images off my webcam in linux. Using gstreamer, it wasn't hard at all.

I don't have a projector, so I can't make a full Linux port Marshie Attacks, yet. This first stab is just to show how to grab images from a v4l2 device in Linux. This program takes time lapse photos, and saves them to disk with a configurable frequency.

Requirements include:
  • Python (2.5)
  • gstreamer 0.10
  • Python Imaging Library

Download the code

Read and Post Comments

Ron Paul on the Dollar, Gold, and Oil

January 09, 2008 at 01:03 AM | categories: python, ron paul, economics, liberty rants | View Comments

Everyone that drives a car knows that gasoline has gotten a lot more expensive over the last few years. But very few people seem to correctly understand why this is. Ron Paul, in his latest debate appearance, laid it out quite plainly, but it takes keen and willing ears to fully appreciate it:

The value of oil hasn't changed at all -- instead, the value of our dollar has tanked. The value of oil, relative to gold, has sat unchanged since 2000 (and even well before that). This means that if you got your paycheck in gold instead of dollars you'd still be paying the same price for gas as you were a decade ago.

The gas prices are not the fault of greedy capitalists. No gas company is "gouging" you. On the contrary, seemingly high gas prices are not due to any free market force at all, but rather due to the fascist cooperation of a complacent, apathetic, congress and the Federal Reserve. Our government has an insatiable appetite to print money out of thin air to support a massive military industrial complex as well as a socialist, redistributionist, welfare state. This monstrously inflates the money supply -- robbing the value of your dollar.

I decided to prove this for myself, that the golden price of oil is relatively stable. I downloaded historical gold spot prices, as well as historical crude oil prices. Using a bit of Python and matplotlib, I produced the following graph:

Oil Prices in Gold, Dollars, and Euros

I'm not an economist, so I was fairly pleased when I saw that what I came up with correlated fairly well with the graph that the Wall Street Journal published. This graph shows that oil costs 3.5 times as many dollars as it did in 2000, and yet the oil price in gold has barely changed at all in the past eight years.

Everyone should be putting their money into an account with interest of some kind, and not just letting their money sit around uninvested. But at the rate that the dollar is losing its value, even "high" interest investments aren't paying out faster than the current rate of inflation (and as the above graph shows, that inflation is a lot more than the 3% the government would tell you). If we simply legalized alternative, market based currencies (as opposed to raiding and plundering them), we could have much larger gains on our investments as well as not losing any value on any money left uninvested.

This issue has been the core of Dr. Paul's career since the 1960's. It has taken the American people a very long time to wake up to this issue, and so it is an immense credit to his character that he has shown an undying vigilance, these many years, to the pursuit of liberty. I too, have hope for America.

(If anyone is interested, here is the python code (as well as the data files) used for the graph. )

Read and Post Comments

« Previous Page -- Next Page »