Skip to content

Commit 821defb

Browse files
committed
style: apply Black code formatting
1 parent 0919017 commit 821defb

6 files changed

Lines changed: 33 additions & 21 deletions

File tree

src/postal_regex/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,5 @@
4949
"record_validation",
5050
"show_stats",
5151
"reset_stats",
52-
"get_stats"
52+
"get_stats",
5353
]

src/postal_regex/analytics.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
STATS_FILE = Path.home() / ".postalregex_stats.json"
55

6+
67
def _load_stats():
78
"""Load statistics from the local JSON file."""
89
if not STATS_FILE.exists():
@@ -13,11 +14,13 @@ def _load_stats():
1314
except json.JSONDecodeError:
1415
return {}
1516

17+
1618
def _save_stats(stats):
1719
"""Save statistics to the local JSON file."""
1820
with open(STATS_FILE, "w") as f:
1921
json.dump(stats, f, indent=2)
2022

23+
2124
def record_validation(country_code, is_valid):
2225
"""
2326
Record a validation attempt for a given country.
@@ -27,7 +30,7 @@ def record_validation(country_code, is_valid):
2730
is_valid (bool): True if the validation was successful, False otherwise.
2831
"""
2932
stats = _load_stats()
30-
33+
3134
# Ensure the country entry exists
3235
if country_code not in stats:
3336
stats[country_code] = {"valid": 0, "invalid": 0}
@@ -39,6 +42,7 @@ def record_validation(country_code, is_valid):
3942
stats[country_code]["invalid"] += 1
4043
_save_stats(stats)
4144

45+
4246
def reset_stats():
4347
"""Clear all recorded statistics."""
4448
if STATS_FILE.exists():
@@ -47,13 +51,15 @@ def reset_stats():
4751
else:
4852
print("No statistics file found to reset.")
4953

54+
5055
def get_stats() -> dict:
5156
"""
5257
Loads and returns the validation statistics from the stats file.
5358
This function only retrieves data and does not print anything.
54-
59+
5560
Returns:
56-
dict: A dictionary containing the validation stats, or an empty dict if none exist.
61+
dict: A dictionary containing the validation stats,
62+
or an empty dict if none exist.
5763
"""
5864
if not STATS_FILE.exists():
5965
return {}
@@ -63,12 +69,13 @@ def get_stats() -> dict:
6369
except (json.JSONDecodeError, FileNotFoundError):
6470
return {}
6571

72+
6673
def show_stats():
6774
"""
6875
Loads and prints a formatted dashboard of the validation statistics.
6976
This function handles all presentation logic.
7077
"""
71-
stats = get_stats() # This is the main change: call the new data function
78+
stats = get_stats() # This is the main change: call the new data function
7279
if not stats:
7380
print("No validation statistics recorded yet.")
7481
return

src/postal_regex/cli.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import argparse
22
from . import analytics
33

4+
45
def main():
56
"""Main function for the command-line interface."""
6-
parser = argparse.ArgumentParser(
7-
description="Postal Regex command-line tools."
8-
)
7+
parser = argparse.ArgumentParser(description="Postal Regex command-line tools.")
98
subparsers = parser.add_subparsers(dest="command", help="Available commands")
109

1110
# The 'stats' command
@@ -26,5 +25,6 @@ def main():
2625
else:
2726
parser.print_help()
2827

28+
2929
if __name__ == "__main__":
3030
main()

src/postal_regex/core.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ def validate(country_identifier: str, postal_code: str, timeout: float = 0.1) ->
7373
except regex.TimeoutError:
7474
return False
7575

76+
7677
def get_supported_countries():
7778
"""
7879
Retrieve supported countries for postal code validation.

tests/test_analytics.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,49 +9,51 @@
99

1010
from postal_regex import analytics
1111

12+
1213
def test_record_validation_and_load_stats(monkeypatch):
1314
"""
1415
Test that recording validations correctly creates and updates the stats file.
1516
"""
1617
temp_stats_file = Path.home() / ".postalregex_stats_temp.json"
17-
monkeypatch.setattr(analytics, 'STATS_FILE', temp_stats_file)
18+
monkeypatch.setattr(analytics, "STATS_FILE", temp_stats_file)
1819

1920
# Ensure the file doesn't exist initially
2021
if temp_stats_file.exists():
2122
temp_stats_file.unlink()
2223

2324
analytics.record_validation("US", is_valid=True)
24-
25+
2526
with open(temp_stats_file, "r") as f:
2627
stats = json.load(f)
27-
28+
2829
assert stats["US"]["valid"] == 1
2930
assert stats["US"]["invalid"] == 0
3031

3132
analytics.record_validation("US", is_valid=False)
32-
33+
3334
with open(temp_stats_file, "r") as f:
3435
stats = json.load(f)
35-
36+
3637
assert stats["US"]["valid"] == 1
3738
assert stats["US"]["invalid"] == 1
3839

3940
analytics.record_validation("CA", is_valid=True)
40-
41+
4142
with open(temp_stats_file, "r") as f:
4243
stats = json.load(f)
43-
44+
4445
assert stats["CA"]["valid"] == 1
45-
assert "invalid" in stats["CA"] # an 'invalid' key should be created
46+
assert "invalid" in stats["CA"] # an 'invalid' key should be created
4647

4748
temp_stats_file.unlink()
4849

50+
4951
def test_get_stats(monkeypatch):
5052
"""
5153
Test that get_stats correctly reads and returns data.
5254
"""
5355
temp_stats_file = Path.home() / ".postalregex_stats_temp.json"
54-
monkeypatch.setattr(analytics, 'STATS_FILE', temp_stats_file)
56+
monkeypatch.setattr(analytics, "STATS_FILE", temp_stats_file)
5557

5658
# 1. Test when file doesn't exist
5759
if temp_stats_file.exists():
@@ -61,21 +63,22 @@ def test_get_stats(monkeypatch):
6163
dummy_data = {"US": {"valid": 5, "invalid": 1}}
6264
with open(temp_stats_file, "w") as f:
6365
json.dump(dummy_data, f)
64-
66+
6567
assert analytics.get_stats() == dummy_data
66-
68+
6769
temp_stats_file.unlink()
6870

71+
6972
def test_reset_stats(monkeypatch):
7073
"""
7174
Test that reset_stats correctly deletes the statistics file.
7275
"""
7376
temp_stats_file = Path.home() / ".postalregex_stats_temp.json"
74-
monkeypatch.setattr(analytics, 'STATS_FILE', temp_stats_file)
77+
monkeypatch.setattr(analytics, "STATS_FILE", temp_stats_file)
7578

7679
with open(temp_stats_file, "w") as f:
7780
json.dump({"US": {"valid": 1, "invalid": 0}}, f)
78-
81+
7982
assert temp_stats_file.exists()
8083

8184
analytics.reset_stats()

tests/test_bulk.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ def test_validate_spark_dataframe_edge_cases():
137137

138138
spark.stop()
139139

140+
140141
# ----------------- bulk load Tests -----------------
141142
def test_load_json():
142143
data = load_json()

0 commit comments

Comments
 (0)