﻿Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.

    ****************************************************************
    Personal firewall software may warn about the connection IDLE
    makes to its subprocess using this computer's internal loopback
    interface.  This connection is not visible on any external
    interface and no data is sent to or received from the Internet.
    ****************************************************************
    
IDLE 1.2      ==== No Subprocess ====
>>> 
>>> 
>>> def f(a, b=[]):
	b.append(a)
	print b

	
>>> f(1)
[1]
>>> f(1)
[1, 1]
>>> f(1)
[1, 1, 1]
>>> f(1)
[1, 1, 1, 1]
>>> f(1)
[1, 1, 1, 1, 1]
>>> 
>>> def f(a, b=None):
	if b == None: b = []
	b.append(a)
	print b

	
>>> f(1)
[1]
>>> f(1)
[1]
>>> f(1)
[1]
>>> f(1)
[1]
>>> 
Bob Smith 303-123-4567 ['coworker']
Sue Jones 303-867-5309 []
>>> 
Bob Smith 303-123-4567 ['coworker']
Sue Jones 970-867-5309 []
None None
['coworker', 'engineer'] <__main__.Contact instance at 0x02A34530>
Traceback (most recent call last):
  File "C:/Mark/training-cd-2.5.1/Extras/pp3e/contacts.py", line 28, in <module>
    print c1.notes, c2,notes
NameError: name 'notes' is not defined
>>> 
Bob Smith 303-123-4567 ['coworker']
Sue Jones 970-867-5309 []
None None
['coworker', 'engineer'] ['manager']
>>> 
Bob Smith 303-123-4567 ['coworker']
Sue Jones 970-867-5309 []
303-123-4567
['303', '123', '4567']
None 970-867-5309
['970', '867', '5309']
None
['coworker', 'engineer'] ['manager']
>>> 
Bob Smith 303-123-4567 ['coworker']
Sue Jones 970-867-5309 []
303 970
['coworker', 'engineer'] ['manager']
>>> 
>>> 
>>> class C:
	def __init__(s):
		s.a = 1
		s.b = 2
		s,c = 3

		
>>> X = C()
Traceback (most recent call last):
  File "<pyshell#26>", line 1, in <module>
    X = C()
  File "<pyshell#25>", line 5, in __init__
    s,c = 3
TypeError: unpack non-sequence
>>> 
>>> class C:
	def __init__(s):
		s.a = 1
		s.b = 2
		s.c = 3

		
>>> X = C()
>>> X.a
1
>>> X.b
2
>>> X.c
3
>>> 
>>> X.__dict__
{'a': 1, 'c': 3, 'b': 2}
>>> 
>>> for attr in X.__dict__.keys():
	print attr, X.__dict__[attr]

	
a 1
c 3
b 2
>>> 
>>> x.a
Traceback (most recent call last):
  File "<pyshell#41>", line 1, in <module>
    x.a
NameError: name 'x' is not defined
>>> X.a
1
>>> X.__dict__['a']
1
>>> getattr(X, 'a')
1
>>> 
>>> class C: pass

>>> X = C()
>>> X.__dict__
{}
>>> X.name = 'bob'
>>> X.job  = 'dev'
>>> X.__dict__
{'job': 'dev', 'name': 'bob'}
>>> 
>>> class C:
	def spam(self):
		return 42

	
>>> C.__dict__
{'__module__': '__main__', '__doc__': None, 'spam': <function spam at 0x02A865B0>}
>>> 
>>> X = C()
>>> X.__dict__
{}
>>> getattr(X, 'spam')
<bound method C.spam of <__main__.C instance at 0x02A9AD00>>
>>> 
>>> X.job  = 'dev'
>>> 
>>> X.__dict__
{'job': 'dev'}
>>> 
>>> getattr(X, 'spam')
<bound method C.spam of <__main__.C instance at 0x02A9AD00>>
>>> 
>>> dir(X)
['__doc__', '__module__', 'job', 'spam']
>>> 
Contact
	phone=303-123-4567
	notes=['coworker']
	name=Bob Smith

Contact
	phone=970-867-5309
	notes=[]
	name=Sue Jones

303 970
Contact
	phone=303-123-4567
	notes=['coworker', 'engineer']
	name=Bob Smith

