Posts Tagged ‘Python’

(on Technorati , Del.icio.us)

Python 3 Iterable Unpacking Catch-all

I’m one of those terrible people that still haven’t switched over to using Python3 …just yet. But one thing that pleasantly surprised me recently was learning of the “new” starred expression catch-all that can be used when unpacking iterables. This lets you use up to one starred expression to receive the rest of the items of an iterable during an unpacking assignment.

full_name = "Steven W J Brown"

#python2
names             = full_name.split()                # ['Steven', 'W', 'J', 'Brown']
first, last       = names[0], names[-1]              # 2.A: ignore middle names
first, mids, last = names[0], names[1:-1], names[-1] # 2.B: store middle names

#python3, 
first, *_, last    = full_name.split() # 3.A: ignore middle names
first, *mids, last = full_name.split() # 3.B: store middle names

It’s easy to read, eliminates the need for an intermediary variable and makes the code cleaner. And it can be used in loops. More details here: PEP 3132. Love it.

I really have to start re-configuring my environments for python3….

Gedit 3.2 GDP Completions on Ubuntu 11.10 Oneiric Ocelot

Curiosity gets the best of me sometimes. Okay, most of the time. Did you know GNOME’s text editor, gedit, has a plethora of extensions which can basically transform it into an IDE? Something I’ve always wanted is intellisense-style autocompletion. The closest thing I’ve found for gedit is GDP Completions Plugin in the gedit-developer-plugins package in Ubuntu.

sudo aptitude install gedit-developer-plugins

However, there’s a bug in that package and the popup menu doesn’t actually work. Ctrl + Space is supposed to bring it up. So you want to add the Gedit Developer Plugins PPA and upgrade to the more recent version.

sudo add-apt-repository ppa:user/ppa-name
sudo apt-get update
sudo apt-get upgrade gedit-developer-plugins

If you try to run gedit now, you’ll notice it won’t… run, that is. Great. I know, right? The problem is that the bzr plugin (also included in the gedit-developer-plugins package) is trying to use the gtk2 version of bzr-gtk, but that doesn’t work in the gtk3 gedit. Anyway, you can pull a copy of the gtk3 bazaar plugin into your local bzr plugins directory. (I found this info here). Create ~/.bazaar/plugins/ if it doesn’t exist.

mkdir ~/.bazaar/plugins
cd ~/.bazaar/plugins
bzr branch lp:bzr-gtk/gtk3 gtk

The gedit-developer-plugins package and gedit should work after that! An alternative to the above would be to add a PPA that includes bzr-gtk 3. Not sure if one exists at the moment, but that would be a cleaner solution. And you thought it would be simple. I know I did. ๐Ÿ˜›

It’s not as polished or featured as other implementations, but it’s a good start. Here’s a screenshot after I type os. then hit Ctrl + Space:

Further reading

External tools plugin

Rolling your own gedit 3 plugin

File List Applet – now with more autotools!

I decided that before I did any more work on the applet, I would improve its installation process to make it easier for people to try it out. So, the process to get and build the source now looks like this:

Download

  • Browse source here.
  • Download the source: bzr branch http://stevenbrown.ca/src/FileListApplet

Install

  1. Install dependencies (Ubuntu package names given): sudo apt-get install python-xdg python-gnome2-desktop python-gtk2 python-pyinotify
  2. Branch the source using the bzr command, above.
  3. cd into the directory.
  4. ./configure --prefix=/usr (the prefix is important!)
  5. make
  6. sudo make install
  7. If the applet does not show up in your Add to Panel menu, try restarting the bonobo-activation-server: killall bonobo-activation-server.

Autotools

Autotools is pretty much the standard in source package management on linux. Except for the name, there is nothing automatic about autotools. Every encounter I’ve had with autotools has usually defeated me and left me frustrated and leaving whatever I was working on to do something else. For me, because I had labeled it the next step, it basically stalled the entire project for a while. Most people tend to copy and paste other projects’ autotools setup, but I figured that was overkill for my purposes and I didn’t find anything that quite suited me. I looked at gnome-blog, but it seemed like some stuff wasn’t quite working properly and some was completely unnecessary… in fact, this seemed to be a trend when looking at the autotools stuff in projects. Why is this? Autotools is not simple and due to this simple fact, I think it fails completely on many levels. Developers massage it enough to get it working, but few actually understand it all – I know I sure don’t! So please forgive the sloppiness and feel free to send patches. ๐Ÿ™‚ I gave up doing a couple things, like getting the revision number (bzr revno) and including it in the version string (see configure.ac). I know it’s probably something super simple, but I couldn’t seem to pass a variable containing a string as the version….

I feel that GNOME, as a platform for development, could seriously benefit from some kind of frontend to autotools that handled GNOME development nicely and hid as much as possible from the developer (including all those nasty config files that pollute the package tree). Anyway, I did not have an enjoyable time grappling with autotools, but I’ll end this mini-rant here.

File List Applet – GNOME Panel Applet

This is kind of a proof of concept I’ve been playing with. The idea is that finding a file within a folder is often easier by type, and you are often only interested in the most recently modified file. The problem with a file manager is that although you can easily sort by either type or modification time, you cannot filter your view of all the other files you’re not interested in. I previously wanted to address this issue within Nautilus, (and I still believe this functionality would be wonderful in Nautilus), but I ended up doing this much less ambitious applet as a proof of concept.

This applet will let you add any number of folders to it, and will try to categorize the files automatically and intelligently. Currently, it’s more automatic than intelligent as it just looks at the mime-type. Even so, I’ve found it especially useful for keeping track of all my downloads:

Steven is catching up on the latest on planet.gnome.org and has downloaded a couple screencasts demoing the latest and greatest. These files are typically 2-10 megabytes, so they didn’t download instantly. Steven continues reading and forgot about the screencasts until a couple hours later. At that time, he can simply click on the File List Applet, select Downloads, select Video, and look at the top of the list for the newest files. Steven is happy. When finished, he can follow the same process to delete them – without once opening his file manager and being assaulted with ALL the files in his Downloads folder.

Ultimately, I would like to extend the idea to provide the same type/subtype menu system for all files under all folders – a type of summary – but I have not implemented that yet. There are other features in the cooking pot, as well, but I have to get started on some “RL” tasks… like my resume. ๐Ÿ™‚

Screencast

I had a problem recording audio, so I ended up typing as narration. Unfortunately, this makes the YouTube one pretty unwatchable, but you can give the “HQ” version a try.

Download

No tarball yet as it’s still extremely rough.
Browse the source here.
Branch the source: bzr branch http://stevenbrown.ca/src/FileListApplet

Install

Update 2009/04/05: Updated install instructions here. (Some people don’t look at the comments….)

Installing will require some manual modifications.

  1. First, make sure you have the following packages (Ubuntu): python-xdg, python-gnome2-desktop, python-gtk2, python-pyinotify
  2. Then branch the source.
  3. Adjust the FileListApplet.server file’s location to wherever you keep it.
  4. Then copy FileListApplet.server to /usr/lib/bonobo/servers/.
  5. Restart the bonobo-activation-server. killall bonobo-activation-server
  6. Add it to the panel like other applets.

Update 2008/12/05: Added a couple screenshots.

WiiWare and WordPress Update

It was a nice fall day a couple days ago. I really enjoyed this sight, in my backyard. ๐Ÿ™‚ Quite overcast and dull, the last couple of days, though.

Games

