Skip to content

Commit 6779dc2

Browse files
committed
Add formatting in number response
1 parent d8f9b1e commit 6779dc2

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed

‎pandasai/core/response/base.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,6 @@ def to_dict(self) -> dict:
4848
def to_json(self) -> str:
4949
"""Return a JSON representation."""
5050
return json.dumps(self.to_dict(), cls=CustomJsonEncoder)
51+
52+
def __format__(self, fmt):
53+
return self.value.__format__(fmt)

‎tests/unit_tests/response/test_error_response.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,17 @@ def test_error_response_with_non_string_value():
3636
assert response.value == 123
3737
assert response.last_code_executed == "test_code"
3838
assert response.error == "error message"
39+
40+
41+
def test_error_response_format_alignment():
42+
"""Test __format__ with string formatting on error message"""
43+
response = ErrorResponse("Error!", "test_code", "error message")
44+
assert f"{response:>10}" == " Error!"
45+
assert f"{response:<10}" == "Error! "
46+
47+
48+
def test_error_response_format_with_fstring():
49+
"""Test __format__ in f-string context"""
50+
response = ErrorResponse("Failed", "test_code", "error message")
51+
result = f"Status: {response:>10}"
52+
assert result == "Status: Failed"

‎tests/unit_tests/response/test_number_response.py‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,55 @@ def test_number_response_with_string_number():
2626
response = NumberResponse("123", "test_code")
2727
assert response.type == "number"
2828
assert response.value == "123" # Value remains as string
29+
30+
31+
def test_number_response_format_decimal():
32+
"""Test __format__ with decimal places"""
33+
response = NumberResponse(3.14159, "test_code")
34+
assert f"{response:.2f}" == "3.14"
35+
assert f"{response:.4f}" == "3.1416"
36+
37+
38+
def test_number_response_format_with_fstring():
39+
"""Test __format__ in f-string context"""
40+
response = NumberResponse(123.456, "test_code")
41+
result = f"Value: {response:.2f}"
42+
assert result == "Value: 123.46"
43+
44+
45+
def test_number_response_format_function():
46+
"""Test __format__ with format() function"""
47+
response = NumberResponse(42.123, "test_code")
48+
assert format(response, ".1f") == "42.1"
49+
50+
51+
def test_number_response_format_scientific():
52+
"""Test __format__ with scientific notation"""
53+
response = NumberResponse(1234.5, "test_code")
54+
assert f"{response:e}" == "1.234500e+03"
55+
56+
57+
def test_number_response_format_percentage():
58+
"""Test __format__ with percentage"""
59+
response = NumberResponse(0.875, "test_code")
60+
assert f"{response:.1%}" == "87.5%"
61+
62+
63+
def test_number_response_format_padding():
64+
"""Test __format__ with padding"""
65+
response = NumberResponse(42, "test_code")
66+
assert f"{response:05d}" == "00042"
67+
assert f"{response:>10}" == " 42"
68+
69+
70+
def test_number_response_format_integer():
71+
"""Test __format__ with integer formatting"""
72+
response = NumberResponse(42, "test_code")
73+
assert f"{response:d}" == "42"
74+
75+
76+
def test_number_response_format_with_str_format():
77+
"""Test __format__ with string .format() method"""
78+
response = NumberResponse(99.9, "test_code")
79+
result = "Price: ${:.2f}".format(response)
80+
assert result == "Price: $99.90"

‎tests/unit_tests/response/test_string_response.py‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,37 @@ def test_string_response_with_non_string_value():
2020
assert response.type == "string"
2121
assert response.value == 123
2222
assert response.last_code_executed == "test_code"
23+
24+
25+
def test_string_response_format_alignment():
26+
"""Test __format__ with string alignment"""
27+
response = StringResponse("hello", "test_code")
28+
assert f"{response:>10}" == " hello" # Right align
29+
assert f"{response:<10}" == "hello " # Left align
30+
assert f"{response:^10}" == " hello " # Center align
31+
32+
33+
def test_string_response_format_with_fstring():
34+
"""Test __format__ in f-string context"""
35+
response = StringResponse("world", "test_code")
36+
result = f"Hello {response:>10}!"
37+
assert result == "Hello world!"
38+
39+
40+
def test_string_response_format_function():
41+
"""Test __format__ with format() function"""
42+
response = StringResponse("test", "test_code")
43+
assert format(response, ">8") == " test"
44+
45+
46+
def test_string_response_format_truncate():
47+
"""Test __format__ with truncation"""
48+
response = StringResponse("hello world", "test_code")
49+
assert f"{response:.5}" == "hello"
50+
51+
52+
def test_string_response_format_with_str_format():
53+
"""Test __format__ with string .format() method"""
54+
response = StringResponse("Python", "test_code")
55+
result = "Language: {:>10}".format(response)
56+
assert result == "Language: Python"

0 commit comments

Comments
 (0)