Skip to content

Commit f63c74b

Browse files
dshkolclaude
andcommitted
Format Python code with Black
- Auto-formatted all Python files to conform to Black standards - Fixes CI formatting check failures - No functional changes, only formatting 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 1934ba5 commit f63c74b

13 files changed

Lines changed: 702 additions & 622 deletions

File tree

pycancensus/__init__.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,36 @@
1313
from .regions import list_census_regions, search_census_regions
1414
from .vectors import list_census_vectors, search_census_vectors
1515
from .datasets import list_census_datasets
16-
from .settings import set_api_key, get_api_key, set_cache_path, get_cache_path, remove_api_key, show_api_key
16+
from .settings import (
17+
set_api_key,
18+
get_api_key,
19+
set_cache_path,
20+
get_cache_path,
21+
remove_api_key,
22+
show_api_key,
23+
)
1724
from .geometry import get_census_geometry
1825
from .cache import list_cache, remove_from_cache, clear_cache
1926
from .hierarchy import parent_census_vectors, child_census_vectors, find_census_vectors
2027

2128
__all__ = [
2229
"get_census",
23-
"list_census_regions",
30+
"list_census_regions",
2431
"search_census_regions",
2532
"list_census_vectors",
26-
"search_census_vectors",
33+
"search_census_vectors",
2734
"list_census_datasets",
2835
"set_api_key",
2936
"get_api_key",
3037
"remove_api_key",
3138
"show_api_key",
32-
"set_cache_path",
39+
"set_cache_path",
3340
"get_cache_path",
3441
"get_census_geometry",
3542
"list_cache",
3643
"remove_from_cache",
3744
"clear_cache",
3845
"parent_census_vectors",
39-
"child_census_vectors",
46+
"child_census_vectors",
4047
"find_census_vectors",
41-
]
48+
]

pycancensus/cache.py

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,35 @@
1616
def get_cached_data(cache_key: str) -> Optional[Any]:
1717
"""
1818
Retrieve data from cache if it exists.
19-
19+
2020
Parameters
2121
----------
2222
cache_key : str
2323
Unique identifier for the cached data.
24-
24+
2525
Returns
2626
-------
2727
Any or None
2828
Cached data if found, None otherwise.
2929
"""
3030
cache_path = Path(get_cache_path())
3131
cache_file = cache_path / f"{cache_key}.pkl"
32-
32+
3333
if cache_file.exists():
3434
try:
3535
with open(cache_file, "rb") as f:
3636
return pickle.load(f)
3737
except Exception:
3838
# If cache file is corrupted, remove it
3939
cache_file.unlink(missing_ok=True)
40-
40+
4141
return None
4242

4343

