Skip to content

Commit e909ad1

Browse files
committed
initialize class attributes and methods lab
0 parents  commit e909ad1

File tree

7 files changed

+276
-0
lines changed

7 files changed

+276
-0
lines changed

.github/workflows/canvas-sync.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Sync with Canvas
2+
3+
on:
4+
push:
5+
branches: [master, main]
6+
paths:
7+
- "README.md"
8+
9+
jobs:
10+
sync:
11+
name: Sync with Canvas
12+
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- uses: actions/checkout@v2
17+
18+
- name: Set up Ruby
19+
uses: ruby/setup-ruby@v1
20+
with:
21+
ruby-version: 2.6
22+
23+
- name: Install github-to-canvas
24+
run: gem install github-to-canvas
25+
26+
# Secret stored in learn-co-curriculum Settings/Secrets
27+
- name: Sync from .canvas file
28+
run: github-to-canvas -a -lr --forkable
29+
env:
30+
CANVAS_API_KEY: ${{ secrets.CANVAS_API_KEY }}
31+
CANVAS_API_PATH: ${{ secrets.CANVAS_API_PATH }}

CONTRIBUTING.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Contributing to Learn.co Curriculum
2+
3+
We're really exited that you're about to contribute to the [open
4+
curriculum](https://learn.co/content-license) on [Learn.co](https://learn.co).
5+
If this is your first time contributing, please continue reading to learn how to
6+
make the most meaningful and useful impact possible.
7+
8+
## Raising an Issue to Encourage a Contribution
9+
10+
If you notice a problem with the curriculum that you believe needs improvement
11+
but you're unable to make the change yourself, you should raise a Github issue
12+
containing a clear description of the problem. Include relevant snippets of the
13+
content and/or screenshots if applicable. Curriculum owners regularly review
14+
issue lists and your issue will be prioritized and addressed as appropriate.
15+
16+
## Submitting a Pull Request to Suggest an Improvement
17+
18+
If you see an opportunity for improvement and can make the change yourself go
19+
ahead and use a typical git workflow to make it happen:
20+
21+
- Fork this curriculum repository
22+
- Make the change on your fork, with descriptive commits in the standard format
23+
- Open a Pull Request against this repo
24+
25+
A curriculum owner will review your change and approve or comment on it in due
26+
course.
27+
28+
## Why Contribute?
29+
30+
Curriculum on Learn is publicly and freely available under Learn's
31+
[Educational Content License](https://learn.co/content-license). By embracing an
32+
open-source contribution model, our goal is for the curriculum on Learn to
33+
become, in time, the best educational content the world has ever seen.
34+
35+
We need help from the community of Learners to maintain and improve the
36+
educational content. Everything from fixing typos, to correcting out-dated
37+
information, to improving exposition, to adding better examples, to fixing
38+
tests—all contributions to making the curriculum more effective are welcome.

LICENSE.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Learn.co Educational Content License
2+
3+
Copyright (c) 2021 Flatiron School, Inc
4+
5+
The Flatiron School, Inc. owns this Educational Content. However, the Flatiron
6+
School supports the development and availability of educational materials in the
7+
public domain. Therefore, the Flatiron School grants Users of the Flatiron
8+
Educational Content set forth in this repository certain rights to reuse, build
9+
upon and share such Educational Content subject to the terms of the Educational
10+
Content License set forth [here](http://learn.co/content-license)
11+
(http://learn.co/content-license). You must read carefully the terms and
12+
conditions contained in the Educational Content License as such terms govern
13+
access to and use of the Educational Content.
14+
15+
Flatiron School is willing to allow you access to and use of the Educational
16+
Content only on the condition that you accept all of the terms and conditions
17+
contained in the Educational Content License set forth
18+
[here](http://learn.co/content-license) (http://learn.co/content-license). By
19+
accessing and/or using the Educational Content, you are agreeing to all of the
20+
terms and conditions contained in the Educational Content License. If you do not
21+
agree to any or all of the terms of the Educational Content License, you are
22+
prohibited from accessing, reviewing or using in any way the Educational
23+
Content.

README.md

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
# Class Attributes and Methods Lab
2+
3+
## Learning Goals
4+
5+
- Use class attributes and methods to write durable and powerful code.
6+
- Store and access song data using class attributes and methods.
7+
- Accomplish complex programming tasks using knowledge from previous modules.
8+
9+
***
10+
11+
## Key Vocab
12+
13+
- **Attribute**: variables that belong to an object.
14+
- **Constant**: variable whose value cannot be changed.
15+
- **Instance**: one specific working copy of a class. It is created when a
16+
class's `__init__` method is called.
17+
- **Class**: a bundle of data and functionality. Can be copied and modified to
18+
accomplish a wide variety of programming tasks.
19+
- **Static**: an attribute or method that cannot manipulate the class or
20+
instance it belongs to.
21+
- **Exception**: an error that occurs during the execution of a program.
22+
Exceptions can be anticipated and handled without disrupting the execution of
23+
the program.
24+
25+
***
26+
27+
## Introduction
28+
29+
In this lab, we'll be dealing with a `Song` class. The `Song` class can produce
30+
individual songs. Each song has a name, an artist and a genre. We need our
31+
`Song` class to be able to keep track of the number of songs that it creates.
32+
33+
```ruby
34+
Song.count
35+
# => 30
36+
```
37+
38+
We need our `Song` class to be able to show us all of the artists of existing
39+
songs:
40+
41+
```ruby
42+
Song.artists
43+
# => ["Jay-Z", "Drake", "Beyonce"]
44+
```
45+
46+
We need our `Song` class to be able to show us all of the genres of existing
47+
songs:
48+
49+
```ruby
50+
Song.genres
51+
# => ["Rap", "Pop"]
52+
```
53+
54+
We also need our `Song` class to be able to keep track of the number of songs of
55+
each genre it creates.
56+
57+
In other words, calling:
58+
59+
```ruby
60+
Song.genre_count
61+
```
62+
63+
Should return something like this;
64+
65+
```ruby
66+
{"rap" => 5, "rock" => 1, "country" => 3}
67+
```
68+
69+
Lastly, we want our `Song` class to reveal to us the number of songs each artist
70+
is responsible for.
71+
72+
```ruby
73+
Song.artist_count
74+
# => {"Beyonce" => 17, "Jay-Z" => 40}
75+
```
76+
77+
We'll accomplish this with the use of **class variables** and **class methods**.
78+
79+
## Instructions
80+
81+
Define your `Song` class such that an individual song is initialized with a
82+
name, artist and genre.
83+
84+
There should be an `attr_accessor` for those three attributes.
85+
86+
```ruby
87+
ninety_nine_problems = Song.new("99 Problems", "Jay-Z", "rap")
88+
89+
ninety_nine_problems.name
90+
# => "99 Problems"
91+
92+
ninety_nine_problems.artist
93+
# => "Jay-Z"
94+
95+
ninety_nine_problems.genre
96+
# => "rap"
97+
```
98+
99+
Create a class variable, `@@count`. We will use this variable to keep track of
100+
the number of new songs that are created from the `Song` class. Set this
101+
variable equal to `0`.
102+
103+
At what point should we increment our `@@count` of songs? Whenever a new song is
104+
created. Your `#initialize` method should use the `@@count` variable and
105+
increment the value of that variable by `1`.
106+
107+
Next, define the following class methods:
108+
109+
`Song.count`: returns the total number of songs created.
110+
111+
`Song.genres`: returns an array of all of the genres of existing songs. This
112+
array should contain only _unique genres_ — no duplicates! Think about what
113+
you'll need to do to get this method working:
114+
115+
- You'll need a class variable, let's call it `@@genres`, that is equal to an
116+
empty array.
117+
- When should you add genres to the array? Whenever a new song is created.
118+
Your `#initialize` method should add the genre of the song being created to
119+
the `@@genres` array. All genres should be added to the array. Control for
120+
duplicates when you code your `.genres` class method, not when you add
121+
genres to the original `@@genres` array. We will want to know how many songs
122+
of each genre have been created. We'll revisit that job a little later on.
123+
124+
`Song.artists`: returns an array of all of the artists of the existing
125+
songs. This array should only contain unique artists––no repeats! Once again
126+
think about what you need to do to implement this behavior.
127+
128+
- You'll need a class variable, let's call it `@@artists`, that is equal to an
129+
empty array.
130+
- When should you add artists to this array? Whenever a new song is
131+
initialized. Your `#initialize` method should add artists to the `@@artists`
132+
array. All artists should be added to the array. Control for duplicates when
133+
you code your `.artists` class method, not when you add artists to the
134+
original `@@artists` array. We will want to know how many songs each have
135+
been assigned to each artist. We'll revisit that job a little later on when
136+
we write our `.artist_count` method.
137+
138+
`Song.genre_count`: returns a hash in which the keys are the names of each
139+
genre. Each genre name key should point to a value that is the number of songs
140+
that have that genre.
141+
142+
```ruby
143+
Song.genre_count
144+
# => {"rap" => 5, "rock" => 1, "country" => 3}
145+
```
146+
147+
This manner of displaying numerical data is called a
148+
[histogram](https://en.wikipedia.org/wiki/Histogram). How will you create your
149+
histogram? There are a few ways!
150+
151+
- You can need to iterate over the `@@genres` array and populate a hash with the
152+
key/value pairs. You will need to check to see if the hash already contains a
153+
key of a particular genre. If so, increment the value of that key by one,
154+
otherwise, create a new key/value pair.
155+
- You can also look into the [`#tally`][tally docs] method.
156+
157+
`Song.artist_count`: returns a histogram similar to the one above, but for
158+
artists rather than genres.
159+
160+
## Resources
161+
162+
- [`#tally`][tally docs]
163+
164+
[tally docs]: https://ruby-doc.org/core-2.7.0/Enumerable.html#method-i-tally
165+
166+
167+
***
168+
169+
## Resources
170+
171+
- [Python Documentation](https://docs.python.org/3/)
172+
- [Classes - Python](https://docs.python.org/3/)
173+
- [Python Class Attributes: An Overly Thorough Guide - Toptal](https://www.toptal.com/python/python-class-attributes-an-overly-thorough-guide)
174+
- [Python's Instance, Class, and Static Methods Demystified - Real Python](https://realpython.com/instance-class-and-static-methods-demystified/)
175+
- [The Factory Method Pattern and Its Implementation in Python - Real Python](https://realpython.com/factory-method-python/)

lib/__init__.py

Whitespace-only changes.

testing/__init__.py

Whitespace-only changes.

testing/conftest.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env python
2+
3+
def pytest_itemcollected(item):
4+
par = item.parent.obj
5+
node = item.obj
6+
pref = par.__doc__.strip() if par.__doc__ else par.__class__.__name__
7+
suf = node.__doc__.strip() if node.__doc__ else node.__name__
8+
if pref or suf:
9+
item._nodeid = ' '.join((pref, suf))

0 commit comments

Comments
 (0)