Selecting and indexing¶
Each selection is a child: it refers to rows of its parent, and runs no query until it is read.
Indexing with brackets¶
__getitem__ ¶
__getitem__(sel)
Select rows by position or by key, as daf[n], daf[n:m] and daf[key] do.
Each gives a selection, not an ordinary Daf, so a large slice stays in the database.
Read it with to_daf(), or to_dict() for one row. For one row as a dict, iloc() and
select_record() are cheaper: they make no selection.
- An int is always a position, from 0, even when the keyfield holds ints. Negative counts
from the end. A position past the end raises IndexError, as in a Daf. For an int key,
use
select_record(), orselect_records()for several. - A slice of positions, as in
sdaf[10:20]. A step is not supported. A slice past the end is empty, as in a Daf. - A str is a key. The keyfield's unique index is made first. A missing key raises KeyError, as in a Daf.
- A list of keys gives their rows in the order of the list, as
select_records()does. A missing key raises KeyError, as in a Daf.
Rows and columns together, as in sdaf[key, 'col'] or sdaf[:, ['a', 'b']], give new
rows with only those columns, as select_cols() makes them. The rows part takes any form
above, or : for all rows. The columns part is a name, a list of names, or : for all.
Read one value with to_value(), as in a Daf.
A value read this way makes a TEMP table, which costs about 1 ms. In a loop over many
keys, select_record() is about 30 times faster.
A negative position, or a slice with a negative end, costs a len() first. In a parent
with daf_rid, or a child whose rows are found, positions are looked up directly. Otherwise
SQLite steps over the rows before them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sel
|
Any
|
An int, a slice, a key or a list of keys. Or a pair of rows and columns. |
required |
Returns:
| Type | Description |
|---|---|
SqlDaf
|
A selection. New rows of the chosen columns, for a pair. |
Raises:
| Type | Description |
|---|---|
IndexError
|
An int position past the end. |
KeyError
|
A key that is missing, or a name that is not a column. |
NotImplementedError
|
Any other form, such as a list of positions, or a slice with a step. |
ValueError
|
An int or a slice, on a parent that has had rows deleted. See SqlDaf. |
Examples:
Create a small SqlDaf table. It is stored on disk, so its size is not limited by memory:
>>> 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], ['p2', 'Ben Okafor', 'Austin', 52], ['p3', 'Chen Wu', 'Boston', 27]])
>>> people_sdaf
| id | name | city | age |
| -: | ---------: | -----: | --: |
| p1 | Ana Lopez | Denver | 34 |
| p2 | Ben Okafor | Austin | 52 |
| p3 | Chen Wu | Boston | 27 |
%% sdaf rows=3; cols=4; keyfield='id'; name='people'
Select two rows by key, in the order given, and look at them directly:
>>> people_sdaf[['p3', 'p1']]
| id | name | city | age |
| -: | --------: | -----: | --: |
| p3 | Chen Wu | Boston | 27 |
| p1 | Ana Lopez | Denver | 34 |
%% sdaf rows=2; cols=4; keyfield='id'; name='daf_tmp_people_v2'
Select one cell, and read it as a value:
>>> people_sdaf['p2', 'age'].to_value()
52
By condition¶
select_where ¶
select_where(where, indirect_col=None, cols=None)
Select the rows for which a Python function is true, as Daf.select_where() does.
The function gets each row as a KeyedList, with Python NULL for
a missing value, and reads cells by name, as in row['n']. It is registered with SQLite
and called for each row when the selection is first read. If it raises, that error is
raised, as itself.
If the test only compares columns to values, use select_by_dict() or select_where_sql(),
which call no Python function and are much faster.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
where
|
Callable[[Any], Any]
|
A function of a row, true for the rows to keep. |
required |
indirect_col
|
str | None
|
Not supported on a SqlDaf. It raises NotImplementedError. |
None
|
Returns:
| Type | Description |
|---|---|
SqlDaf
|
A selection. |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
|
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], ['p2', 'Ben Okafor', 'Austin', 52], ['p3', 'Chen Wu', 'Boston', 27]])
>>> people_sdaf
| id | name | city | age |
| -: | ---------: | -----: | --: |
| p1 | Ana Lopez | Denver | 34 |
| p2 | Ben Okafor | Austin | 52 |
| p3 | Chen Wu | Boston | 27 |
%% sdaf rows=3; cols=4; keyfield='id'; name='people'
Select the people who live in Austin or Boston, with a Python function:
>>> people_sdaf.select_where(lambda row: row['city'] in ('Austin', 'Boston'))
| id | name | city | age |
| -: | ---------: | -----: | --: |
| p2 | Ben Okafor | Austin | 52 |
| p3 | Chen Wu | Boston | 27 |
%% sdaf rows=2; cols=4; keyfield='id'; name='daf_tmp_people_v1'
select_where_sql ¶
select_where_sql(where_sql, params=())
Select the rows for which a SQL condition is true.
The condition is the text after WHERE. Name the columns as they are, in double quotes if
they need it, and pass values as params, with ? in the text. Python NULL in params
becomes SQL NULL. Test for a missing value with IS NULL. This is the fastest way to select,
since SQLite does all the work.
Nothing runs yet. The rows are found when the selection is first read. See SqlDaf.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
where_sql
|
str
|
The condition, as SQL text. |
required |
params
|
Iterable[Any]
|
Values for the |
()
|
Returns:
| Type | Description |
|---|---|
SqlDaf
|
A selection. |
Examples:
Create a small SqlDaf table of people. It is stored on disk, so its size is not limited by memory:
>>> 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], ['p2', 'Ben Okafor', 'Austin', 52], ['p3', 'Chen Wu', 'Boston', 27]])
>>> people_sdaf
| id | name | city | age |
| -: | ---------: | -----: | --: |
| p1 | Ana Lopez | Denver | 34 |
| p2 | Ben Okafor | Austin | 52 |
| p3 | Chen Wu | Boston | 27 |
%% sdaf rows=3; cols=4; keyfield='id'; name='people'
Select the people over 30 with a SQL condition, the value passed as a parameter:
>>> people_sdaf.select_where_sql('age > ?', [30])
| id | name | city | age |
| -: | ---------: | -----: | --: |
| p1 | Ana Lopez | Denver | 34 |
| p2 | Ben Okafor | Austin | 52 |
%% sdaf rows=2; cols=4; keyfield='id'; name='daf_tmp_people_v1'
select_by_dict ¶
select_by_dict(
selector_da, expectmax=-1, inverse=False, keyfield=""
)
Select the rows that match every field of a dict, or any one of a list of dicts.
It works as Daf.select_by_dict() does, with the SQL written for you. A dict is an AND of
equalities, and a list of dicts is an OR of those. With inverse=True, the rows that do
not match. An empty dict matches every row, and an empty list none. A dict that names a
column that is not there matches no row. Python NULL as a value matches a missing value.
Values are compared with SQL's IS, which treats SQL NULL as a value, so the inverse keeps rows with a missing value. As in Python, 1 equals 1.0, and the text '1' does not equal 1.
Nothing runs yet, unless expectmax is given: then its rows are found at once, to
count the matches.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
selector_da
|
T_da | List[T_da]
|
A dict of column and value, or a list of such dicts. |
required |
expectmax
|
int
|
If not -1, and more rows match, raise LookupError. |
-1
|
inverse
|
bool
|
If True, select the rows that do not match. |
False
|
keyfield
|
str
|
The keyfield of the selection. This SqlDaf's if empty. |
''
|
Returns:
| Type | Description |
|---|---|
SqlDaf
|
A selection. |
Raises:
| Type | Description |
|---|---|
LookupError
|
More than |
TypeError
|
|
Examples:
Create a small SqlDaf table of people, one with no city given:
>>> 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], ['p2', 'Ben Okafor', 'Austin', 52],
... ['p3', 'Chen Wu', 'Austin', 27], ['p4', 'Dana Smith', '', 41]])
>>> people_sdaf
| id | name | city | age |
| -: | ---------: | -----: | --: |
| p1 | Ana Lopez | Denver | 34 |
| p2 | Ben Okafor | Austin | 52 |
| p3 | Chen Wu | Austin | 27 |
| p4 | Dana Smith | | 41 |
%% sdaf rows=4; cols=4; keyfield='id'; name='people'
Select the rows whose city is Austin:
>>> people_sdaf.select_by_dict({'city': 'Austin'})
| id | name | city | age |
| -: | ---------: | -----: | --: |
| p2 | Ben Okafor | Austin | 52 |
| p3 | Chen Wu | Austin | 27 |
%% sdaf rows=2; cols=4; keyfield='id'; name='daf_tmp_people_v1'
Select the row with no city. Python NULL matches an empty cell:
>>> people_sdaf.select_by_dict({'city': ''}).keys()
['p4']
Select the rows that do not match, with inverse=True:
>>> people_sdaf.select_by_dict({'city': 'Austin'}, inverse=True).keys()
['p1', 'p4']
Give the selection another keyfield, here the name:
>>> people_sdaf.select_by_dict({'city': 'Austin'}, keyfield='name').keys()
['Ben Okafor', 'Chen Wu']
Check how many rows match, with expectmax. More than that raises:
>>> people_sdaf.select_by_dict({'city': 'Austin'}, expectmax=1)
Traceback (most recent call last):
...
LookupError: select_by_dict: more than 1 rows match {'city': 'Austin'}.
split_where ¶
split_where(where, *, indirect_col=None, cols=None)
Split the rows in two, by a Python function that is true or false for each row.
This is deprecated, as Daf.split_where() is. Use group_where() with a function that
returns True or False.
It works as Daf.split_where() does, with the rows staying in the database. It is two
calls of select_where(): one keeps the rows where the function is true, and the other
the rest. Nothing runs yet. Each side is found when it is first read, and the function is
called again for each side that is read. So it should give the same answer each time.
If the test is that columns equal values, two calls of select_by_dict(), the second with inverse=True,
are much faster, since they call no function. On 1,000,000 rows, reading both sides took
1.79 s here, and 0.21 s that way.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
where
|
Callable[[Any], Any]
|
A function of a row, true for the rows of the first side. |
required |
indirect_col
|
str | None
|
Not supported on a SqlDaf. It raises NotImplementedError. |
None
|
Returns:
| Type | Description |
|---|---|
Tuple[SqlDaf, SqlDaf]
|
A tuple of the selection of the rows where the function is true and the selection of the others. |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
|
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], ['p2', 'Ben Okafor', 'Austin', 52], ['p3', 'Chen Wu', 'Boston', 27]])
Split the people into those over 30 and the rest, in one call:
>>> older_sdaf, younger_sdaf = people_sdaf.split_where(lambda row: row['age'] > 30)
>>> older_sdaf
| id | name | city | age |
| -: | ---------: | -----: | --: |
| p1 | Ana Lopez | Denver | 34 |
| p2 | Ben Okafor | Austin | 52 |
%% sdaf rows=2; cols=4; keyfield='id'; name='daf_tmp_people_v1'
>>> younger_sdaf
| id | name | city | age |
| -: | ------: | -----: | --: |
| p3 | Chen Wu | Boston | 27 |
%% sdaf rows=1; cols=4; keyfield='id'; name='daf_tmp_people_v2'
select_first_row_by_dict ¶
select_first_row_by_dict(selector_da, inverse=False)
Get the first row that matches every field of a dict, as Daf.select_first_row_by_dict() does.
The matching rule is that of select_by_dict(). The search stops at the first match.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
selector_da
|
T_da
|
The column names and the values they must have. |
required |
inverse
|
bool
|
If True, find the first row that does not match. |
False
|
Returns:
| Type | Description |
|---|---|
T_da
|
The row as a dict, or an empty dict if none matches. |
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], ['p2', 'Ben Okafor', 'Austin', 52], ['p3', 'Chen Wu', 'Austin', 27]])
Find the first person in Austin, as a dict:
>>> people_sdaf.select_first_row_by_dict({'city': 'Austin'})
{'id': 'p2', 'name': 'Ben Okafor', 'city': 'Austin', 'age': 52}
Find the first person not in Austin, with inverse=True:
>>> people_sdaf.select_first_row_by_dict({'city': 'Austin'}, inverse=True)
{'id': 'p1', 'name': 'Ana Lopez', 'city': 'Denver', 'age': 34}
No match gives an empty dict:
>>> people_sdaf.select_first_row_by_dict({'city': 'Miami'})
{}
Columns¶
select_cols ¶
select_cols(cols=None, exclude_cols=None)
Make a new table with only some columns, chosen by name, as Daf.select_cols() does.
The columns come in the order of cols. A name given twice is used once. A name that is
not a column raises KeyError. With no cols, all columns are kept, in this table's order.
Then exclude_cols leaves out the columns you name. A name in exclude_cols that is not
a column is ignored. The rows keep their order.
The result is new rows, as in a Daf, not a selection of this table's rows. It is resolved the first time it is read, into a TEMP table, as a join is. The keyfield and dtypes carry over for the columns kept.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cols
|
T_ls | None
|
The names of the columns to keep, in the order you want. If empty, all columns. |
None
|
exclude_cols
|
T_ls | None
|
The names of the columns to leave out. |
None
|
Returns:
| Type | Description |
|---|---|
SqlDaf
|
A SqlDaf of the chosen columns, resolved when first read. |
Raises:
| Type | Description |
|---|---|
KeyError
|
A name in |
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], ['p2', 'Ben Okafor', 'Austin', 52]])
Select some columns, in the order given:
>>> people_sdaf.select_cols(['age', 'name'])
| age | name |
| --: | ---------: |
| 34 | Ana Lopez |
| 52 | Ben Okafor |
%% sdaf rows=2; cols=2; keyfield=''; name='daf_tmp_people_v1'
Select every column but some:
>>> people_sdaf.select_cols(exclude_cols=['city']).columns()
['id', 'name', 'age']
calc_cols ¶
calc_cols(
include_cols=None,
exclude_cols=None,
include_types=None,
exclude_types=None,
)
Choose column names by rules, as Daf.calc_cols() does.
It reads only the column names and the dtypes, never the rows.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
include_cols
|
Iterable | None
|
Keep only these names. |
None
|
exclude_cols
|
Iterable | None
|
Leave out these names. |
None
|
include_types
|
List[Type] | None
|
Keep only columns whose dtype is one of these. |
None
|
exclude_types
|
List[Type] | None
|
Leave out columns whose dtype is one of these. |
None
|
Returns:
| Type | Description |
|---|---|
T_ls
|
The chosen column names, in column order. |
Examples:
Create a small SqlDaf table of people, with a type for each column:
>>> import tempfile
>>> SqlDaf.db_path = tempfile.mkdtemp() + '/job.db'
>>> people_sdaf = SqlDaf('people', cols=['id', 'name', 'age', 'height_m'], keyfield='id',
... dtypes={'id': str, 'name': str, 'age': int, 'height_m': float}, lol=[['p1', 'Ana Lopez', 34, 1.68]])
Choose the columns of numbers, by type:
>>> people_sdaf.calc_cols(include_types=[int, float])
['age', 'height_m']
Choose every column that is not text:
>>> people_sdaf.calc_cols(exclude_types=[str])
['age', 'height_m']
Choose from a list of names, leaving some out:
>>> people_sdaf.calc_cols(include_cols=['id', 'age', 'height_m'], exclude_cols=['height_m'])
['id', 'age']
By key¶
__contains__ ¶
__contains__(key)
Say whether a row has this key, as in key in sdaf.
It is one lookup on the keyfield, through its index. A table with no rows contains nothing, as in a Daf.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Any
|
The key to look for. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if a row has this key. |
Raises:
| Type | Description |
|---|---|
ValueError
|
There is no keyfield, and there are rows. |
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]])
Ask whether a row has this key:
>>> 'p2' in people_sdaf, 'p9' in people_sdaf
(True, False)
keys ¶
keys(
*,
silent_error: bool = True,
astype: Literal["list"] = "list",
) -> T_la
keys(
*, silent_error: bool = True, astype: Literal["view"]
) -> T_kva
keys(
*, silent_error: bool = True, astype: str = "list"
) -> T_la | T_kva
keys(*, silent_error=True, astype='list')
Get the keys, the keyfield's values in row order, as Daf.keys() does.
Every key is read into memory. This takes the arguments of Daf.keys(), so code written
for a Daf runs on a SqlDaf. With astype='view' it gives a KeysView of a dict made from
the keys, in row order, with a repeated key once. A Daf gives a view of its key index,
which costs nothing. Here the dict is built, so it holds every key.
It exists for that compatibility. On a large table there are better ways, which read no key into Python and run the same on a Daf:
- To compare two tables, select the rows:
a.select_records(b, inverse=True, silent_error=True). - To pass the keys to another method, give the SqlDaf itself, as in
b.select_records(a). - To test one key, use
key in table, ortable.select_record(key)for its row. - To sample, take the first rows:
table[0:3].keys(). - To count, use
len()of the selection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
silent_error
|
bool
|
If False, raise an error when there is no keyfield. |
True
|
astype
|
str
|
|
'list'
|
Returns:
| Type | Description |
|---|---|
T_la | T_kva
|
The keys. Without a keyfield, an empty list, as in a Daf. |
Raises:
| Type | Description |
|---|---|
KeysDisabledError
|
There is no keyfield, and |
ValueError
|
|
Examples:
Create a small SqlDaf table of people:
>>> import tempfile
>>> SqlDaf.db_path = tempfile.mkdtemp() + '/job.db'
>>> people_sdaf = SqlDaf('people', cols=['id', 'city', 'age'], keyfield='id', lol=[
... ['p1', 'Denver', 34], ['p2', 'Austin', 52], ['p3', 'Denver', 27]])
Read the keys, in row order:
>>> people_sdaf.keys()
['p1', 'p2', 'p3']
Read them as a view, for fast membership tests:
>>> people_sdaf.keys(astype='view')
dict_keys(['p1', 'p2', 'p3'])
Read the keys of a selection:
>>> people_sdaf.select_by_dict({'city': 'Denver'}).keys()
['p1', 'p3']
With no keyfield, the answer is an empty list, or an error with silent_error=False:
>>> plain_sdaf = SqlDaf('plain', cols=['id'], lol=[['p1']])
>>> plain_sdaf.keys()
[]
>>> plain_sdaf.keys(silent_error=False)
Traceback (most recent call last):
...
daffodil.daf.KeysDisabledError: keys: this SqlDaf has no keyfield.
get_existing_keys ¶
get_existing_keys(keylist)
Keep the keys of a list that have a row, in the order of the list, as Daf.get_existing_keys() does.
The keys are looked up in SQL, all at once. Without a keyfield, the result is empty, as in a Daf.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keylist
|
T_la
|
The keys to check. |
required |
Returns:
| Type | Description |
|---|---|
T_la
|
The keys from the list that have a row. |
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]])
Keep the keys of a list that have a row, in the order of the list:
>>> people_sdaf.get_existing_keys(['p3', 'p9', 'p1'])
['p3', 'p1']
select_krows ¶
select_krows(krows, inverse=False, silent_error=False)
Select rows by key, as Daf.select_krows() does.
- A key, or a list or other iterable of keys, selects those rows, in the order of the keys.
This is
select_records(). - A tuple of two keys selects every row from the first key to the last, both included, in row order. Either may be None, for the start or the end. A tuple of one key runs to the end.
A key range is found by the place of its keys among the rows, not by row positions. So it works on a table that has had rows deleted.
A slice is not supported. In a Daf, a slice here means row positions. Use sdaf[n:m].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
krows
|
Any
|
A key, a list of keys, or a tuple of a first and last key. |
required |
inverse
|
bool
|
If True, select the rows that are not selected. |
False
|
silent_error
|
bool
|
If True, keys that are not found are ignored. A key of a range must be found. |
False
|
Returns:
| Type | Description |
|---|---|
SqlDaf
|
A selection. |
Raises:
| Type | Description |
|---|---|
KeyError
|
A key is not found. |
NotImplementedError
|
|
TypeError
|
|
ValueError
|
There is 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], ['p4', 'Dana Smith', 41]])
Select by a list of keys, in the order given:
>>> people_sdaf.select_krows(['p3', 'p1'])
| id | name | age |
| -: | --------: | --: |
| p3 | Chen Wu | 27 |
| p1 | Ana Lopez | 34 |
%% sdaf rows=2; cols=3; keyfield='id'; name='daf_tmp_people_v2'
Select a range of keys, in row order. One end may be left open:
>>> people_sdaf.select_krows(('p2', 'p3')).keys()
['p2', 'p3']
>>> people_sdaf.select_krows(('p3',)).keys()
['p3', 'p4']
>>> people_sdaf.select_krows((None, 'p2')).keys()
['p1', 'p2']
Select the rows not listed, with inverse=True:
>>> people_sdaf.select_krows(['p1', 'p2'], inverse=True).keys()
['p3', 'p4']
Ignore keys that are not found, with silent_error=True:
>>> people_sdaf.select_krows(['p1', 'p9'], silent_error=True).keys()
['p1']
select_records ¶
select_records(keys_ls, inverse=False, silent_error=False)
Select the rows with these keys, as Daf.select_records() does.
select_records_daf() is the old name of this method. It still works, and is deprecated.
The keys may be a list or any iterable, an in-memory Daf, or another SqlDaf.
A SqlDaf in another database file is brought in first, as in join().
A SqlDaf's keys are used in SQL, as a join, so no key comes into Python.
The other forms are written to a TEMP table first. The selected rows come in
the order of the keys, as in a Daf. With inverse=True, the rows whose key is not among
them, in this table's order.
A Python NULL key gives undefined results: see SqlDaf.
As in a Daf, a key that is not found raises KeyError, unless silent_error is True. That
check runs at once, as one query. The selection itself runs when first read.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys_ls
|
Any
|
The keys: a list, an iterable, a Daf or a SqlDaf. |
required |
inverse
|
bool
|
If True, select the rows whose key is not among them. |
False
|
silent_error
|
bool
|
If True, keys that are not found are ignored. |
False
|
Returns:
| Type | Description |
|---|---|
SqlDaf
|
A selection. |
Raises:
| Type | Description |
|---|---|
KeyError
|
A key is not found, and |
ValueError
|
This SqlDaf, or a SqlDaf given as the keys, has no keyfield. |
Examples:
Create a small SqlDaf table of people, from a Daf in memory:
>>> import tempfile
>>> SqlDaf.db_path = tempfile.mkdtemp() + '/job.db'
>>> people_daf = Daf(cols=['id', 'name', 'age'], keyfield='id', lol=[
... ['p1', 'Ana Lopez', 34], ['p2', 'Ben Okafor', 52], ['p3', 'Chen Wu', 27]])
>>> people_sdaf = SqlDaf.from_daf(people_daf, name='people')
Select the rows with these keys, in the order given:
>>> people_sdaf.select_records(['p3', 'p1'])
| id | name | age |
| -: | --------: | --: |
| p3 | Chen Wu | 27 |
| p1 | Ana Lopez | 34 |
%% sdaf rows=2; cols=3; keyfield='id'; name='daf_tmp_people_v2'
Select the rows whose keys are not listed, with inverse=True:
>>> people_sdaf.select_records(['p1'], inverse=True).keys()
['p2', 'p3']
A key that is not found raises. With silent_error=True it is ignored:
>>> people_sdaf.select_records(['p1', 'p9'], silent_error=True).keys()
['p1']
The keys may come from another table, as its keyfield:
>>> visits_sdaf = SqlDaf('visits', cols=['id', 'visits'], keyfield='id', lol=[['p2', 7], ['p3', 12]])
>>> people_sdaf.select_records(visits_sdaf).keys()
['p2', 'p3']