Skip to content

Commit

Permalink
Updated some code examples
Browse files Browse the repository at this point in the history
Signed-off-by: Rick Copeland <[email protected]>
  • Loading branch information
rick446 committed Oct 8, 2012
1 parent f49eca1 commit fd3d2af
Show file tree
Hide file tree
Showing 15 changed files with 66 additions and 51 deletions.
5 changes: 5 additions & 0 deletions 01-BasicPythonSyntax/hello.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env python
def sayhello(name):
print 'Hello, ' + name

sayhello('Rick')
4 changes: 0 additions & 4 deletions 01-BasicPythonSyntax/reverse_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,3 @@ def reverse_list(lst):

print reverse_list([1,2,3])

def reverse_list_alt(lst):
return reversed(lst)

print reverse_list([1,2,3])
15 changes: 2 additions & 13 deletions 01-BasicPythonSyntax/sum_even_values.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
def sum_even_values(lst):
result = 0
for index, element in enumerate(lst):
if index % 2 == 0:
result += element
return result

print sum_even_values([1,2,3])
print sum_even_values([100, 200])


def sum_even_values_alt(lst):
result = 0
lst1 = lst[::2]
for element in lst1:
result += element
return result

print sum_even_values_alt([1,2,3])
print sum_even_values_alt([100, 200])
print sum_even_values([1,2,3])
print sum_even_values([100, 200])
11 changes: 10 additions & 1 deletion 02-Builtins/ascii2str.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
def ascii2str(lst):
return ''.join(map(chr, lst))
s = ''
characters = map(chr, lst)
for ch in characters:
s += ch
return s

print ascii2str([86, 77, 87, 97, 114, 101])

def ascii2str_alt(lst):
characters = map(chr, lst)
return ''.join(characters)

print ascii2str_alt([86, 77, 87, 97, 114, 101])
4 changes: 2 additions & 2 deletions 03-FileIO/print_file.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
def print_file(fp):
for line in fp:
print line.rstrip() # remove trailing whitespace
print line[:-1]

fp = open('/etc/hosts')
print_file(fp)
fp.close()

def print_file_line_numbers(fp):
for index, line in enumerate(fp):
print index + 1, line.rstrip()
print index + 1, line[:-1]

fp = open('/etc/hosts')
print_file_line_numbers(fp)
Expand Down
2 changes: 2 additions & 0 deletions 04-UsingModules/convert_dt_to_ts.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
import time

def dt_to_ts(dt):
return time.mktime(dt.timetuple())
2 changes: 1 addition & 1 deletion 04-UsingModules/print_file.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
def print_file_line_numbers(fp):
for index, line in enumerate(fp):
print index + 1, line.rstrip()
print index + 1, line,

import StringIO

Expand Down
2 changes: 1 addition & 1 deletion 04-UsingModules/print_file_debug.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
def print_file_line_numbers(fp):
import pdb; pdb.set_trace()
for index, line in enumerate(fp):
print index + 1, line.rstrip()
print index + 1, line[:-1]

import StringIO

Expand Down
22 changes: 22 additions & 0 deletions 05-Strings/get_word_count.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import StringIO

text = '''
The quick brown fox jumped over the lazy dog.
The dog was very lazy and the fox was quite quick.
'''

def isalpha(ch):
return ch.isalpha()

def get_words_count(fp):
result = {}
for line in fp:
for word in line.split():
word = ''.join(filter(isalpha, word.lower()))
if word in result:
result[word] += 1
else:
result[word] = 1
return result

print get_words_count(StringIO.StringIO(text))
33 changes: 7 additions & 26 deletions 05-Strings/get_words.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
import StringIO

text = '''
The quick brown fox jumped over the lazy dog.
The dog was very lazy and the fox was quite quick.
'''

def isalpha(ch):
return ch.isalpha()

Expand All @@ -9,30 +16,4 @@ def get_words(fp):
result.append(word)
return result

text = '''
The quick brown fox jumped over the lazy dog.
The dog was very lazy and the fox was quite quick.
'''

import StringIO
print get_words(StringIO.StringIO(text))

def get_words_count(fp):
result = {}
for line in fp:
for word in line.split():
word = ''.join(filter(isalpha, word.lower()))
if word in result:
result[word] += 1
else:
result[word] = 1
return result

print get_words_count(StringIO.StringIO(text))


def print_centered_words(words):
for word in words:
print word.center(80)

print_centered_words(['The', 'quick', 'brown', 'fox'])
5 changes: 5 additions & 0 deletions 05-Strings/print_centered_words.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def print_centered_words(words):
for word in words:
print word.title().center(80)

print_centered_words(['The', 'quick', 'brown', 'fox'])
4 changes: 2 additions & 2 deletions 06-Regex/regex_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ def find_integers(fp):
import StringIO
print 'Integers:', find_integers(StringIO.StringIO(text))

re_capword = re.compile(r'(?:\W|^)([A-Z]\w*)')
re_capword = re.compile(r"(\W|^)([A-Z][A-Za-z']*)")
def find_capwords(fp):
result = []
for line in fp:
for match in re_capword.finditer(line):
result.append(match.group(1))
result.append(match.group(2))
return result

print 'Capwords:', find_capwords(StringIO.StringIO(text))
Expand Down
1 change: 1 addition & 0 deletions 06a-Packages/mypackage/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
print 'importing mypackage'
import mysubpackage
5 changes: 5 additions & 0 deletions 06a-Packages/mypackage/mysubpackage/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
print 'importing mypackage.mysubpackage'





2 changes: 1 addition & 1 deletion index.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
[01-basic-syntax]: ./01-BasicPythonSyntax.html
[02-builtins]: ./02-Builtins.html
[03-fileio]: ./03-FileIO.html
[04-modules]: ./04-Modules.html
[04-modules]: ./04-UsingModules.html
[05-strings]: ./05-Strings.html
[06-regex]: ./06-Regex.html

0 comments on commit fd3d2af

Please sign in to comment.