<jupyter_start><jupyter_text>PART ONE: Write your few tiny functions*depth_to_words will describe the earthquake's depth*magnitude_to_words will describe the earthquake's power*day_in_words should be the day of the week*time_in_words should be "morning", "afternoon", "evening" or "night"*date_in_words should be "Monthname day", e.g. "June 22"*Any other functions as necessary<jupyter_code>earthquake = {
      'rms': '1.85',
      'updated': '2014-06-11T05:22:21.596Z',
      'type': 'earthquake',
      'magType': 'mwp',
      'longitude': '-136.6561',
      'gap': '48',
      'depth': '10',
      'dmin': '0.811',
      'mag': '5.7',
      'time': '2014-06-04T11:58:58.200Z',
      'latitude': '59.0001',
      'place': '73km WSW of Haines, Alaska',
      'net': 'us',
      'nst': '',
      'id': 'usc000rauc'}
def depth_to_words(str_depth):
    #'depth': '10'
    depth=int(str_depth)
    if depth < 70:#not depth > 0
        return "shallow"
    elif depth < 300:
        return "intermediate"
    else:#not depth >300
        return "deep"
print(depth_to_words(40))
print(depth_to_words(255))
print(depth_to_words(600))
print(depth_to_words(1000))
print(depth_to_words(-1))
#rewrite the function to accept a dictionary instead of a number
def depth_to_words(earthquake):
    depth = int(earthquake['depth'])
    if depth < 70:
        return "shallow"
    elif depth < 300:
        return "intermediate"
    else:
        return "shallow"

# You would call it like this
depth_to_words(earthquake)
def magnitude_to_words(earthquake):
    mag=float(earthquake['mag'])# not round number, use float()
    if mag <3:# when do scale, only use one side, no need for >=
        return "minor"
    elif mag <5:
        return "intermediate"
    elif mag <7:
        return "high"
    elif mag <9:
        return "outstanding"
    else:
        return "super"
#print(magnitude_to_words(4))
#print(magnitude_to_words(1))
#print(magnitude_to_words(0.5))
#print(magnitude_to_words(9.5))
#print(magnitude_to_words("11"))
print(magnitude_to_words(earthquake))
#timestring = '2014-06-04T11:58:58.200Z'
#yourdate = dateutil.parser.parse(timestring)
import dateutil.parser
#when dealing with time in python, use strftime
def day_in_words(earthquake):
    datestring=earthquake['time']
    eq_date=dateutil.parser.parse(datestring)
    return eq_date.strftime('%A')
print(day_in_words(earthquake))
#morning:before noon
#afternoon: before 6
#evening:before 9
#.hour is going to give us the hour of the day from the object
def time_in_words(earthquake):
    datestring=earthquake['time']
    eq_date=dateutil.parser.parse(datestring) #object
    if eq_date.hour <3:
        return "early morning"
    elif eq_date.hour < 12:
        return "morning"
    elif eq_date.hour < 18:
        return "afternoon"
    else:
        return "night"
print(time_in_words(earthquake))
def date_in_words(earthquake):
    datestring=earthquake['time']
    eq_date=dateutil.parser.parse(datestring)
    return (eq_date.strftime('%B %-d').replace(" 0", " "))
print(date_in_words(earthquake))<jupyter_output>June 4<jupyter_text>PART TWO: Write the eq_to_sentence functionWrite a function called eq_to_sentence that, when called, returns the whole sentence mentioned above, "A DEPTH DESCRIPTION, MAGNITUDE earthquake was reported DAY TIME_OF_DAY on DATE LOCATION."Print out the result for the sample earthquake.<jupyter_code>#depth_to_words will describe the earthquake's depth
#magnitude_to_words will describe the earthquake's power
#day_in_words should be the day of the week
#time_in_words should be "morning", "afternoon", "evening" or "night"
#date_in_words should be "Monthname day", e.g. "June 22"
#Any other functions as necessary

depth=depth_to_words(earthquake)
power=magnitude_to_words(earthquake)
day=day_in_words(earthquake)
time_of_the_day=time_in_words(earthquake)
date=date_in_words(earthquake)
"A {} {} {} earthquake was reported {} {} on {}, {}".format(depth, power,earthquake['mag'], day, time_of_the_day, date,earthquake['place'])
type(earthquake['mag'])
#when you have a dictionary, and you do format, use(** format)
name = "Smushface"
animal = "cat"
"My name is {} and I am a {}".format(name, animal)
animal_dictionary = {
    'name': 'Smushface',
    'animal': 'cat'
}
"I am a {animal} and my name is {name}".format(**animal_dictionary)
def eq_to_sentence(earthquake):
    eq_to_sentence = {
         'depth':depth_to_words(earthquake),
         'power':magnitude_to_words(earthquake),
         'day':day_in_words(earthquake),
         'time_of_the_day':time_in_words(earthquake),
         'date':date_in_words(earthquake),
         'location':earthquake['place'],
         'magnitude': earthquake['mag']
        }
    return "A {depth} {power} {magnitude} earthquake was reported {day} {time_of_the_day} on {location}, {magnitude}".format(**eq_to_sentence)

