PYTHON: read.py from pathlib import Path import csv from sportclub import SportClub from typing import List, Tuple def readFile(file: Path) -> List[Tuple[str, str, str]]:    """Read a CSV file and return its content. A good CSV file will have the header "City,Team Name,Sport" and appropriate content. Args: file: a path to the file to be read Returns: a list of tuples that each contain (city, name, sport) of the SportClub Raises: ValueError: if the reading csv has missing data (empty fields)"""    # TODO: Complete the function def readAllFiles() -> List[SportClub]:    """Read all the csv files in the current working directory to create a list of SportClubs that contain unique SportClubs with their corresponding counts. Take all the csv files in the current working directory, calls readFile(file) on each of them, and accumulates the data gathered into a list of SportClubs.Create a new file called "report.txt" in the current working directory containing the number of good files and good lines read. Create a new file called "error_log.txt" in the current working directory containing the name of the error/bad files read.Returns: a list of unique SportClub objects with their respective counts    """    # TODO: Complete the function sportclub.py class SportClub:    """A simple class to store and handle information about SportClubs.    Attributes:        city (str): The city the SportClub is based in.        name (str): The name of the SportClub.        sport (str): The sport the club plays.        count (int): The amount of time the SportClub has been seen.    Todo:        complete the __eq__ and __lt__ functions of this class    """    def __init__(self,  city: str = "", name: str = "", sport: str= "", count: int = 0) -> None:        self.setCity(city)        self.setName(name)        self.setSport(sport)        self.count = count    def setName(self, name: str) -> None:        self.name = name    def setCity(self, city: str) -> None:        self.city = city    def setSport(self, sport: str) -> None:        self.sport = sport    def getName(self) -> str:        return self.name.title()    def getCity(self):        return self.city.title()    def getSport(self):        return self.sport.upper()    def getCount(self):        return self.count    def incrementCount(self) -> None:        self.count += 1    def __hash__(self) -> int:        unique_identifier = (self.getCity(), self.getName(), self.getSport())        return hash(unique_identifier)    def __str__(self) -> str:        return f"Name: {self.getCity()} {self.getName()}, Sport: {self.getSport()}, Count: {self.getCount()}"    def __eq__(self, o: object) -> bool:        """Check if another object is equal to self.        Returns:            True if they are equal, False otherwise        """        # TODO: Complete the function, which can be useful for ordering a List of Sportclub objects        # hint: object o may not be of type SportClub.    def __lt__(self, o: object) -> bool:        """Check if self is less than another object.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

PYTHON:

read.py

from pathlib import Path
import csv
from sportclub import SportClub
from typing import List, Tuple
def readFile(file: Path) -> List[Tuple[str, str, str]]:
   """Read a CSV file and return its content. A good CSV file will have the header "City,Team Name,Sport" and appropriate content. Args: file: a path to the file to be read Returns: a list of tuples that each contain (city, name, sport) of the SportClub Raises: ValueError: if the reading csv has missing data (empty fields)"""
   # TODO: Complete the function

def readAllFiles() -> List[SportClub]:
   """Read all the csv files in the current working directory to create a list of SportClubs that contain unique SportClubs with their corresponding counts. Take all the csv files in the current working directory, calls readFile(file) on each of them, and accumulates the data gathered into a list of SportClubs.Create a new file called "report.txt" in the current working directory containing the number of good files and good lines read. Create a new file called "error_log.txt" in the current working directory containing the name of the error/bad files read.Returns: a list of unique SportClub objects with their respective counts
   """
   # TODO: Complete the function
sportclub.py

