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

Nanny state taken to ridiculous and disturbing new heights

May 10, 2008 at 10:22 AM | categories: stupidity, liberty rants | View Comments

Man Jailed After Daughter Fails To Get GED

What has our world come to? A man gets put in jail because his daughter hasn't gotten some dumb, meaningless certificate? A man is losing his job, by dictate of the state, because his daughter, even though she's actively going to school, can't pass her math test?

Oh, but judge David Niehaus says that "if she passes the test, her father could get out of jail before his six-months sentence is up." Thank you David Niehaus for your generous and fair --- ZZZZTTT GOVERNMENT COMPLIANCE CIRCUITS FAILING --- Nowwaitjustagoddamnminute, how is this guy supposed to help his daughter pass the test if he's in jail??

ZZZTTTT ... This blog post is now interrupted by this Big Booming Governmental Voice:

"Be careful children, because Nanny Government is watching you! If you break any of our arbitrary rules you too may find your parents taken away because obviously they are very bad people and you deserve to be taken care of by the magnificent and omnipresent State. We'll make sure you get the indoctrination .. ahem .. education you need that your parents are unworthy and furthermore incapable of providing, despite their meaningless, pitifull attempts. Do not resist. You will bend to our desires. Don't worry, out of all of this, you will become a more productive and obedient citizen of our slave-driven society, we promise!"

ZZZZTTT -- Think for yourselves People!

The courts are subverted. They will never serve us. They serve the desires of big government and we're only going to see more and more of this leviathan usurp our rights and lay waste to our economy and our liberties.

Oh, and fuck you DIS-honorable David Niehaus.

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

"Libertarian" Hypocrites -- authoritarians in disguise.

April 29, 2008 at 08:38 PM | categories: liberty rants | View Comments

I shredded my Libertarian Party membership card today. I'll be calling the national office tomorrow to formally rescind my membership. (Thank you Ian for the great example.)

Power corrupts. This is even true when it comes to so-called libertarians when they feel that they can champion the cause of liberty from within the political juggernaut. Instead, they let the power consume them and they forget the very reason they fight at all. Case in point: last Friday the Libertarian party issued a press release "calling for increased coordination and communication between federal and state law enforcement agencies in order to help to apprehend and convict child predators and those who engage in child pornography."

It's not the going after child predators part that bothers me. It's the complete reversal of the party's principal of individual voluntaryism in favor of authoritarian statism that bothers me. It used to be that the LP wanted to get RID OF the FBI and the CIA etc (completely! It was actually listed in the party platform.) Now they want to expand the relationship between federal and state agencies. Tell me, how exactly does giving the federal government control over yet another area of our lives constitute anything but an "initiation of force"? Not only has the LP platform been utterly compromised by this act; what they are suggesting is UNCONSTITUTIONAL!

Article 1, section 8 and Article 3 section 3 of the US constitution clearly lays out three crimes that are under the jurisdiction of the federal government:

  • Piracy in international waters.
  • Counterfeiting US coins
  • Treason against the US

(Source: Harry Browne)

All other crimes are within the jurisdiction of the states by virtue of the 9th and 10th amendments. (And the states are more than well equipped to handle the cases at hand.)

I cannot, in good conscience, give one more dime to an organization that claims to want to reduce the size and scope of government and then does the exact opposite whenever it suits them.

What's worse is that this seems all to just be a power play to get rid of Mary Ruwart as a candidate for president. Shameful! Mary Ruwart is about as libertarian as they get when it comes to people directly involved with the LP. She alone has brought more people to the ideas of liberty (through her numerous books and essays) than all the officers of the LP combined. I would vote for Mary Ruwart with a clear conscience. I would never, ever, vote for what has become of the status quo LP candidates today -- The likes of Bob Barr, George Phillies, Mike Gravel (!!?!?). They are all phonies. They are infiltrators. They care nothing at all for the principals of freedom and the the ideals of non initiation of force. If they ever had any desire at all to advance liberty, they must have grown tired of being all alone with their "unpopular" ideas and are willing to pervert their principles so as to gain acceptance by the mediocre majority. For all purposes, the LP is dead.

I'm afraid the days of the Ron Pauls, the Harry Browns, and even the Michael Badnariks leading the LP are long gone. To take their place : unprincipled, power hungry, cronyistic, hypocritical fakes. I'm outta here.

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

« Previous Page -- Next Page »