4444
def cache_data(cache_key: str, data: Any) -> None:
4545
"""
4646
Cache data to disk.
47-
47+
4848
Parameters
4949
----------
5050
cache_key : str
@@ -54,9 +54,9 @@ def cache_data(cache_key: str, data: Any) -> None:
5454
"""
5555
cache_path = Path(get_cache_path())
5656
cache_path.mkdir(parents=True, exist_ok=True)
57-
57+
5858
cache_file = cache_path / f"{cache_key}.pkl"
59-
59+
6060
try:
6161
with open(cache_file, "wb") as f:
6262
pickle.dump(data, f)
@@ -67,7 +67,7 @@ def cache_data(cache_key: str, data: Any) -> None:
6767
def list_cache() -> pd.DataFrame:
6868
"""
6969
List all cached data files.
70-
70+
7171
Returns
7272
-------
7373
pd.DataFrame
@@ -77,66 +77,71 @@ def list_cache() -> pd.DataFrame:
7777
- size_mb: File size in MB
7878
- created: Creation timestamp
7979
- modified: Last modification timestamp
80-
80+
8181
Examples
8282
--------
8383
>>> import pycancensus as pc
8484
>>> cache_list = pc.list_cache()
8585
>>> print(cache_list)
8686
"""
8787
cache_path = Path(get_cache_path())
88-
88+
8989
if not cache_path.exists():
90-
return pd.DataFrame(columns=["cache_key", "file_path", "size_mb", "created", "modified"])
91-
90+
return pd.DataFrame(
91+
columns=["cache_key", "file_path", "size_mb", "created", "modified"]
92+
)
93+
9294
cache_files = []
93-
95+
9496
for cache_file in cache_path.glob("*.pkl"):
9597
try:
9698
stat = cache_file.stat()
97-
cache_files.append({
98-
"cache_key": cache_file.stem,
99-
"file_path": str(cache_file),
100-
"size_mb": round(stat.st_size / (1024 * 1024), 2),
101-
"created": pd.Timestamp.fromtimestamp(stat.st_ctime),
102-
"modified": pd.Timestamp.fromtimestamp(stat.st_mtime),
103-
})
99+
cache_files.append(
100+
{
101+
"cache_key": cache_file.stem,
102+
"file_path": str(cache_file),
103+
"size_mb": round(stat.st_size / (1024 * 1024), 2),
104+
"created": pd.Timestamp.fromtimestamp(stat.st_ctime),
105+
"modified": pd.Timestamp.fromtimestamp(stat.st_mtime),
106+
}
107+
)
104108
except Exception:
105109
continue
106-
110+
107111
return pd.DataFrame(cache_files)
108112

109113

110-
def remove_from_cache(cache_keys: Optional[List[str]] = None,
111-
all_cache: bool = False) -> None:
114+
def remove_from_cache(
115+
cache_keys: Optional[List[str]] = None, all_cache: bool = False
116+
) -> None:
112117
"""
113118
Remove items from cache.
114-
119+
115120
Parameters
116121
----------
117122
cache_keys : list of str, optional
118123
Specific cache keys to remove. If None and all_cache=False,
119124
does nothing.
120125
all_cache : bool, default False
121126
If True, removes all cached data.
122-
127+
123128
Examples
124129
--------
125130
>>> import pycancensus as pc
126131
>>> # Remove specific cache entries
127132
>>> pc.remove_from_cache(["regions_CA16", "vectors_CA16"])
128-
>>>
133+
>>>
129134
>>> # Remove all cache (use with caution!)
130135
>>> pc.remove_from_cache(all_cache=True)
131136
"""
132137
cache_path = Path(get_cache_path())
133-
138+
134139
if not cache_path.exists():
135140
print("No cache directory found.")
136141
return
137-
142+
138143
removed_count = 0
139-
144+
140145
if all_cache:
141146
# Remove all .pkl files
142147
for cache_file in cache_path.glob("*.pkl"):
@@ -145,9 +150,9 @@ def remove_from_cache(cache_keys: Optional[List[str]] = None,
145150
removed_count += 1
146151
except Exception as e:
147152
print(f"Warning: Failed to remove {cache_file}: {e}")
148-
153+
149154
print(f"Removed {removed_count} cached files.")
150-
155+
151156
elif cache_keys:
152157
# Remove specific cache keys
153158
for cache_key in cache_keys:
@@ -161,7 +166,7 @@ def remove_from_cache(cache_keys: Optional[List[str]] = None,
161166
print(f"Warning: Failed to remove {cache_key}: {e}")
162167
else:
163168
print(f"Cache key not found: {cache_key}")
164-
169+
165170
if removed_count > 0:
166171
print(f"Removed {removed_count} cached files.")
167172
else:
@@ -171,12 +176,12 @@ def remove_from_cache(cache_keys: Optional[List[str]] = None,
171176
def clear_cache() -> None:
172177
"""
173178
Clear all cached data.
174-
179+
175180
This is an alias for remove_from_cache(all_cache=True).
176-
181+
177182
Examples
178183
--------
179184
>>> import pycancensus as pc
180185
>>> pc.clear_cache()
181186
"""
182-
remove_from_cache(all_cache=True)
187+
remove_from_cache(all_cache=True)

0 commit comments

Comments
 (0)