ElementTree Tidy HTML Tree Builder

The TidyHTMLTreeBuilder parser can read (almost) arbitrary HTML files, and turn them into well-formed element trees. This parser uses a library version of Dave Raggett’s HTML Tidy utility to fix any problems with the HTML before converting it to XHTML (the XML version of HTML). Note: If you don’t want to (or cannot) install binary Python extensions, you can use the TidyTools module […]

ElementTree Tidy HTML Tree Builder Read More »

The ElementSoup Module

The ElementSoup module is a (slightly experimental) wrapper for Leonard Richardson’s robust BeautifulSoup HTML parser, which turns the BeautifulSoup data structure into an element tree. The resulting combo is similar to ElementTidy, but a lot less picky. And therefore, a lot more practical. Which is good. Code (latest versions): Just grab the files and put them in your project directory,

The ElementSoup Module Read More »

How do you update the button text in Tkinter?

To update the text displayed on a button in Tkinter, you can use the config() method or the configure() method to modify the button’s text attribute. Here’s an example: import tkinter as tk def update_text(): button.config(text=”New Text”) # Update the button text root = tk.Tk() button = tk.Button(root, text=”Old Text”) button.pack() update_button = tk.Button(root, text=”Update

How do you update the button text in Tkinter? Read More »

The xmllib module

