wipe_book.ipynb

Wipe book from a database.

import shutil

import bookhoarder as bh
%opts magic unavailable (pyparsing cannot be imported)
%compositor magic unavailable (pyparsing cannot be imported)

Let’s say you have bought and read Ursula K. Le Guin’s The Dispossessed (as you should). We now want to wipe the book from our database, both in the books and in the prices tables.

First thing, we create a copy of the database database.db, and we rename it wiped.db.

shutil.copyfile("database.db", "wiped.db")
'wiped.db'
books = bh.read_database(
    table_name="books",
    url="sqlite:///database.db",
    schema=bh.schema(),
)
prices = bh.read_database(
    table_name="prices",
    url="sqlite:///database.db",
)
books, prices
(shape: (2, 21)
 ┌──────────────┬─────────────┬─────────────┬──────┬───┬────────┬───────────┬─────────────┬─────────┐
 │ isbn         ┆ author      ┆ title       ┆ year ┆ … ┆ libuni ┆ mondadori ┆ osiander    ┆ rizzoli │
 │ ---          ┆ ---         ┆ ---         ┆ ---  ┆   ┆ ---    ┆ ---       ┆ ---         ┆ ---     │
 │ str          ┆ str         ┆ str         ┆ str  ┆   ┆ str    ┆ str       ┆ str         ┆ str     │
 ╞══════════════╪═════════════╪═════════════╪══════╪═══╪════════╪═══════════╪═════════════╪═════════╡
 │ 978386647325 ┆ Karl Marx   ┆ Das Kapital ┆ 2009 ┆ … ┆ null   ┆ null      ┆ https://www ┆ null    │
 │ 6            ┆             ┆             ┆      ┆   ┆        ┆           ┆ .osiander.d ┆         │
 │              ┆             ┆             ┆      ┆   ┆        ┆           ┆ e/shop/h…   ┆         │
 │ 978185798882 ┆ Ursula K.   ┆ The Disposs ┆ 1999 ┆ … ┆ null   ┆ null      ┆ https://www ┆ null    │
 │ 6            ┆ Le Guin     ┆ essed       ┆      ┆   ┆        ┆           ┆ .osiander.d ┆         │
 │              ┆             ┆             ┆      ┆   ┆        ┆           ┆ e/shop/h…   ┆         │
 └──────────────┴─────────────┴─────────────┴──────┴───┴────────┴───────────┴─────────────┴─────────┘,
 shape: (60, 4)
 ┌───────────────┬─────────────┬───────┬────────────┐
 │ isbn          ┆ site        ┆ price ┆ date       │
 │ ---           ┆ ---         ┆ ---   ┆ ---        │
 │ str           ┆ str         ┆ f64   ┆ str        │
 ╞═══════════════╪═════════════╪═══════╪════════════╡
 │ 9783866473256 ┆ buecher     ┆ 7.95  ┆ 2024-10-10 │
 │ 9783866473256 ┆ osiander    ┆ 4.99  ┆ 2024-10-10 │
 │ 9781857988826 ┆ buecher     ┆ 13.0  ┆ 2024-10-10 │
 │ 9781857988826 ┆ feltrinelli ┆ 12.49 ┆ 2024-10-10 │
 │ 9781857988826 ┆ ibs         ┆ 11.87 ┆ 2024-10-10 │
 │ …             ┆ …           ┆ …     ┆ …          │
 │ 9783866473256 ┆ osiander    ┆ 7.95  ┆ 2024-10-19 │
 │ 9781857988826 ┆ buecher     ┆ 9.49  ┆ 2024-10-19 │
 │ 9781857988826 ┆ feltrinelli ┆ 12.49 ┆ 2024-10-19 │
 │ 9781857988826 ┆ ibs         ┆ 11.87 ┆ 2024-10-19 │
 │ 9781857988826 ┆ osiander    ┆ 9.69  ┆ 2024-10-19 │
 └───────────────┴─────────────┴───────┴────────────┘)

What was the ISBN of The Dispossessed? We can just retrieve it with (this match is case-insensitive):

