Identifiers and Keywords

When we create a data item we can either assign it to a variable, or insert it Object into a collection. (As we noted in the preceding chapter, when we assign in refer- Python, what really happens is that we bind an object reference to refer to ences the object in memory that holds the data.) The names we give to our object 16 references are called identifiers or just plain names. A valid Python identifier is a nonempty sequence of characters of any length that consists of a start character and...

Bigdigitspy

The first program we will review is quite short, although it has some subtle aspects, including a list of lists. Here is what it does Given a number on the command line, the program outputs the same number onto the console using big digits. At sites where lots of users share a high-speed line printer, it used to be common practice for each user's print job to be preceded by a cover page that showed their username and some other identifying details printed using this kind of technique. We will...

Main WindowStyle Programs

Although dialog-style programs are often sufficient for simple tasks, as the range of functionality a program offers grows it often makes sense to create a complete main-window-style application with menus and toolbars. Such applications are usually easier to extend than dialog-style programs since we can add extra menus or menu options and toolbar buttons without affecting the main window's layout. In this section we will review the bookmarks-tk.pyw program shown in Figure 15.4. The program...

BNF Syntax and Parsing Terminology

Parsing is a means of transforming data that is in some structured format whether the data represents actual data, or statements in a programming language, or some mixture of both into a representation that reflects the data's structure and that can be used to infer the meaning that the data represents. The parsing process is most often done in two phases lexing also called lexical analysis, tokenizing, or scanning , and parsing proper also called syntactic analysis . For example, given a...

Lex YaccStyle Parsing with PLY

PLY Python Lex Yacc is a pure Python implementation of the classic Unix tools, lex and yacc. Lex is a tool that creates lexers, and yacc is a tool that creates parsers often using a lexer created by lex. PLY is described by its author, David Beazley, as reasonably efficient and well suited for larger grammars. It provides most of the standard lex yacc features including support for empty productions, precedence rules, error recovery, and support for ambiguous grammars. PLY is straightforward to...

Playlist Data Parsing

Hand- In the previous section's second subsection we created a handcrafted regex- PLY crafted based parser for .m3u files. In this subsection we will create a parser to do the m3u m u same thing, but this time using the PyParsing module. An extract from a .m3u parser file is shown in Figure 14.6 523 lt , and the BNF is shown in Figure 14.7 557 As we did when reviewing the previous subsection's .pls parser, we will review the .m3u parser in three parts first the creation of the parser, then the...

Thedel Special Method

Values Used Class Hierarchy

The_del_ self special method is called when an object is destroyed at least in theory. In practice,_del_ may never be called, even at program termination. Furthermore, when we write del x, all that happens is that the object reference x is deleted and the count of how many object references refer to the object that was referred to by x is decreased by 1. Only when this count reaches 0 is_del_ likely to be called, but Python offers no guarantee that it will ever be called. In view of this,_del_...

Generategridpy

One frequently occurring need is the generation of test data. There is no single generic program for doing this, since test data varies enormously. Python is often used to produce test data because it is so easy to write and modify Python programs. In this subsection we will create a program that generates a grid of random integers the user can specify how many rows and columns they want and over what range the integers should span. We'll start by looking at a sample run invalid literal for int...