Skip to content

Modifying

Adding rows, changing values, and changing the columns.

Changing values

__setitem__

__setitem__(slice_spec, value)

Assign by key and column name, as in sdaf[key, 'v'] = 1.

These forms are supported:

  • sdaf[key, col] = value sets one cell. A list of one value gives that value, as in a Daf.
  • sdaf[key] = row sets the whole row with that key, from a dict or a list. Columns the row lacks become Python NULL, so a dict without the keyfield empties the key, which raises ValueError.
  • sdaf[:, col] = values sets a column. A list goes in row order. A short list leaves the rows past its end as they were, and a long one is cut. A single value sets every row.

A row is found by its key, never by its position, so these work after a delete. The key must be a str. An int in brackets is a position in a Daf, so an int row raises NotImplementedError, even when the keyfield holds ints. To change a row with an int key, use update_record_krow(). On a selection, the parent's rows change, as a Daf selection shares its rows.

Parameters:

Name Type Description Default
slice_spec Any

A key, or a tuple of a key or : and a column name.

required
value Any

The value, row or list to assign.

required

Raises:

Type Description
KeyError

The key or the column is not found.

NotImplementedError

Any other form, such as an int row or several columns.

ValueError

The change gives a Python NULL key. Nothing changes.

IntegrityError

The change gives a repeated key. Nothing changes.

Examples:

Create a small SqlDaf table of people:

>>> import tempfile
>>> SqlDaf.db_path = tempfile.mkdtemp() + '/job.db'
>>> people_sdaf = SqlDaf('people', cols=['id', 'name', 'age'], keyfield='id', lol=[
...     ['p1', 'Ana Lopez', 34], ['p2', 'Ben Okafor', 52], ['p3', 'Chen Wu', 27]])
>>> people_sdaf

| id |    name    | age |
| -: | ---------: | --: |
| p1 |  Ana Lopez |  34 |
| p2 | Ben Okafor |  52 |
| p3 |    Chen Wu |  27 |

%% sdaf rows=3; cols=3; keyfield='id'; name='people'

Set one cell, by key and column name:

>>> people_sdaf['p2', 'age'] = 53
>>> people_sdaf

| id |    name    | age |
| -: | ---------: | --: |
| p1 |  Ana Lopez |  34 |
| p2 | Ben Okafor |  53 |
| p3 |    Chen Wu |  27 |

%% sdaf rows=3; cols=3; keyfield='id'; name='people'

Set a whole row, by key. Columns the dict lacks become Python NULL:

>>> people_sdaf['p3'] = {'id': 'p3', 'name': 'Chen Wu'}
>>> people_sdaf

| id |    name    | age |
| -: | ---------: | --: |
| p1 |  Ana Lopez |  34 |
| p2 | Ben Okafor |  53 |
| p3 |    Chen Wu |     |

%% sdaf rows=3; cols=3; keyfield='id'; name='people'

Set a column to one value. Every row gets it:

>>> people_sdaf[:, 'age'] = 40
>>> people_sdaf

| id |    name    | age |
| -: | ---------: | --: |
| p1 |  Ana Lopez |  40 |
| p2 | Ben Okafor |  40 |
| p3 |    Chen Wu |  40 |

%% sdaf rows=3; cols=3; keyfield='id'; name='people'

Set a column from a list. A short list leaves the rows past its end as they were:

>>> people_sdaf[:, 'age'] = [35, 54]
>>> people_sdaf

| id |    name    | age |
| -: | ---------: | --: |
| p1 |  Ana Lopez |  35 |
| p2 | Ben Okafor |  54 |
| p3 |    Chen Wu |  40 |

%% sdaf rows=3; cols=3; keyfield='id'; name='people'

An int row is a position in a Daf, so it raises here:

>>> people_sdaf[0, 'age'] = 36
Traceback (most recent call last):
NotImplementedError: sdaf[(0, 'age')] = ...: an int row is a position, as in a Daf, and a SqlDaf does not assign by position. ...

assign_record

assign_record(record)

Put one row in by its key, replacing the row that has that key, as Daf.assign_record() does.

The whole row is replaced, in its place. Columns the dict lacks become Python NULL. A new key is added at the end. Use update_record_krow() to change only some cells.

Parameters:

Name Type Description Default
record T_da

The row, as a dict. It must have the keyfield.

required

Returns:

Type Description
SqlDaf

This SqlDaf, which has been changed.

Raises:

Type Description
KeyError

The record lacks the keyfield.

ValueError

There is no keyfield, or the key is a Python NULL.

Examples:

Create a small SqlDaf table of people:

>>> import tempfile
>>> SqlDaf.db_path = tempfile.mkdtemp() + '/job.db'
>>> people_sdaf = SqlDaf('people', cols=['id', 'name', 'age'], keyfield='id', lol=[['p1', 'Ana Lopez', 34], ['p2', 'Ben Okafor', 52]])
>>> people_sdaf

| id |    name    | age |
| -: | ---------: | --: |
| p1 |  Ana Lopez |  34 |
| p2 | Ben Okafor |  52 |

%% sdaf rows=2; cols=3; keyfield='id'; name='people'

Replace a whole row by its key. Columns the dict lacks become Python NULL:

>>> people_sdaf.assign_record({'id': 'p1', 'age': 35})

| id |    name    | age |
| -: | ---------: | --: |
| p1 |            |  35 |
| p2 | Ben Okafor |  52 |

%% sdaf rows=2; cols=3; keyfield='id'; name='people'

A new key is added at the end:

>>> people_sdaf.assign_record({'id': 'p3', 'name': 'Chen Wu', 'age': 27})

| id |    name    | age |
| -: | ---------: | --: |
| p1 |            |  35 |
| p2 | Ben Okafor |  52 |
| p3 |    Chen Wu |  27 |

%% sdaf rows=3; cols=3; keyfield='id'; name='people'

update_record_krow

update_record_krow(key, record=None)

Change some cells in the row that has this key, as Daf.update_record_krow() does.

Only the columns named in the dict change. An int is always a key here, never a position, so it suits a keyfield of ints. It is one UPDATE. On a selection, the parent's row changes, and only a row of the selection is found. For many rows that take the same values, use update_by_keylist().

Parameters:

Name Type Description Default
key Any

The key of the row to change.

required
record T_da | None

The new values, by column name. Names that are not columns are ignored.

None

Returns:

Type Description
SqlDaf

This SqlDaf, which has been changed. A record of None changes nothing.

Raises:

Type Description
KeyError

No row has this key.

ValueError

There is no keyfield, or the change gives a Python NULL key. Nothing changes.

IntegrityError

The change gives a repeated key. Nothing changes.

Examples:

Create a small SqlDaf table whose keys are ints, as employee numbers:

>>> import tempfile
>>> SqlDaf.db_path = tempfile.mkdtemp() + '/job.db'
>>> staff_sdaf = SqlDaf('staff', cols=['emp_no', 'name', 'title'], keyfield='emp_no', lol=[
...     [1042, 'Ana Lopez', 'Analyst'], [2071, 'Ben Okafor', 'Engineer']])
>>> staff_sdaf

| emp_no |    name    |  title   |
| -----: | ---------: | -------: |
|   1042 |  Ana Lopez |  Analyst |
|   2071 | Ben Okafor | Engineer |

%% sdaf rows=2; cols=3; keyfield='emp_no'; name='staff'

Change some cells of the row with key 2071. An int here is a key, not a position:

>>> staff_sdaf.update_record_krow(2071, {'title': 'Lead Engineer'})

