Querying

sir.querying.iter_bounds(db_session, column, batch_size, importlimit)[source]

Return a list of (lower bound, upper bound) tuples which contain row ids to iterate through a table in batches of batch_size. If importlimit is greater than zero, return only enough tuples to contain importlimit rows. The second element of the last tuple in the returned list may be None. This happens if the last batch will contain less than batch_size rows.

Parameters:
Return type:

[(int, int)]

sir.querying.iterate_path_values(path, obj)[source]

Return an iterator over all values for path on obj, an instance of a declarative class by first splitting the path into its elements by splitting it on the dots, resulting in a list of path elements. Then, for each element, a call to getattr() is made - the arguments will be the current model (which initially is the model assigned to the SearchEntity) and the current path element. After doing this, there are two cases:

  1. The path element is not the last one in the path. In this case, the getattr() call returns one or more objects of another model which will replace the current one.
  2. The path element is the last one in the path. In this case, the value returned by the getattr() call will be returned and added to the list of values for this field.

To give an example, lets presume the object we’re starting with is an instance of Artist and the path is “begin_area.name”. The first getattr() call will be:

getattr(obj, "begin_area")

which returns an Area object, on which the call:

getattr(obj, "name")

will return the final value:

>>> from mbdata.models import Artist, Area
>>> artist = Artist(name="Johan de Meij")
>>> area = Area(name="Netherlands")
>>> artist.begin_area = area
>>> list(iterate_path_values("begin_area.name", artist))
['Netherlands']

One-to-many relationships will of course be handled as well:

>>> from mbdata.models import Recording, ISRC
>>> recording = Recording(name="Fortuna Imperatrix Mundi: O Fortuna")
>>> recording.isrcs.append(ISRC(isrc="DEF056730100"))
>>> recording.isrcs.append(ISRC(isrc="DEF056730101"))
>>> list(iterate_path_values("isrcs.isrc", recording))
['DEF056730100', 'DEF056730101']