1. Wywietlanie zawartoci katalogu. ******************************************

import os, sys, stat
def describedir(start):
    def describedir_helper(arg, dirname, files):
        """ Funkcja pomocnicza dla opisywania katalogw """
        print "Directory %s has files:" % dirname
        for file in files:
            # znajduje pen ciek do pliku (katalog + nazwa pliku)
            fullname = os.path.join(dirname, file)
            if os.path.isdir(fullname):
                # jeli jest to katalog, nie trzeba znajdowa rozmiaru
                print ' '+ file + ' (subdir)'
            else:
                # znajdowanie rozmiaru i drukowanie info.
                size = os.stat(fullname) [stat.ST_SIZE]
                print '  '+file+' size=' + `size`

    # Rozpoczynamy "spacer"
    os.path.walk(start, describedir_helper, None)

>>> import describedir
>>> describedir.describedir('testdir')
Directory testdir has files:
  describedir.py size=939
  subdir1 (subdir)
  subdir2 (subdir)
Directory testdir\subdir1 has files:
  makezeros.py size=125
  subdir3 (subdir)
Directory testdir\subdir1\subdir3 has files:
Directory testdir\subdir2 has files:

 
2. Modyfikowanie znaku zachty. ***********************************************

# modifyprompt.py

import sys, os
class MyPrompt:
    def __init__(self, subprompt='>>> '):
        self.lineno = 0
        self.subprompt = subprompt
    def __repr__(self):
        self.lineno = self.lineno + 1
        return os.getcwd()+'|%d'%(self.lineno)+self.subprompt

sys.ps1 = MyPrompt()
sys.ps2 = MyPrompt('... ')

h:\David\book> python -i modifyprompt.py
h:\David\book|1>>> x = 3
h:\David\book|2>>> y = 3
h:\David\book|3>>> def foo():
h:\David\book|3...     x = 3    # drugorzdny znak zachty jest obsugiwany
h:\David\book|3...
h:\David\book|4>>> import os
h:\David\book|5>>> os.chdir('..')
h:\David\6>>>                   # uwaga, znak zachty si zmieni!


3. Unikanie wyrae regularnych. **********************************************

import string
file = open('pepper.txt')
text = file.read()
paragraphs = string.split(text, '\n\n')

def find_indices_for(big, small):
    indices = []
    cum = 0
    while 1:
        index = string.find(big, small)
        if index == -1:
            return indices
        indices.append(index+cum)
        big = big[index+len(small):]
        cum = cum + index+ len(small)

def fix_paragraphs_with_word(paragraphs, word):
    lenword = len(word)
    for par_no in range(len(paragraphs)):
        p = paragraphs[par_no]
        wordpositions = find_indices_for(p, word)
        if wordpositions == []: return
        for start in wordpositions:
            # szuka 'pepper' naprzd
            indexpepper = string.find(p, 'pepper')
            if indexpepper == -1: return -1
            if string.strip(p[start:indexpepper]) != '':
                # co innego ni spacja jest pomidzy nimi!
                continue
            where = indexpepper+len('pepper')
            if p[where:where+len('corn')] == 'corn':
                # po tym natychmiast nastpuje 'corn'!
                continue
            if string.find(p, 'salad') < where:
                # po tym nie nastpuje 'salad'
                continue
            # Wreszcie! mamy zrobi zmian!
            p = p[:start] + 'bell' +p[start+lenword:]
            paragraphs[par_no] = p           # zmienia argument zmienny!

fix_paragraphs_with_word(paragraphs, 'red')
fix_paragraphs_with_word(paragraphs, 'green')

for paragraph in paragraphs:
    print paragraph+'\n'


4. Opakowywanie pliku tekstowego za pomoc klasy. *****************************

import string

class FileStrings:
    def __init__(self, filename=None, data=None):
        if data == None:
            self.data = open(filename).read()
        else:
            self.data = data
        self.paragraphs = string.split(self.data, '\n\n')
        self.lines = string.split(self.data, '\n')
        self.words = string.split(self.data)
    def __repr__(self):
        return self.data
    def paragraph(self, index):
        return FileStrings(data=self.paragraphs[index])
    def line(self, index):
        return FileStrings(data=self.lines[index])
    def word(self, index):
        return self.words[index]

>>> from FileStrings import FileStrings
>>> bigtext = FileStrings('pepper.txt')
>>> print bigtext.paragraph(0)
This is a paragraph that mentions bell peppers multiple times. For
one, here is a red Pepper and dried tomato salad recipe. I don't like
to use green peppers in my salads as much because they have a harsher
flavor.
>>> print bigtext.line(0)
This is a paragraph that mentions bell peppers multiple times. For
>>> print bigtext.line(-4)
aren't peppers, they're chilies, but would you rather have a good cook
>>> print bigtext.word(-4)
botanist

>>> print bigtext.paragraph(2).line(2).word(-1)
'cook'
