1616def 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
4444def 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:
6767def 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,
171176def 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