| emp_no |    name    |     title     |
| -----: | ---------: | ------------: |
|   1042 |  Ana Lopez |       Analyst |
|   2071 | Ben Okafor | Lead Engineer |

%% sdaf rows=2; cols=3; keyfield='emp_no'; name='staff'

update_by_keylist

update_by_keylist(keylist=None, record=None)

Change some cells in the rows that have the given keys, as Daf.update_by_keylist() does.

Only the columns named in the dict change. A key that is not found is skipped. It is one UPDATE for all the keys. For one row, update_record_krow() raises KeyError for a missing key. On a selection, the parent's rows change, as a Daf selection shares its rows.

Parameters:

Name Type Description Default
keylist T_la | None

The keys of the rows to change.

None
record T_da | None

The new values, by column name. Names that are not columns are ignored.

None

Returns:

Type Description
SqlDaf

This SqlDaf, which has been changed. With no keyfield, keys or record, nothing happens.

Raises:

Type Description
ValueError

The change gives a Python NULL key. Nothing changes.

IntegrityError

The change gives a repeated key. Nothing changes.

Examples:

Create a small SqlDaf table of people:

>>> import tempfile
>>> SqlDaf.db_path = tempfile.mkdtemp() + '/job.db'
>>> people_sdaf = SqlDaf('people', cols=['id', 'name', 'city'], keyfield='id', lol=[
...     ['p1', 'Ana Lopez', 'Denver'], ['p2', 'Ben Okafor', 'Austin'], ['p3', 'Chen Wu', 'Denver']])
>>> people_sdaf

| id |    name    |  city  |
| -: | ---------: | -----: |
| p1 |  Ana Lopez | Denver |
| p2 | Ben Okafor | Austin |
| p3 |    Chen Wu | Denver |

%% sdaf rows=3; cols=3; keyfield='id'; name='people'

Change the city of two people. A key that is not found, and a name that is not a column, are ignored:

>>> people_sdaf.update_by_keylist(['p1', 'p3', 'p9'], {'city': 'Boulder', 'email': 'ignored'})

| id |    name    |  city   |
| -: | ---------: | ------: |
| p1 |  Ana Lopez | Boulder |
| p2 | Ben Okafor |  Austin |
| p3 |    Chen Wu | Boulder |

%% sdaf rows=3; cols=3; keyfield='id'; name='people'

assign_col

assign_col(colname, la=None, default=NULL)

Fill a column by name with the values of a list, or add it if it is new, as Daf.assign_col() does.

The values go to the rows in order. A short list is filled out with default, and a long one is cut. With no list, every row gets default. A new column is added at the end, with ALTER TABLE, on a parent only.

Parameters:

Name Type Description Default
colname str

The column name.

required
la T_la | None

The values, in row order.

None
default Any

The value for rows that the list does not reach.

NULL

Returns:

Type Description
SqlDaf

This SqlDaf, which has been changed.

Raises:

Type Description
NotImplementedError

The column is new, and this is a selection.

ValueError

The change gives a Python NULL key. Nothing changes.

IntegrityError

The change gives a repeated key. Nothing changes.

Examples:

Create a small SqlDaf table of people:

>>> import tempfile
>>> SqlDaf.db_path = tempfile.mkdtemp() + '/job.db'
>>> people_sdaf = SqlDaf('people', cols=['id', 'age'], keyfield='id', lol=[['p1', 34], ['p2', 52], ['p3', 27]])
>>> people_sdaf

| id | age |
| -: | --: |
| p1 |  34 |
| p2 |  52 |
| p3 |  27 |

%% sdaf rows=3; cols=2; keyfield='id'; name='people'

Fill a column from a list, in row order:

>>> people_sdaf.assign_col('age', [35, 53, 28])

| id | age |
| -: | --: |
| p1 |  35 |
| p2 |  53 |
| p3 |  28 |

%% sdaf rows=3; cols=2; keyfield='id'; name='people'

A short list is filled out with default:

>>> people_sdaf.assign_col('age', [40], default=0)

| id | age |
| -: | --: |
| p1 |  40 |
| p2 |   0 |
| p3 |   0 |

%% sdaf rows=3; cols=2; keyfield='id'; name='people'

A new column is added at the end, here with the default in every row:

>>> people_sdaf.assign_col('city', default='unknown')

| id | age |  city   |
| -: | --: | ------: |
| p1 |  40 | unknown |
| p2 |   0 | unknown |
| p3 |   0 | unknown |

%% sdaf rows=3; cols=3; keyfield='id'; name='people'

apply_to_col

apply_to_col(col, func, **kwargs)

Replace each value of a column by the result of a function, in place, as Daf.apply_to_col() does.

It is one UPDATE, with the function registered in SQLite. The function gets Python NULL for SQL NULL. An error it raises is raised as itself, and nothing changes.

Parameters:

Name Type Description Default
col str

The column.

required
func Callable

Called as func(value, **kwargs). It returns the new value.

required
**kwargs Any

Passed on to func.

{}

Returns:

Type Description
SqlDaf

This SqlDaf, which has been changed.

Raises:

Type Description
ColumnNotFoundError

The name is not a column.

ValueError

The change gives a Python NULL key. Nothing changes.

IntegrityError

The change gives a repeated key. Nothing changes.

Examples:

Create a small SqlDaf table of people, with heights in centimeters. One is not known:

>>> import tempfile
>>> SqlDaf.db_path = tempfile.mkdtemp() + '/job.db'
>>> people_sdaf = SqlDaf('people', cols=['id', 'height'], keyfield='id', lol=[['p1', 168], ['p2', ''], ['p3', 175]])
>>> people_sdaf

| id | height |
| -: | -----: |
| p1 |    168 |
| p2 |        |
| p3 |    175 |

%% sdaf rows=3; cols=2; keyfield='id'; name='people'

Convert each height to meters. Keyword arguments go to the function:

>>> def to_meters(height, per_meter=100):
...     return height / per_meter if height != '' else ''
>>> people_sdaf.apply_to_col('height', to_meters, per_meter=100)

| id | height |
| -: | -----: |
| p1 |   1.68 |
| p2 |        |
| p3 |   1.75 |

%% sdaf rows=3; cols=2; keyfield='id'; name='people'

If the function raises, nothing changes:

>>> people_sdaf.apply_to_col('height', lambda height: 1 / (height - 1.68))
Traceback (most recent call last):
ZeroDivisionError: float division by zero
>>> people_sdaf

| id | height |
| -: | -----: |
| p1 |   1.68 |
| p2 |        |
| p3 |   1.75 |

%% sdaf rows=3; cols=2; keyfield='id'; name='people'

apply_in_place

apply_in_place(func, by='row', rowkeys=None, **kwargs)

Apply a function to each row and store the results, as Daf.apply_in_place() does.

With by='row', the function gets each row as a dict and returns a dict. Its values are written back by column name. A column the dict lacks keeps its value. With by='row_klist', the function gets a KeyedList, changes it, and returns nothing. The whole row is written back.

Rows are read and written in batches, in one transaction. If the function raises, nothing changes. With rowkeys, only those rows change, and a key that is not found is skipped, as in a Daf. On a selection, the parent's rows change.

Parameters:

Name Type Description Default
func Callable[..., Any]

The function to apply to each row.

required
by str

'row' or 'row_klist'.

'row'
rowkeys T_la | None

The keys of the rows to change. All rows if None.

None
**kwargs Any

Passed on to func.

{}

