Asn0: Places

places.py

"Classes" version, with disk=file reading.
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 15 13:05:08 2021

@author: rmontant
"""

class Place:
    def __init__(self, name, lat, long, pop):
        self.name = name
        self.coords = (lat, long)
        self.size = pop

    def relative(self, other):
        if self.coords[0] < other.coords[0]:
            return 'south of'
        else:
            return 'lost'

    def ratio(self, other):
        return other.size / self.size
#--------

# Make sure this script, and the "earthquakes" data file,
# are in the same folder!
# Or else specify the full path to the data file, like
# 'C:\Users\YOURACCOUNT\Desktop\earthquakes-fixed.csv"
# or wherever it is.
#handle = open('earthquakes-fixed.csv', 'r')
handle = open('//buad.bloomu.edu/data/UsersMNO/rmontant/Downloads/earthquakes-fixed.csv', 'r')

places = []

for line in handle.readlines():
    if line[0] == '#':      # is it just a comment?
        continue
    ts = line[3:26]                 #
    lat = float( line[26:40] )      # SLICES of the line
    long = float( line[40:55] )     #
    locn = line[78:]                #

    l = Place(locn, lat, long, float(line[66:74]))
    places.append(l)

handle.close()  # tidy up after we're done

print(len(places), 'total earthquakes')

# Find the big ones:
big = 0
for place in places:
    if place.size > 5:
        print('{:26s}  {:6.1f}Lat, {:6.1f}Long, {:3f}' \
              .format(place.name, place.coords[0],place.coords[1], place.size))
        big += 1
print('{:d} big earthquakes'.format(big))