QueryTool documentation

This section describes the querytool class for querying nodes with an easy Python interface.

class aiida.orm.querytool.QueryTool[source]

Class to make easy queries without extensive knowledge of SQL, Django and/or the internal storage mechanism of AiiDA.

Note

This feature is under constant development, so the name of the methods may change in future versions to allow for increased querying capabilities.

Todo

missing features:

  • add __in filter
  • allow __in filter to accept other querytool objects to perform a single query
  • implement searches through the TC table
  • document the methods
  • allow to get attributes of queried data via a single query with suitable methods
  • add checks to verify whether filters as <=, ==, etc are valid for the specified data type (e.g., __gt only with numbers and dates, ...)
  • probably many other things...
add_attr_filter(key, filtername, value, negate=False, relnode=None, relnodeclass=None)[source]

Add a new filter on the value of attributes of the nodes you want to query.

Parameters:
  • key – the value of the key
  • filtername – the type of filter to apply. Multiple filters are supported (depending on the type of value), like ‘<=’, ‘<’, ‘>’, ‘>=’, ‘=’, ‘contains’, ‘iexact’, ‘startswith’, ‘endswith’, ‘istartswith’, ‘iendswith’, ... (the prefix ‘i’ means “case-insensitive”, in the case of strings).
  • value – the value of the attribute
  • negate – if True, add the negation of the current filter
  • relnode – if specified, asks to apply the filter not on the node that is currently being queried, but rather on a node linked to it. Can be “res” for output results, “inp.LINKNAME” for input nodes with a given link name, “out.LINKNAME” for output nodes with a given link name.
  • relnodeclass – if relnode is specified, you can here add a further filter on the type of linked node for which you are executing the query (e.g., if you want to filter for outputs whose ‘energy’ value is lower than zero, but only if ‘energy’ is in a ParameterData node).
add_extra_filter(key, filtername, value, negate=False, relnode=None, relnodeclass=None)[source]

Add a new filter on the value of extras of the nodes you want to query.

Parameters:
  • key – the value of the key
  • filtername – the type of filter to apply. Multiple filters are supported (depending on the type of value), like ‘<=’, ‘<’, ‘>’, ‘>=’, ‘=’, ‘contains’, ‘iexact’, ‘startswith’, ‘endswith’, ‘istartswith’, ‘iendswith’, ... (the prefix ‘i’ means “case-insensitive”, in the case of strings).
  • value – the value of the extra
  • negate – if True, add the negation of the current filter
  • relnode – if specified, asks to apply the filter not on the node that is currently being queried, but rather on a node linked to it. Can be “res” for output results, “inp.LINKNAME” for input nodes with a given link name, “out.LINKNAME” for output nodes with a given link name.
  • relnodeclass – if relnode is specified, you can here add a further filter on the type of linked node for which you are executing the query (e.g., if you want to filter for outputs whose ‘energy’ value is lower than zero, but only if ‘energy’ is in a ParameterData node).
create_attrs_dict()[source]

Return a dictionary of the raw data from the attributes associated to the queried nodes.

create_extras_dict()[source]

Return a dictionary of the raw data from the extras associated to the queried nodes.

get_attributes()[source]

Get the raw values of all the attributes of the queried nodes.

limit_pks(pk_list)[source]

Limit the query to a given list of pks.

Parameters:pk_list – the list of pks you want to limit your query to.
run_query(with_data=False, order_by=None)[source]

Run the query using the filters that have been pre-set on this class, and return a generator of the obtained Node (sub)classes.

Parameters:order_by – if specified, order by the given field
set_class(the_class)[source]

Pass a class to filter results only of a specific Node (sub)class, and its subclasses.

set_group(group, exclude=False)[source]

Filter calculations only within a given node. This can be called multiple times for an AND query.

Todo

Add the possibility of specifying the group as an object rather than with its name, so that one can also query special groups, etc.

Todo

Add the possibility of specifying “OR” type queries on multiple groups, and any combination of AND, OR, NOT.

Parameters:
  • group – the name of the group
  • exclude – if True, excude results