Exemple #1
0
def create_type_specific_record(resource):
    """
    Obtain data pertaining to either a Book or Movie resource and write data to
    either the Book.csv file or the Movie.csv file.
    
    Parameters:
        resource (Resource): Resource object; either Book or Movie
    """
    
    if resource["Type"] == "book":
            
        # Get book data
        book = get_book(resource["UID"])
               
        # Obtain correct csv file path for books
        file_path = get_csv_path("Book.csv")
        
        # Write resource record
        write_dict_to_csv(file_path, book, 'a')
        
    else:
        
        # Get movie data
        movie = get_movie(resource["UID"])
        
        # Obtain correct csv file path for movies
        file_path = get_csv_path("Movie.csv")
    
        # Write resource record
        write_dict_to_csv(file_path, movie, 'a')
Exemple #2
0
def write_resource_record(resource):
    """
    Writes one record to the Resources.csv file.
    
    Parameters:
        resource_list(Resource): Resource object; either Book or Movie
    """
    
    # Obtain correct csv file path for resources
    file_path = get_csv_path("Resource.csv")
    
    # Write resource record
    write_dict_to_csv(file_path, resource, 'a')
Exemple #3
0
def write_book_csv(resource_list, csv_file):
    """
    Write book data to Book.csv file
    
    Parameters:
        resource_list (list): List of resource objects; either Book or Movie
        csv_file (string): File path to CSV file
    """
    
    # Keep track of whether a record should be written or appended
    book_written = False
    
    # Loop through each resource
    for i in range(len(resource_list)):
        
        if resource_list[i].resource_type == "book":
            
            # No record has been written yet
            if book_written == False:
                
                # Overwrite file
                file_mode = 'w'
            
            else:
                
                # Append to end of file
                file_mode = 'a'
            
            # Write book data
            dictionary = {"UID": resource_list[i].get_uid(),
                          "Publisher": resource_list[i].publisher,
                          "City": resource_list[i].city,
                          "Category": resource_list[i].category}
            
            write_dict_to_csv(csv_file, dictionary, file_mode)
            
            # Resource has been writetn
            book_written = True
Exemple #4
0
def write_resource_csv(file_path, resource_list):
    """
    Writes Resource objects contained in the resource list to file at file path.
    
    Parameters:
        file_path (string): Path to csv file
        resource_list (list): List of Resource objects; either Book or Movie
    """
    
    # Write initial record to overwrite CSV file
    dictionary = {"UID": resource_list[0].get_uid(), 
                  "Title": resource_list[0].title, 
               "Creator": resource_list[0].creator.get_full_name(), 
               "Summary": resource_list[0].summary, 
               "Genre": resource_list[0].genre,
               "Language": resource_list[0].language, 
               "Year": resource_list[0].year, 
               "Country": resource_list[0].country, 
               "Length": resource_list[0].length,
               "Type": resource_list[0].resource_type}
    
    write_dict_to_csv(file_path, dictionary, 'w')
    
    # Loop through each resource and create a list to write to csv file
    for i in range(1, len(resource_list)):
        
        dictionary = {"UID": resource_list[i].get_uid(),
                      "Title": resource_list[i].title,
                      "Creator": resource_list[i].creator.get_full_name(),
                      "Summary": resource_list[i].summary,
                      "Genre": resource_list[i].genre,
                      "Language": resource_list[i].language,
                      "Year": resource_list[i].year,
                      "Country": resource_list[i].country,
                      "Length": resource_list[i].length,
                      "Type": resource_list[i].resource_type}
        
        write_dict_to_csv(file_path, dictionary, 'a')
Exemple #5
0
def write_movie_csv(resource_list, csv_file):
    """
    Write movie data to Movie.csv file
    
    Parameters:
        resource_list (list): List of resource objects; either Book or Movie
        csv_file (string): File path to CSV file
    """
    
    # Keep track of whether a record should be written or appended
    movie_written = False
    
    # Loop through each resource
    for i in range(len(resource_list)):
    
        if resource_list[i].resource_type == "movie":
                
                # No record has been written yet
                if movie_written == False:
                    
                    # Overwrite file
                    file_mode = 'w'
                
                else:
                    
                    # Append to end of file
                    file_mode = 'a'
               
                # Write movie data
                dictionary = {"UID": resource_list[i].get_uid(), 
                              "Rating": resource_list[i].rating}
            
                write_dict_to_csv(csv_file, dictionary, file_mode)
                
                # Resource has been writetn
                movie_written = True