>>> 
>>> 
>>> C
<class __main__.C at 0x02A32D20>
>>> 
>>> X
<__main__.C instance at 0x02A9AD00>
>>> 
>>> 
>>> from contact import Displayer
>>> 
>>> class C(Displayer):
	def spam(self):
		return 42

	
>>> X = C()
>>> X.name = 'bob'
>>> X.age = 40
>>> X.job = 'dev'
>>> print X
C:
job => dev
age => 40
name => bob

>>> 
Contact
	name=Bob Smith
	notes=['coworker']
	phone=303-123-4567

Contact
	name=Sue Jones
	notes=[]
	phone=970-867-5309

303 970
Contact
	name=Bob Smith
	notes=['coworker', 'engineer']
	phone=303-123-4567

>>> 
>>> import shelve
>>> db = shelve.open('contactsdb')
>>> bob = db['Bob Smith']
>>> print bob
Contact:
phone => 303-123-4567
notes => ['coworker']
name => Bob Smith

>>> db.keys()
['Bob Smith', 'Sue Jones', 'Ann Smith']
>>> 
>>> len(db)
3
>>> 
>>> for key in db:
	print key, '=', db[key]

	
Bob Smith = Contact:
phone => 303-123-4567
notes => ['coworker']
name => Bob Smith

Sue Jones = Contact:
phone => 970-867-5309
notes => []
name => Sue Jones

Ann Smith = Contact:
phone => 303-111-2222
notes => ["Bob's wife"]
name => Ann Smith

>>> print bob
Contact:
phone => 303-123-4567
notes => ['coworker']
name => Bob Smith

>>> bob.areaCode()
'303'
>>> bob.addNote('spam!')
>>> print bob
Contact:
phone => 303-123-4567
notes => ['coworker', 'spam!']
name => Bob Smith

>>> db['Bob Smith'] = bob
>>> db.close()
>>> 
>>> 
>>> db = shelve.open('contactsdb')
>>> bob2 = db['Bob Smaith']
Traceback (most recent call last):
  File "<pyshell#107>", line 1, in <module>
    bob2 = db['Bob Smaith']
  File "C:\Python25\lib\shelve.py", line 112, in __getitem__
    f = StringIO(self.dict[key])
  File "C:\Python25\lib\bsddb\__init__.py", line 223, in __getitem__
    return _DeadlockWrap(lambda: self.db[key])  # self.db[key]
  File "C:\Python25\lib\bsddb\dbutils.py", line 62, in DeadlockWrap
    return function(*_args, **_kwargs)
  File "C:\Python25\lib\bsddb\__init__.py", line 223, in <lambda>
    return _DeadlockWrap(lambda: self.db[key])  # self.db[key]
KeyError: 'Bob Smaith'
>>> 
>>> bob2 = db['Bob Smith']
>>> print bob2
Contact:
phone => 303-123-4567
notes => ['coworker', 'spam!']
name => Bob Smith

>>> 
>>> import contact
>>> tom = contact.Contact('Tom Jones', '123-456-7890')
>>> db[tom.name] = tom
>>> db.close()
>>> 
>>> 
>>> db = shelve.open('contactsdb')
>>> len(db)
4
>>> db.keys()
['Bob Smith', 'Sue Jones', 'Ann Smith', 'Tom Jones']
>>> 
>>> tom2 = db['Tom Jones']
>>> print tom2
Contact:
phone => 123-456-7890
notes => []
name => Tom Jones

>>> 
>>> for key in db:
	print key, '=', db[key]

	
Bob Smith = Contact:
phone => 303-123-4567
notes => ['coworker', 'spam!']
name => Bob Smith

Sue Jones = Contact:
phone => 970-867-5309
notes => []
name => Sue Jones

Ann Smith = Contact:
phone => 303-111-2222
notes => ["Bob's wife"]
name => Ann Smith

Tom Jones = Contact:
phone => 123-456-7890
notes => []
name => Tom Jones

>>> 
>>> for key in db:
	rec = db[key]
	if rec.phone.startswith('303'):
		print rec

		
Contact:
phone => 303-123-4567
notes => ['coworker', 'spam!']
name => Bob Smith

Contact:
phone => 303-111-2222
notes => ["Bob's wife"]
name => Ann Smith

>>> [rec for key in db if rec.phone.startswith('303')]
[]
>>> 
>>> [db[key] for key in db if db[key].phone.startswith('303')]
[<contact.Contact instance at 0x02D1B3C8>, <contact.Contact instance at 0x02D1B940>]
>>> 
>>> for x in [db[key] for key in db if db[key].phone.startswith('303')]:
	print x

	
