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 databaselast()
, returning the last entry from the databasefilter()
, 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()
andlast()
should return a singlePerson
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.
Stats:
Created | May 4, 2016 |
Warriors Trained | 50 |
Total Skips | 1 |
Total Code Submissions | 387 |
Total Times Completed | 13 |
Python Completions | 13 |
Total Stars | 3 |
% of votes with a positive feedback rating | 30% of 5 |
Total "Very Satisfied" Votes | 1 |
Total "Somewhat Satisfied" Votes | 1 |
Total "Not Satisfied" Votes | 3 |
Total Rank Assessments | 5 |
Average Assessed Rank | 6 kyu |
Highest Assessed Rank | 5 kyu |
Lowest Assessed Rank | 6 kyu |