The xmllib module This module provides a simple XML parser, using regular expressions to pull the XML data apart. The parser does basic checks on the document, such as checking that there is only one top-level element, and checking that all tags are balanced. You feed XML data to this parser piece by piece (as

The xmllib module Read More »

(the eff-bot guide to) The Standard Python Library 

Overview This chapter describes a number of modules that are used to parse different file formats. Markup Languages Python comes with extensive support for the Extensible Markup Language XML and Hypertext Markup Language (HTML) file formats. Python also provides basic support for Standard Generalized Markup Language (SGML). All these formats share the same basic structure (this isn’t so strange, since both

(the eff-bot guide to) The Standard Python Library  Read More »

atexit module in python

You’ve been working on a complex Python script, investing hours of effort and dedication to ensure its smooth execution. You’ve accounted for every possible scenario and exception, meticulously crafting your code. However, there’s one often-overlooked aspect: the graceful exit. When your script completes its task or encounters an unexpected error, how can you ensure that

atexit module in python Read More »

xmlhttp: A Simple XML-Over-HTTP Class

This module implements a simple helper class, HTTPClient, which can send an XML document (represented either as an element tree or a string) to a remote server, and parse the result into an element tree. A Simple XML-Over-HTTP Helper (File: HTTPClient.py) The main workhorse is the do_request method, which uses the httplib library module for all protocol-related stuff. The HTTP class represents a connection to

xmlhttp: A Simple XML-Over-HTTP Class Read More »

The Tkinter Label Widget

The Label widget is a standard Tkinter widget used to display a text or image on the screen. The label can only display text in a single font, but the text may span more than one line. In addition, one of the characters can be underlined, for example to mark a keyboard shortcut. When to use the

The Tkinter Label Widget Read More »

The email Package

(New in 2.2) Tools for manipulation of all sorts of email messages. The email package replaces simpler modules, such as rfc822 and mimetools, with a much more flexible message object, and associated parsers. Parsing Messages  The easiest way to parse messages is to use the message_from_file or message_from_string helpers in the email toplevel module. The former takes a file handle, the latter a string object: Parsing

The email Package Read More »

Pixel Access Objects

I just added support for a new “pixel access” method to PIL 1.1.6. The load method now returns a pixel access object, which behaves like a 2-dimensional mapping. You can use the access object on both sides of an assignment statement; as an expression, it fetches the given pixel value, and as an assignment target, it updates

Pixel Access Objects Read More »

Storing BLOB Data in SQLITE

The SQLITE database has limited support for large binary objects (BLOBS). There’s a limitation of 1 megabyte for each row of data, and the database uses NUL bytes to separate columns in the storage. Note: This limitation has been removed in SQLITE 3.0. For best operation in large tables, the SQLITE author recommends keeping the row size around

Storing BLOB Data in SQLITE Read More »

SQLite Basics

The SQLite library is a light-weight embedded SQL engine, with a nice DB API compliant Python binding, originally developed by Michael Owens. A newer version, called sqlite3, was added to Python’s standard library in Python 2.5. import sqlite3 db = sqlite3.connect(“database.db”) c = db.cursor() c.execute(“create table mytable (timestamp, size, file)”) for file in os.listdir(“.”): c.execute( “insert into mytable values (?, ?,

SQLite Basics Read More »

The Standard Python Library: New Modules in Python 2.1

__future__ A registry of features supported by the from __future__ compiler directive.difflib Tools to detect differences and calculate deltas between lists of objects.distutils (package) Tools to distribute and install Python extensions.doctest A test framework, based on docstrings. To use this for general testing, you can write test scripts and use doctest to make sure the test scripts work as

The Standard Python Library: New Modules in Python 2.1 Read More »

The Tkinter BitmapImage Class

The Tkinter BitmapImage Class The BitmapImage class provides a simple image class, for monochrome (two-color) images. When to use the BitmapImage Class This class can be used to display bitmap images in labels, buttons, canvases, and text widgets. The bitmap loader reads X11 bitmap files. To use other formats, use the PhotoImage class. Patterns An X11 bitmap image consists of

The Tkinter BitmapImage Class Read More »

A Python Code Generator

A Python Code Generator A common complaint about Python’s syntax is that it’s impossible to generate Python code on the fly, from a program. Here’s a simple helper class that helps you do exactly that: # # a Python code generator backend # # fredrik lundh, march 1998 # # fredrik@pythonware.com # http://www.pythonware.com # import sys,

A Python Code Generator Read More »

The ImageGrab Module

The ImageGrab Module The ImageGrab module can be used to copy the contents of the screen or the clipboard to a PIL image memory. The current version works on Windows only. Functions  grab  ImageGrab.grab() ⇒ image ImageGrab.grab(bbox) ⇒ image (New in 1.1.3) Take a snapshot of the screen, and return an “RGB” image. The bounding box argument can be used

The ImageGrab Module Read More »

Element Library Functions

Element Library Functions March 22, 2004 | Fredrik Lundh ElementTree 1.3 may include a new module, ElementLib, with a number of convenient helper functions. The exact contents are yet to be determined; here are some of the current proposals (from various sources, in no specific order): Helpers to add subelements, with a nicer syntax. Wrappers to access

Element Library Functions Read More »

The StringIO module

This module implements an in-memory file object. This object can be used as input or output to most functions that expect a standard file object. Example: Using the StringIO module to read from a static file # File: stringio-example-1.py import StringIO MESSAGE = “That man is depriving a village somewhere of a computer scientist.” file

The StringIO module Read More »

XPath in ElementTree

ElementTree provides limited support for XPath expressions. The goal is to support a small subset of the abbreviated syntax; a full XPath engine is outside the scope of the core library. The 1.2 release supports simple element location paths. In its simplest form, a location path is one or more tag names, separated by slashes (/).

XPath in ElementTree Read More »

The ImageEnhance Module in PIL

The ImageEnhance module contains a number of classes that can be used for image enhancement. Example  Vary the Sharpness of an Image import ImageEnhance enhancer = ImageEnhance.Sharpness(image) for i in range(8): factor = i / 4.0 enhancer.enhance(factor).show(“Sharpness %f” % factor) Also see the enhancer.py demo program in the Scripts directory. Interface  All enhancement classes implement a common interface, containing a single

The ImageEnhance Module in PIL Read More »

The Tkinter Radio Button Widget

The Radiobutton is a standard Tkinter widget used to implement one-of-many selections. Radiobuttons can contain text or images, and you can associate a Python function or method with each button. When the button is pressed, Tkinter automatically calls that function or method. The button can only display text in a single font, but the text may span more than

The Tkinter Radio Button Widget Read More »

Python Imaging Library

The Python Imaging Library (PIL) adds image processing capabilities to your Python interpreter. This library supports many file formats, and provides powerful image processing and graphics capabilities. The current stable version is 1.1.7, released in November 2009. The next release will be 1.2, with “early 2011” as the release target. Prebuilt versions of PIL are available in most Linux

Python Imaging Library Read More »

Python ImagingBook Concepts

The Python Imaging Library handles raster images; that is, rectangles of pixel data. Bands An image can consist of one or more bands of data. The Python Imaging Library allows you to store several bands in a single image, provided they all have the same dimensions and depth. To get the number and names of bands

Python ImagingBook Concepts Read More »

Tkinter Popup Menus

Tkinter is a standard library in Python that is used to create Graphical User Interface (GUI) based desktop applications. It is pronounced (Tk inter).  You can learn more about Tkinter in the tkinter tutorial. How to create pop-up menus in tkinter?

Tkinter Popup Menus Read More »

The Tkinter Spinbox Widget

When to use the Spinbox Widget The Spinbox widget can be used instead of an ordinary Entry, in cases where the user only has a limited number of ordered values to choose from. Note that the spinbox widget is only available in Python 2.3 and later when linked against Tk 8.4 or later. Also, note that several

The Tkinter Spinbox Widget Read More »

The Consumer Interface in Python

The consumer interface is a simple “data sink” interface, used by standard Python modules such as xmllib and sgmllib. Other examples include the GZIP consumer and PIL’s ImageParser class. The consumer will typically convert incoming raw data in some way, and pass it on to a another layer. For example, XML parsers implementing this protocol usually parse the data stream into a stream of

The Consumer Interface in Python Read More »