Archive for July 18th, 2012

SQLite is a self-contained, server-less, config-free transactional SQL database engine. Python gained the sqlite3 module all the way back in version 2.5 which means that you can create SQLite database with any current Python without downloading any additional dependencies. Mozilla uses SQLite databases for its popular Firefox browser to store bookmarks and other various pieces of information. In this article you will learn the following:

  • How to create a SQLite database
  • How to insert data into a table
  • How to edit the data
  • How to delete the data
  • Basic SQL queries

This article will be similar in function to the recent SQLAlchemy tutorial that appeared on this site earlier this month. If you want to inspect your database visually, you can use the SQLite Manager plugin for Firefox or if you like the command line, you can use SQLite’s command line shell (more…)

Note: The following post was originally published over on Dzone. I changed the title because I already wrote several XML parsing articles and don’t want my readers to get this one confused with the others.

One of the common tasks I am given in my day job is to take some data format input and parse it to create a report or some other document. Today we’ll look at taking some XML input, parsing it with the Python programming language and then creating a letter in PDF format using Reportlab, a 3rd party package for Python. Let’s say my company receives an order for three items that I need to fulfill. The XML for that could look like the following code: (more…)

Occasionally I’ll see someone on the wxPython users group ask about how to make the wx.Notebook change pages (or tabs) programmatically. So I decided it was about time I figured it out. Here is some code that works for me:

import random
import wx
 
########################################################################
class TabPanel(wx.Panel):
    #----------------------------------------------------------------------
    def __init__(self, parent, page):
        """"""
        wx.Panel.__init__(self, parent=parent)
        self.page = page
 
        colors = ["red", "blue", "gray", "yellow", "green"]
        self.SetBackgroundColour(random.choice(colors))
 
        btn = wx.Button(self, label="Change Selection")
        btn.Bind(wx.EVT_BUTTON, self.onChangeSelection)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(btn, 0, wx.ALL, 10)
        self.SetSizer(sizer)
 
    #----------------------------------------------------------------------
    def onChangeSelection(self, event):
        """
        Change the page!
        """
        notebook = self.GetParent()
        notebook.SetSelection(self.page)
 
########################################################################
class DemoFrame(wx.Frame):
    """
    Frame that holds all other widgets
    """
 
    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""        
        wx.Frame.__init__(self, None, wx.ID_ANY, 
                          "Notebook Tutorial",
                          size=(600,400)
                          )
        panel = wx.Panel(self)
 
        notebook = wx.Notebook(panel)
        tabOne = TabPanel(notebook, 1)
        notebook.AddPage(tabOne, "Tab 1")
 
        tabTwo = TabPanel(notebook, 0)
        notebook.AddPage(tabTwo, "Tab 2")
 
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(notebook, 1, wx.ALL|wx.EXPAND, 5)
        panel.SetSizer(sizer)
        self.Layout()
 
        self.Show()
 
#----------------------------------------------------------------------
if __name__ == "__main__":
    app = wx.App(False)
    frame = DemoFrame()
    app.MainLoop()

The main thing to know is that you need to use SetSelection (or ChangeSelection) to force the Notebook widget to change pages. That’s it! This code was tested on Windows 7 with Python 2.7.3 and wxPython 2.9.3.1 (Classic). See also this discussion on Nabble.