tag archive: python

Python Sequence Generator

I was looking for Python “sequence generator” code earlier today. By that, I’m thinking of a method to increment an arbitrary string of letters and numbers in an orderly way.

I figured there should be code samples out there to make quick work of it, but it was more challenging to find something than expected. Part of it might be my weak conceptual grasp of the subject. Maybe I just lacked the necessary search terms. With the words I did have — sequence and generator — I got a lot of static since these terms describe basic Python concepts.

I finally resorted to crafting my own sequence generator. To you, Future Searcher, I hope it will be useful. It is easy enough to convert this into a function, which I did for my script. (Or maybe you’ll want an actual generator!) I won’t elaborate on it more. If you’re looking for this, you probably know everything you need to know.

(I’m releasing this into the public domain. Use the code freely and …

Python CSS Shrinker for WordPress

This isn’t really specific to WordPress, but it preserves the first comment for WordPress theme management, WordPress is awesome, and I’m specifically using this for WordPress style sheets, so I’m making the connection.

I wrote this script to condense my style sheets into smaller files for faster download times. I’m all about customer service here.

And it’s pleasing to me to distill the CSS into a purer essence and reduce network overhead. (Not that the throngs of visitors to this site are noticeably plugging the tubes.)

One benefit of using compression this way is that I can be more liberal in my stylesheet comments, knowing they won’t burden the “production” .css file delivered to my patrons.

I didn’t go for maximum shrinkage, but this provides sufficient minification for my tastes. Read below for additional commentary and caveats.

(I’m putting this in the public domain. Use at your own risk!)

css_shrinker.py

#!/usr/bin/python3

import sys
import re

css = open(sys.argv[1], 'r').read()
unshrunk_size = len(css)

# save first comment which wordpress needs to display …

Python script: Extract slashdot user names and ID numbers from a story discussion

I was reading slashdot yesterday and some (inane) comments about people’s user ID numbers made me curious about the overall distribution of IDs in slashdot discussions. It didn’t take long to take what I’d learned about the Beautiful Soup screen-scraping library from writing my Twitter status backup script to get some simple information.

Here’s a quick and crude little Python script that does the job as of yesterday’s slashdot HTML/CSS scheme. It spits out the ID number and username, which you then might send through the Unix sort command with various options, e.g.:

slashdot_info.py ‘[slashdot story url]‘ | sort -un

I’m placing this code into the public domain after disclaiming any responsibility for your use of it. Suggested enhancement: Add option to get number of comments for each user and allow sorting by that variable also.

(Update: This only grabs the first 50 comments. I’m not invested enough in the problem at the moment to want to figure out how to grab the entire discussion. That’s another exercise left to you.)…

NetBeans Python Plugin

NetBeans speaks Python now! In my first pass at learning Python last year, I had looked around at IDE options and noted that NetBeans didn’t seem to support it yet. Eclipse probably had a Python plugin at the time, but it may have seemed too daunting to try out. I can’t remember now. In any case, I wasn’t needing a full-fledged IDE then. Python’s interactive prompt and IPython were good for my purposes, but I knew eventually I’d want something more.

Now I’m back into learning more Python, and was excited and happy to see all the work that has been done for the language in NetBeans. I downloaded a standalone installer for NetBeans 6.5.1 which painlessly installed the IDE on my Ubuntu 8.10 machine, and then from Tools » Plugins selected Python, and that was that. Starting a new project let me select a Python project, and now I have all the power of a robust, professional IDE to use in developing Python programs.

The NetBeans …

Python script for backing up Twitter statuses

I was looking for a way to backup my tweets (I find that I’m reluctantly giving over to calling Twitter posts by that name, but I don’t feel good about it) and found this “recipe” at ActiveState’s web site:

Recipe 576594: Backup/download your tweets or anyone’s tweets.

It’s a nice simple Python script that saves messages to a text file. Works great, although I wanted some more. It doesn’t save timestamps and also leaves HTML link tags in there. The recipe also introduced me to a nifty library, “Beautiful Soup“, which is a “Python HTML/XML parser designed for quick turnaround projects like screen-scraping.”

Since I’ve been intending to get back into learning and using Python, this was a good opportunity to play around a bit. Starting from Zach’s code and with the nice Beautiful Soup documentation, it didn’t take long to figure out how to extract more from the Twitter pages. Also there was some fun with regular …

Python: Regex Test Function

 

More fun with Python and regular expressions. Following up on a previous post, I wanted to share a little test regex function I wrote in Python to help me as I work through the regular expression book.

I’m mostly working at the interactive prompt and had been running commands from Python re (the regex module) as I experimented with different regular expressions. This was good as I spent time in help(re) and built up some muscle memory for Python regex functions, but it was becoming repetitious to keep typing the commands for analyzing the results of a match. Once I started learning about writing functions in Python, I realized it was time to enhance my regex learning experience with a simple Python function.

I know there are sophisticated regex tools out there and probably simple functions that do more than this, but it was fun to …

Goals: Learning Python and Regular Expressions

I don’t want to go all 7 Habits of Highly Effective People on you, but I’ll say this: I think it’s good to have goals and to work towards your goals.

I like this quote, which I’ve seen attributed to Zig Ziglar:

“Most people fail to reach their goals not because their plans are too simple or too complicated. Most people don’t reach their goals because they’re not committed and willing to follow their plans.”

I think that’s very true. There are so many things I haven’t done because I didn’t follow through on a plan.

What this means for you, loyal reader, is that one of the reasons I’m not accomplishing the goal of writing more for the web site (including a write-up of my trip to Boston for the FSF meeting) is that I’m working towards other goals of learning Python and more about regular expressions.

These two books are great:

Tomboy: Bulk import files with the D-Bus interface and Python

Last month I mentioned I wanted to import a bunch of notes from my old PIM into Tomboy, but expected a lot of copying and pasting busywork since I didn’t know how to do a mass import. Fortunately, a real live Tomboy developer dropped by to clue me in on the D-Bus interface with which I could use Python to script something up. (Thanks, Sandy!)

Having a starting point, my first search turned up a great Ars Technica article by Ryan Paul that gave me all the information I needed: “Using the Tomboy D-Bus interface.” Read it for an explanation of what DBus is about, and some good tips for using Tomboy’s API.

With this post I’m just going to focus on the simple task of loading a bunch of flat files in to Tomboy, with some elaboration on character set issues I ran in to along the way.

(Python was great …

NautilusSvn and New Emblems

After writing last week about how I missed the Windows-only Tortoise(CVS|SVN) in GNU/Linux and sharing my simple scripts for running Subversion commands from Nautilus, I found Nautilus Svn by Jason Field, an Extension written in Python. (And available under the GPL v2.) It’s very nice, and adds (among other things) a major feature that you want in a graphical source control tool integrated with your file manager: visual cues of file status via icons. It uses Nautilus’s Emblems:

Those are what I have by default with the “Human” theme in Ubuntu 7.04/Feisty Fawn. So what are emblems? I’ve previously noticed the house emblem on my home dir and the lock emblem on system directories (hard to miss them). There is also a tab in file properties dialogs for Emblems, and in Nautilus there is the Edit » Backgrounds and Emblems… menu. I had seen the hints, but so far I haven’t had cause to …