Archive for May, 2012

Last week, I was reading Brett Cannon’s blog where he talks about function signatures and decorating the main function. I didn’t follow everything he talked about, but I thought the concept was really interesting. The following code is an example based on a recipe that Mr. Cannon mentioned. I think it illustrates what he’s talking about, but basically if provides a way to remove the standard

if __name__ == "__main__":
   doSomething()

Anyway, here’s the code.

#----------------------------------------------------------------------
def doSomething(name="Mike"):
    """"""
    print "Welcome to the program, " + name
 
#----------------------------------------------------------------------
def main(f):
    """"""
    if f.__module__ == '__main__':
        f()
    return f
 
main(doSomething)

The nice part about this is that the main function can be anywhere in the code. If you like to use decorators, then you can re-write this as follows:

#----------------------------------------------------------------------
def main(f):
    """"""
    if f.__module__ == '__main__':
        f()
    return f
 
#----------------------------------------------------------------------
@main
def doSomething(name="Mike"):
    """"""
    print "Welcome to the program, " + name

Note that the main function has to come before you can decorate the doSomething function. I’m not sure how or even if I would use this sort of trick, but I thought it might be fun to experiment with at some point.

Everyone who uses computers regularly knows that they can copy and paste text. What they might not know is that when you copy something, it goes into a location known as the “clipboard”. Most programs provide access to a clipboard of some sort, whether it be just within the program itself or to the system clipboard, which allows items to be copied to other applications. The wxPython GUI toolkit also provides clipboard access, which you can use to copy text to and from within your program and even to the system clipboard. You can also copy images to the clipboard. In this tutorial, we’ll take a look at how you can do this in your own code. (more…)

I haven’t done a weekly round-up of Python links because no one seemed to really care when I was doing those. However I thought I’d give it one more try and see if there was any interest this time around. This past week, I finished reading my first web2py cookbook. I’m told there’s another book too, but I haven’t checked it out yet. I suspect had I read the other one first, the cookbook would have made more sense. If you’ve been a regular reader of this site, you’ll notice I’ve done several other book reviews the last couple of weeks too. Speaking of reading, here are just a few of the articles that stuck out to me this week:

I don’t want to overload you on links and I know this one is older, but I have been really interesting in following Katie C’s gaming adventures. So I’ll leave you on that note. I hope you enjoy the articles as much as I did.

I have read about web2py on several occasions, but never used it myself. Then a few weeks ago, a representative from Packt Publishing contacted me about reviewing their new cookbook about web2py. It’s written by seven authors, namely: Richard Gordon, Pablo Martin Mulone, Mariano Reingart, Bruno Cezar Rocha, Massimo Di Pierro, Michele Comitini and Jonathan Lundell. I have to admit that I wondered how you could have a coherent book with so many authors, but since it’s a cookbook, it works out pretty well. (more…)

Robin Dunn, creator and mastermind behind wxPython, announced today on his blog and the wxPython-dev mailing list that he had gotten wxPython 2.9 (Phoenix) to build successfully for Python 3.2 on Mac. In fact, he posted a Quicktime video that shows the build and the tests running in Python 3! According to wxPython-dev, once they have some Python 3 buildbot slaves set up, then snapshot builds can be made and posted here.

I’m pretty excited! Now if only the Python Imaging Library would convert too…

I’m a little excited today as my first patch (and first ticket even!) has been accepted. And it really didn’t take very long either. Less than 24 hours after I had submitted my first patch, I got my contribution added. I did have to submit to more variations of the patch though as my first one wasn’t quite right. I wanted to give a shout out to Brian Curtin and Eli Bendersky who helped me figure all this stuff out and made my first foray into core Python development a success. Personally, I think it would have been a success even if the patch wasn’t accepted as I still learned a lot along the way.

Things to take away from the experience:

  • Try to stay on topic! I actually found a second issue with the paragraph I was fixing in the devguide and that probably should have gone in a separate bug report.
  • Number your patches! I don’t know why I didn’t think of that, but Eli told me I should do that in the future to make it less confusing for the committer. That was a face palm moment.

I’ve been reading some of the supposedly “easy bugs” and trying to figure out where else I can help. I already spotted another typo in the docs that are included with Python itself which I’ll probably try to fix. Of course, I want to actually contribute to the code, not just the documentation, but I am probably more likely to be able to find documentation bugs I can help with. Hopefully with more experience I’ll be able to contribute more effectively. Happy hacking my fellow Pythoneers!

As I mentioned in my last article, I figured I’d try to find something that I could patch in Python and submit it. While writing the other article, I stumbled on a minor error in the Python devguide in the Windows section. While it’s nowhere near as cool to patch a piece of documentation as I think it would be to patch Python, I think it’s rather appropriate for me as I tend to contribute more documentation than anything else lately. So I am going to explain the process as I found it. (more…)

I was reading Hynek Schlawack’s excellent article on becoming a Python core developer and decided to find out just how hard it would be to get set up on my machine so that I could be ready to do core development myself, should I ever get the honor of being a part of the team. Since I run on Windows the most, I’m just going to talk about how I got set up for that OS. I’ve been thinking about trying to help with core development for a while anyway, so now’s as good a time as any. Let’s find out just how easy or hard the setting up process is! (more…)

The other day, I thought it would be fun to create a little program that could generate QR codes and show them onscreen with wxPython. Of course, I wanted to do it all with Python, so after a little looking, I came across 3 candidates:

I tried python-qrcode and pyqrnative since they worked on Windows as well as Mac and Linux. They also didn’t require anything except the Python Imaging Library. The pyqrcode project requires several other prerequisites and didn’t work on Windows, so I didn’t want to mess with it. I ended up taking some old code based on my Photo Viewer application and modified it slightly to make this a QR Code viewer. If I’ve piqued your interest, then read on! (more…)

I recently bought Modern Tkinter for Busy Python Developers by Mark Roseman from Amazon and just finished it yesterday. I think it’s pretty new, but I can’t find the release date for it now. Anyway, let’s get on with the review!

Quick Review

  • Why I picked it up: I bought this book because I’d been planning to dig into other Python GUI toolkits anyway and I haven’t seen a new Tkinter book since John Grayson’s Python and Tkinter Programming
  • Why I finished it: It has a pretty good writing style, although the widgets chapters started to drag
  • I’d give it to: Anyone wanting to make their Tkinter applications look more native or learn a little about Tkinters new theming system.

(more…)