I updated my About page with my Tetris Party friend code. (Add me!!) It’s pretty fun, but I say that as someone who has never owned a Tetris game before, so I’m not a Tetris vet… (unlike Shirley and Alex, who think the DS game is far superior, apparently). My only real complaint with the game so far is the music – they could have done AWESOME things with it. I want a hoppin 8bit remix of these classic tunes… the included midi, even the classic stuff, is pretty dry and slow. Need something with a faster pace. The computer moves at seemingly impossible speeds at levels above 12, but I suspect that some people out there can play like that, as well. Scary. (Especially the top ranked in the Americas, Java AI … hmmm…) Anyway, I can’t beat level 13 yet. And I think I still like Dr Mario for VS more… so fun. … but Tetris Party offers tonnes of modes, including 4-player vs! That’s pretty cool. Maybe I’ll become a Tetris snob, yet.

While on the topic of Wii(Ware), I also got World of Goo. It’s a really great physics based puzzle title. I really love the dark and comical graphic design (reminds me of Tim Burton’s stuff). The music in Goo is epic, as well. I can’t believe everything was made by two guys (+1 for Wii optimizations); two guys in debt! They’re my heroes. Seriously. Both Tetris Party and World of Goo are the most expensive games on WiiWare ($12 US, $15 US, respectively), but I haven’t been disappointed. And with Goo especially, I don’t have any problem supporting the little guys making great stuff. That’s where I’d like to be. ๐Ÿ™‚

WordPress

I updated my wp-upgrade script to display a big warning to deactivate all your plugins before continuing (wouldn’t want to damage your database!). Then I used it to update to WordPress 2.6.3. I think that makes me largely up-to-date.

Rhythmbox Plugin: Jump to Playing 0.3(.1)

Update 2012-01-29 – Deprecated:
Brief: This plugin has been deprecated. For future versions and updates, please go here.

