Skip to content

Commit

Permalink
readme
Browse files Browse the repository at this point in the history
  • Loading branch information
jph00 committed Sep 6, 2024
1 parent b6708b3 commit 2bef060
Showing 1 changed file with 58 additions and 8 deletions.
66 changes: 58 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -386,17 +386,19 @@ possible, they support Python dictionaries, dataclasses, and classes.

### .insert()

Creates a record.
Creates a record. In the name of flexibility, we test that dictionaries,
dataclasses, and classes all work. Returns an instance of the updated
record.

Insert a dictionary.
Insert using a dictionary.

``` python
cats.insert({'name': 'Rex', 'weight': 12.2})
```

Cat(id=1, name='Rex', weight=12.2, uid=UNSET)

Insert a dataclass.
Insert using a dataclass.

``` python
CatDC = cats.dataclass()
Expand All @@ -405,24 +407,72 @@ cats.insert(CatDC(name='Tom', weight=10.2))

Cat(id=2, name='Tom', weight=10.2)

Insert a standard Python class
Insert using a standard Python class

``` python
cat = cats.insert(Cat(name='Jerry', weight=5.2))
```

### .update()

Updates a record.
Updates a record using a Python dict, dataclasses, and classes all work
and returns an instance of the updated record.

Update a dictionary:
Updating from a Python dict:

``` python
cats.update(dict(id=cat.id, name='Jerry', weight=6.2))
```

Cat(id=3, name='Jerry', weight=6.2)

Updating from a dataclass:

``` python
cats.update(CatDC(id=cat.id, name='Jerry', weight=6.3))
```

Cat(id=3, name='Jerry', weight=6.3)

Updating using a class:

``` python
cats.update(Cat(id=cat.id, name='Jerry', weight=5.7))
```

Cat(id=3, name='Jerry', weight=5.7)

### .delete()

Removing data is done by providing the primary key value of the record.

``` python
# Farewell Jerry!
cats.delete(cat.id)
```

<Table cat (id, name, weight)>

### Importing CSV/TSV/etc

You can pass a file name, string, bytes, or open file handle to
`import_file` to import a CSV:

``` python
db = Database(":memory:")
csv_data = """id,name,age
1,Alice,30
2,Bob,25
3,Charlie,35"""

table = db.import_file("people", csv_data)
table()
```

[{'id': 1, 'name': 'Alice', 'age': 30},
{'id': 2, 'name': 'Bob', 'age': 25},
{'id': 3, 'name': 'Charlie', 'age': 35}]

## Diagrams

If you have [graphviz](https://pypi.org/project/graphviz/) installed,
Expand All @@ -432,7 +482,7 @@ you can create database diagrams:
diagram(db.tables)
```

![](index_files/figure-commonmark/cell-41-output-1.svg)
![](index_files/figure-commonmark/cell-45-output-1.svg)

Pass a subset of tables to just diagram those. You can also adjust the
size and aspect ratio.
Expand All @@ -441,4 +491,4 @@ size and aspect ratio.
diagram(db.t['Artist','Album','Track','Genre','MediaType'], size=8, ratio=0.4)
```

![](index_files/figure-commonmark/cell-42-output-1.svg)
![](index_files/figure-commonmark/cell-46-output-1.svg)

0 comments on commit 2bef060

Please sign in to comment.