revealOptions | ||
---|---|---|
|
Song
- Data: artist, title
- Behavior: play()
Create a file called music.py
and implement a Song
class.
class Song:
def __init__(self, artist, title):
# TODO...
- Finish the constructor.
- Add a
play()
method that prints out'PLAYING: '
along with the artist and title.
song = Song(artist='Hot Chip', title='The Warning')
song.play() # => 'PLAYING: Hot Chip - The Warning'
class Song:
def __init__(self, artist, title):
self.artist = artist
self.title = title
def play(self):
print('PLAYING: ' + self.artist + ' - ' + self.title)
song = Song(artist='Hot Chip', title='The Warning')
song.play() # => 'PLAYING: Hot Chip - The Warning'
How would we design a Playlist
class?
Playlist
- Data: name, songs, current song
- Behavior: play_one(), play_all(), shuffle()
Add the following to music.py
class Playlist:
def __init__(self, name):
self.name = name
self.current_song_index = 0
# A list of Song instances
self.songs = []
def add_song(self, song):
self.songs.append(song)
def play_one(self):
current_song = self.songs[self.current_song_index]
current_song.play()
self.current_song_index += 1
Note: We'll come back to this later
playlist = Playlist('Old Kanye')
song1 = Song(artist='Kanye', title='Stronger')
song2 = Song(artist='Kanye ft. Lupe Fiasco', title='Touch The Sky')
playlist.add_song(song1)
playlist.add_song(song2)
playlist.play_one() # => PLAYING: Kanye - Stronger
Don't write this code just yet. We'll revisit this later.
Breaking big programs into smaller parts
- Simplicity: focus on on thing at a time
- Maintainability: changing one piece won't break other pieces
- Reusability: less copying and pasting!
Your building blocks
- Collection of related code
- May contain variables, functions, and classes
Python's standard library contains lots of convenient modules
import math
Run the python
shell and try following
>>> import math
>>> math.sqrt(4)
2.0
Go to https://docs.python.org/3/library/math.html .
Try out some of the functions in the math
module.
from math import sqrt
Try the following:
>>> from math import ceil, floor
>>> ceil(12.3)
13
>>> floor(45.6)
45
Now that you know how to import built-in modules, let's use modules in a program.
Create a file called coin.py
.
Write a function flip
that randomly returns either 'H'
or 'T'
.
Hint: Check out the random
module in the standard library.
import random
def flip():
return random.choice(['H', 'T'])
print(flip()) # => 'H'
print(flip()) # => 'T'
print(flip()) # => 'H'
Also valid
from random import choice
def flip():
return choice(['H', 'T'])
Before we move on, make sure your coin.py
looks like this:
import random
def flip():
return random.choice(['H', 'T'])
Note: remove any print statements
Turns out, you've already created modules.
In Python, a module is a file with a .py extension.
Yay, no new syntax!Run the python
shell and try following
>>> import coin
>>> coin.flip()
'H'
>>> coin.flip()
'T'
>>> coin.flip()
'T'
>>> from music import Song
>>> s = Song('The Beatles', 'Help!')
>>> s.play()
PLAYING: The Beatles - Help!
In the python
shell, import the Song
and Playlist
classes.
Make a Song
instance and a Playlist
instance.
Add the song to the playlist.
Then play the song.
>>> from music import Song, Playlist
>>> s = Song('The Beatles', 'Help!')
>>> p = Playlist('faves')
>>> p.add_song(s)
>>> p.play_one()
PLAYING: The Beatles - Help!
module: A collection of related variables, functions, and classes
package: A collection of related modules
If a module is a .py
file, what do you think a package is?
A folder of .py
files!
Create a folder called fun
. Move coin.py
and music.py
into fun
.
fun
├── coin.py
└── music.py
import fun.coin
from fun.music import Song, Playlist
Try the following in the python
shell:
>>> import fun.coin
>>> fun.coin.flip()
'T'
>>> from fun.music import Song
>>> Song
<class 'fun.music.Song'>
Packages can be nested
fun
└── writing
├── base.py
└── markers.py
├── coin.py
├── music.py
# fun/writing/base.py
class WritingImplement:
def __init__(self, color):
self.color = color
def write(self, text):
return self.color + ': ' + text
# fun/writing/markers.py
from fun.writing.base import WritingImplement
class DryEraseMarker(WritingImplement):
# ...
These are equivalent.
Absolute
# fun/writing/markers.py
from fun.writing.base import WritingImplement
Relative
# fun/writing/markers.py
from .base import WritingImplement
These are also equivalent.
Absolute
# fun/writing/markers.py
from fun.coin import flip
Relative
# fun/writing/markers.py
from ..coin import flip
..
refers to the package above the current one.
How would we...
fun
└── writing
├── base.py # <-- 1. import Song?
└── markers.py
├── coin.py
├── music.py # <-- 2. import DryEraseMarker?
# fun/writing/base.py
from fun.songs import Song
# OR
from ..songs import Song
# fun/music.py
from fun.writing.markers import DryEraseMarker
# OR
from .writing.markers import DryEraseMarker
A way to install packages from the Python Package Index (PyPI)
pip install crayons
Run python
and use the package you installed.
Now uninstall crayons
:
pip uninstall crayons
Create a file called requirements.txt
with the following 2 package
names to install:
crayons
hodor
Install the packages.
pip install -r requirements.txt
List your installed packages.
pip freeze
Use your packages.
Note: To get an understanding of what virtualenvs are and why they're useful, let's look at Netflix
Note: Virtual environments are useful for the same reason netflix profiles are useful...avoiding conflict...
Different projects on the same computer may depend on conflicting 3rd-party packages.
They may even depend on different Python versions.Each project gets its own isolated environment with
- its own copy of
python
- its own copy of
pip
- its own folder of 3rd-party packages
"Python Env Wrapper"
pip install pew
Let's see which python we're using now.
which python
# => /usr/local/bin/python
which pip
# => /usr/local/bin/pip
Right now, we're using the system python.
pew new --python=python3 gdi
pew
will automatically activate the new virtualenv
which python
# => /Users/sloria/.local/share/virtualenvs/gdi/bin/python
which pip
# => /Users/sloria/.local/share/virtualenvs/gdi/bin/pip
We're no longer using the system python
.
We're using our virtualenv's python
.
Hit ctrl+D
to exit.
which python
# => /usr/local/bin/python
Back to system python.
pew workon gdi
which python
# => /Users/sloria/.local/share/virtualenvs/gdi/bin/python
- While your
gdi
virtualenv is activated, install thedjango
package - Enter the python shell and
import django
- Now exit your virtualenv
- Enter the python shell and import django again. You should see an error.
to
- Add a
play_all()
method toPlaylist
that plays all the songs in the playlist. - Modify your
WritingImplement.write
method to actually print out a colored string using thecrayons
package installed from PyPI. - Write a script that creates two
Playlist
instances and add songs to each of them. Then use yourcoin.flip()
function to choose which playlist to play. Play the chosen playlist.