Less Brief:If you’re using a newer version of Rhythmbox, you’ll need a newer version of this plugin. I’m not sure exactly which RB version the plugin format changed, but I am currently using Rhythmbox 2.90.1 on Ubuntu 11.10 and this plugin no longer works. However, Timo Loewe has ported jump-to-playing to Rhythmbox 3, all properly hosted and everything! Get it and any future updates here (https://github.com/dmo60/JumpToPlaying). This is the version I’m now using. Thanks, Timo! ๐Ÿ˜€

– – – – –

The following pertains to the deprecated version of the plugin…

– – – – –

This plugin will display the View : Jump to Playing Song link as a button in the toolbar and/or as link in the Browserโ€™s context menu. Other Rhythmbox plugins can be found here.

Screenshots


Using a future version of Rhythmbox – patch here – the menu item will appear in a plugin placeholder, above Properties. Otherwise, it will appear at the bottom, like previous versions.


From version 0.3, the Open Folder plugin will also be placed in the plugin placeholder.

Changes Since 0.2

Just a couple small changes since 0.2.

  • Selecting the context menu option in the preferences will now display the link in PodcastView and PlaylistView popups, as well as BrowserView and QueuePlaylistView.
  • Assuming the patch on bug 557152 is applied, this will place the context menu items in a plugin placeholder and allow the Preferences to remain the last menu item.
  • Update (Nov 10 2008): Modified version of patch has been applied to RB development trunk, so the next version of Rhythmbox will have this update. Yay! Version 0.3.1 of jump-to-playing is to account for the modifications. Please use it. ๐Ÿ™‚

Download

jump-to-playing-0.3.tar.gz jump-to-playing-0.3.1.tar.gz
Browse the Source: Here
Grab the Source: bzr branch "http://stevenbrown.ca/src/jump-to-playing/"

Installation

  1. Extract the jump-to-playing folder into your ~/.gnome2/rhythmbox/plugins/ directory (or ~/.local/share/rhythmbox/plugins/ directory, if the .gnome2 directory doesn’t exist). Completely replace any previous versions.
  2. (Re)Start Rhythmbox and enable the plugin in Edit : Plugins.

Todo

From my previous post.

  • the gconf keys in gconf-editor say they have no schema. The main pluginsโ€™ keys have a schema and donโ€™t give a warning. Definitely not serious, though.
  • it currently adds/removes the ui string when the options are toggled in the configure dialog. I have a feeling it might be better to only add/remove them in the activation/deactivation, and just hide/show here. Maybe faster?
  • it currently hides the browser button in small display mode. That has nothing to do with the jump-to-playing button. That should be in core, if it was decided that was the desired behaviour.
  • to hide the buttons in the small display, it checks the value at activation, and it connects to the View menuโ€™s toggleButtonโ€™s โ€œtoggledโ€ signal. So whenever itโ€™s toggled, the gconf value for the small display mode is checked, but I think thereโ€™s a delay sometimes. Pushing Ctrl D quickly a few times may result in incorrect UI presented. I remember deciding that this is due to a delay set on the gconf callback to overcome some other bugโ€ฆ.

All patches are welcome! ๐Ÿ™‚

Update 2008/10/26: Added screenshot, descriptions to screenshots, and link to main plugin page.

Update 2008/11/10: Added link to version 0.3.1 and added description.

Update 2010/12/08: Added alternative installation directory.

Jump-to-Playing Rhythmbox Plugin TODO

I haven’t looked at the plugin for a while, but I’ve been meaning to reproduce my “todo” list for it that I wrote on the rb-dev list a while back.

  • the gconf keys in gconf-editor say they have no schema. The main plugins’ keys have a schema and don’t give a warning. Definitely not serious, though.
  • it currently adds/removes the ui string when the options are toggled in the configure dialog. I have a feeling it might be better to only add/remove them in the activation/deactivation, and just hide/show here. Maybe faster?
  • it currently hides the browser button in small display mode. That has nothing to do with the jump-to-playing button. That should be in core, if it was decided that was the desired behaviour.
  • to hide the buttons in the small display, it checks the value at activation, and it connects to the View menu’s toggleButton’s “toggled” signal. So whenever it’s toggled, the gconf value for the small display mode is checked, but I think there’s a delay sometimes. Pushing Ctrl D quickly a few times may result in incorrect UI presented. I remember deciding that this is due to a delay set on the gconf callback to overcome some other bug….
  • in the context menus, ‘Properties’ should really be the last item. They need a placeholder put in the UI core. UPDATE: I’ve filed a bug with a patch attached here. Jump-to-Playing and other plugins will need to be updated when the patch is applied. I’ve just done it on my local copy w/Rhythmbox HEAD…. Looks like this:
    Before (red) and After (green) applying the patch and using an updated plugin

    Before (red) and After (green) applying the patch and using an updated plugin

  • Show the context menu item in PodcastView and PlaylistView popups, as well. I’ve added this in my local copy, already. Maybe I should just bump the version and release….

WordPress Upgrade Script

Even though the WordPress upgrade is easy, it’s troublesome. So I wrote a script to do it for me. Yep. There’s lots of these out there. And a lot of hosts (including mine) have a one-click install/update thing. But for some reason, I decided to write my own script. In python. Got to use a bunch of modules I’ve never used before, so it was a good experience.

How to Use

Run this script from the directory that contains your wordpress directory, on your server. I think it requires Python version 2.3. I ran it with 2.3.5. Use python -V to check the version. There’s two methods to run it.

With Prompts

1: Be prompted to push the Enter key to continue at each major step. Nice for the first time.

python wp-upgrade.py

Example output:

python ../src/wp-upgrade/wp-upgrade.py 
Current WordPress Version: '2.5'
Newest WordPress Version:  2.6.1
Downloading wordpress-2.6.1.tar.gz ...
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 
Create working wordpress directory... 
			[[Press Enter to Continue]]

Update WordPress root contents...  
			[[Press Enter to Continue]]

'/tmp/tmprpJTKu/wordpress' -> 'wordpress.working'
wp-trackback.php,  wp-config-sample.php,  wp-settings.php,  wp-rss2.php,  readme.html,  index.php,  wp-links-opml.php,  wp-pass.php,  wp-feed.php,  wp-register.php,  wp-rdf.php,  wp-rss.php,  wp-commentsrss2.php,  license.txt,  wp-comments-post.php,  wp-blog-header.php,  wp-load.php,  wp-mail.php,  wp-atom.php,  wp-cron.php,  wp-app.php,  xmlrpc.php,  wp-login.php,  
Replace wp-admin and wp-includes... 
			[[Press Enter to Continue]]


Update default themes and plugins...  
			[[Press Enter to Continue]]

'/tmp/tmprpJTKu/wordpress/wp-content' -> 'wordpress.working/wp-content'
index.php,  
'/tmp/tmprpJTKu/wordpress/wp-content/plugins' -> 'wordpress.working/wp-content/plugins'
akismet,  hello.php,  
'/tmp/tmprpJTKu/wordpress/wp-content/themes' -> 'wordpress.working/wp-content/themes'
default,  classic,  

Backup original, Rename working....
			[[Press Enter to Continue]]


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
### VISIT 'http://stevenbrown.ca/blog/wordpress/wp-admin/upgrade.php' in your browser. ###
After that, you're All Done!
Go re-enable all your plugins and make sure everything works.
If you need to, you can always roll back by renaming the backup to 'wordpress'.


			[[Press Enter to Continue]]

Without Prompts

2. Do everything without a prompt. Do this by passing the -q option. The -q is for quiet and prints the same messages, but doesn’t wait for the user to press Enter. Blasts through the whole upgrade in one step.

python wp-upgrade.py -q

Example output would be the same as the above, minus the “[[Push Enter to Continue]]” bits.

What’s it do?

If you look at the above output, which is from my own site, you can see what it does. It will compare your installed wordpress version and to the latest available from http://www.wordpress.org. If your version is older, it will download the new one, extract it, perform the appropriate steps to update the old one. Note that the default themes and plugins will be overwritten, which is fine as long as you didn’t customize them. After it’s done, your wordpress directory should be up-to-date (you just have to visit the upgrade page), and you should have a wordpress 2.5 backup containing the directory as it was before running the script (and 2.5 would be the appropriate version). Also, there’s two variables you will (probably) want to customize: wpsite and wpdir. That’s it, basically.

Download

Browse the source and download the script here.

python treeview toggle

Had this post sitting around. Seems finished. May be helpful to someone. /me waves Wand of Publish +1

I was confused when I was playing around with this basic concept: adding a toggle widget to the treeview in pygtk. The reason for this is an inconsistency in the api model – or at least how I perceived it. With a regular toggle button, you create the widget and can manipulate it once it is drawn. The checkmark is toggled and the “toggled” signal is emitted. I only connect a signal handler to the toggled signal after. However, following this same logic, I created a list with a column of toggle widgets and tried clicking them… but nothing happened. The problem here was the toggled value was linked to the list’s data, and the data wasn’t actually changing. Even though the toggle signal was being emitted, the checkmark wasn’t being toggled because the data wasn’t changing, so I thought there was a problem with my code and the signal wasn’t being emitted. But actually, it would make more sense if it was a “clicked” signal that was being emitted, not the “toggled” signal.

Example source code:

#!/usr/bin/env python

# example basictreeviewtoggle.py

import pygtk
pygtk.require('2.0')
import gtk
import gobject

class BasicTreeViewToggleExample:

    # close the window and quit
    def delete_event(self, widget, event, data=None):
        gtk.main_quit()
        return False

    def column_toggled(self, cell, path, model):
        # get toggled iter, and value at column 0
        iter = model.get_iter((int(path),))
        val = model.get_value(iter, 0)

        # toggle the value
        val = not val

        # set new value
        model.set(iter, 0, val)


    def __init__(self):
        # create a new window
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.set_title("Basic TreeView Toggle Example")
        self.window.set_size_request(200, 200)
        self.window.connect("delete_event", self.delete_event)

        # create a ListStore with two columns to use as the model
        self.model = gtk.ListStore(gobject.TYPE_BOOLEAN, str)

        # create some list items
        for item in range(5):
            self.model.append([False, 'item %i' % item])

        # create the TreeView using the model
        self.view = gtk.TreeView(self.model)

        # create a CellRendererText to render the data
        self.cellrenderer_text = gtk.CellRendererText()
        self.cellrenderer_toggle = gtk.CellRendererToggle()
        self.cellrenderer_toggle.connect('toggled', self.column_toggled, self.model)

        # create the TreeViewColumns to display the data
        self.tvcolumntext = gtk.TreeViewColumn('TreeViewColumn 1')
        self.tvcolumntoggle = gtk.TreeViewColumn('tog', self.cellrenderer_toggle, active=0)

        # add the TreeViewColumns to the TreeView
        self.view.append_column(self.tvcolumntoggle)
        self.view.append_column(self.tvcolumntext)




        # add the cell to the tvcolumn and allow it to expand
        self.tvcolumntoggle.pack_start(self.cellrenderer_toggle, False)
        self.tvcolumntext.pack_start(self.cellrenderer_text, True)

        
        # set the cell "text" attribute to column 0 - retrieve text
        # from that column in treestore
        self.tvcolumntext.add_attribute(self.cellrenderer_text, 'text', 1)


        
        # make it searchable
        self.view.set_search_column(1)

        # Allow sorting on the column
        self.tvcolumntext.set_sort_column_id(1)

        # Allow drag and drop reordering of rows
        self.view.set_reorderable(True)

        self.sw = gtk.ScrolledWindow()
        self.sw.add(self.view)
        self.window.add(self.sw)

        self.window.show_all()

def main():
    gtk.main()

if __name__ == "__main__":
    tvexample = BasicTreeViewToggleExample()
    main()

AttrDict Python Module

I’ve been doing quite a bit of Python hacking in my recent free time. A couple days ago, I had the desire for a dictionary that I could access key values just like object attribute names. For example, in the dictionary d={'key':'value'}, I wanted to be able to use d.key to return 'value'. I thought this would be a common desire and probably already exists (and might even be built-in somehow), so I went on to #python and asked the folk there. Nobody knew of anything. So I set off to write my own. I called it AttrDict.

Yesterday, I found something similar already exists. It’s called ObDict. Similar? Very! ๐Ÿ˜› But different enough that I continued mine… and have a somewhat complete python module of my own. So far it’s been a nice opportunity to learn more about Python objects and use the unittest module for unit testing.

It’s not 100% coverage of the dict interface, but it’s pretty close and I think it’s usable enough that I can slap a quick release together and work on other stuff. Bugs/patches/testing/suggestions welcome.

Update:

Wooops, forgot to write a bit more about it. Here’s the docstring from the module:

Simple extension of the built-in dictionary so that dictionary keys are mirrored
as object attributes. This is for convenience. You can do things like this:

d = dict() #standard dict, for comparison
a = AttrDict() #

d[‘ok’] = ‘this is ok’
d.ok -> AttributeError raised
d.ok = ‘test’ -> AttributeError raised

You cannot assign attributes to standard Python dicts.

a[‘ok’] = ‘this is ok’
a.ok -> ‘this is ok’ #attribute is automatically created
a.ok = ‘changed’
a[‘ok’] -> ‘changed’ #and the dict value is automatically updated
a.ok2 = ‘new value’ #adding new attribute, ok2
a[‘ok2’] -> ‘new value’ #dict key is automatically created

This introduces a limitation on the dictionary keys such
that they must be strings and provide valid Python syntax for accessing.

For example:

{‘123′:’valid’} #is a valid dictionary

but

mydict.123 #is not valid Python syntax.

Attempting to create a key that cannot be accessed through an attribute name
will raise an AttributeError Exception.

This module has not been built for optmization. Some of the method
documentation has been taken from Python’s dict documentation. For more info
on a particular method, refer to that. I’ve tried to mirror the effects of
the standard dict as closely as possible.