Skip to content

Commit d46ea18

Browse files
committed
adde sample codes.
1 parent 02419d4 commit d46ea18

File tree

269 files changed

+5545
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

269 files changed

+5545
-0
lines changed

1261_02_Code/1261_2_01-basic_class.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class MyFirstClass:
2+
pass
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class MyFirstClass:
2+
pass
3+
4+
a = MyFirstClass()
5+
b = MyFirstClass()
6+
print(a)
7+
print(b)

1261_02_Code/1261_2_03_attributes.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Point:
2+
pass
3+
4+
p1 = Point()
5+
p2 = Point()
6+
7+
p1.x = 5
8+
p1.y = 4
9+
10+
p2.x = 3
11+
p2.y = 6
12+
13+
print(p1.x, p1.y)
14+
print(p2.x, p2.y)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Point:
2+
def reset(self):
3+
self.x = 0
4+
self.y = 0
5+
6+
p = Point()
7+
p.reset()
8+
print(p.x, p.y)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Point:
2+
def reset(self):
3+
self.x = 0
4+
self.y = 0
5+
6+
p = Point()
7+
Point.reset(p)
8+
print(p.x, p.y)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import math
2+
3+
class Point:
4+
def move(self, x, y):
5+
self.x = x
6+
self.y = y
7+
def reset(self):
8+
self.move(0, 0)
9+
def calculate_distance(self, other_point):
10+
return math.sqrt(
11+
(self.x - other_point.x)**2 +
12+
(self.y - other_point.y)**2)
13+
14+
15+
# how to use it:
16+
point1 = Point()
17+
point2 = Point()
18+
19+
point1.reset()
20+
point2.move(5,0)
21+
print(point2.calculate_distance(point1))
22+
assert (point2.calculate_distance(point1) ==
23+
point1.calculate_distance(point2))
24+
point1.move(3,4)
25+
print(point1.calculate_distance(point2))
26+
print(point1.calculate_distance(point1))
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Point:
2+
def __init__(self, x, y):
3+
self.move(x, y)
4+
5+
def move(self, x, y):
6+
self.x = x
7+
self.y = y
8+
9+
def reset(self):
10+
self.move(0, 0)
11+
12+
# Constructing a Point
13+
point = Point(3, 5)
14+
print(point.x, point.y)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Point:
2+
def __init__(self, x=0, y=0):
3+
self.move(x, y)

1261_02_Code/1261_2_09-docstrings.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import math
2+
3+
class Point:
4+
'Represents a point in two-dimensional geometric coordinates'
5+
6+
def __init__(self, x=0, y=0):
7+
'''Initialize the position of a new point. The x and y coordinates can
8+
be specified. If they are not, the point defaults to the origin.'''
9+
self.move(x, y)
10+
11+
def move(self, x, y):
12+
"Move the point to a new location in two-dimensional space."
13+
self.x = x
14+
self.y = y
15+
16+
def reset(self):
17+
'Reset the point back to the geometric origin: 0, 0'
18+
self.move(0, 0)
19+
20+
def calculate_distance(self, other_point):
21+
"""Calculate the distance from this point to a second point passed
22+
as a parameter.
23+
24+
This function uses the Pythagorean Theorem to calculate the distance
25+
between the two points. The distance is returned as a float."""
26+
27+
return math.sqrt(
28+
(self.x - other_point.x)**2 +
29+
(self.y - other_point.y)**2)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Database:
2+
# the database implementation
3+
pass
4+
5+
database = Database()
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Database:
2+
# the database implementation
3+
pass
4+
5+
database = None
6+
7+
def initialize_database():
8+
global database
9+
database = Database()
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class UsefulClass:
2+
'''This class might be useful to other modules.'''
3+
pass
4+
5+
def main():
6+
'''creates a useful class and does something with it for our module.'''
7+
useful = UsefulClass()
8+
print(useful)
9+
10+
if __name__ == "__main__":
11+
main()
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
def format_string(string, formatter=None):
2+
'''Format a string using the formatter object, which
3+
is expected to have a format() method that accepts
4+
a string.'''
5+
class DefaultFormatter:
6+
'''Format a string in title case.'''
7+
def format(self, string):
8+
return str(string).title()
9+
10+
if not formatter:
11+
formatter = DefaultFormatter()
12+
13+
return formatter.format(string)
14+
15+
hello_string = "hello world, how are you today?"
16+
print(" input: " + hello_string)
17+
print("output: " + format_string(hello_string))
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class SecretString:
2+
'''A not-at-all secure way to store a secret string.'''
3+
4+
def __init__(self, plain_string, pass_phrase):
5+
self.__plain_string = plain_string
6+
self.__pass_phrase = pass_phrase
7+
8+
def decrypt(self, pass_phrase):
9+
'''Only show the string if the pass_phrase is correct.'''
10+
if pass_phrase == self.__pass_phrase:
11+
return self.__plain_string
12+
else:
13+
return ''