class SportClub:
   """A simple class to store and handle information about SportClubs.

   Attributes:
       city (str): The city the SportClub is based in.
       name (str): The name of the SportClub.
       sport (str): The sport the club plays.
       count (int): The amount of time the SportClub has been seen.

   Todo:
       complete the __eq__ and __lt__ functions of this class
   """
   def __init__(self,  city: str = "", name: str = "", sport: str= "", count: int = 0) -> None:
       self.setCity(city)
       self.setName(name)
       self.setSport(sport)
       self.count = count

   def setName(self, name: str) -> None:
       self.name = name

   def setCity(self, city: str) -> None:
       self.city = city

   def setSport(self, sport: str) -> None:
       self.sport = sport

   def getName(self) -> str:
       return self.name.title()

   def getCity(self):
       return self.city.title()

   def getSport(self):
       return self.sport.upper()

   def getCount(self):
       return self.count

   def incrementCount(self) -> None:
       self.count += 1

   def __hash__(self) -> int:
       unique_identifier = (self.getCity(), self.getName(), self.getSport())
       return hash(unique_identifier)

   def __str__(self) -> str:
       return f"Name: {self.getCity()} {self.getName()}, Sport: {self.getSport()}, Count: {self.getCount()}"

   def __eq__(self, o: object) -> bool:
       """Check if another object is equal to self.

       Returns:
           True if they are equal, False otherwise
       """
       # TODO: Complete the function, which can be useful for ordering a List of Sportclub objects
       # hint: object o may not be of type SportClub.

   def __lt__(self, o: object) -> bool:
       """Check if self is less than another object.

       Returns:
           True if self is less than o, False otherwise
       """
       # TODO: Complete the function, which can be useful for ordering a List of Sportclub objects
       # hint: object o may not be of type SportClub.

write.py

import csv
from sportclub import SportClub
from typing import List, Iterable

def separateSports(all_clubs: List[SportClub]) -> Iterable[List[SportClub]]:
   """Separate a list of SportClubs into their own sports. For example, given the list [SportClub("LA", "Lakers", "NBA"), SportClub("Houston", "Rockets", "NBA"), SportClub("LA", "Angels", "MLB")],
   return the iterable [[SportClub("LA", "Lakers", "NBA"), SportClub("Houston", "Rockets", "NBA")], [SportClub("LA", "Angels", "MLB")]] Args: all_clubs: A list of SportClubs that contain SportClubs of 1 or more sports. Returns: An iterable of lists of sportclubs that only contain clubs playing the same sport. 
   """
   # TODO: Complete the function


def sortSport(sport: List[SportClub]) -> List[SportClub]:
   """Sort a list of SportClubs by the inverse of their count and their name. For example, given the list [SportClub("Houston", "Rockets", "NBA", 80), SportClub("LA", "Warriors", "NBA", 130), SportClub("LA", "Lakers", "NBA", 130)] return the list [SportClub("LA", "Lakers", "NBA", 130), SportClub("LA", "Warriors", "NBA", 130), SportClub("Houston", "Rockets", "NBA", 80)] Args: sport: A list of SportClubs that only contain clubs playing the same sport Returns: A sorted list of the SportClubs  
   """
   # TODO: Complete the function
   # hint: check documentation for sorting lists 
  
   def outputSports(sorted_sports: Iterable[List[SportClub]]) -> None:
   """Create the output csv given an iterable of list of sorted clubs. Create the csv "survey_database.csv" in the current working directory, and output the information: "City,Team Name,Sport,Number of Times Picked" for the top 3 teams in each sport. Args: sorted_sports: an Iterable of different sports, each already sorted correctly
   """
   # TODO: Complete the function

Transcription of the Educational Text:

---

I need help with this program that I'm working on...

I need to create a program that will do the following:

1. Consolidate all the information from the CSV files they have produced into a single CSV file called "survey_database.csv"
2. Report on a summary of the collected data in a file called "report.txt"
3. Report an error log for the collected data in a file called "error_log.txt"

The following are their detailed requirements for the program:

- **Survey Database**
  - I want the program to be able to search, find and read all the CSV files in the current working directory to create the "survey_database.csv" file in the current working directory.
  - The CSV should contain the following information (defined in self-identifying column header names):
    - "City", "Team Name", "Sport", "Number of Times Picked"
  - The rows in the following table should contain this information for the top 3 most picked teams in each sport.
    - "picked" in this context is the team appearing once in the CSV files
    - The ranking of the teams is defined by the inverse of the times they were picked and their alphabetic Team Name. In other words, teams that appear more often in the read CSV files are at the top, in the event of ties in their count, their names are used to resolve the ties.

- **Report**
  - I want a new report created every time they run the program. The following is the content of the summary report in a "report.txt" file created in the current working directory. The report is simply a text file with the following information:
    - Number of files read: (num good files read)
    - Number of lines read: (num lines read in good files read)

