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...

String Operators and Methods

Since strings are immutable sequences, all the functionality that can be used with immutable sequences can be used with strings. This includes membership testing with in, concatenation with +, appending with + , replication with *, and augmented assignment replication with * . We will discuss all of these in the context of strings in this subsection, in addition to discussing many of the string methods. Tables 2.8,2.9, and 2.10 summarize all the string methods, except for two rather specialized...

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...

Times and Dates

The calendar and datetime modules provide functions and classes for date and time handling. However, they are based on an idealized Gregorian calendar, so they are not suitable for dealing with pre-Gregorian dates. Time and date handling is a very complex topic the calendars in use have varied in different places and at different times, a day is not precisely 24 hours, a year is not exactly 365 days, and daylight saving time and time zones vary. The date-time.datetime class but not the...

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_...