API: short access to relations of schema and to columns of relations
Feature 1: Shorthand for relations of schemata and columns of relations
Current state
# assuming a certain schema sch
rel = sch.relations.my_table
col = rel.columns.id
Fact
There are good reasons for not giving direct access to the target objects (e.g., sch.my_table
, tbl.id
), but still, writting .relations
and .columns
often sucks.
Proposal
Having an alias r
of relations
in schema, and an alias c
of columns
in relations. E.g.,
# assuming a certain schema sch
rel = sch.r.my_table
col = rel.c.id
Feature 2: Dynamic access of store elements by unambiguous names
Current state
# assuming a certain schema sch
table_name = "my_table"
rel = getattr(sch.relations, table_name)
col_name = "id"
col = getattr(rel.columns, col_name)
Fact
There are good reasons for not getting elements by name, through, e.g., rel = sch.relations[table_name]
or col = rel.columns[col_name]
, but still, having to use getattr
sucks.
Proposal 1
Add a mapping attribute to stores, that provides a view on unambiguously keyed (named) elements.
# assuming a certain schema sch
table_name = "my_table"
rel = sch.relations._[table_name]
col_name = "id"
col = rel.columns._[col_name]
I'm open to discussion about the attribute name (here, _
). The point is not to shadow an element that could have the same name (e.g., sch.relations._
is never the relation named _
, but the name-to-relation mapping; to get a relation named _
, one will have to do sch.relations._["_"]
which seems to be acceptable).
Proposal 2
Make stores callable.
# assuming a certain schema sch
table_name = "my_table"
rel = sch.relations(table_name)
col_name = "id"
col = rel.columns(col_name)
The stores could also accept an optional second parameter, to give a default value, if the name does not belong to the set of unambiguous names of the store, e.g.,
col = rel.columns(col_name, rel.columns[0])
# or
col = rel.columns(col_name, default=rel.columns[0])
Feature 3: Easily getting sub-sequences of elements of stores
Actually, the use case would be for columns of relations.
Current state
# assuming rel is a suitable relation
sq = rel.select(columns=(rel.columns[0], rel.columns[2], rel.columns[1], rel.columns[4]))
Proposal
Making __getitem__
of stores accept list of indices.
# assuming rel is a suitable relation
sq = rel.select(columns=rel.columns[[0, 2, 1, 4]])
I guess the best way is to have rel.columns[[0, 2, 1, 4]]
return a store (of type type(rel.columns)
) with the selected elements in order.
Combined with the above Feature 2, one would expect to allow this, as well:
# assuming rel is a suitable relation
sq = rel.select(columns.rel.columns._[["id", "target", "source", "value"]])