1261_02_Code/1261_2_15-note_object.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import datetime
2+
3+
last_id = 0
4+
5+
class Note:
6+
'''Represent a note in the notebook. Match against a
7+
string in searches and store tags for each note.'''
8+
9+
10+
def __init__(self, memo, tags=''):
11+
'''initialize a note with memo and optional
12+
space-separated tags. Automatically set the note's
13+
creation date and a unique id'''
14+
self.memo = memo
15+
self.tags = tags
16+
self.creation_date = datetime.date.today()
17+
global last_id
18+
last_id += 1
19+
self.id = last_id
20+
21+
def match(self, filter):
22+
'''Determine if this note matches the filter
23+
text. Return True if it matches, False otherwise.
24+
25+
Search is case sensitive and matches both text and
26+
tags.'''
27+
return filter in self.memo or filter in self.tags
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Notebook:
2+
'''Represent a collection of notes that can be tagged,
3+
modified, and searched.'''
4+
5+
def __init__(self):
6+
'''Initialize a notebook with an empty list.'''
7+
self.notes = []
8+
9+
def new_note(self, memo, tags=''):
10+
'''Create a new note and add it to the list.'''
11+
self.notes.append(Note(memo, tags))
12+
13+
def modify_memo(self, note_id, memo):
14+
'''Find the note with the given id and change its
15+
memo to the given value.'''
16+
for note in self.notes:
17+
if note.id == note_id:
18+
note.memo = memo
19+
break
20+
21+
def modify_tags(self, note_id, tags):
22+
'''Find the note with the given id and change its
23+
tags to the given value.'''
24+
for note in self.notes:
25+
if note.id == note_id:
26+
note.tags = tags
27+
break
28+
29+
def search(self, filter):
30+
'''Find all notes that match the given filter
31+
string.'''
32+
return [note for note in self.notes if
33+
note.match(filter)]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def _find_note(self, note_id):
2+
'''Locate the note with the given id.'''
3+
for note in self.notes:
4+
if note.id == note_id:
5+
return note
6+
return None
7+
8+
def modify_memo(self, note_id, memo):
9+
'''Find the note with the given id and change its
10+
memo to the given value.'''
11+
self._find_note(note_id).memo = memo
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import sys
2+
from notebook import Notebook, Note
3+
4+
class Menu:
5+
'''Display a menu and respond to choices when run.'''
6+
def __init__(self):
7+
self.notebook = Notebook()
8+
self.choices = {
9+
"1": self.show_notes,
10+
"2": self.search_notes,
11+
"3": self.add_note,
12+
"4": self.modify_note,
13+
"5": self.quit
14+
}
15+
16+
def display_menu(self):
17+
print("""
18+
Notebook Menu
19+
20+
1. Show all Notes
21+
2. Search Notes
22+
3. Add Note
23+
4. Modify Note
24+
5. Quit
25+
""")
26+
27+
def run(self):
28+
'''Display the menu and respond to choices.'''
29+
while True:
30+
self.display_menu()
31+
choice = input("Enter an option: ")
32+
action = self.choices.get(choice)
33+
if action:
34+
action()
35+
else:
36+
print("{0} is not a valid choice".format(choice))
37+
38+
def show_notes(self, notes=None):
39+
if not notes:
40+
notes = self.notebook.notes
41+
for note in notes:
42+
print("{0}: {1}\n{2}".format(
43+
note.id, note.tags, note.memo))
44+
45+
def search_notes(self):
46+
filter = input("Search for: ")
47+
notes = self.notebook.search(filter)
48+
self.show_notes(notes)
49+
50+
def add_note(self):
51+
memo = input("Enter a memo: ")
52+
self.notebook.new_note(memo)
53+
print("Your note has been added.")
54+
55+
def modify_note(self):
56+
id = input("Enter a note id: ")
57+
memo = input("Enter a memo: ")
58+
tags = input("Enter tags: ")
59+
if memo:
60+
self.notebook.modify_memo(id, memo)
61+
if tags:
62+
self.notebook.modify_tags(id, tags)
63+
64+
def quit(self):
65+
print("Thank you for using your notebook today.")
66+
sys.exit(0)
67+
68+
if __name__ == "__main__":
69+
Menu().run()
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def _find_note(self, note_id):
2+
'''Locate the note with the given id.'''
3+
for note in self.notes:
4+
if str(note.id) == str(note_id):
5+
return note
6+
return None
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def modify_memo(self, note_id, memo):
2+
'''Find the note with the given id and change its
3+
memo to the given value.'''
4+
note = self._find_note(note_id)
5+
if note:
6+
note.memo = memo
7+
return True
8+
return False

0 commit comments

Comments
 (0)