Contact:
phone => 303-123-4567
notes => ['coworker', 'spam!']
name => Bob Smith

Contact:
phone => 303-111-2222
notes => ["Bob's wife"]
name => Ann Smith

>>> db.items()
[('Bob Smith', <contact.Contact instance at 0x02D1B328>), ('Sue Jones', <contact.Contact instance at 0x02D1B990>), ('Ann Smith', <contact.Contact instance at 0x02D28058>), ('Tom Jones', <contact.Contact instance at 0x02D28210>)]
>>> 
>>> 
>>> 
>>> line = 'aaa   bbb ccc     ddd'
>>> 
>>> line.split()
['aaa', 'bbb', 'ccc', 'ddd']
>>> 
>>> line = 'aaa,bbb,ccc,ddd'
>>> line.split(',')
['aaa', 'bbb', 'ccc', 'ddd']
>>> 
>>> line = 'aa++bbb++ccc++ddd'
>>> line.split('++')
['aa', 'bbb', 'ccc', 'ddd']
>>> 
>>> line = '111,222,333,444'
>>> 
>>> cols = line.split(',')
>>> cols
['111', '222', '333', '444']
>>> 
>>> int('11')
11
>>> map(int, cols)
[111, 222, 333, 444]
>>> 
>>> [int(x) for x in cols]
[111, 222, 333, 444]
>>> 
>>> map(eval, cols)
[111, 222, 333, 444]
>>> 
>>> map(eval, ['1 + 2', '2**2', '3'])
[3, 4, 3]
>>> map(eval, ['1 + 2', '__import__("os").system("rm -rf *)', '3'])
KeyboardInterrupt
>>> 
>>> nums = map(int, cols)
>>> nums
[111, 222, 333, 444]
>>> 
>>> tot = 0
>>> \
KeyboardInterrupt
>>> 
>>> tot = 0
>>> for x in nums: tot += x

>>> tot
1110
>>> 
>>> import operator
>>> operator.add
<built-in function add>
>>> 
>>> reduce(operator.add, nums)
1110
>>> def myreduce(func, seq):
	res = seq[0]
	for next in seq[1:]:
		res = func(res, next)
	return res

>>> myreduce(operator.add, nums)
1110
>>> nums
[111, 222, 333, 444]
>>> 
>>> sum(nums)
1110
>>> 
>>> file = open('data1.txt')
>>> file.next()
'111,222,,333,444\n'
>>> file.next()
'123,456--789\n'
>>> file.next()
'100,,200:300,:,400\n'
>>> file.next()
'200,300,400:500\n'
>>> file.next()
Traceback (most recent call last):
  File "<pyshell#198>", line 1, in <module>
    file.next()
StopIteration
>>> 
>>> 
>>> file = open('data1,txt')
>>> file.next()
'111,222,333,444\n'
>>> file.next()
'100,200,300,400\n'
>>> file.next()
'100,100,100,100\n'
>>> file.next()
Traceback (most recent call last):
  File "<pyshell#205>", line 1, in <module>
    file.next()
StopIteration
>>> 
1110
1000
400
800
>>> 
1110
1000
400
800
>>> 
1110
1000
400
800
[1110, 1000, 400, 800]
>>> 
[511, 722, 933, 1144]
>>> 
>>> D = {'a': 1, 'b': 2}
>>> 
>>> D.get('a', 0)
1
>>> D.get('c', 0)
0
>>> 
>>> 
[511, 722, 933, 1144]
{0: 511, 1: 722, 2: 933, 3: 1144}
>>> 
[511, 722, 933, 1144]
0 = 511
1 = 722
2 = 933
3 = 1144
>>> 
>>> 
[511, 722, 933, 1044]
0 = 511
1 = 722
2 = 933
3 = 1044
>>> 
>>> 
>>> import re
>>> res = re.split('111,222:333', '[,:]')
>>> res
['[,:]']
>>> 
>>> res = re.split('[,:]', '111,222:333')
>>> res
['111', '222', '333']
>>> 
>>> help(re.split)
Help on function split in module re:

split(pattern, string, maxsplit=0)
    Split the source string by the occurrences of the pattern,
    returning a list containing the resulting substrings.

