Skip to content

Commit

Permalink
Implemented cursor['attrs']. Refs: #114
Browse files Browse the repository at this point in the history
  • Loading branch information
spanezz committed Nov 22, 2018
1 parent 812d955 commit 20ae5b2
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
2 changes: 2 additions & 0 deletions python/cursor-access.in.cc
Expand Up @@ -46,6 +46,7 @@ PyObject* enqpy(db::CursorStationData& cur, const char* key, unsigned len)
case "station": return dbstation_to_python(c->cur->station);
case "var": return (PyObject*)throw_ifnull(wrpy->var_create_copy(*c->cur->value));
case "context_id": return dballe_int_to_python(c->cur->value.data_id);
case "attrs": return attrs_to_python(*c->cur->value);
default: PyErr_Format(PyExc_KeyError, "key %s not found", key); throw PythonException();
}
} else {
Expand Down Expand Up @@ -86,6 +87,7 @@ PyObject* enqpy(db::CursorData& cur, const char* key, unsigned len)
case "level": return level_to_python(c->get_levtr().level);
case "trange": return trange_to_python(c->get_levtr().trange);
case "var": return (PyObject*)throw_ifnull(wrpy->var_create_copy(*c->cur->value));
case "attrs": return attrs_to_python(*c->cur->value);
case "context_id": return dballe_int_to_python(c->cur->value.data_id);
default: PyErr_Format(PyExc_KeyError, "key %s not found", key); throw PythonException();
}
Expand Down
12 changes: 12 additions & 0 deletions python/test-db.py
Expand Up @@ -322,6 +322,18 @@ def test_import_importerfile(self):
self.db.import_messages(importer.from_file(fp))
self.assertEqual(self.db.query_data({}).remaining, 371)

def test_query_attrs(self):
# See #114
with self.db.query_data({"var": "B01011"}) as cur:
self.assertEqual(cur.remaining, 1)
self.assertEqual(cur["var"].code, "B01011")
self.assertCountEqual(cur["attrs"], [])

with self.db.query_data({"var": "B01011", "query": "attrs"}) as cur:
self.assertEqual(cur.remaining, 1)
self.assertEqual(cur["var"].code, "B01011")
self.assertCountEqual((repr(x) for x in cur["attrs"]), ["Var('B33007', 50)", "Var('B33036', 75)"])


class FullDBTestMixin(CommonDBTestMixin):
def test_transaction_enter_exit(self):
Expand Down
9 changes: 9 additions & 0 deletions python/types.cc
Expand Up @@ -1079,6 +1079,15 @@ void set_values_from_python(Values& values, wreport::Varcode code, PyObject* val
template void set_values_from_python(Values& values, wreport::Varcode code, PyObject* val);
template void set_values_from_python(DBValues& values, wreport::Varcode code, PyObject* val);

PyObject* attrs_to_python(const wreport::Var& var)
{
pyo_unique_ptr list(PyList_New(0));
for (const wreport::Var* a = var.next_attr(); a; a = a->next_attr())
if (PyList_Append(list, (PyObject*)wrpy->var_create_copy(*a)) == -1)
throw PythonException();
return list.release();
}

void add_var_to_dict(PyObject* dict, const wreport::Var& var)
{
char bcode[7];
Expand Down
3 changes: 3 additions & 0 deletions python/types.h
Expand Up @@ -159,6 +159,9 @@ extern template void set_values_from_python(DBValues& values, wreport::Varcode c

std::set<wreport::Varcode> varcodes_from_python(PyObject* o);

/// Return a list with all the attributes in var [Var]
PyObject* attrs_to_python(const wreport::Var& var);

void add_var_to_dict(PyObject* dict, const wreport::Var& var);

void query_setpy(core::Query& query, const char* key, unsigned len, PyObject* val);
Expand Down

0 comments on commit 20ae5b2

Please sign in to comment.