Skip to content

Commit 7c75e29

Browse files
Merge branch 'main' into zach-mini-capstone
2 parents 12ade66 + a4eab01 commit 7c75e29

35 files changed

+1428
-12
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
class PhoneBook:
2+
# attributes, characteristics
3+
def __init__(self):
4+
self.contacts = [] #list of contacts
5+
self.path = '' #path to csv
6+
self.headers = [] #stored headers
7+
8+
9+
def load(self, path):
10+
"""
11+
returns a list of dictionaries representating a contact list csv
12+
reads csv
13+
"""
14+
self.path = path #set path
15+
with open(self.path) as f:
16+
lines = f.read().split('\n')
17+
18+
# process csv into list of dictionaries
19+
self.headers = lines[0].split(',')
20+
for i in range(1, len(lines)):
21+
row = lines[i].split(',')
22+
contact = dict(zip(self.headers, row))
23+
self.contacts.append(contact)
24+
return len(self.contacts)
25+
26+
def count(self):
27+
"""return the length of self.contacts"""
28+
29+
return len(self.contacts)
30+
31+
def save(self):
32+
"""
33+
1) open 'self.file' with open 'w' for write
34+
2) convert the dictionaries into lines
35+
3) & write each line into to the file
36+
"""
37+
lines = []
38+
for contact in self.contacts:
39+
lines.append( ",".join(list(contact.values())))
40+
41+
with open(self.path, 'w') as phone_book_file:
42+
phone_book_file.write('\n'.join(lines))
43+
44+
45+
def print(self):
46+
"""
47+
loop over self.contacts
48+
print the information for each contact
49+
"""
50+
for contact in self.contacts:
51+
print(contact)
52+
53+
def add(self, name, phone_number, state):
54+
"""
55+
create a new dictionary
56+
add the new dictionary to self.contacts
57+
"""
58+
59+
new_contact = {
60+
"name": f"{name}",
61+
"phone_number": f"{phone_number}",
62+
"state": f"{state}"
63+
}
64+
65+
self.contacts.append(new_contact)
66+
67+
def remove(self, name):
68+
"""
69+
find the contact in self-contacts with the given name
70+
remove the element at that index
71+
"""
72+
for i in self.contacts:
73+
if i["name"] == name:
74+
self.contacts.remove(i)
75+
76+
def update(self, old_name, new_name, new_phone_number, new_state):
77+
"""
78+
find the contact in self.contacts with the given old_name
79+
set that contacts' name, phone number, etc to the given values
80+
"""
81+
for i in self.contacts:
82+
if i["name"] == old_name:
83+
self.add(new_name, new_phone_number, new_state)
84+
self.remove(old_name)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import PhoneBook
2+
3+
def main():
4+
phone_book = PhoneBook.PhoneBook() # create an instance of our class
5+
print('Welcome to the Phone Book')
6+
while True:
7+
command = input('Enter a command: ')
8+
if command == 'load':
9+
path = input('Enter the path to your CSV: ')
10+
phone_book.load(path)
11+
print(f'Loaded {phone_book.count()} contacts.')
12+
elif command == 'save':
13+
phone_book.save()
14+
print(f'Saved {phone_book.count()} contacts.')
15+
elif command == 'print':
16+
phone_book.print()
17+
elif command == 'add':
18+
print('Enter info of contact to add:')
19+
name = input('Name: ')
20+
phone_number = input('Phone Number: ')
21+
state = input('state: ')
22+
phone_book.add(name, phone_number, state)
23+
elif command == 'remove':
24+
name = input('Name of contact to remove: ')
25+
phone_book.remove(name)
26+
elif command == 'update':
27+
print('Enter info of contact to add:')
28+
old_name = input('Name of contact to update: ')
29+
new_name = input('New Name: ')
30+
new_phone_number = input('New Phone Number: ')
31+
new_state = input('New state: ')
32+
phone_book.update(old_name, new_name, new_phone_number,
33+
new_state)
34+
elif command == 'help':
35+
print('Available commands:')
36+
print('load - load all contacts from the file')
37+
print('save - save contacts to a file')
38+
print('print - print all contacts')
39+
print('add - add a new contact')
40+
print('remove - remove a contact')
41+
print('update - update a contact')
42+
print('q - quit the program')
43+
elif command == 'q':
44+
break
45+
else:
46+
print('Command not recognized')
47+
48+
49+
if __name__ == "__main__":
50+
main()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Sarah Beth,123432112,VA
2+
3+
Frog,123456,florida
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
name,phone number,state,title,office
2+
Sarah Beth,123432112,VA,teacher,home
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import PhoneBook
2+
3+
book = PhoneBook.PhoneBook()
4+
book.load('work_contacts.csv')
5+
book.print()
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import pylyrics3
2+
# https://github.com/jameswenzel/pylyrics3
3+
4+
5+
bon_iver_lyrics = pylyrics3.get_artist_lyrics('bon iver')
6+
7+
print(bon_iver_lyrics)
8+
9+
10+
while True:
11+
artist = input('What artist do you want to look up?')
12+
print(pylyrics3.get_artist_lyrics(artist))
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
# Lab 1: Turtle
2+
3+
## Explanation
4+
5+
Turtle is a python `module` that allows us to move a virtual turtle around the screen using programming statements. This turtle has a position and a heading. Below are a list of commands, you can more in the [turtle docs](https://docs.python.org/3.6/library/turtle.html).
6+
7+
- `forward(distance)` moves the turtle forward the given number of pixels
8+
- `left(angle)` and `right(angle)` turns the turtle left or right by the given angle (in degrees)
9+
- `color(color_name)` sets the pen's color, which can be `penup()` `penup()` `penup()`
10+
- `penup()` raises the pen, a line won't be drawn when the turtle moves, `pendown()` lowers the pen again
11+
12+
- `setposition(x, y)` moves the turtle to the given position
13+
14+
- `fillcolor(color_name)` sets the fill color, `begin_fill()` indicates you'd like to begin filling in whatever you draw, `end_fill()` actually fills the shape in.
15+
16+
Use these functions to draw a stick figure with a head, body, two arms, and two legs. Once you're done, go through the examples below and create your own drawing.
17+
18+
## Examples
19+
20+
21+
### Drawing a Square
22+
23+
```python
24+
from turtle import *
25+
26+
forward(100)
27+
left(90)
28+
forward(100)
29+
left(90)
30+
forward(100)
31+
left(90)
32+
forward(100)
33+
left(90)
34+
35+
done()
36+
37+
```
38+
39+
### Filling in a Square
40+
41+
```python
42+
from turtle import *
43+
44+
fillcolor('red')
45+
begin_fill()
46+
47+
forward(100)
48+
left(90)
49+
forward(100)
50+
left(90)
51+
forward(100)
52+
left(90)
53+
forward(100)
54+
left(90)
55+
56+
end_fill()
57+
58+
done()
59+
60+
```
61+
62+
### Drawing a Star
63+
64+
```python
65+
from turtle import *
66+
67+
forward(100)
68+
right(144)
69+
forward(100)
70+
right(144)
71+
forward(100)
72+
right(144)
73+
forward(100)
74+
right(144)
75+
forward(100)
76+
77+
done()
78+
79+
```
80+
81+
82+
### Drawing a Square with a Loop
83+
84+
```python
85+
from turtle import *
86+
87+
i = 0
88+
while i < 4:
89+
forward(100)
90+
left(90)
91+
i = i + 1
92+
93+
done()
94+
```
95+
96+
### Drawing a Circle with a Loop
97+
```python
98+
from turtle import *
99+
100+
i = 0
101+
while i < 100:
102+
forward(2)
103+
left(360/100)
104+
i = i + 1
105+
106+
done()
107+
```
108+
109+
### Drawing a Staircase
110+
111+
```python
112+
from turtle import *
113+
114+
i = 0
115+
while i < 4:
116+
forward(100)
117+
left(90)
118+
forward(100)
119+
right(90)
120+
i = i + 1
121+
done()
122+
```
123+
124+
125+
### Filling in a Square
126+
127+
```python
128+
from turtle import *
129+
130+
fillcolor('red')
131+
begin_fill()
132+
133+
forward(100)
134+
left(90)
135+
forward(100)
136+
left(90)
137+
forward(100)
138+
left(90)
139+
forward(100)
140+
left(90)
141+
142+
end_fill()
143+
144+
done()
145+
146+
```
147+
148+
### Draw an N-Sided figure
149+
```python
150+
from turtle import *
151+
152+
edge_length = 100
153+
n_sides = 5
154+
155+
i = 0
156+
while i < n_sides:
157+
forward(edge_length/n_sides)
158+
right(360/n_sides)
159+
i = i + 1
160+
161+
done()
162+
163+
```
164+
165+
166+
### 8-Sided Spiral
167+
168+
```python
169+
from turtle import *
170+
171+
fillcolor('blue')
172+
173+
n_sides = 8
174+
edge_length = 0
175+
176+
i = 0
177+
begin_fill()
178+
while i < 150:
179+
forward(edge_length)
180+
right(360/n_sides)
181+
i = i + 1
182+
edge_length = edge_length + 1
183+
end_fill()
184+
done()
185+
186+
```
187+
188+
### Expanding Star
189+
190+
```python
191+
from turtle import *
192+
193+
edge_length = 0
194+
i = 0
195+
while i < 100:
196+
forward(edge_length)
197+
right(144)
198+
199+
edge_length += 4
200+
201+
done()
202+
```

0 commit comments

Comments
 (0)