Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added notes.txt
Empty file.
17 changes: 17 additions & 0 deletions src/Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]
black = "*"
pylint = "*"
pytest = "*"

[packages]

[requires]
python_version = "3.7"

[pipenv]
allow_prereleases = true
250 changes: 250 additions & 0 deletions src/Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

109 changes: 60 additions & 49 deletions src/cityreader/cityreader.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Create a class to hold a city location. Call the class "City". It should have
# fields for name, lat and lon (representing latitude and longitude).


# We have a collection of US cities with population over 750,000 stored in the
# file "cities.csv". (CSV stands for "comma-separated values".)
#
Expand All @@ -14,58 +13,70 @@
#
# Note that the first line of the CSV is header that describes the fields--this
# should not be loaded into a City object.

# # #GGA work

# skip header
#https://www.alexkras.com/how-to-read-csv-file-in-python/

# import library
import csv # for opening csv files

# creating an OOP Class
class City():
# constructor method (function)
def __init__(self, name, lat, lon):
# new attributes (name, latitude, longitude)
self.name = name
self.lat = lat
self.lon = lon
# Just to be thorough:
# this allows for clean printing of objects for inspection
def __str__(self):
# using fstring to manage the format of priting
return f'City("{self.name}", {self.lat}, {self.lon}),'



# create variables
#
# cities-list will contain the target results
cities = []

# instructions:
# Implement the functionality to read from the 'cities.csv' file
# For each city record, create a new City instance and add it to the
# `cities` list

# function to read and process a CSV file
def cityreader(cities=[]):
# TODO Implement the functionality to read from the 'cities.csv' file
# For each city record, create a new City instance and add it to the
# `cities` list

## this is the full list of options for column-features
## city,state_name,county_name,lat,lng,population,density,timezone,zips

# opens csv file
with open('cities.csv') as csvfile:

# reading the csv file using the imported library
readCSV = csv.reader(csvfile, delimiter=',')

# skip header
next(readCSV)

# this loop iterates through the csv
# and pulls out the 3 required columns
for row in readCSV:
cities.append(City(str(row[0]), float(row[3]), float(row[4])))

# finally this returns the completed list of city-objects
return cities

cityreader(cities)
#run function
cities = cityreader(cities)

# Print the list of cities (name, lat, lon), 1 record per line.
for c in cities:
print(c)
# print a list of just names
# for all the listed cites
# print([i.name for i in cities])
# print(cities)

# STRETCH GOAL!
#
# Allow the user to input two points, each specified by latitude and longitude.
# These points form the corners of a lat/lon square. Pass these latitude and
# longitude values as parameters to the `cityreader_stretch` function, along
# with the `cities` list that holds all the City instances from the `cityreader`
# function. This function should output all the cities that fall within the
# coordinate square.
#
# Be aware that the user could specify either a lower-left/upper-right pair of
# coordinates, or an upper-left/lower-right pair of coordinates. Hint: normalize
# the input data so that it's always one or the other, then search for cities.
# In the example below, inputting 32, -120 first and then 45, -100 should not
# change the results of what the `cityreader_stretch` function returns.
#
# Example I/O:
#
# Enter lat1,lon1: 45,-100
# Enter lat2,lon2: 32,-120
# Albuquerque: (35.1055,-106.6476)
# Riverside: (33.9382,-117.3949)
# San Diego: (32.8312,-117.1225)
# Los Angeles: (34.114,-118.4068)
# Las Vegas: (36.2288,-115.2603)
# Denver: (39.7621,-104.8759)
# Phoenix: (33.5722,-112.0891)
# Tucson: (32.1558,-110.8777)
# Salt Lake City: (40.7774,-111.9301)

# TODO Get latitude and longitude values from the user

def cityreader_stretch(lat1, lon1, lat2, lon2, cities=[]):
# within will hold the cities that fall within the specified region
within = []

# TODO Ensure that the lat and lon valuse are all floats
# Go through each city and check to see if it falls within
# the specified coordinates.

return within
for c in cities:
print(c)
Loading