Archive for June, 2012

Recently I needed the ability to use Reportlab’s flowables, but place them in fixed locations. Some of you are probably wondering why I would want to do that. The nice thing about flowables, like the Paragraph, is that they’re easily styled. If I could bold something or center something AND put it in a fixed location, then that would rock! It took a lot of Googling and trial and error, but I finally got a decent template put together that I could use for mailings. In this article, I’m going to show you how to do this too. (more…)

The other day I saw a question on StackOverflow about how to dynamically destroy and create panels after a certain amount of time has passed. I told the fellow that he could use the examples from one of my blog articles where I destroyed and created buttons, but the dude just didn’t get it. So I wrote a simple example where the panel displays a count down and then destroys itself and is promptly replaced with another panel. (more…)

I meant to write up a post on this within a week of reading about it, but then things got busy and I forgot. Anyway, there’s a new widget in the wxPython toolkit called the ShortcutEditor. You can read about it on the wxPython mailing list. You’ll probably have to update the agw lib folder on your local drive unless you run from SVN a lot. It may be included in the latest Phoenix builds too, but I’m not sure. If you want to check it out, do the following on the command line (assuming you have svn installed)


svn checkout http://svn.wxwidgets.org/svn/wx/wxPython/3rdParty/AGW/agw/

That will create an “agw” folder in whatever your current directory is. You’ll want to copy that over the top of the one that’s in your wxPython installation. On my machine, it was in the following location: C:\Python27\Lib\site-packages\wx-2.9.3-msw\wx\lib There’s a simple demo application included in the source code. I’m reproducing it here so you can see try running it too:


import wx
import wx.lib.agw.shortcuteditor as SE

class MyFrame(wx.Frame):

def __init__(self, parent):

wx.Frame.__init__(self, parent, -1, "ShortcutEditor Demo")

bar = wx.MenuBar()
menu = wx.Menu()

menu.Append(101, "&Mercury", "This the text in the Statusbar")
menu.Append(102, "&Venus", "")
menu.Append(103, "&Earth", "You may select Earth too")
menu.AppendSeparator()
menu.Append(104, "&Close", "Close this frame")

bar.Append(menu, 'File')
self.SetMenuBar(bar)

dlg = SE.ShortcutEditor(self)
dlg.FromMenuBar(self)

if dlg.ShowModal() == wx.ID_OK:
# Changes accepted, send back the new shortcuts to the TLW wx.MenuBar
dlg.ToMenuBar(self)

dlg.Destroy()

# our normal wxApp-derived class, as usual

app = wx.App(0)

frame = MyFrame(None)
app.SetTopWindow(frame)
frame.Show()

app.MainLoop()

If you run the code above, you should see something like this on your screen:

Then you can start changing short cut keys. I thought it was really cool. According to the file’s internal documentation, it can change menu shortcut keys and AcceleratorTables. You should give it a try sometime. I think this will be a pretty helpful utility to add to my own scripts.

Most computer users of this day and age use drag and drop (DnD) instinctively. You probably used it to transfer some files from one folder to another this week! The wxPython GUI toolkit provides drag and drop functionality baked in. In this tutorial, we’ll see just how easy it is to implement! (more…)

The other day I was asked if there was a way to sort a dictionary by value. If you use Python regularly, then you know that the dictionary data structure is by definition an unsorted mapping type. Some would define a dict as a hash table. Regardless, I needed a way to sort a nested dictionary (i.e. a dictionary of dictionaries) based on a value in the nested dictionaries so I could iterate over the keys in the specified order. We’ll spend some time looking at an implementation I found. (more…)

There are several cool ways to create PDFs with Python. In this article we will be focusing on a cool little tool called rst2pdf, which takes a text file that contains Restructured Text and converts it to a PDF. The rst2pdf package requires Reportlab to function. This won’t be a tutorial on Restructured Text, although we’ll have to discuss it to some degree just to understand what’s going on. (more…)

I didn’t think I’d do one of these link roundups this week, but I decided to go for it anyway. In fact, I found a cool little tool for obtaining information about installed Python packages and querying packages available on PyPI (Python Package Index) called yolk too! Anyway, here’s this week’s reading list. I hope you enjoy them!

Feel free to drop me a line on your favorite articles this week in the comments below.

There are several code analysis tools for Python. The most well known is pylint. Then there’s pychecker and now we’re moving on to pyflakes. The pyflakes project is a part of something known as the Divmod Project. Pyflakes doesn’t actually execute the code it checks, unlike pychecker. Of course, pylint also doesn’t execute the code. Regardless, we’ll take a quick look at it and see how pyflakes works and if it’s better than the competition. (more…)

Python code analysis can be a heavy subject, but it can be very helpful in making your programs better. There are several Python code analyzers that you can use to check your code and see if they conform to standards. pylint is probably the most popular. It’s very configurable, customizable and pluggable too. It also checks your code to see if it conforms to PEP8, the official style guide of Python Core and it looks for programming errors too. We’re going to spend a few minutes looking at some of the things you can do with this handy tool. (more…)

Today we’ll spend some time looking at three different ways to make Python submit a web form. In this case, we will be doing a web search with duckduckgo.com searching on the term “python” and saving the result as an HTML file. We will use Python’s included urllib modules and two 3rd party packages: requests and mechanize. We have three small scripts to cover, so let’s get cracking! (more…)