Skip to content

Commit 52aeb7c

Browse files
committed
Add explicit docs
1 parent b650b13 commit 52aeb7c

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

docs/how-to-guides/how-to-use-openscmdb.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import concurrent.futures
2727
import contextlib
2828
import itertools
29+
import tarfile
2930
import tempfile
3031
import traceback
3132
from functools import partial
@@ -200,6 +201,82 @@
200201
# %% [markdown]
201202
# ## Advanced topics
202203

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+
203280
# %% [markdown]
204281
# ### Grouping data
205282
#

0 commit comments

Comments
 (0)