Returns:

Type Description
SqlDaf

This SqlDaf, which has been changed.

Raises:

Type Description
NotImplementedError

by is not 'row' or 'row_klist'.

ValueError

With by='row' the function returned None, or a key became a Python NULL.

IntegrityError

A key became repeated. Nothing changes.

Examples:

Create a small SqlDaf table of orders, with a price and a quantity, and a total to fill in:

>>> import tempfile
>>> SqlDaf.db_path = tempfile.mkdtemp() + '/job.db'
>>> orders_sdaf = SqlDaf('orders', cols=['id', 'price', 'qty', 'total'], keyfield='id', lol=[
...     ['o1', 12.5, 2, ''], ['o2', 4.0, 6, ''], ['o3', 30.0, 1, '']])
>>> orders_sdaf

| id | price | qty | total |
| -: | ----: | --: | ----: |
| o1 |  12.5 |   2 |       |
| o2 |   4.0 |   6 |       |
| o3 |  30.0 |   1 |       |

%% sdaf rows=3; cols=4; keyfield='id'; name='orders'

With by='row', the function returns the cells to change, by name:

>>> def add_total(row, tax=0.0):
...     return {'total': row['price'] * row['qty'] * (1 + tax)}
>>> orders_sdaf.apply_in_place(add_total)

| id | price | qty | total |
| -: | ----: | --: | ----: |
| o1 |  12.5 |   2 |  25.0 |
| o2 |   4.0 |   6 |  24.0 |
| o3 |  30.0 |   1 |  30.0 |

%% sdaf rows=3; cols=4; keyfield='id'; name='orders'

Change only some rows, with rowkeys. Other keywords go to the function:

>>> orders_sdaf.apply_in_place(add_total, rowkeys=['o2'], tax=0.25)

| id | price | qty | total |
| -: | ----: | --: | ----: |
| o1 |  12.5 |   2 |  25.0 |
| o2 |   4.0 |   6 |  30.0 |
| o3 |  30.0 |   1 |  30.0 |

%% sdaf rows=3; cols=4; keyfield='id'; name='orders'

With by='row_klist', the function changes a KeyedList and returns nothing:

>>> def double_qty(row):
...     row['qty'] = row['qty'] * 2
>>> orders_sdaf.apply_in_place(double_qty, by='row_klist')

| id | price | qty | total |
| -: | ----: | --: | ----: |
| o1 |  12.5 |   4 |  25.0 |
| o2 |   4.0 |  12 |  30.0 |
| o3 |  30.0 |   2 |  30.0 |

%% sdaf rows=3; cols=4; keyfield='id'; name='orders'

Adding rows

append

append(
    data_item=None,
    respect_kd=False,
    *,
    lol=None,
    la=None,
    fast=False,
)

Add one row, or several, to the end, as Daf.append() does.

  • A dict or a KeyedList is one row, placed by column name.
  • A list of values is one row, in column order. A short list is padded with Python NULL.
  • A list of dicts is several rows. See extend().
  • A Daf or a SqlDaf is several rows. See concat().
  • lol= is several rows, each a list. la= is one row.

Each call is one transaction, and each commit writes to disk. On 1,000,000 rows, one call took 9.7 ms on its own, and 55 us inside transaction(). So add many rows in one call, with extend(), or put a loop of single rows inside transaction().

Keys are unique in a SqlDaf. A key that is already there raises sqlite3.IntegrityError, and nothing is added. With respect_kd=True, the row with that key is replaced, in its place. A Python NULL key raises ValueError, as in concat().

On a selection, the selection gets rows of its own first, as concat() does. Its parent is not changed.

Parameters:

Name Type Description Default
data_item Any

A dict, a KeyedList, a list, a list of dicts, or a Daf.

None
respect_kd bool

If True, replace the row that has the same key.

False
lol T_lola | None

Several rows, each a list in column order.

None
la T_la | None

One row, a list in column order.

None
fast bool

Accepted for Daf code. Every row is checked anyway.

False

Returns:

Type Description
SqlDaf

This SqlDaf, which has been changed.

Raises:

Type Description
TypeError

More than one of data_item, lol and la, or a row of an unknown type.

ValueError

A row has more values than there are columns, or a key is a Python NULL.

IntegrityError

A key would be repeated. Nothing is added.

Examples:

Create a small SqlDaf table of people:

>>> import tempfile
>>> SqlDaf.db_path = tempfile.mkdtemp() + '/job.db'
>>> people_sdaf = SqlDaf('people', cols=['id', 'name', 'age'], keyfield='id', lol=[['p1', 'Ana Lopez', 34], ['p2', 'Ben Okafor', 52]])
>>> people_sdaf

| id |    name    | age |
| -: | ---------: | --: |
| p1 |  Ana Lopez |  34 |
| p2 | Ben Okafor |  52 |

%% sdaf rows=2; cols=3; keyfield='id'; name='people'

Add one row as a dict, placed by column name:

>>> people_sdaf.append({'age': 27, 'id': 'p3', 'name': 'Chen Wu'})

| id |    name    | age |
| -: | ---------: | --: |
| p1 |  Ana Lopez |  34 |
| p2 | Ben Okafor |  52 |
| p3 |    Chen Wu |  27 |

%% sdaf rows=3; cols=3; keyfield='id'; name='people'

Add one row as a list, in column order. A short list is padded with Python NULL:

>>> people_sdaf.append(['p4', 'Dana Smith'])

| id |    name    | age |
| -: | ---------: | --: |
| p1 |  Ana Lopez |  34 |
| p2 | Ben Okafor |  52 |
| p3 |    Chen Wu |  27 |
| p4 | Dana Smith |     |

%% sdaf rows=4; cols=3; keyfield='id'; name='people'

Add several rows as lists, or one row with la=:

>>> people_sdaf.append(lol=[['p5', 'Eva Novak', 45], ['p6', 'Femi Ade', 38]])

| id |    name    | age |
| -: | ---------: | --: |
| p1 |  Ana Lopez |  34 |
| p2 | Ben Okafor |  52 |
| p3 |    Chen Wu |  27 |
| p4 | Dana Smith |     |
| p5 |  Eva Novak |  45 |
| p6 |   Femi Ade |  38 |

%% sdaf rows=6; cols=3; keyfield='id'; name='people'

>>> people_sdaf.append(la=['p7', 'Gus Berg', 61])

| id |    name    | age |
| -: | ---------: | --: |
| p1 |  Ana Lopez |  34 |
| p2 | Ben Okafor |  52 |
| p3 |    Chen Wu |  27 |
| p4 | Dana Smith |     |
| p5 |  Eva Novak |  45 |
| p6 |   Femi Ade |  38 |
| p7 |   Gus Berg |  61 |

%% sdaf rows=7; cols=3; keyfield='id'; name='people'

A key that is there raises, and nothing is added:

>>> people_sdaf.append({'id': 'p1', 'name': 'Ana Lopez', 'age': 35})
Traceback (most recent call last):
sqlite3.IntegrityError: UNIQUE constraint failed: people.id

Replace that row instead, in its place, with respect_kd=True:

>>> people_sdaf.append({'id': 'p1', 'name': 'Ana Lopez', 'age': 35}, respect_kd=True)

| id |    name    | age |
| -: | ---------: | --: |
| p1 |  Ana Lopez |  35 |
| p2 | Ben Okafor |  52 |
| p3 |    Chen Wu |  27 |
| p4 | Dana Smith |     |
| p5 |  Eva Novak |  45 |
| p6 |   Femi Ade |  38 |
| p7 |   Gus Berg |  61 |

%% sdaf rows=7; cols=3; keyfield='id'; name='people'

Add the rows of a Daf with the same columns:

>>> people_sdaf.append(Daf(cols=['id', 'name', 'age'], lol=[['p8', 'Hana Sato', 29]]))

| id |    name    | age |
| -: | ---------: | --: |
| p1 |  Ana Lopez |  35 |
| p2 | Ben Okafor |  52 |
| p3 |    Chen Wu |  27 |
| p4 | Dana Smith |     |
| p5 |  Eva Novak |  45 |
| p6 |   Femi Ade |  38 |
| p7 |   Gus Berg |  61 |
| p8 |  Hana Sato |  29 |

%% sdaf rows=8; cols=3; keyfield='id'; name='people'

fast=True is accepted, so code written for a Daf runs. Every row is checked anyway:

>>> people_sdaf.append(['p9', 'Ivan Petrov', 50], fast=True)

| id |    name     | age |
| -: | ----------: | --: |
| p1 |   Ana Lopez |  35 |
| p2 |  Ben Okafor |  52 |
| p3 |     Chen Wu |  27 |
| p4 |  Dana Smith |     |
| p5 |   Eva Novak |  45 |
| p6 |    Femi Ade |  38 |
| p7 |    Gus Berg |  61 |
| p8 |   Hana Sato |  29 |
| p9 | Ivan Petrov |  50 |

%% sdaf rows=9; cols=3; keyfield='id'; name='people'

record_append

record_append(record, respect_kd=True)

Add one row given as a dict or a KeyedList, as Daf.record_append() does.

By default the row with the same key is replaced, in its place. This is the default here, unlike append(). An empty record adds nothing.

Parameters:

Name Type Description Default
record T_da | KeyedList

The row, as a dict or a KeyedList.

required
respect_kd bool

If True, the default, replace the row that has the same key.

True

Returns:

Type Description
SqlDaf

This SqlDaf, which has been changed.

Raises:

Type Description
TypeError

The record is not a dict or a KeyedList.

ValueError

The key is a Python NULL.

IntegrityError

The key is there, and respect_kd is False. Nothing is added.

Examples:

Create a small SqlDaf table of people:

>>> import tempfile
>>> SqlDaf.db_path = tempfile.mkdtemp() + '/job.db'
>>> people_sdaf = SqlDaf('people', cols=['id', 'name', 'age'], keyfield='id', lol=[['p1', 'Ana Lopez', 34], ['p2', 'Ben Okafor', 52]])
>>> people_sdaf

| id |    name    | age |
| -: | ---------: | --: |
| p1 |  Ana Lopez |  34 |
| p2 | Ben Okafor |  52 |

%% sdaf rows=2; cols=3; keyfield='id'; name='people'

Add a row with a new key:

>>> people_sdaf.record_append({'id': 'p3', 'name': 'Chen Wu', 'age': 27})

| id |    name    | age |
| -: | ---------: | --: |
| p1 |  Ana Lopez |  34 |
| p2 | Ben Okafor |  52 |
| p3 |    Chen Wu |  27 |

%% sdaf rows=3; cols=3; keyfield='id'; name='people'

A key that is there replaces its row, by default:

>>> people_sdaf.record_append({'id': 'p1', 'name': 'Ana Lopez', 'age': 35})

| id |    name    | age |
| -: | ---------: | --: |
| p1 |  Ana Lopez |  35 |
| p2 | Ben Okafor |  52 |
| p3 |    Chen Wu |  27 |

%% sdaf rows=3; cols=3; keyfield='id'; name='people'

With respect_kd=False, a key that is there raises instead:

>>> people_sdaf.record_append({'id': 'p1', 'name': 'Ana Lopez', 'age': 36}, respect_kd=False)
Traceback (most recent call last):
sqlite3.IntegrityError: UNIQUE constraint failed: people.id

extend

extend(records_lod=None, respect_kd=False, *, lol=None)

Add several rows, as dicts or as lists, as Daf.extend() does.

Each dict is placed by column name. Each list of lol is in column order. All rows are added in one transaction. If one fails, none is added.

Parameters:

Name Type Description Default
records_lod List[T_da] | None

The rows, as dicts.

None
respect_kd bool

If True, replace the row that has the same key.

False
lol T_lola | None

The rows, as lists in column order.

None

Returns:

Type Description
SqlDaf

This SqlDaf, which has been changed.

Raises:

Type Description
TypeError

Both records_lod and lol are given, or a row of lol is not a list.

ValueError

A row has more values than there are columns, or a key is a Python NULL.

IntegrityError

A key would be repeated. Nothing is added.

Examples:

Create a small SqlDaf table of people:

>>> import tempfile
>>> SqlDaf.db_path = tempfile.mkdtemp() + '/job.db'
>>> people_sdaf = SqlDaf('people', cols=['id', 'age'], keyfield='id', lol=[['p1', 34]])
>>> people_sdaf

| id | age |
| -: | --: |
| p1 |  34 |

%% sdaf rows=1; cols=2; keyfield='id'; name='people'

Add several rows as dicts, each placed by column name:

>>> people_sdaf.extend([{'id': 'p2', 'age': 52}, {'age': 27, 'id': 'p3'}])

| id | age |
| -: | --: |
| p1 |  34 |
| p2 |  52 |
| p3 |  27 |

%% sdaf rows=3; cols=2; keyfield='id'; name='people'

Add rows as lists. With respect_kd=True, a key that is there replaces its row:

>>> people_sdaf.extend(lol=[['p4', 41], ['p1', 35]], respect_kd=True)

| id | age |
| -: | --: |
| p1 |  35 |
| p2 |  52 |
| p3 |  27 |
| p4 |  41 |

%% sdaf rows=4; cols=2; keyfield='id'; name='people'

If one row fails, none is added:

>>> people_sdaf.extend(lol=[['p5', 45], ['p2', 53]])
Traceback (most recent call last):
sqlite3.IntegrityError: UNIQUE constraint failed: people.id
>>> people_sdaf

| id | age |
| -: | --: |
| p1 |  35 |
| p2 |  52 |
| p3 |  27 |
| p4 |  41 |

%% sdaf rows=4; cols=2; keyfield='id'; name='people'

concat

concat(other_instance, respect_kd=False)

Add the rows of another Daf or SqlDaf to the end of this one, as Daf.concat() does.

The other must have the same columns, in the same order, as in a Daf. An other with no rows adds nothing, whatever its columns, as in a Daf. So the empty result of apply(), which has no columns, can be added with no error. A SqlDaf of the same database file is copied in SQL, in its row order, so no row comes into Python. A SqlDaf of another file is first copied into a TEMP table here, which is deleted after. An in-memory Daf is written row by row. All of it is one transaction.

If this is a child, it first becomes a table of its own, by copy(inplace=True), as appending to a selection of a Daf adds only to that selection. The parent is not changed.

A Daf appends a row whose key is already there without a word. A SqlDaf with a keyfield has a unique index on it, so a repeated key raises instead, and nothing is added. A Python NULL key raises ValueError, and nothing is added.

Parameters:

Name Type Description Default
other_instance Daf

The Daf or SqlDaf whose rows are added. It is not changed.

required
respect_kd bool

If True, a row whose key is there replaces that row, in its place.

False

Returns:

Type Description
SqlDaf

This SqlDaf.

Raises:

Type Description
KeyError

The columns differ, and the other has rows.

