Online Book Reader

Home Category

Learning Python - Mark Lutz [600]

By Root 1605 0
= ['black', 'purple'] # Customize to change color choices

MyGui(Tk(), 'main')

MyGui(Toplevel())

MySubGui(Toplevel())

mainloop()

# Email inbox scanning and maintenance utility

"""

scan pop email box, fetching just headers, allowing

deletions without downloading the complete message

"""

import poplib, getpass, sys

mailserver = 'your pop email server name here' # pop.rmi.net

mailuser = 'your pop email user name here' # brian

mailpasswd = getpass.getpass('Password for %s?' % mailserver)

print('Connecting...')

server = poplib.POP3(mailserver)

server.user(mailuser)

server.pass_(mailpasswd)

try:

print(server.getwelcome())

msgCount, mboxSize = server.stat()

print('There are', msgCount, 'mail messages, size ', mboxSize)

msginfo = server.list()

print(msginfo)

for i in range(msgCount):

msgnum = i+1

msgsize = msginfo[1][i].split()[1]

resp, hdrlines, octets = server.top(msgnum, 0) # Get hdrs only

print('-'*80)

print('[%d: octets=%d, size=%s]' % (msgnum, octets, msgsize))

for line in hdrlines: print(line)

if input('Print?') in ['y', 'Y']:

for line in server.retr(msgnum)[1]: print(line) # Get whole msg

if input('Delete?') in ['y', 'Y']:

print('deleting')

server.dele(msgnum) # Delete on srvr

else:

print('skipping')

finally:

server.quit() # Make sure we unlock mbox

input('Bye.') # Keep window up on Windows

# CGI server-side script to interact with a web browser

#!/usr/bin/python

import cgi

form = cgi.FieldStorage() # Parse form data

print("Content-type: text/html\n") # hdr plus blank line

print("")

print("Reply Page") # HTML reply page

print("")

if not 'user' in form:

print("

Who are you?

")

else:

print("

Hello %s!

" % cgi.escape(form['user'].value))

print("")

# Database script to populate and query a MySql database

from MySQLdb import Connect

conn = Connect(host='localhost', user='root', passwd='darling')

curs = conn.cursor()

try:

curs.execute('drop database testpeopledb')

except:

pass # Did not exist

curs.execute('create database testpeopledb')

curs.execute('use testpeopledb')

curs.execute('create table people (name char(30), job char(10), pay int(4))')

curs.execute('insert people values (%s, %s, %s)', ('Bob', 'dev', 50000))

curs.execute('insert people values (%s, %s, %s)', ('Sue', 'dev', 60000))

curs.execute('insert people values (%s, %s, %s)', ('Ann', 'mgr', 40000))

curs.execute('select * from people')

for row in curs.fetchall():

print(row)

curs.execute('select * from people where name = %s', ('Bob',))

print(curs.description)

colnames = [desc[0] for desc in curs.description]

while True:

print('-' * 30)

row = curs.fetchone()

if not row: break

for (name, value) in zip(colnames, row):

print('%s => %s' % (name, value))

conn.commit() # Save inserted records

# Database script to populate a shelve with Python objects

# see also Chapter 27 shelve and Chapter 30 pickle examples

rec1 = {'name': {'first': 'Bob', 'last': 'Smith'},

'job': ['dev', 'mgr'],

'age': 40.5}

rec2 = {'name': {'first': 'Sue', 'last': 'Jones'},

'job': ['mgr'],

'age': 35.0}

import shelve

db = shelve.open('dbfile')

db['bob'] = rec1

db['sue'] = rec2

db.close()

# Database script to print and update shelve created in prior script

import shelve

db = shelve.open('dbfile')

for key in db:

print(key, '=>', db[key])

bob = db['bob']

bob['age'] += 1

db['bob'] = bob

db.close()

Index

* * *

A note on the digital index


A link in an index entry is displayed as the section title in which that entry appears. Because some sections have multiple index markers, it is not unusual for an entry to have several links to the same section. Clicking on any link will take you directly to the place in the text in which the marker appears.

* * *

Symbols


= and == (equality operators), Comparisons, Equality, and Truth

* (repetition) operator, Basic List Operations

@ symbol, Function Decorator Basics

\ (backslash), Statement rule special cases, A Few Special

Return Main Page Previous Page Next Page

®Online Book Reader