DEV Community

Cover image for Unraveling the Script: A Dive into Dynamic Data Fetching and Storage
Rikin Patel
Rikin Patel

Posted on • Edited on

Unraveling the Script: A Dive into Dynamic Data Fetching and Storage

In the world of programming, versatility and adaptability are paramount. The script presented here demonstrates an insightful combination of various Python modules and concepts to dynamically fetch data from an API, process it based on user preferences, and systematically store it. Let's embark on a journey to unravel the key concepts encapsulated in this intriguing piece of code.

1. Dependency Management and Exception Handling

The script starts with a robust approach to dependency management and exception handling. It uses the try and except blocks to handle the potential absence of required modules gracefully. If a module is not found, it prompts the user to download dependencies from a requirement.txt file, ensuring a seamless setup.

try: 
    import os
    import requests
    import csv
    import time 
    import json
    import schedule
    import yaml
    from xml.etree import ElementTree as ET
    from datetime import datetime
except ModuleNotFoundError:
    print("Please download dependencies from requirement.txt")
except Exception as ex:
    print(ex)
Enter fullscreen mode Exit fullscreen mode

2. The classifier Class: A Blueprint for Dynamic Handling

The heart of the script is the classifier class, embodying the concept of dynamic handling based on user inputs. This class encapsulates methods to create structured paths for data storage, fetch data from a specified URL, and store data in different formats using a dynamic switch mechanism.

class classifier:
    def __init__(self, url: str, file_type: str)-> None :
        # Initialization with URL and file type
        self.file_type = str(file_type)
        self.url = url

    def pathmaker(self)-> str:
        # Generating structured directory and file name based on timestamp
        # ...

    def fetchdata(self)-> requests.Response:
        # Fetching data from the specified URL in the chosen format
        # ...

    def storedata(self, data: requests.Response, file_path: str)-> None:
        # Storing fetched data in different formats (JSON, CSV, YAML, XML)
        # ...

    @staticmethod
    def job(fileformat)-> None:
        # Method to handle dynamic job scheduling
        # ...
Enter fullscreen mode Exit fullscreen mode

3. Dynamic Switching with a Dictionary

One standout feature is the dynamic switching mechanism employed to handle various file formats. The script uses a dictionary (switch_dict) to map file types to their respective storage functions. This not only adds flexibility but also enhances the script's readability and maintainability.

switch_dict = {
    'json': store_json,
    'csv': store_csv,
    'yaml': store_yaml,
    'xml': store_xml,
}
selected_function = switch_dict.get(self.file_type, lambda: print("Invalid format"))
selected_function()
Enter fullscreen mode Exit fullscreen mode

4. User Interaction and Scheduled Jobs

The script doesn't just stop at fetching and storing data; it actively engages the user by prompting for the desired file format. The use of a while loop ensures that the user gets to make a valid choice before proceeding.

while True:
    fileformat = input('Enter the file format that you want to fetch: \ncsv \njson \nyaml \nxml\n')

    if fileformat.lower() in validformats:
        break
    else:
        print(f"Invalid file format entered! Please choose from {', '.join(validformats)}")
Enter fullscreen mode Exit fullscreen mode

Additionally, the script employs the schedule module to run a job at regular intervals (every 3 seconds in this case), showcasing the capability to automate tasks.

5. Continuous Execution with While Loop

The use of an outer while True loop ensures the script keeps running, allowing scheduled jobs to execute periodically.

while True:
    schedule.run_pending()
    time.sleep(1)
Enter fullscreen mode Exit fullscreen mode

Conclusion

In essence, this script serves as an insightful exploration of dynamic handling, user interaction, and scheduled automation. By combining these concepts, it creates a versatile tool that can adapt to different scenarios, making it a valuable addition to the arsenal of any Python programmer. The code structure and design choices offer a plethora of learning opportunities, making it a fascinating piece for enthusiasts eager to delve into the realms of dynamic data handling and automation.

Top comments (0)