Advanced Python
Project Discussion, Session 6

6.1 class Deck. Create a class, Deck, which starts with a list of all 52 cards, and allows the user to "pick a card" from the deck, after which the deck will no longer contain the card. The class also allows for shuffling (randomly reordering) of the deck.

The class will have four methods: a. __init__(): the deck generation code supplied in the first example should go here. Please make sure to test this code before you add it to the class -- generally when are working with new code or new technologies we want to see them work in isolation, before adding more complications that might make the actual issues harder to see. Obviously the deck should be stored in one of the object's attributes (i.e., self.deck =). b. def pick(self): the method simply removes the first "card" (a string) off of the "deck" (a list of strings) using the list .pop(0) method (0 means first item). This both removes and returns the card. You can then return the card from the method. However if you attempt to remove a card from an empty list, the method will raise an IndexError exception. You can either trap for this (preferred) or test the length of the list before removing. If there are no more cards left in the deck, the method should return None. c. def pick_any(self): the method uses the supplied random code to randomly select a "card" (a string) from the "deck" (a list of strings). But random.choice() doesn't remove this card from the list. This is why we need the remove() code snippet to remove this card from the list so it will not appear again. The method must also be able to detect when there are no cards left, and return None if pick_any() is called when there are no more cards. random.choice() raises IndexError if you ask it to choose from an empty list; you can trap this exception or check the length of the list first. d. def shuffle(self): the method uses the supplied random code to shuffle the internal list of cards. You could test this by generating a deck, printing it out (you should see the cards in order starting with the Ace of Hearts); then shuffling it and printing it out again! The inclass exercises take you through three important components of this solution: - the creation of an instance with __init__() - the use of __init__() to initialize an attribute in the instance - the use of a method to set an instance attribute (i.e. self.attr = value) or get one (i.e. return self.attr). Your pick() and pick_any() will not return the value of an attribute, but it will return one item from the list stored in an attribute Your solution will be similar to the exercise solutions in the above respects.

 
6.2 List of a Maximum Size. Create a class, MaxSizeList, that constructs instances that have a list as an attribute (set as an empty list in __init__). The constructor takes an integer as an argument, which becomes the maximum size of the list (stored as a second attribute).

Since the instance/object must be aware of its max size and it must also maintain a list of values, it will have two attributes: an integer and a list (i.e. self.maxsize, self.thislist). The list will be initialized in __init__ as an empty list and the max size will be initialized in __init__ as the value passed to the constructor. Then in push(), we must determine whether the list size will exceed the max size if we append another value. Since both the max size and the list are stored in the instance, we can simply compare the length of the list to the max size value to see if a value needs to be removed from the list. To remove the first element of a list, use list.pop(0), so in this case instance.thislist.pop(0) (where thislist is the name of the attribute used to store the list) before or after appending the new value. get_list() simply returns the list stored in the instance. For the extra credit you must use a 'class variable' (a variable initialized inside the class) that is also accessible as a class attribute (i.e., Classname.attribute). When a new instance is created (i.e., inside __init__()), this variable should be incremented (i.e., Classname.attribute += 1) ('Classname' and 'attribute' should be substituted with the names of your class and the class variable name).

 

EXTRA CREDIT / SUPPLEMENTARY

6.3 class Text. No discussion here, let me know if you have questions.
 
6.4 Python API for Alphavantage. No discussion here, let me know if you have questions.
 
[pr]