ValueError

A key added is a Python NULL. Nothing is added.

IntegrityError

A key would be repeated. Nothing is added.

NotImplementedError

The other is a selection or TEMP table of another thread's connection.

Examples:

Create two small SqlDaf tables of people, with one key in both:

>>> import tempfile
>>> SqlDaf.db_path = tempfile.mkdtemp() + '/job.db'
>>> people_sdaf = SqlDaf('people', cols=['id', 'name', 'age'], keyfield='id', lol=[['p1', 'Ana Lopez', 34], ['p2', 'Ben Okafor', 52]])
>>> more_sdaf = SqlDaf('more', cols=['id', 'name', 'age'], keyfield='id', lol=[['p2', 'Ben Okafor', 53], ['p3', 'Chen Wu', 27]])

Adding a key that is already there raises, and nothing is added:

>>> people_sdaf.concat(more_sdaf)
Traceback (most recent call last):
sqlite3.IntegrityError: UNIQUE constraint failed: people.id

With respect_kd=True, a row whose key is there replaces that row, in its place:

>>> people_sdaf.concat(more_sdaf, respect_kd=True)

| id |    name    | age |
| -: | ---------: | --: |
| p1 |  Ana Lopez |  34 |
| p2 | Ben Okafor |  53 |
| p3 |    Chen Wu |  27 |

%% sdaf rows=3; cols=3; keyfield='id'; name='people'

The columns must match, in name and order:

>>> people_sdaf.concat(Daf(cols=['id', 'name', 'city'], lol=[['p4', 'Dana Smith', 'Austin']]))
Traceback (most recent call last):
KeyError: "concat() into 'people': the columns differ. Only in this table: ['age']. Only in the other: ['city']."

Removing rows

delete_records

delete_records(keys_ls, inplace=False, silent_error=False)

Remove the rows with these keys, as Daf.delete_records() does. A Python NULL key gives undefined results: see SqlDaf.

By default nothing is changed: a selection without those rows is returned, as select_records(keys, inverse=True) gives. That is always safe.

With inplace=True, the rows are removed from this SqlDaf, and it is returned.

  • On a selection, its references to those rows are removed, and the rest renumbered. The parent is not changed, so this is safe, as in a Daf.
  • On a parent, the rows are deleted from the database. After that, it cannot be read by position: see SqlDaf. A selection made from it earlier still counts a deleted row in len(), though to_daf() leaves it out. So delete in place from a parent only when nothing else uses it.

The keys may be in any form that select_records() takes. A key that is not found raises KeyError, and nothing is removed, unless silent_error is True.

Parameters:

Name Type Description Default
keys_ls Any

The keys of the rows to remove, in any form select_records() takes.

required
inplace bool

If True, remove the rows from this SqlDaf and return it. If False, return a selection.

False
silent_error bool

If True, keys that are not found are ignored.

False

Returns:

Type Description
SqlDaf

This SqlDaf if inplace, otherwise a selection without the rows.

Raises:

Type Description
KeyError

A key is not found, and silent_error is False. Nothing is removed.

ValueError

This SqlDaf, or a SqlDaf given as the keys, has no keyfield.

Examples:

Create a small SqlDaf table of people:

>>> import tempfile
>>> SqlDaf.db_path = tempfile.mkdtemp() + '/job.db'
>>> people_sdaf = SqlDaf('people', cols=['id', 'name', 'age'], keyfield='id', lol=[
...     ['p1', 'Ana Lopez', 34], ['p2', 'Ben Okafor', 52], ['p3', 'Chen Wu', 27]])
>>> people_sdaf

| id |    name    | age |
| -: | ---------: | --: |
| p1 |  Ana Lopez |  34 |
| p2 | Ben Okafor |  52 |
| p3 |    Chen Wu |  27 |

%% sdaf rows=3; cols=3; keyfield='id'; name='people'

By default, get a selection without the rows. The table keeps them:

>>> people_sdaf.delete_records(['p2'])

| id |   name    | age |
| -: | --------: | --: |
| p1 | Ana Lopez |  34 |
| p3 |   Chen Wu |  27 |

%% sdaf rows=2; cols=3; keyfield='id'; name='daf_tmp_people_v2'

>>> people_sdaf

| id |    name    | age |
| -: | ---------: | --: |
| p1 |  Ana Lopez |  34 |
| p2 | Ben Okafor |  52 |
| p3 |    Chen Wu |  27 |

%% sdaf rows=3; cols=3; keyfield='id'; name='people'

A key that is not found raises, unless silent_error=True:

>>> people_sdaf.delete_records(['p9'])
Traceback (most recent call last):
KeyError: 'p9'
>>> people_sdaf.delete_records(['p9', 'p3'], silent_error=True)

| id |    name    | age |
| -: | ---------: | --: |
| p1 |  Ana Lopez |  34 |
| p2 | Ben Okafor |  52 |

%% sdaf rows=2; cols=3; keyfield='id'; name='daf_tmp_people_v5'

Delete the rows from the table itself, with inplace=True:

>>> people_sdaf.delete_records(['p2'], inplace=True)

| id |   name    | age |
| -: | --------: | --: |
| p1 | Ana Lopez |  34 |
| p3 |   Chen Wu |  27 |

%% sdaf rows=2; cols=3; keyfield='id'; name='people'

remove_dups

remove_dups(keyfield='')

Split the rows into those with a unique key and those with a repeated key, as Daf.remove_dups() does.

Only the key column is compared, not the whole row. For each key, the last row is the unique one. The earlier rows with that key are the duplicates. The unique rows come in the order in which their keys first appear. The duplicates keep this table's order.

Both are selections of this table's rows, as a Daf shares its rows with both. Both have the key column as their keyfield. SQL NULL and '' are one key here, as Python NULL is one key in a Daf. No key index is made, so the column may hold repeated keys. Each side is one GROUP BY, run when it is first read.

A key of several columns is not supported, since a SqlDaf keyfield is one column.

Parameters:

Name Type Description Default
keyfield str

The column that identifies a row. If empty, the keyfield of this SqlDaf.

''

Returns:

Type Description
Tuple[SqlDaf, SqlDaf]

A tuple of the selection of unique rows and the selection of duplicate rows.

Raises:

Type Description
KeysDisabledError

There is no keyfield, and this SqlDaf has none.

KeyError

The key column is not found.

NotImplementedError

The key is several columns.

Examples:

Create a small SqlDaf table of sign-ups, where one email appears twice:

>>> import tempfile
>>> SqlDaf.db_path = tempfile.mkdtemp() + '/job.db'
>>> signups_sdaf = SqlDaf('signups', cols=['email', 'name'], lol=[
...     ['ana@example.com', 'Ana Lopez'], ['ben@example.com', 'Ben Okafor'], ['ana@example.com', 'Ana L.']])

Split it into the first row for each email, and the repeats:

>>> unique_sdaf, dups_sdaf = signups_sdaf.remove_dups('email')
>>> unique_sdaf

|      email      |    name    |
| --------------: | ---------: |
| ana@example.com |     Ana L. |
| ben@example.com | Ben Okafor |

%% sdaf rows=2; cols=2; keyfield='email'; name='daf_tmp_signups_v1'

>>> dups_sdaf

|      email      |   name    |
| --------------: | --------: |
| ana@example.com | Ana Lopez |

%% sdaf rows=1; cols=2; keyfield='email'; name='daf_tmp_signups_v2'

Columns and layout

insert_col

insert_col(colname, col_la=None, icol=-1, default=NULL)

