From fd3d2af419b51af24d2570c254404c26731a572e Mon Sep 17 00:00:00 2001 From: Rick Copeland Date: Tue, 9 Oct 2012 06:53:27 +0800 Subject: [PATCH] Updated some code examples Signed-off-by: Rick Copeland --- 01-BasicPythonSyntax/hello.py | 5 +++ 01-BasicPythonSyntax/reverse_list.py | 4 --- 01-BasicPythonSyntax/sum_even_values.py | 15 ++------- 02-Builtins/ascii2str.py | 11 ++++++- 03-FileIO/print_file.py | 4 +-- 04-UsingModules/convert_dt_to_ts.py | 2 ++ 04-UsingModules/print_file.py | 2 +- 04-UsingModules/print_file_debug.py | 2 +- 05-Strings/get_word_count.py | 22 +++++++++++++ 05-Strings/get_words.py | 33 ++++--------------- 05-Strings/print_centered_words.py | 5 +++ 06-Regex/regex_tests.py | 4 +-- 06a-Packages/mypackage/__init__.py | 1 + .../mypackage/mysubpackage/__init__.py | 5 +++ index.md | 2 +- 15 files changed, 66 insertions(+), 51 deletions(-) create mode 100755 01-BasicPythonSyntax/hello.py create mode 100644 05-Strings/get_word_count.py create mode 100644 05-Strings/print_centered_words.py diff --git a/01-BasicPythonSyntax/hello.py b/01-BasicPythonSyntax/hello.py new file mode 100755 index 0000000..7c2a58a --- /dev/null +++ b/01-BasicPythonSyntax/hello.py @@ -0,0 +1,5 @@ +#!/usr/bin/env python +def sayhello(name): + print 'Hello, ' + name + +sayhello('Rick') diff --git a/01-BasicPythonSyntax/reverse_list.py b/01-BasicPythonSyntax/reverse_list.py index 93094f9..e8487c1 100644 --- a/01-BasicPythonSyntax/reverse_list.py +++ b/01-BasicPythonSyntax/reverse_list.py @@ -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]) diff --git a/01-BasicPythonSyntax/sum_even_values.py b/01-BasicPythonSyntax/sum_even_values.py index d92d011..45b62d6 100644 --- a/01-BasicPythonSyntax/sum_even_values.py +++ b/01-BasicPythonSyntax/sum_even_values.py @@ -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]) diff --git a/02-Builtins/ascii2str.py b/02-Builtins/ascii2str.py index df55e3a..7e65b83 100644 --- a/02-Builtins/ascii2str.py +++ b/02-Builtins/ascii2str.py @@ -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]) diff --git a/03-FileIO/print_file.py b/03-FileIO/print_file.py index 5c9e93d..c8a3c62 100644 --- a/03-FileIO/print_file.py +++ b/03-FileIO/print_file.py @@ -1,6 +1,6 @@ def print_file(fp): for line in fp: - print line.rstrip() # remove trailing whitespace + print line[:-1] fp = open('/etc/hosts') print_file(fp) @@ -8,7 +8,7 @@ def print_file(fp): 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) diff --git a/04-UsingModules/convert_dt_to_ts.py b/04-UsingModules/convert_dt_to_ts.py index 7292679..a4c5ad2 100644 --- a/04-UsingModules/convert_dt_to_ts.py +++ b/04-UsingModules/convert_dt_to_ts.py @@ -1,2 +1,4 @@ +import time + def dt_to_ts(dt): return time.mktime(dt.timetuple()) diff --git a/04-UsingModules/print_file.py b/04-UsingModules/print_file.py index 15a4829..21bfc61 100644 --- a/04-UsingModules/print_file.py +++ b/04-UsingModules/print_file.py @@ -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 diff --git a/04-UsingModules/print_file_debug.py b/04-UsingModules/print_file_debug.py index 03e06cf..00fd714 100644 --- a/04-UsingModules/print_file_debug.py +++ b/04-UsingModules/print_file_debug.py @@ -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 diff --git a/05-Strings/get_word_count.py b/05-Strings/get_word_count.py new file mode 100644 index 0000000..40258b7 --- /dev/null +++ b/05-Strings/get_word_count.py @@ -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)) diff --git a/05-Strings/get_words.py b/05-Strings/get_words.py index 5d84bee..ee59582 100644 --- a/05-Strings/get_words.py +++ b/05-Strings/get_words.py @@ -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() @@ -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']) diff --git a/05-Strings/print_centered_words.py b/05-Strings/print_centered_words.py new file mode 100644 index 0000000..0615a1f --- /dev/null +++ b/05-Strings/print_centered_words.py @@ -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']) diff --git a/06-Regex/regex_tests.py b/06-Regex/regex_tests.py index 495eabe..cbd9a8d 100644 --- a/06-Regex/regex_tests.py +++ b/06-Regex/regex_tests.py @@ -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)) diff --git a/06a-Packages/mypackage/__init__.py b/06a-Packages/mypackage/__init__.py index 26fdc25..c96b389 100644 --- a/06a-Packages/mypackage/__init__.py +++ b/06a-Packages/mypackage/__init__.py @@ -1 +1,2 @@ print 'importing mypackage' +import mysubpackage diff --git a/06a-Packages/mypackage/mysubpackage/__init__.py b/06a-Packages/mypackage/mysubpackage/__init__.py index b79b9b9..40bf536 100644 --- a/06a-Packages/mypackage/mysubpackage/__init__.py +++ b/06a-Packages/mypackage/mysubpackage/__init__.py @@ -1 +1,6 @@ print 'importing mypackage.mysubpackage' + + + + + diff --git a/index.md b/index.md index c0c3638..c351ed0 100644 --- a/index.md +++ b/index.md @@ -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