>>> 
>>> line = '111,222:333'
>>> 
>>> res = re.split('[,:]', line)
>>> res
['111', '222', '333']
>>> 
>>> line.split(',')
['111', '222:333']
>>> 
[511, 722, 933, 1044]
>>> 
[511, 722, 933, 1044]
>>> 
[511, 722, 933, 1044]
>>> 
[511, 722, 933, 1044]
>>> 
[511, 722, 933, 1044]
>>> 
[511, 722, 933, 1044]
>>> 
>>> 
>>> M = [[1, 2, 3],
         [4, 5, 6],
         [7, 8, 9]]
>>> 
>>> [row[0] for row in M]
[1, 4, 7]
>>> 
>>> N = []
>>> for i in range(len(M)):
	N.append([row[i] for row in M])

	
>>> N
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
>>> 
>>> [[row[i] for row in M] for i in range(len(M))]
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
>>> 
>>> def transpose(M):
	return [[row[i] for row in M] for i in range(len(M))]

>>> M
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> transpose(M)
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
>>> 
>>> 
>>> imprt urllib
SyntaxError: invalid syntax
>>> 
>>> import urllib
>>> reply = urllib.urlopen('http://www.python.org').read()
Traceback (most recent call last):
  File "<pyshell#257>", line 1, in <module>
    reply = urllib.urlopen('http://www.python.org').read()
  File "C:\Python25\lib\urllib.py", line 82, in urlopen
    return opener.open(url)
  File "C:\Python25\lib\urllib.py", line 190, in open
    return getattr(self, name)(url)
  File "C:\Python25\lib\urllib.py", line 325, in open_http
    h.endheaders()
  File "C:\Python25\lib\httplib.py", line 856, in endheaders
    self._send_output()
  File "C:\Python25\lib\httplib.py", line 728, in _send_output
    self.send(msg)
  File "C:\Python25\lib\httplib.py", line 695, in send
    self.connect()
  File "C:\Python25\lib\httplib.py", line 663, in connect
    socket.SOCK_STREAM):
IOError: [Errno socket error] (11001, 'getaddrinfo failed')
>>> 
>>> 
>>> 
>>> reply = urllib.urlopen('http://www.python.org').read()
>>> 
>>> print reply[:2000]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <title>Python Programming Language -- Official Website</title>
  <meta name="keywords" content="python programming language object oriented web free source" />
  <meta name="description" content="      Home page for Python, an interpreted, interactive, object-oriented, extensible
      programming language. It provides an extraordinary combination of clarity and
      versatility, and is free and comprehensively ported." />
  <link rel="alternate" type="application/rss+xml" title="Community Events"
        href="http://www.python.org/channews.rdf" />
  <link rel="alternate" type="application/rss+xml" title="Python Recipes"
        href="http://aspn.activestate.com/ASPN/Cookbook/Python/index_rss" />
  <link rel="alternate" type="application/rss+xml" title="Usergroup News"
        href="http://python-groups.blogspot.com/feeds/posts/default" />
  <link rel="alternate" type="application/rss+xml" title="Python Screencasts"
        href="http://www.showmedo.com/latestVideoFeed/rss2.0?tag=python" />
  <link rel="alternate" type="application/rss+xml" title="Python Podcasts"
        href="http://www.awaretek.com/python/index.xml" />
  <link rel="alternate" type="application/rss+xml" title="Foundation News"
        href="http://pyfound.blogspot.com/feeds/posts/default" />
  <link rel="alternate" type="application/rss+xml" title="Python Enhancement Proposals"
        href="http://www.python.org/dev/peps/peps.rss" />
  <link rel="stylesheet" type="text/css" media="screen" id="screen-switcher-stylesheet"
        href="/styles/screen-switcher-default.css" />
  <link rel="stylesheet" type="text/css" media="sc&#82;een"
        href="/styles/netscape4.css" />
  <link rel="stylesheet" type="text/css" media="print"

>>> 
>>> reply = urllib.urlopen('http://www.seagate.com').read()
>>> print reply
<html>
<head>
<title>Seagate Technology</title>
<META HTTP-EQUIV="REFRESH" CONTENT="0;URL=http://www.seagate.com/www/en-us/">
</head>
<body>
</body>
</html>

>>> reply = urllib.urlopen('http://www.seagate.com/www/en-us').read()
>>> print reply




















<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html dir="ltr">

	<head>

		

<!-- Begin Comp -->


. . .
