In searching how to do conversions recently, I found some nice code for this, using bit shifting: [...] But then I learned there is now a built-in bin function included in 2.6: [...] And now in 2.6, the binary literal (0b) removes the need for a function at all: [...] I was interested to find Guido Van Rossum’s comment opposed to a bin function, from a 2006 posting: [...] Which is sensible, but I still like having the bin function and I’m glad they added it anyway.
tag archive: python
Python Sequence Generator
Updated, 11 December 2010
Here’s some code to generate an arbitrary unique sequence of any combination of numbers, letters, or any character other than whitespace. (See also “Old Post” below for more on the what and why.)
I turned this into a class that implements the iterator protocol, and added a unit test module. I may have went overboard with the unit testing, but I looked at it as a learning exercise, and it’s kind of rewarding in a deep and meaningful way to be able to press a button and put a module through its paces. Final code counts:
sequence_generator.py180 lines
sequence_generator_test.py317 lines
The code is now released with the GNU GPL license v3:
sequence_generator.tgz (16K)
It’s written for Python 2.6, but I ran the files through the 2to3 tool with an eye to an easy conversion to Python 3.
The constructor takes …
20 November 2009 at 10:31 pm • 670 words
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 …
15 November 2009 at 10:58 am • 570 words
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 …
27 June 2009 at 12:57 pm • 400 words
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. …
5 April 2009 at 2:10 pm • 510 words
Python script for backing up Twitter statuses
Update, 14 October 2012 I don’t think this script works anymore, but I’ve found that the All My Tweets web site is good for my purposes. As of today, it will create a single page of your most recent 3200 tweets, which is the current limit of the API provided by Twitter.
4 comments • 18 March 2009 at 8:51 pm • 880 words
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 …
2 comments • 29 March 2008 at 10:34 am • 680 words
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 …
3 comments • 23 March 2008 at 10:33 pm • 680 words
category: free software • tags: books, python, regex
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 …
12 comments • 23 February 2008 at 4:11 pm • 1,360 words
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 …
9 comments • 6 November 2007 at 8:40 pm • 1,180 words