- **Error Log**
  - A new error log needs to be created every time they run the program. This error log simply has the name of the files that contain errors, on their separate line. An error could be missing strings or empty strings. Create a file called "error_log.txt" in the current working directory with that information.

**Important details:**
1. If a file has an error (i.e. it is missing one of the 3 fields it should have (City, Team Name or Sport)), it file is considered corrupted and it will not count AT ALL. In other words, if you read 5
Transcribed Image Text:Transcription of the Educational Text: --- I need help with this program that I'm working on... I need to create a program that will do the following: 1. Consolidate all the information from the CSV files they have produced into a single CSV file called "survey_database.csv" 2. Report on a summary of the collected data in a file called "report.txt" 3. Report an error log for the collected data in a file called "error_log.txt" The following are their detailed requirements for the program: - **Survey Database** - I want the program to be able to search, find and read all the CSV files in the current working directory to create the "survey_database.csv" file in the current working directory. - The CSV should contain the following information (defined in self-identifying column header names): - "City", "Team Name", "Sport", "Number of Times Picked" - The rows in the following table should contain this information for the top 3 most picked teams in each sport. - "picked" in this context is the team appearing once in the CSV files - The ranking of the teams is defined by the inverse of the times they were picked and their alphabetic Team Name. In other words, teams that appear more often in the read CSV files are at the top, in the event of ties in their count, their names are used to resolve the ties. - **Report** - I want a new report created every time they run the program. The following is the content of the summary report in a "report.txt" file created in the current working directory. The report is simply a text file with the following information: - Number of files read: (num good files read) - Number of lines read: (num lines read in good files read) - **Error Log** - A new error log needs to be created every time they run the program. This error log simply has the name of the files that contain errors, on their separate line. An error could be missing strings or empty strings. Create a file called "error_log.txt" in the current working directory with that information. **Important details:** 1. If a file has an error (i.e. it is missing one of the 3 fields it should have (City, Team Name or Sport)), it file is considered corrupted and it will not count AT ALL. In other words, if you read 5
```python
from read import readAllFiles
from write import sortSport, separateSports, outputSports

def main() -> None:
    separated_sports = separateSports(readAllFiles())
    sorted_sports = map(sortSport, separated_sports)
    outputSports(sorted_sports)

if __name__ == "__main__":
    main()
```

### Explanation

This code is a Python script designed to process sports data. Here's a breakdown of its functionality:

1. **Imports**:
   - `readAllFiles` is imported from the `read` module. This function is likely responsible for reading data from multiple files.
   - `sortSport`, `separateSports`, and `outputSports` are imported from the `write` module. These functions are used to process, categorize, and output sports data.

2. **Main Function**:
   - The `main()` function orchestrates the workflow:
     - `separateSports(readAllFiles())`: Reads and then separates the sports data into distinct categories.
     - `map(sortSport, separated_sports)`: Applies the `sortSport` function to each category to sort the sports data.
     - `outputSports(sorted_sports)`: Outputs or saves the sorted sports data.

3. **Execution**:
   - The `if __name__ == "__main__":` block ensures that the `main()` function is called only when the script is run directly, not when imported as a module. 

This structure is useful for organizing a sports data processing pipeline effectively.
Transcribed Image Text:```python from read import readAllFiles from write import sortSport, separateSports, outputSports def main() -> None: separated_sports = separateSports(readAllFiles()) sorted_sports = map(sortSport, separated_sports) outputSports(sorted_sports) if __name__ == "__main__": main() ``` ### Explanation This code is a Python script designed to process sports data. Here's a breakdown of its functionality: 1. **Imports**: - `readAllFiles` is imported from the `read` module. This function is likely responsible for reading data from multiple files. - `sortSport`, `separateSports`, and `outputSports` are imported from the `write` module. These functions are used to process, categorize, and output sports data. 2. **Main Function**: - The `main()` function orchestrates the workflow: - `separateSports(readAllFiles())`: Reads and then separates the sports data into distinct categories. - `map(sortSport, separated_sports)`: Applies the `sortSport` function to each category to sort the sports data. - `outputSports(sorted_sports)`: Outputs or saves the sorted sports data. 3. **Execution**: - The `if __name__ == "__main__":` block ensures that the `main()` function is called only when the script is run directly, not when imported as a module. This structure is useful for organizing a sports data processing pipeline effectively.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY