Draft

Querysets

Description:

You are about to write a QuerySet-like object known from Django. Obviously, it is not that easy to implement and test a real database on codewars, but this kata is more about class magic methods and general filtering concept, than it is about databases.

You have a Person model with the following fields:

  • first_name (string),
  • last_name (string),
  • age (int)

The Person model defined a objects property, which returns your ResultSet class instance. That ResultSet represents data fetched from the "database" (in this example this is just a list of instances, but hey, that's all I can do).

You have to define the following methods:

  • first(), returning the first entry from the database
  • last(), returning the last entry from the database
  • filter(), returning a filtered "list" (we'll get to that later) of results. If an argument is None it should not be filtered.
  • get() returning the first element matched. It should raise a ValueError if more than one entry is found, and a TypeError if no entries are found in the database.
  • exists() returning True if at least one matching record is found.

The filter() and get() methods should accept the following optional arguments:

  • first_name
  • last_name
  • age

If any of the above is ommitted it should not be filtered.

Now, for the tricky part:

  • filter() calls should be chainable. It should be possible to either use .filter(first_name='A', age=20) or .filter(first_name='A').filter(age=20).
  • get(), first() and last() should return a single Person instance.
  • .exists() should be possible to get called on any ResultSet instance, wheter it is filtered or not.
  • It should be possible to iterate over a ResultSet to read all its results.
  • It should be able to ask for a ResultsSet item like from a list, to get the n-th result.
Fundamentals

Stats:

CreatedMay 4, 2016
Warriors Trained50
Total Skips1
Total Code Submissions387
Total Times Completed13
Python Completions13
Total Stars3
% of votes with a positive feedback rating30% of 5
Total "Very Satisfied" Votes1
Total "Somewhat Satisfied" Votes1
Total "Not Satisfied" Votes3
Total Rank Assessments5
Average Assessed Rank
6 kyu
Highest Assessed Rank
5 kyu
Lowest Assessed Rank
6 kyu
Ad
Contributors
  • samupl Avatar
  • FArekkusu Avatar
Ad