Untitled

 avatar
unknown
plain_text
3 months ago
3.3 kB
6
Indexable
import sys
import argparse

# Sample database containing object information
HOME_DATABASE = """shoes,floor,8/23/23,10:00am
papers,cubbard,7/23/23,1:00am
picture frames,wall,6/23/23,10:00pm
tshirts,cubbard,1/13/23,9:00am
soccer balls,basket,2/9/22,12:00pm
kitchen tables,floor,3/23/23,4:00pm
cabinets,corner,5/30/23,10:30pm""" 

def main(name):
    """Search for an item in HOME_DATABASE and return its details."""
    
    # If no argument is given, return an error message
    if name is None:
        return "Error: No item name was provided. Please run the script with an item name."

    # This will convert database into a list of strings by using splitlines
    items = HOME_DATABASE.splitlines()  
    
    # Iterate through items to find a match
    for item in items:
        parts = item.split(",")  # Split each line into individual parts
        item_name, loc_item, date_moved, time_moved = parts

        # This is where after the input was given will give the sentence and also make it case insensitive
        if item_name.lower() == name.lower():
            return f"The {item_name} were found in the {loc_item} and were placed there on {date_moved} at {time_moved}."

    # If no match is found, return an error message **outside** the loop
    return f"Sorry, could not find your item named {name} within the database."

def parse_args(args_list):
    """
    Takes a list of strings from the command prompt and passes them through as arguments.

    Args:
        args_list (list): The list of strings from the command prompt.

    Returns:
        args (ArgumentParser): Parsed arguments object.
    """

    # For the sake of readability, it is important to insert comments throughout.
    # Complicated operations get a few lines of comments before the operations commence.
    # Non-obvious ones get comments at the end of the line.

    # This function uses the argparse module to parse command line arguments.
    parser = argparse.ArgumentParser()  # Create an ArgumentParser object.

    # Add arguments to this parser object.
    # In this case, we have a required positional argument.
    # Followed by an optional keyword argument which contains a default value.
    parser.add_argument(
        'object', 
        type=str, 
        nargs='?',  # Allows argument to be optional
        default=None,  # Default value if no argument is provided
        help="Please enter the name that we are searching for."
    )

    # Parse the list of command line arguments using this object.
    args = parser.parse_args(args_list)  
    return args

if __name__ == "__main__":
    # If name == main statements check:
    # Is the current script being run natively or as a module?
    # If the script is being run as a module, the block of code under this will not be executed.
    # If the script is being run natively, the block of code below this will be executed.

    # Pass in the list of command line arguments to the parse_args function.
    arguments = parse_args(sys.argv[1:])  

    # The returned object contains those command line arguments as attributes.
    # We will pass this argument into the main function.
    # Note that you do not need a main function, but you might find it helpful.
    # Keep minimal code under the 'if __name__ == "__main__":' statement.
    print(main(arguments.object))
Editor is loading...
Leave a Comment