<jupyter_start><jupyter_text>Part 2: why objects can be useful  In part 1 we used some Fruit objects. But realistically, it's tricky to tell what great value came from it, as the problem was so simple. In this section we're going to go a bit beyond, and make a proper little market. ```Toy problem 2: We want to make a little market. It will have Fruits, and Toilet Paper. Toiletpaper has slightly different properties than Fruit. - price - expiration date (which will be super long) - resistance We will also have the concept of a Basket. We can do a few things with it: - Add stuff (fruits, toilet paper) - Show which items will expire during the next n days - Remove stuff - Examine what's in the basket - Check total price ```_Note: We made some fruits in Notebook 1, so we'll just import them here._<jupyter_code>from utils import Fruit, Toiletpaper, Basket, get_fruits
apples, bananas, oranges = get_fruits()   
# feel free to ignore this cell, I'm just bringing in code we'd already defined<jupyter_output><empty_output><jupyter_text>Let's start by making two types of toilet paper, one simple, and one luxurious.  You will notice that the attributes are slightly different from those of Fruits, as Toilet paper has `thickness`, which would not make sense in a Fruit.<jupyter_code>luxurious_toilet_paper = Toiletpaper(name='Smooth and dry toilet paper',
                                 thickness='Tripple leaf', 
                                 price_per_unit=50, 
                                 days_until_expired=2000, 
                                 nr_units=1)

basic_toilet_paper = Toiletpaper(name='Basic toilet paper',
                                 thickness='Single leaf', 
                                 price_per_unit=3, 
                                 days_until_expired=365, 
                                 nr_units=1)<jupyter_output><empty_output><jupyter_text>You may have wondered why we would bother to have a `name` argument, and not just use the name of the variable. The simple answer is that you can't print the name of the variable, so if you add `basic_toilet_paper` to a list or anything else, you will struggle to figure out what it was. So it's always a good idea to have a name.<jupyter_code># not very useful, we can only see that it is an instance of Toiletpaper
print(basic_toilet_paper) 
# Quite clear! 
print(basic_toilet_paper.name)<jupyter_output>Basic toilet paper<jupyter_text>---- Great, now for our basket. We will create a basket called `my_basket`, and will use it to pick up some fruits and toilet paper. We will then use some cool "basket" specific methods to calculate prices, and to see if any items are about to expire.<jupyter_code># create the basket (this is called "instantiate", you'll hear this word around)
my_basket = Basket()
# add some apples 
my_basket.add_item(apples)
# add some bananas 
my_basket.add_item(bananas)
# add some oranges 
my_basket.add_item(oranges)<jupyter_output><empty_output><jupyter_text>So what does our basket look like now?<jupyter_code>my_basket<jupyter_output><empty_output><jupyter_text>Oh right, we are just calling the object, so it's just telling us where it is in memory.   Fortunately, the basket has a cool method (a function that "lives in the class") for this:<jupyter_code>my_basket.examine_basket()   # <-- examine_basket is a method that all baskets share<jupyter_output>The total price is 75
- 10 apples (total price 10)
- 2 oranges (total price 6)
- 1 Basic toilet paper (total price 3)
- 2 oranges (total price 6)
- 1 Smooth and dry toilet paper (total price 50)<jupyter_text>Nice. Now, time to get some wonderfull, tripple leaf toilet paper!<jupyter_code># add the toilet paper! 
my_basket.add_item(luxurious_toilet_paper)
my_basket.examine_basket()<jupyter_output>The total price is 75
- 10 apples (total price 10)
- 2 oranges (total price 6)
- 1 Basic toilet paper (total price 3)
- 2 oranges (total price 6)
- 1 Smooth and dry toilet paper (total price 50)<jupyter_text>Oh my, we are already at 78 euros? We probably should get rid of this crazy expensive toilet paper then, and replace it with some basic stuff:<jupyter_code>my_basket.remove_item(luxurious_toilet_paper)
my_basket.add_item(basic_toilet_paper)
my_basket.examine_basket()<jupyter_output>The total price is 31
- 10 apples (total price 10)
- 6 bananas (total price 12)
- 2 oranges (total price 6)
- 1 Basic toilet paper (total price 3)<jupyter_text>Great, we're almost done. I'm just a bit worried that some of the fruit may be about to expire... let's take a quick look:<jupyter_code>my_basket.check_for_items_close_to_expire(10)<jupyter_output>The item bananas will expire in 7 days<jupyter_text>Ah, got it. Then let's replace the bananas with some more oranges.<jupyter_code>my_basket.remove_item(bananas)
my_basket.add_item(oranges)
my_basket.examine_basket()<jupyter_output>The total price is 25
- 10 apples (total price 10)
- 2 oranges (total price 6)
- 1 Basic toilet paper (total price 3)
- 2 oranges (total price 6)
{'fraction_non_alphanumeric': 0.06826347305389222, 'fraction_numerical': 0.015169660678642715, 'mean_word_length': 3.88401559454191, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '583166', 'license': 'MIT', 'n_tokens_mistral': 1619, 'n_tokens_neox': 1503, 'n_words': 633, 'path': 'Week 05/SLU09 - OOP Basics/Learning Notebook Part 2 - Why use objects .ipynb', 'repo_name': 'LDSSA/ds-prep-course-2022', 'size': '5010'}