|
6 | 6 | import sys |
7 | 7 |
|
8 | 8 |
|
9 | | -def convert_to_json(data: dict, filename: str, position: str = None): |
| 9 | +def convert_to_json(data: dict, filename: str, position: str = None) -> None: |
10 | 10 | """ |
11 | | - Convert data to JSON format and save it to a file. |
| 11 | + Converts a dictionary to a JSON file and saves it at a specified location. |
12 | 12 |
|
13 | 13 | Args: |
14 | | - data (dict): Data to save. |
15 | | - filename (str): Name of the file to save without .json extension. |
16 | | - position (str): Directory where the file should be saved. If None, |
17 | | - the directory of the caller script will be used. |
| 14 | + data (dict): The data to be converted into JSON format. |
| 15 | + filename (str): The name of the output JSON file, without the '.json' extension. |
| 16 | + position (str, optional): The file path where the JSON file should be saved. Defaults to the directory of the caller script if not provided. |
18 | 17 |
|
| 18 | + Returns: |
| 19 | + None: The function does not return anything. |
| 20 | + |
19 | 21 | Raises: |
20 | | - ValueError: If filename contains '.json'. |
21 | | - FileNotFoundError: If the specified directory does not exist. |
22 | | - PermissionError: If the program does not have permission to write to the directory. |
| 22 | + ValueError: If 'filename' contains '.json'. |
| 23 | + FileNotFoundError: If the specified directory does not exist. |
| 24 | + PermissionError: If write permissions are lacking for the directory. |
| 25 | +
|
| 26 | + Example: |
| 27 | + >>> convert_to_json({'id': [1, 2], 'value': [10, 20]}, 'output', '/path/to/save') |
| 28 | + Saves a JSON file named 'output.json' at '/path/to/save'. |
| 29 | +
|
| 30 | + Notes: |
| 31 | + This function automatically ensures the directory exists before attempting to write the file. If the directory does not exist, it will attempt to create it. |
23 | 32 | """ |
| 33 | + |
24 | 34 | if ".json" in filename: |
25 | | - filename = filename.replace(".json", "") # Remove .csv extension |
| 35 | + filename = filename.replace(".json", "") # Remove .json extension |
26 | 36 |
|
27 | 37 | # Get the directory of the caller script |
28 | 38 | if position is None: |
|
0 commit comments