-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
63 lines (47 loc) · 1.71 KB
/
utils.py
File metadata and controls
63 lines (47 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# -*- coding: utf-8 -*-
"""
Utility functions for Stackademy.
"""
import json
# ANSI color codes
colors = {
"blue": "\033[94m", # Bright blue
"green": "\033[92m", # Bright green
"reset": "\033[0m", # Reset to default color
}
def color_text(text, color="blue"):
"""
Colors a string as blue or green.
Args:
text (str): The string to color
color (str): Color to apply - either "blue" or "green" (default: "blue")
Returns:
str: The colored string with ANSI escape codes
Raises:
ValueError: If color is not "blue" or "green"
"""
if color not in ["blue", "green"]:
raise ValueError("Color must be either 'blue' or 'green'")
return f"{colors[color]}{text}{colors['reset']}"
def dump_json_colored(data, color="reset", indent=2, sort_keys=False):
"""
Dumps a JSON dictionary with colored text output.
Args:
data: Dictionary or JSON-serializable object to dump
color: Color for the text output ("blue" or "green")
indent: Number of spaces for JSON indentation (default: 2)
sort_keys: Whether to sort dictionary keys (default: True)
Returns:
str: Colored JSON string
Raises:
ValueError: If color is not "blue" or "green"
TypeError: If data is not JSON serializable
"""
if color not in ["blue", "green"]:
raise ValueError("Color must be either 'blue' or 'green'")
try:
json_str = json.dumps(data, indent=indent, sort_keys=sort_keys, ensure_ascii=False)
colored_json = f"{colors[color]}{json_str}{colors['reset']}"
return colored_json
except (TypeError, ValueError) as e:
raise TypeError(f"Data is not JSON serializable: {e}") from e