Add a named column, or fill it if the name is there, as Daf.insert_col() does.

A new column goes at the end, since ALTER TABLE adds columns only there. Any other position raises NotImplementedError. To read the columns in another order, use select_cols(). It gives new rows, in the order you name.

A list that is too short is filled out with default. With no list, every row gets default. If the name is there, its values are replaced, and icol is ignored. An empty name does nothing. A new column is added on a parent only, as assign_col() adds it.

Parameters:

Name Type Description Default
colname str

The name of the column.

required
col_la T_la | None

The values, in row order.

None
icol int

The position. Only the end is supported: -1, or the number of columns.

-1
default Any

The value for rows that the list does not reach.

NULL

Returns:

Type Description
SqlDaf

This SqlDaf, which has been changed.

Raises:

Type Description
NotImplementedError

A new column at a position other than the end, or on a selection.

Examples:

Create a small SqlDaf table of people:

>>> import tempfile
>>> SqlDaf.db_path = tempfile.mkdtemp() + '/job.db'
>>> people_sdaf = SqlDaf('people', cols=['id', 'name'], keyfield='id', lol=[['p1', 'Ana Lopez'], ['p2', 'Ben Okafor']])

Add a column at the end, from a list. A short list is filled out with default:

>>> people_sdaf.insert_col('city', ['Denver'], default='unknown')

| id |    name    |  city   |
| -: | ---------: | ------: |
| p1 |  Ana Lopez |  Denver |
| p2 | Ben Okafor | unknown |

%% sdaf rows=2; cols=3; keyfield='id'; name='people'

A column at another position is not supported on a SqlDaf:

>>> people_sdaf.insert_col('age', icol=1)
Traceback (most recent call last):
NotImplementedError: ...

insert_idx_col

insert_idx_col(colname='idx', icol=0, startat=0)

Insert a column of row numbers, as Daf.insert_idx_col() does.

The rows are numbered from startat, in their order here, so a selection is numbered from its own first row. The column goes at icol. A negative icol, or one past the last column, puts it at the end. If the name is a column already, its values are replaced.

SQLite adds a column only at the end, so this makes a TEMP copy with the column in place, numbered in SQL, and this SqlDaf then refers to it, as a sort does. The table it came from is not changed. That copies every row, so number a selection rather than a whole large table.

Parameters:

Name Type Description Default
colname str

The name of the new column. An empty name does nothing.

'idx'
icol int

The column position.

0
startat int

The number of the first row.

0

Returns:

Type Description
SqlDaf

This SqlDaf, which now refers to the TEMP copy.

Examples:

Create a small SqlDaf table of people:

>>> import tempfile
>>> SqlDaf.db_path = tempfile.mkdtemp() + '/job.db'
>>> people_sdaf = SqlDaf('people', cols=['id', 'name', 'city'], keyfield='id', lol=[
...     ['p1', 'Ana Lopez', 'Denver'], ['p2', 'Ben Okafor', 'Austin'], ['p3', 'Chen Wu', 'Denver']])

Number the people in Denver, from 1. The selection gets rows of its own first, so the table is not changed:

>>> people_sdaf.select_by_dict({'city': 'Denver'}).insert_idx_col(startat=1)

| idx | id |   name    |  city  |
| --: | -: | --------: | -----: |
|   1 | p1 | Ana Lopez | Denver |
|   2 | p3 |   Chen Wu | Denver |

%% sdaf rows=2; cols=4; keyfield='id'; name='daf_tmp_people_v2'

>>> people_sdaf

| id |    name    |  city  |
| -: | ---------: | -----: |
| p1 |  Ana Lopez | Denver |
| p2 | Ben Okafor | Austin |
| p3 |    Chen Wu | Denver |

%% sdaf rows=3; cols=3; keyfield='id'; name='people'

Give the column another name, and put it in another place:

>>> people_sdaf.insert_idx_col('row_no', icol=1)

| id | row_no |    name    |  city  |
| -: | -----: | ---------: | -----: |
| p1 |      0 |  Ana Lopez | Denver |
| p2 |      1 | Ben Okafor | Austin |
| p3 |      2 |    Chen Wu | Denver |

%% sdaf rows=3; cols=4; keyfield='id'; name='daf_tmp_people_v3'

drop_cols

drop_cols(exclude_cols=None)

Remove columns from the table, in place, as Daf.drop_cols() does.

Each column goes with ALTER TABLE DROP COLUMN, which rewrites the table. On 1,000,000 rows of 6 columns, one drop took 0.73 s, twice the time of copy(). A name that is not a column is ignored. With no names, nothing happens. The dtypes lose the columns dropped.

If the keyfield is dropped, it is cleared, as in a Daf, and its index goes with it. Set a keyfield again with set_keyfield().

A selection has its parent's columns, so a drop on one raises NotImplementedError. A selection made before the drop still names the dropped column. Reading it then raises sqlite3.OperationalError, so make it again.

Parameters:

Name Type Description Default
exclude_cols T_ls | None

The names of the columns to remove.

None

Returns:

Type Description
SqlDaf

This SqlDaf, which has been changed.

Raises:

Type Description
NotImplementedError

This is a selection.

OperationalError

An index of your own is on a column dropped. Nothing changes.

Examples:

Create a small SqlDaf table of people:

>>> import tempfile
>>> SqlDaf.db_path = tempfile.mkdtemp() + '/job.db'
>>> people_sdaf = SqlDaf('people', cols=['id', 'name', 'city', 'age'], keyfield='id', lol=[['p1', 'Ana Lopez', 'Denver', 34]])

Drop a column from the table:

>>> people_sdaf.drop_cols(['city'])

| id |   name    | age |
| -: | --------: | --: |
| p1 | Ana Lopez |  34 |

%% sdaf rows=1; cols=3; keyfield='id'; name='people'

rename_cols

rename_cols(from_to_dict)

Rename columns in place, as Daf.rename_cols() does.

Each is ALTER TABLE RENAME COLUMN, which changes only the schema. On 1,000,000 rows it took 15 ms. Names that are not in the mapping stay as they are. Names in the mapping that are not columns are ignored. Names may be swapped, as in {'a': 'b', 'b': 'a'}. The dtypes are renamed too.

The keyfield follows the rename, as in a Daf. Its index is made again under the new name, the next time a key is used.

A Daf lets two columns get the same name. A SqlDaf raises then, and nothing changes. A selection has its parent's columns, so a rename on one raises NotImplementedError. A selection made before the rename still names the old columns. Reading it then raises sqlite3.OperationalError, so make it again.

Parameters:

Name Type Description Default
from_to_dict T_ds

Maps old names to new names.

required

Returns:

Type Description
SqlDaf

This SqlDaf, which has been changed.

Raises:

Type Description
NotImplementedError

This is a selection.

ValueError

A new name is reserved by SqlDaf. Nothing changes.

OperationalError

Two columns would have the same name. Nothing changes.

Examples:

Create a small SqlDaf table of people, keyed by id:

>>> import tempfile
>>> SqlDaf.db_path = tempfile.mkdtemp() + '/job.db'
>>> people_sdaf = SqlDaf('people', cols=['id', 'name'], keyfield='id', lol=[['p1', 'Ana Lopez']])

Rename the key column. The keyfield follows the new name:

>>> people_sdaf.rename_cols({'id': 'person_id'})

| person_id |   name    |
| --------: | --------: |
|        p1 | Ana Lopez |

%% sdaf rows=1; cols=2; keyfield='person_id'; name='people'

