Appearance
question:You are tasked with implementing a script that processes a directory of text files containing numerical data. The script should perform the following steps: 1. **Search for all `.txt` files in a specified directory.** 2. **For each `.txt` file, read its content and compute statistical properties: mean, median, and variance.** 3. **Output the computed statistics to a new file named `summary.txt` in the same directory.** 4. **Optionally, print the top N lines from each file as an additional summary if a command-line argument specifying the number of lines is provided.** # Your task is to implement the following function: ```python import os import glob import statistics import argparse from typing import List def process_directory(directory: str, top_lines: int = None) -> None: Processes all .txt files in the specified directory, computes statistical properties for each file, and summarizes the results in 'summary.txt'. Optionally prints the top N lines from each file. Args: directory (str): The directory to search for .txt files. top_lines (int, optional): The number of top lines to print from each file. Defaults to None. Returns: None # Your implementation here ``` # Requirements: - The function should: - Search for `.txt` files in the given directory using the `glob` module. - Read each `.txt` file and assume each line contains a single floating-point number. - Compute the mean, median, and variance for the numbers in each file using the `statistics` module. - Write a summary of computed statistics to a file named `summary.txt` in the given directory. - If the `top_lines` argument is provided, print the specified number of top lines from each file. # Input: - `directory`: A string specifying the directory to process. - `top_lines`: An optional integer specifying the number of top lines to print from each file (using the command-line argument `--lines`). # Output: - A file named `summary.txt` in the given directory containing the mean, median, and variance of the numerical data from each `.txt` file. - Optionally, prints the top N lines from each file to the console. # Example Usage: ```shell python script.py /path/to/directory --lines=5 ``` This command will process all `.txt` files in `/path/to/directory`, compute the required statistics, write them to `summary.txt`, and print the top 5 lines from each file. If the `--lines` argument is omitted, it will only compute and write the statistics without printing any lines. # Constraints: - The directory will contain only `.txt` files, each formatted correctly with one floating-point number per line. - You may assume the directory and files exist and are accessible. # Hints: - Use the `glob` module to list `.txt` files in the directory. - Use the `statistics` module to compute the mean, median, and variance. - Use the `argparse` module to handle optional command-line arguments for the number of lines to print.
answer:import os import glob import statistics import argparse from typing import List def process_directory(directory: str, top_lines: int = None) -> None: Processes all .txt files in the specified directory, computes statistical properties for each file, and summarizes the results in 'summary.txt'. Optionally prints the top N lines from each file. Args: directory (str): The directory to search for .txt files. top_lines (int, optional): The number of top lines to print from each file. Defaults to None. Returns: None txt_files = glob.glob(os.path.join(directory, "*.txt")) summary_data = [] for txt_file in txt_files: with open(txt_file, 'r') as file: lines = file.readlines() numbers = [float(line.strip()) for line in lines] mean = statistics.mean(numbers) median = statistics.median(numbers) variance = statistics.variance(numbers) if len(numbers) > 1 else 0.0 summary_data.append(f"File: {os.path.basename(txt_file)}") summary_data.append(f"Mean: {mean:.2f}") summary_data.append(f"Median: {median:.2f}") summary_data.append(f"Variance: {variance:.2f}") summary_data.append("n") if top_lines: print(f"Top {top_lines} lines from {os.path.basename(txt_file)}:") for line in lines[:top_lines]: print(line.strip()) print("n") with open(os.path.join(directory, "summary.txt"), 'w') as summary_file: summary_file.write("n".join(summary_data))
question:Objective Implement and demonstrate the use of the `zoneinfo` module in Python 3.9. You will create timezone-aware datetime objects, handle timezone transitions, and manage ambiguous times using the fold attribute. Requirements 1. Define a function `get_aware_datetime(timezone, year, month, day, hour, minute)` that takes a timezone string and a datetime (year, month, day, hour, minute), and returns a timezone-aware datetime object using the `ZoneInfo` module. 2. Define a function `calculate_future_time(base_datetime, delta_days)` that takes a timezone-aware datetime object and a number of days to add (delta_days), and returns the future datetime accounting for any potential timezone transitions. 3. Define a function `handle_ambiguous_time(timezone, year, month, day, hour, minute)` that returns two timezone-aware datetime objects representing the ambiguous time with `fold=0` and `fold=1`. Input and Output Formats 1. `get_aware_datetime(timezone, year, month, day, hour, minute)` - **Input:** - `timezone`: A string representing the IANA timezone (e.g., "America/New_York"). - `year`: An integer representing the year. - `month`: An integer representing the month. - `day`: An integer representing the day. - `hour`: An integer representing the hour. - `minute`: An integer representing the minute. - **Output:** A timezone-aware `datetime` object. 2. `calculate_future_time(base_datetime, delta_days)` - **Input:** - `base_datetime`: A timezone-aware `datetime` object. - `delta_days`: An integer representing the number of days to add. - **Output:** A `datetime` object representing the future time. 3. `handle_ambiguous_time(timezone, year, month, day, hour, minute)` - **Input:** - `timezone`: A string representing the IANA timezone. - `year`: An integer representing the year. - `month`: An integer representing the month. - `day`: An integer representing the day. - `hour`: An integer representing the hour. - `minute`: An integer representing the minute. - **Output:** A tuple with two timezone-aware `datetime` objects representing the ambiguous time with `fold=0` and `fold=1`. Constraints - If the provided timezone is not found, an appropriate exception should be raised. - Valid timezone strings should adhere to IANA time zone database standards (e.g., "America/New_York"). Example Usage ```python from zoneinfo import ZoneInfo from datetime import datetime, timedelta # Example inputs to function get_aware_datetime dt = get_aware_datetime("America/Los_Angeles", 2020, 10, 31, 12, 0) print(dt) # Output: 2020-10-31 12:00:00-07:00 # Example inputs to function calculate_future_time future_dt = calculate_future_time(dt, 1) print(future_dt) # Output: 2020-11-01 12:00:00-08:00 # Example inputs to function handle_ambiguous_time ambiguous_times = handle_ambiguous_time("America/Los_Angeles", 2020, 11, 1, 1, 0) print(ambiguous_times[0]) # Output: 2020-11-01 01:00:00-07:00 print(ambiguous_times[1]) # Output: 2020-11-01 01:00:00-08:00 ``` # Notes: - Ensure your code is well-documented. - Handle edge cases such as invalid timezones gracefully. - Make sure to test your functions thoroughly with various timezones and dates.
answer:from datetime import datetime, timedelta from zoneinfo import ZoneInfo, ZoneInfoNotFoundError def get_aware_datetime(timezone, year, month, day, hour, minute): Returns a timezone-aware datetime object. try: tz = ZoneInfo(timezone) dt = datetime(year, month, day, hour, minute, tzinfo=tz) return dt except ZoneInfoNotFoundError: raise ValueError(f"The timezone '{timezone}' is not found.") def calculate_future_time(base_datetime, delta_days): Returns a future datetime after adding a number of days, accounting for timezone transitions. if not base_datetime.tzinfo: raise ValueError("base_datetime must be timezone-aware.") future_datetime = base_datetime + timedelta(days=delta_days) return future_datetime def handle_ambiguous_time(timezone, year, month, day, hour, minute): Returns two timezone-aware datetime objects representing the ambiguous time (fold=0 and fold=1). try: tz = ZoneInfo(timezone) naive_dt = datetime(year, month, day, hour, minute) ambiguous_0 = naive_dt.replace(tzinfo=tz, fold=0) ambiguous_1 = naive_dt.replace(tzinfo=tz, fold=1) return (ambiguous_0, ambiguous_1) except ZoneInfoNotFoundError: raise ValueError(f"The timezone '{timezone}' is not found.")
question:# Python Coding Assessment Question - `site` Module Customization Objective Your task is to create a utility function that facilitates the management of Python site-specific paths and configuration. This function will allow the user to dynamically manage site-specific paths and check if specific paths or directories are included in the current `sys.path`. Function Signature ```python def manage_site_path(action: str, path: str = None) -> list: ``` Input - **action (str):** The action to perform. It can be one of the following: - `"add"`: Add the specified path to `sys.path` and process its `.pth` files. - `"remove"`: Remove the specified path from `sys.path` if it exists. - `"list"`: List all directories currently in `sys.path`. - `"user_site_enabled"`: Return whether the user site-packages directory is enabled. - **path (str, optional):** The path to be added or removed. This is required for the `"add"` and `"remove"` actions. It should be a valid directory path string. Output - **list:** A list of strings representing directories in the `sys.path` for the `"add"`, `"remove"`, and `"list"` actions. - **bool:** A boolean value indicating whether the user site-packages directory is enabled for the `"user_site_enabled"` action. Constraints and Requirements - Use the `site` module's functionalities like `addsitedir`, `ENABLE_USER_SITE`, and `sys.path` manipulation. - Handle invalid actions gracefully by raising a `ValueError` with a descriptive message. - Ensure that the `.pth` processing adheres to the rules specified in the `site` module documentation. - The solution should work across major operating systems (Windows, macOS, Unix). Example Usage ```python #Example 1: Adding a directory to sys.path current_paths = manage_site_path("add", "/path/to/custom/site-packages") print(current_paths) #Example 2: Removing a directory from sys.path current_paths = manage_site_path("remove", "/path/to/custom/site-packages") print(current_paths) #Example 3: Listing current sys.path directories paths = manage_site_path("list") print(paths) #Example 4: Checking if user site-packages directory is enabled is_enabled = manage_site_path("user_site_enabled") print(is_enabled) ``` Notes - Ensure that you validate the `path` parameter before attempting to add or remove it from `sys.path`. - Use the `site.addsitedir` function to handle the addition of paths, which will also process `.pth` files if they exist in the specified directory. - You can use the `site.ENABLE_USER_SITE` flag to check if the user site-packages directory is enabled. - Only directories should be added to or removed from `sys.path`. - Ensure that the added or removed paths are reflected properly in the output list. Testing your Implementation You can test your function using various scenarios, including: - Adding and then listing paths. - Removing paths. - Checking if user site-packages are enabled or not, depending on your Python environment settings.
answer:import sys import site def manage_site_path(action: str, path: str = None) -> list: if action == "add": if not path: raise ValueError("Path must be provided to add to sys.path") site.addsitedir(path) return sys.path elif action == "remove": if not path: raise ValueError("Path must be provided to remove from sys.path") if path in sys.path: sys.path.remove(path) return sys.path elif action == "list": return sys.path elif action == "user_site_enabled": return site.ENABLE_USER_SITE else: raise ValueError(f"Invalid action specified: {action}")
question:**Seaborn Advanced Visualization Challenge** **Objective:** Demonstrate your understanding of the `seaborn.objects` module to create advanced visualizations. You will create a series of plots showcasing different relationships in the dataset using various functionalities like pairing, faceting, and customizing labels. **Dataset:** We will use the built-in `mpg` dataset from the Seaborn library. You can load this dataset using: ```python from seaborn import load_dataset mpg = load_dataset("mpg") ``` **Task:** 1. **Plot Pairing:** Create a plot that shows the relationship between `acceleration` and both `displacement` and `weight` using `pair` on the x-axis. Add points to the plot using `Dots`. 2. **Multiple Pairwise Relationships:** Create a plot that shows multiple pairwise relationships by passing lists to both `x` and `y` for the variables `displacement`, `weight`, `horsepower`, and `acceleration`. Use `Dots` to add points to the plot. 3. **Combining Pairing with Faceting:** Create a plot pairing `weight` on the x-axis with `horsepower` and `acceleration` on the y-axis, and facet the plot by the `origin` variable. Use `Dots` to add points to the plot. 4. **Custom Labels:** Create a plot that pairs `weight` and `displacement` on the x-axis with `mpg` on the y-axis. Customize the plot by adding labels: "Weight (lb)" for `x0`, "Displacement (cu in)" for `x1`, and "MPG" for `y`. **Constraints:** - Ensure your plots are well-labeled and aesthetically clear. - Each plot should be generated using the `seaborn.objects` module. - Follow the cell execution sequence provided below to ensure clarity and correctness of your plots. **Example Code Execution:** ```python import seaborn.objects as so from seaborn import load_dataset # Load the dataset mpg = load_dataset("mpg") # Plot 1: Pairing acceleration with displacement and weight ( so.Plot(mpg, y="acceleration") .pair(x=["displacement", "weight"]) .add(so.Dots()) ) # Plot 2: Multiple pairwise relationships ( so.Plot(mpg) .pair(x=["displacement", "weight"], y=["horsepower", "acceleration"]) .add(so.Dots()) ) # Plot 3: Pairing with Faceting ( so.Plot(mpg, x="weight") .pair(y=["horsepower", "acceleration"]) .facet(col="origin") .add(so.Dots()) ) # Plot 4: Custom Labels ( so.Plot(mpg, y="mpg") .pair(x=["weight", "displacement"]) .label(x0="Weight (lb)", x1="Displacement (cu in)", y="MPG") .add(so.Dots()) ) ``` **Submission:** Submit your Jupyter notebook file containing: - The well-commented code implementing the four tasks. - The resultant plots. Ensure your notebook is clear and easy to understand, demonstrating your proficiency with Seaborn for creating advanced visualizations.
answer:import seaborn.objects as so from seaborn import load_dataset def seaborn_visualizations(): # Load the dataset mpg = load_dataset("mpg") # Plot 1: Pairing acceleration with displacement and weight plot1 = ( so.Plot(mpg, y="acceleration") .pair(x=["displacement", "weight"]) .add(so.Dots()) ) # Plot 2: Multiple pairwise relationships plot2 = ( so.Plot(mpg) .pair(x=["displacement", "weight"], y=["horsepower", "acceleration"]) .add(so.Dots()) ) # Plot 3: Pairing with Faceting plot3 = ( so.Plot(mpg, x="weight") .pair(y=["horsepower", "acceleration"]) .facet(col="origin") .add(so.Dots()) ) # Plot 4: Custom Labels plot4 = ( so.Plot(mpg, y="mpg") .pair(x=["weight", "displacement"]) .label(x0="Weight (lb)", x1="Displacement (cu in)", y="MPG") .add(so.Dots()) ) return plot1, plot2, plot3, plot4 # Call the function to generate plots seaborn_visualizations()