to_delete = bh.find_book("dispossessed", url="sqlite:///wiped.db")
to_delete
shape: (1, 21)
isbnauthortitleyearpublisherlanguageformatfull_pricemin_priceadelphibuecherfeltrinellihoeplihugendubelibslibcooplibracciolibunimondadoriosianderrizzoli
strstrstrstrstrstrstrf64f64strstrstrstrstrstrstrstrstrstrstrstr
"9781857988826""Ursula K. Le Guin""The Dispossessed""1999""Orion Publishing Co"nullnull13.09.49null"https://www.buecher.de/artikel…"https://www.lafeltrinelli.it/d…nullnull"https://www.ibs.it/dispossesse…nullnullnullnull"https://www.osiander.de/shop/h…null

Its isbn is, in particular:

to_delete["isbn"][0]
'9781857988826'

Then we can just use the function wipe_book to wipe a book from a given database:

bh.wipe_book(to_delete["isbn"][0], url="sqlite:///wiped.db")

Checking the wiped.db database, we can finally see that all records of The Dispossessed disappeared:

books = bh.read_database(
    table_name="books",
    url="sqlite:///wiped.db",
    schema=bh.schema(),
)
prices = bh.read_database(
    table_name="prices",
    url="sqlite:///wiped.db",
)
books, prices
(shape: (1, 21)
 ┌───────────────┬───────────┬─────────────┬──────┬───┬────────┬───────────┬──────────────┬─────────┐
 │ isbn          ┆ author    ┆ title       ┆ year ┆ … ┆ libuni ┆ mondadori ┆ osiander     ┆ rizzoli │
 │ ---           ┆ ---       ┆ ---         ┆ ---  ┆   ┆ ---    ┆ ---       ┆ ---          ┆ ---     │
 │ str           ┆ str       ┆ str         ┆ str  ┆   ┆ str    ┆ str       ┆ str          ┆ str     │
 ╞═══════════════╪═══════════╪═════════════╪══════╪═══╪════════╪═══════════╪══════════════╪═════════╡
 │ 9783866473256 ┆ Karl Marx ┆ Das Kapital ┆ 2009 ┆ … ┆ null   ┆ null      ┆ https://www. ┆ null    │
 │               ┆           ┆             ┆      ┆   ┆        ┆           ┆ osiander.de/ ┆         │
 │               ┆           ┆             ┆      ┆   ┆        ┆           ┆ shop/h…      ┆         │
 └───────────────┴───────────┴─────────────┴──────┴───┴────────┴───────────┴──────────────┴─────────┘,
 shape: (20, 4)
 ┌───────────────┬──────────┬───────┬────────────┐
 │ isbn          ┆ site     ┆ price ┆ date       │
 │ ---           ┆ ---      ┆ ---   ┆ ---        │
 │ str           ┆ str      ┆ f64   ┆ str        │
 ╞═══════════════╪══════════╪═══════╪════════════╡
 │ 9783866473256 ┆ buecher  ┆ 7.95  ┆ 2024-10-10 │
 │ 9783866473256 ┆ osiander ┆ 4.99  ┆ 2024-10-10 │
 │ 9783866473256 ┆ buecher  ┆ 7.95  ┆ 2024-10-11 │
 │ 9783866473256 ┆ osiander ┆ 4.99  ┆ 2024-10-11 │
 │ 9783866473256 ┆ buecher  ┆ 7.95  ┆ 2024-10-12 │
 │ …             ┆ …        ┆ …     ┆ …          │
 │ 9783866473256 ┆ osiander ┆ 7.95  ┆ 2024-10-17 │
 │ 9783866473256 ┆ buecher  ┆ 7.95  ┆ 2024-10-18 │
 │ 9783866473256 ┆ osiander ┆ 7.95  ┆ 2024-10-18 │
 │ 9783866473256 ┆ buecher  ┆ 7.95  ┆ 2024-10-19 │
 │ 9783866473256 ┆ osiander ┆ 7.95  ┆ 2024-10-19 │
 └───────────────┴──────────┴───────┴────────────┘)