set_cols

set_cols(
    new_cols=None, sanitize_cols=True, unnamed_prefix="col"
)

Set the column names by position, in place, as Daf.set_cols() does.

The names are made as in a Daf. Without a list, they are A, B, C and so on. With sanitize_cols, a repeated name gets a suffix, and an empty name becomes the prefix and its position. Each name that changes is renamed as rename_cols() renames it, so the rows stay as they are. The keyfield and the dtypes follow the names.

Parameters:

Name Type Description Default
new_cols T_ls | None

The names, in order. If None, spreadsheet names are made.

None
sanitize_cols bool

If True, make the names unique and not empty.

True
unnamed_prefix str

The start of a name made for an empty one.

'col'

Returns:

Type Description
SqlDaf

This SqlDaf, which has been changed.

Raises:

Type Description
AttributeError

The number of names is not the number of columns, as in a Daf.

NotImplementedError

This is a selection.

Examples:

Create a small SqlDaf table with plain column names:

>>> import tempfile
>>> SqlDaf.db_path = tempfile.mkdtemp() + '/job.db'

Set all the names at once. Repeated and empty names are taken as given:

>>> SqlDaf('t1', cols=['a', 'b', 'c'], lol=[['Ana Lopez', 'Denver', 34]]).set_cols(['name', 'name', ''])

|   name    | name_1 | col2 |
| --------: | -----: | ---: |
| Ana Lopez | Denver |   34 |

%% sdaf rows=1; cols=3; keyfield=''; name='t1'

With sanitize_cols=True, an empty name gets one made from unnamed_prefix, and a repeated name gets a number:

>>> SqlDaf('t2', cols=['a', 'b', 'c'], lol=[['Ana Lopez', 'Denver', 34]]).set_cols(['name', '', 'name'], sanitize_cols=True)

|   name    |  col1  | name_2 |
| --------: | -----: | -----: |
| Ana Lopez | Denver |     34 |

%% sdaf rows=1; cols=3; keyfield=''; name='t2'

>>> SqlDaf('t3', cols=['a', 'b', 'c'], lol=[['Ana Lopez', 'Denver', 34]]).set_cols(['name', '', 'age'], sanitize_cols=True, unnamed_prefix='col')

|   name    |  col1  | age |
| --------: | -----: | --: |
| Ana Lopez | Denver |  34 |

%% sdaf rows=1; cols=3; keyfield=''; name='t3'

set_dtypes

set_dtypes(default_type=str, typ_to_cols_dict=None)

Set the dtype of every column, from a default and a few exceptions, as Daf.set_dtypes() does.

No value is converted. apply_dtypes() does that. On a permanent table with a record, the record is changed too, as set_keyfield() changes it. The record can store these dtypes: int, float, bool, str, list and dict.

Parameters:

Name Type Description Default
default_type Type

The type of every column that is not listed.

str
typ_to_cols_dict Dict[Type, T_ls] | None

Maps a type to the names of the columns of that type.

None

Returns:

Type Description
SqlDaf

This SqlDaf, which has been changed.

Raises:

Type Description
NotImplementedError

A type the record cannot hold, on a table with a record. Nothing changes.

Examples:

Create a small SqlDaf table of people, loaded as text:

>>> import tempfile
>>> SqlDaf.db_path = tempfile.mkdtemp() + '/job.db'
>>> people_sdaf = SqlDaf('people', cols=['id', 'name', 'age'], lol=[['p1', 'Ana Lopez', '34']])

Record str as the type of every column, and int for the age. Nothing is converted:

>>> people_sdaf.set_dtypes(str, {int: ['age']}).dtypes
{'id': <class 'str'>, 'name': <class 'str'>, 'age': <class 'int'>}

attach_schema

attach_schema(schema)

Attach a @schemaclass to this SqlDaf, as Daf.attach_schema() does.

It stores the schema, then fills in the dtypes and the keyfield, each only if there is none. No value is converted, and nothing is checked against the rows. Call apply_dtypes() to convert. On a permanent table with a record, the record is changed too, as set_keyfield() changes it.

A Daf with no columns takes them from the schema. A SqlDaf gets its columns when its table is made, so one with no columns raises NotImplementedError.

Parameters:

Name Type Description Default
schema type

A class decorated with @schemaclass.

required

Returns:

Type Description
SqlDaf

This SqlDaf, which has been changed.

Raises:

Type Description
TypeError

The class is not a @schemaclass.

KeyError

The schema's keyfield is not a column.

NotImplementedError

This SqlDaf has no columns, or a dtype the record cannot hold.

Examples:

Describe a table of people with a schema class: its columns, their types and its keyfield:

>>> import tempfile
>>> from daffodil.lib.schemaclass import schemaclass
>>> @schemaclass
... class Person:
...     __keyfield__ = 'id'
...     id: str = ''
...     name: str = ''
...     age: int = 0

Create a small SqlDaf table, loaded as text, and attach the schema to it:

>>> SqlDaf.db_path = tempfile.mkdtemp() + '/job.db'
>>> people_sdaf = SqlDaf('people', cols=['id', 'name', 'age'], lol=[['p1', 'Ana Lopez', '34']]).attach_schema(Person)

The keyfield and the dtypes now come from the schema:

>>> people_sdaf.keyfield, people_sdaf.dtypes
('id', {'id': <class 'str'>, 'name': <class 'str'>, 'age': <class 'int'>})

clone_empty

clone_empty(lol=None, cols=None, name='')

Make a new table with the same layout, as Daf.clone_empty() does.

It has the same columns, keyfield and dtypes, and no rows unless lol is given. It is a TEMP table, as copy() makes, so save() keeps it. With cols that differ, it has those columns. The dtypes then keep the entries for the columns still there. The keyfield is cleared if its column is gone.

Parameters:

Name Type Description Default
lol T_lola | None

Rows for the new table, written as the constructor writes them. None for no rows.

None
cols T_ls | None

Column names to use instead of these.

None
name str

The name of the new table. Generated if empty, as in daf_tmp_people_v4.

''

Returns:

Type Description
SqlDaf

The new SqlDaf.

Examples:

Create a small SqlDaf table of people:

>>> import tempfile
>>> SqlDaf.db_path = tempfile.mkdtemp() + '/job.db'
>>> people_sdaf = SqlDaf('people', cols=['id', 'name', 'age'], keyfield='id', lol=[['p1', 'Ana Lopez', 34]])

Make a new table with the same columns and keyfield, and no rows:

>>> empty_sdaf = people_sdaf.clone_empty()
>>> empty_sdaf.columns(), empty_sdaf.keyfield, len(empty_sdaf)
(['id', 'name', 'age'], 'id', 0)

Give it rows of its own, other columns, or a name:

>>> people_sdaf.clone_empty(lol=[['p7', 'Gus Berg', 61]])

| id |   name   | age |
| -: | -------: | --: |
| p7 | Gus Berg |  61 |

%% sdaf rows=1; cols=3; keyfield='id'; name='daf_tmp_people_v2'

>>> people_sdaf.clone_empty(cols=['id', 'email']).columns()
['id', 'email']
>>> people_sdaf.clone_empty(name='people_blank').name
'people_blank'

Sorting

sort_by_colname

sort_by_colname(
    colname,
    *,
    reverse=False,
    length_priority=False,
    as_str=False,
)

Sort the rows by one column, in place, as Daf.sort_by_colname() does.

See sort_by_colnames() for how the rows are sorted and where they go.

Parameters:

Name Type Description Default
colname str

The column to sort by.

required
reverse bool

If True, sort from high to low.

False
length_priority bool

If True, sort by length first, then by value.

False
as_str bool

If True, sort by the text of each value, and Python NULL sorts as ''.

False

Returns:

Type Description
SqlDaf

This SqlDaf, now a sorted TEMP table.

Raises:

Type Description
KeyError

The column name is not found.

Examples:

Create a small SqlDaf table of people:

>>> import tempfile
>>> SqlDaf.db_path = tempfile.mkdtemp() + '/job.db'
>>> people_sdaf = SqlDaf('people', cols=['id', 'name', 'age'], lol=[
...     ['p1', 'Ana Lopez', 34], ['p2', 'Ben Okafor', 52], ['p3', 'Chen Wu', 27]])
>>> people_sdaf

| id |    name    | age |
| -: | ---------: | --: |
| p1 |  Ana Lopez |  34 |
| p2 | Ben Okafor |  52 |
| p3 |    Chen Wu |  27 |

%% sdaf rows=3; cols=3; keyfield=''; name='people'

Sort by age, from low to high, and from high to low:

>>> people_sdaf.sort_by_colname('age')

| id |    name    | age |
| -: | ---------: | --: |
| p3 |    Chen Wu |  27 |
| p1 |  Ana Lopez |  34 |
| p2 | Ben Okafor |  52 |

%% sdaf rows=3; cols=3; keyfield=''; name='daf_tmp_people_v1'

>>> people_sdaf.sort_by_colname('age', reverse=True)

| id |    name    | age |
| -: | ---------: | --: |
| p2 | Ben Okafor |  52 |
| p1 |  Ana Lopez |  34 |
| p3 |    Chen Wu |  27 |

%% sdaf rows=3; cols=3; keyfield=''; name='daf_tmp_daf_tmp_people_v1_v2'

Sort text of digits with shorter values first, with length_priority=True, so '9' comes before '10':

>>> SqlDaf('codes', cols=['code'], lol=[['10'], ['9'], ['100']]).sort_by_colname('code', length_priority=True)

| code |
| ---: |
|    9 |
|   10 |
|  100 |

%% sdaf rows=3; cols=1; keyfield=''; name='daf_tmp_codes_v3'

A column of numbers mixed with text sorts in SQLite's order, numbers first, with no error. With as_str=True it sorts by text, where '10' comes before '9':

>>> mixed_sdaf = SqlDaf('mixed', cols=['id', 'v'], lol=[['a', 10], ['b', 9], ['c', 'x']])
>>> mixed_sdaf

| id | v  |
| -: | -: |
|  a | 10 |
|  b |  9 |
|  c |  x |

%% sdaf rows=3; cols=2; keyfield=''; name='mixed'

>>> mixed_sdaf.sort_by_colname('v')

| id | v  |
| -: | -: |
|  b |  9 |
|  a | 10 |
|  c |  x |

%% sdaf rows=3; cols=2; keyfield=''; name='daf_tmp_mixed_v4'

>>> mixed_sdaf.sort_by_colname('v', as_str=True)

| id | v  |
| -: | -: |
|  a | 10 |
|  b |  9 |
|  c |  x |

%% sdaf rows=3; cols=2; keyfield=''; name='daf_tmp_daf_tmp_mixed_v4_v5'

sort_by_colnames

sort_by_colnames(
    colnames,
    reverse=False,
    length_priority=False,
    as_str=False,
)

Sort the rows by several columns, in place, as Daf.sort_by_colnames() does.

The first column is the main sort key. The others break ties. Rows that tie on every column keep their order, as Python's sort keeps them.

Where the sorted rows go:

The rows are copied in sorted order into a TEMP table, and this SqlDaf becomes it. This is copy(inplace=True) with an ORDER BY, in one statement. The table it came from is not changed, so its other selections stay as they were. A Daf selection sorts its own list of rows, and leaves its parent alone too. On a permanent table, the table in the file keeps its order. Use save() to keep the sorted copy. On 1,000,000 rows of 6 columns, a sort by one text column took 2.4 s, against 1.0 s for a Daf.

How values compare:

  • Python NULL, SQL NULL or '', sorts first, as an empty cell sorts first in a Daf.
  • Text compares by code point, as Python compares it.
  • length_priority sorts by the length of the text first, then by the text. That gives numeric order for whole numbers held as text, when none is negative or has a leading zero. It is not a numeric sort for negative numbers or decimals.
  • as_str sorts by the text of each value. A float such as 1e20 is written differently by SQLite.
  • A column that mixes numbers and text sorts every number before all text. A Daf raises TypeError on such a column instead.

Parameters:

Name Type Description Default
colnames T_ls

The columns to sort by, main key first.

required
reverse bool

If True, sort from high to low.

False
length_priority bool

If True, sort by length first, then by value.

False
as_str bool

If True, sort by the text of each value, and Python NULL sorts as ''.

False

Returns:

Type Description
SqlDaf

This SqlDaf, now a sorted TEMP table.

Raises:

Type Description
KeyError

A column name is not found.

Examples:

Create a small SqlDaf table of people:

>>> import tempfile
>>> SqlDaf.db_path = tempfile.mkdtemp() + '/job.db'
>>> people_sdaf = SqlDaf('people', cols=['id', 'city', 'name'], lol=[
...     ['p1', 'Denver', 'Chen Wu'], ['p2', 'Austin', 'Ben Okafor'], ['p3', 'Denver', 'Ana Lopez']])
>>> people_sdaf

| id |  city  |    name    |
| -: | -----: | ---------: |
| p1 | Denver |    Chen Wu |
| p2 | Austin | Ben Okafor |
| p3 | Denver |  Ana Lopez |

%% sdaf rows=3; cols=3; keyfield=''; name='people'

Sort by city, then by name within each city:

>>> people_sdaf.sort_by_colnames(['city', 'name'])

| id |  city  |    name    |
| -: | -----: | ---------: |
| p2 | Austin | Ben Okafor |
| p3 | Denver |  Ana Lopez |
| p1 | Denver |    Chen Wu |

%% sdaf rows=3; cols=3; keyfield=''; name='daf_tmp_people_v1'

Sort by name from high to low, or with shorter names first:

>>> people_sdaf.sort_by_colnames(['name'], reverse=True)

| id |  city  |    name    |
| -: | -----: | ---------: |
| p1 | Denver |    Chen Wu |
| p2 | Austin | Ben Okafor |
| p3 | Denver |  Ana Lopez |

%% sdaf rows=3; cols=3; keyfield=''; name='daf_tmp_daf_tmp_people_v1_v2'

>>> people_sdaf.sort_by_colnames(['name'], length_priority=True)

| id |  city  |    name    |
| -: | -----: | ---------: |
| p1 | Denver |    Chen Wu |
| p3 | Denver |  Ana Lopez |
| p2 | Austin | Ben Okafor |

%% sdaf rows=3; cols=3; keyfield=''; name='daf_tmp_daf_tmp_daf_tmp_people_v1_v2_v3'

Sort numbers mixed with text by their text, with as_str=True:

>>> SqlDaf('mixed', cols=['id', 'v'], lol=[['a', 10], ['b', 9], ['c', 'x']]).sort_by_colnames(['v'], as_str=True)

| id | v  |
| -: | -: |
|  a | 10 |
|  b |  9 |
|  c |  x |

%% sdaf rows=3; cols=2; keyfield=''; name='daf_tmp_mixed_v4'