<jupyter_start><jupyter_text>HW4 Part 1 - Review of Python ClassesApril 21, 2020The following is meant to be a brief review of classes in Python. If you feel comfortable with building and manipulating classes in Python, feel free to skim this part<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
import pandas as pd<jupyter_output><empty_output><jupyter_text>1a) Create a class "`Dog`" with an `__init__` function that takes in the variable `name`<jupyter_code>class Dog:

    def __init__(self, name):
        self.name = name
mydog = Dog('Jojo')
mydog.name<jupyter_output><empty_output><jupyter_text>1b) Add a method to this class `add_trick` that takes in a variable `trick`Keep in mind that a Dog might learn many tricks - you might keep track of the tricks it learns in a list<jupyter_code>class Dog:

    def __init__(self, name):
        self.name = name 
        self.tricks = []    # creates a new empty list for each dog

    def add_trick(self, trick):
        self.tricks.append(trick)
# test your code
mydog = Dog('Jojo')
mydog.add_trick('roll over')
mydog.add_trick('play dead')
mydog.add_trick('flippy flip')

# print the tricks here
mydog.tricks<jupyter_output><empty_output><jupyter_text>1c) Modify the class so that it keeps track of the dog's age, gender and speciesWould these be better as a data attributes or as a method attributes?<jupyter_code># your code here
class Dog:

    def __init__(self, name,gender,age,species):
        self.name = name 
        self.age = age 
        self.gender = gender 
        self.species = species
        self.tricks = []    # creates a new empty list for each dog

    def add_trick(self, trick):
        self.tricks.append(trick)<jupyter_output><empty_output><jupyter_text>1d) Create 3 dogs with various characteristics, and assign them different tricks<jupyter_code># your code here
jojo = Dog(name='Jojo',age=12,gender='M',species='collie')

misha = Dog(name='Misha',age=2,gender='F',species='german shepherd')

bo = Dog(name='Bo',age=1,gender='F',species='bulldog')


jojo.add_trick('roll over')
misha.add_trick('roll over')
misha.add_trick('play dead')<jupyter_output><empty_output><jupyter_text>1e) Create a method/function `print_summary` for the Dog class that prints all the relevant data and tricks<jupyter_code># your code here
class Dog:

    def __init__(self, name,gender,age,species):
        self.name = name 
        self.age = age 
        self.gender = gender 
        self.species = species
        self.tricks = []    # creates a new empty list for each dog

    def add_trick(self, trick):
        self.tricks.append(trick)
        
    def print_summary(self):
        print('---------')
        print('Dog Summary')
        print('---------')
        print('Name: ',self.name)
        print('Age: ',self.age)
        print('Gender: ',self.gender)
        print('Species: ',self.species)
        print('Tricks: ',self.tricks)
jojo = Dog(name='Jojo',age=12,gender='M',species='collie')

misha = Dog(name='Misha',age=2,gender='F',species='german shepherd')

bo = Dog(name='Bo',age=1,gender='F',species='bulldog')

jojo.add_trick('roll over')
misha.add_trick('roll over')
misha.add_trick('play dead')
dogs = [jojo,bo,misha]

for d in dogs:
    d.print_summary()<jupyter_output>---------
Dog Summary
---------
Name:  Jojo
Age:  12
Gender:  M
Species:  collie
Tricks:  ['roll over']
---------
Dog Summary
---------
Name:  Bo
Age:  1
Gender:  F
Species:  bulldog
Tricks:  []
---------
Dog Summary
---------
Name:  Misha
Age:  2
Gender:  F
Species:  german shepherd
Tricks:  ['roll over', 'play dead']<jupyter_text>1f) Finally, create a function/method that returns a pandas dataframe of the dog's name, age, gender species and tricks<jupyter_code>class Dog:

    def __init__(self, name,gender,age,species):
        self.name = name 
        self.age = age 
        self.gender = gender 
        self.species = species
        self.tricks = []    # creates a new empty list for each dog

    def add_trick(self, trick):
        self.tricks.append(trick)
        
    def print_summary(self):
        print('---------')
        print('Dog Summary')
        print('---------')
        print('Name: ',self.name)
        print('Age: ',self.age)
        print('Gender: ',self.gender)
        print('Species: ',self.species)
        print('Tricks: ',self.tricks)
        
    def return_df(self):
        
        # put into dictionary
        d  = {'name':self.name,
              'age':self.age,
              'gender':self.gender,
              'species':self.species,
              'tricks':self.tricks}
              
        df = pd.DataFrame(d)
        return df<jupyter_output><empty_output><jupyter_text>This code should run. What do you notice about having multiple tricks?<jupyter_code>jojo = Dog(name='Jojo',age=12,gender='M',species='collie')

jojo.add_trick('roll over')
jojo.add_trick('flippy flip')

df = jojo.return_df()

df.head()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1347160692710431, 'fraction_numerical': 0.005839710028191704, 'mean_word_length': 3.1495405179615705, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '546679', 'license': 'MIT', 'n_tokens_mistral': 1696, 'n_tokens_neox': 1617, 'n_words': 438, 'path': 'Homeworks/HW4/HW4-Part1-solutions.ipynb', 'repo_name': 'gorkaLasso/PythonDataCourse', 'size': '4966'}