eq_to_sentence(earthquake)
earthquakes_df = pd.read_csv("http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/1.0_month.csv")
earthquakes_df.head()
earthquakes = earthquakes_df.to_dict('records')<jupyter_output><empty_output><jupyter_text>PART THREE: Doing it in bulkRead in the csv of the past 30 days of 1.0+ earthquke activity from http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/1.0_month.csv (tip: read_csv works with URLs!)Because we haven't covered looping through pandas, use the following code to convert a pandas DataFrame into a list of dictionaries that you can loop through.earthquakes_df = pd.read_csv("1.0_month.csv") earthquakes = earthquakes_df.to_dict('records')(If you really want to do it with pandas, it's for index, row in earthquakes_df.iterrows():)Loop through each earthquake, printing sentence descriptions for the ones that are above or equal to 4.0 on the Richter scale.<jupyter_code>#convert the dataframe into a list of dictionaries and pass them to our function, slice off the first 100
for earthquake in earthquakes[0:100]:
    print(eq_to_sentence(earthquake))<jupyter_output>A shallow intermediate 3.37 earthquake was reported Tuesday night on 3km W of Cobb, California, 3.37
A shallow minor 1.7 earthquake was reported Tuesday night on 2km SW of Cobb, California, 1.7
A shallow minor 1.67 earthquake was reported Tuesday night on 5km ESE of The Geysers, California, 1.67
A shallow minor 1.6 earthquake was reported Tuesday night on 92km N of Redoubt Volcano, Alaska, 1.6
A shallow minor 2.0 earthquake was reported Tuesday night on 91km N of Redoubt Volcano, Alaska, 2.0
A shallow intermediate 4.7 earthquake was reported Tuesday night on South of the Fiji Islands, 4.7
A shallow intermediate 4.6 earthquake was reported Tuesday night on 236km WNW of Tual, Indonesia, 4.6
A shallow minor 1.12 earthquake was reported Tuesday night on 10km NE of Indio, CA, 1.12
A shallow minor 1.6 earthquake was reported Tuesday night on 13km SSW of La Parguera, Puerto Rico, 1.6
A shallow high 5.2 earthquake was reported Tuesday night on Southern Mid-Atlantic Ridge, 5.2
A shallow minor 1[...]<jupyter_text>PART FOUR: The other bitsIf the earthquake is anything other than an earthquake (e.g. explosion or quarry blast), printThere was also a magnitude MAGNITUDE TYPE_OF_EVENT on DATE LOCATION.For example,There was also a magnitude 1.29 quarry blast on June 19 12km SE of Tehachapi, California.with TYPE_OF_EVENT being explosion, quarry blast, etc and LOCATION being 'place' - e.g. '0km N of The Geysers, California'.<jupyter_code>earthquakes = earthquakes_df.to_dict('records')
for earthquake in earthquakes:
    if earthquake['type'] != 'earthquake':
        print("There was also a magnitude", earthquake['mag'], earthquake['type'], "at", earthquake['place'])
    else:
        pass<jupyter_output>There was also a magnitude 1.98 explosion at 5km NNW of Bend, Oregon
There was also a magnitude 1.77 explosion at 2km NW of Sweet Home, Oregon
There was also a magnitude 1.33 quarry blast at 11km E of Quarry near Portola Valley, CA
There was also a magnitude 1.85 explosion at 2km E of Granite Falls, Washington
There was also a magnitude 1.23 quarry blast at 13km W of Mojave, CA
There was also a magnitude 1.77 quarry blast at 7km SSE of Home Gardens, CA
There was also a magnitude 1.29 other event at 10km SW of Bridgeport, Washington
There was also a magnitude 1.95 explosion at 1km SSW of Princeton, Canada
There was also a magnitude 1.36 other event at 10km SW of Bridgeport, Washington
There was also a magnitude 1.27 other event at 30km ESE of Sweet Home, Oregon
There was also a magnitude 1.74 explosion at 9km S of Princeton, Canada
There was also a magnitude 1.22 explosion at 8km E of Yacolt, Washington
There was also a magnitude 1.99 explosion at 26km WSW of Cheney, Washington
There wa[...]
{'fraction_non_alphanumeric': 0.09916024387438169, 'fraction_numerical': 0.032900034510525714, 'mean_word_length': 4.555271565495207, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 17, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '323019', 'license': 'MIT', 'n_tokens_mistral': 3087, 'n_tokens_neox': 2739, 'n_words': 959, 'path': 'homework 9/homework 9.ipynb', 'repo_name': 'sz2472/foundations-homework', 'size': '8693'}