|
26 | 26 | import concurrent.futures |
27 | 27 | import contextlib |
28 | 28 | import itertools |
| 29 | +import tarfile |
29 | 30 | import tempfile |
30 | 31 | import traceback |
31 | 32 | from functools import partial |
|
200 | 201 | # %% [markdown] |
201 | 202 | # ## Advanced topics |
202 | 203 |
|
| 204 | +# %% [markdown] |
| 205 | +# ### Sharing the database |
| 206 | +# |
| 207 | +# If you need to share a database, |
| 208 | +# you can zip it and pass it to someone else. |
| 209 | + |
| 210 | +# %% [markdown] |
| 211 | +# We start by putting some data in a database. |
| 212 | + |
| 213 | +# %% |
| 214 | +top_level_dir = Path(tempfile.mkdtemp()) |
| 215 | + |
| 216 | +# %% |
| 217 | +db_start = OpenSCMDB( |
| 218 | + db_dir=top_level_dir / "start", |
| 219 | + backend_data=DATA_BACKENDS.get_instance("csv"), |
| 220 | + backend_index=INDEX_BACKENDS.get_instance("csv"), |
| 221 | +) |
| 222 | +db_start.save(df_timeseries_like) |
| 223 | + |
| 224 | +# %% [markdown] |
| 225 | +# Then we create a gzipped tar archive of our database. |
| 226 | + |
| 227 | +# %% |
| 228 | +gzipped = top_level_dir / "db_archive.tar.gz" |
| 229 | +db_start.to_gzipped_tar_archive(gzipped) |
| 230 | + |
| 231 | +# %% [markdown] |
| 232 | +# To demonstrate that this does not rely on the original data, |
| 233 | +# we delete the original database. |
| 234 | + |
| 235 | +# %% |
| 236 | +db_start.delete() |
| 237 | + |
| 238 | +# %% [markdown] |
| 239 | +# We can inspect the tar file's contents. |
| 240 | + |
| 241 | +# %% |
| 242 | +with tarfile.open(gzipped) as tar: |
| 243 | + print(f"{tar.getmembers()=}") |
| 244 | + |
| 245 | +# %% [markdown] |
| 246 | +# A new database can be initialised from the gzipped tar archive. |
| 247 | + |
| 248 | +# %% |
| 249 | +db_moved = OpenSCMDB.from_gzipped_tar_archive( |
| 250 | + gzipped, |
| 251 | + db_dir=top_level_dir / "moved", |
| 252 | +) |
| 253 | +db_moved |
| 254 | + |
| 255 | +# %% [markdown] |
| 256 | +# As above, we remove the archive |
| 257 | +# to demonstrate that there is no reliance on it |
| 258 | +# for the following operations. |
| 259 | + |
| 260 | +# %% |
| 261 | +gzipped.unlink() |
| 262 | + |
| 263 | +# %% [markdown] |
| 264 | +# You can then use this database like normal, |
| 265 | +# but now from the new location |
| 266 | +# (whether on your machine or someone else's). |
| 267 | + |
| 268 | +# %% |
| 269 | +db_moved.load() |
| 270 | + |
| 271 | +# %% |
| 272 | +db_moved.load(pix.isin(unit="J")) |
| 273 | + |
| 274 | +# %% [markdown] |
| 275 | +# We clean up the files before moving onto the next demonstration. |
| 276 | + |
| 277 | +# %% |
| 278 | +db_moved.delete() |
| 279 | + |
203 | 280 | # %% [markdown] |
204 | 281 | # ### Grouping data |
205 | 282 | # |
|
0 commit comments