Commit 183fd77e authored by O'Reilly Media, Inc.'s avatar O'Reilly Media, Inc.
Browse files

Initial commit

parents
Loading
Loading
Loading
Loading
+27 −0
Changes for LP4E-examples/README.txt: 27 added lines, 0 removed lines.
Original line number Diff line number Diff line
This is the distribution package for source-code examples in the book 
Learning Python 4th Edition (September, 2009).

Here, you will find one text file per book chapter, containing all the 
source code in that chapter.
  
Almost all the code in this book is short code snippets shown running 
at the interactive prompt, rather than named source files.  Because of 
that, all the code in a chapter is collected in a single file, whether
it is run interactively or not.  If a code section is listed as a file 
in the book, its name is given in the chapter file as a comment line 
of this form:

### file: spam.py

Cut and paste its code from the chapter's text file into a text editor
to save as a separate file on your computer.

These files may work best when used sequentially, in parallel with the 
book itself.  To find specific code at random, try a text search within
the chapter's file. 

For more book details, including its updates page, see:

    http://www.rmi.net/~lutz/about-lp4e.html

--Mark Lutz, November 12, 2009
+68 −0
Changes for LP4E-examples/appendixA.txt: 68 added lines, 0 removed lines.
Original line number Diff line number Diff line
setenv PYTHONPATH /usr/home/pycode/utilities:/usr/lib/pycode/package1

export PYTHONPATH="/usr/home/pycode/utilities:/usr/lib/pycode/package1"



### file: C:\Python30\mypath.pth

c:\pycode\utilities
d:\pycode\package1




#### File: main.py
import sys
print(sys.argv)



c:\Python30> python main.py a b c            # Most common: run a script file
['main.py', 'a', 'b', '-c']



c:\Python30> python -c "print(2 ** 100)"      # Read code from command argument
1267650600228229401496703205376

c:\Python30> python -c "import main"          # Import a file to run its code
['-c']

c:\Python30> python - < main.py a b c        # Read code from standard input
['-', 'a', 'b', '-c']

c:\Python30> python - a b -c < main.py        # Same effect as prior line
['-', 'a', 'b', '-c']


c:\Python30> python -m main a b c             # Locate/run module as script
['c:\\Python30\\main.py', 'a', 'b', '-c']




c:\Python30> python -m pdb main.py a b -c              # Debug a script
--Return--
> c:\python30\lib\io.py(762)closed()->False
-> return self.raw.closed
(Pdb) c

c:\Python30> C:\python26\python -m pdb main.py a b -c  # Better in 2.6?
> c:\python30\main.py(1)<module>()
-> import sys
(Pdb) c

c:\Python30> python -m profile main.py a b -c          # Profile a script

c:\Python30> python -m cProfile main.py a b -c         # Low-overhead profiler


c:\Python30> python u main.py a b -c          # Unbuffered output streams 

c:\Python30> python -?




+1850 −0

File added.

Preview size limit exceeded, changes collapsed.

LP4E-examples/ch02.txt

0 → 100644
+5 −0
Changes for LP4E-examples/ch02.txt: 5 added lines, 0 removed lines.
Original line number Diff line number Diff line
### file: script0.py

print('hello world')
print(2 ** 100)

LP4E-examples/ch03.txt

0 → 100644
+152 −0
Changes for LP4E-examples/ch03.txt: 152 added lines, 0 removed lines.
Original line number Diff line number Diff line
>>> print('Hello world!')
Hello world!
>>> print(2 ** 8)
256



>>> lumberjack = 'okay'
>>> lumberjack
'okay'
>>> 2 ** 8
256



>>> 'Spam!' * 8        # <== Learning by trying
'Spam!Spam!Spam!Spam!Spam!Spam!Spam!Spam!'



>>> X                  # <== Making mistakes
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'X' is not defined



>>> import os
>>> os.getcwd()        # <== Testing on the fly
'c:\\Python30'



>>> for x in 'spam':
...     print(x)       # <== Press Enter twice here to make this loop run
...



>>> for x in 'spam':
...     print(x)       # <== Need to press Enter twice before a new statement
... print('done')
File "<stdin>", line 3
 print('done')
     ^
SyntaxError: invalid syntax



### file: script1.py

# A first Python script
import sys                # Load a library module
print(sys.platform)
print(2 ** 100)           # Raise 2 to a power
x = 'Spam!'
print(x * 8)              # String repetition



### file: brian

#!/usr/local/bin/python                      # or: #!/usr/bin/env python
print('The Bright Side ' + 'of Life...')     # + means concatenate for strings



### file: script1.py (modified)

# A first Python script
import sys                # Load a library module
print(sys.platform)
print(2 ** 100)           # Raise 2 to a power  (later changed to 2 ** 16)
x = 'Spam!'
print(x * 8)              # String repetition
input()                   # <== ADDED           (use raw_input() in Python 2.X)



#### file: myfile.py

title = "The Meaning of Life"



>>> import myfile               # Run file; load module as a whole
>>> print(myfile.title)         # Use its attribute names: '.' to qualify
The Meaning of Life

>>> from myfile import title    # Run file; copy its names
>>> print(title)                # Use name directly: no need to qualify
The Meaning of Life



### file: threenames.py

a = 'dead'         # Define three attributes
b = 'parrot'       # Exported to other files
c = 'sketch'
print(a, b, c)     # Also used in this file



>>> import threenames                     # Grab the whole module
dead parrot sketch
>>>
>>> threenames.b, threenames.c
('parrot', 'sketch')
>>>
>>> from threenames import a, b, c        # Copy multiple names
>>> b, c
('parrot', 'sketch')

>>> dir(threenames)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'a', 'b', 'c']



>>> exec(open('script1.py').read())
win32
65536
Spam!Spam!Spam!Spam!Spam!Spam!Spam!Spam!

...change script1.py in a text edit window...

>>> exec(open('script1.py').read())
win32
4294967296
Spam!Spam!Spam!Spam!Spam!Spam!Spam!Spam!

>>> x = 999
>>> exec(open('script1.py').read())   # Code run in this namespace by default
...same outout...
>>> x                                 # Its assignments can overwrite names here
'Spam!'



### C language code (embedding)

#include <Python.h>
...
Py_Initialize();                                    // This is C, not Python
PyRun_SimpleString("x = 'brave ' + 'sir robin'");   // But it runs Python code



L = [1, 2]        # Make a 2-item list
L.append(L)       # Append L as a single item to itself
L                 # Print L
Loading