Biocatch Pred
Biocatch Pred
Your assignment is to implement a python module that can accept and parse
predicates, and can then test these predicates against python objects.
Focus
For this assignment, focus on code quality as well as functionality. Your code
should be readable and easily extendible.
• Use type annotations on all functions
• You don’t need to add a docstring to every function, but it should be clear
from the function signature what each function is doing.
• Add comments to pieces of your code that you think aren’t self-explanatory.
• You don’t need to add tests, but your code should be easily testable.
• If you see something that should be done but you don’t have time to do,
you can add a #todo comment, explaining what needs to be done.
• Keep performance in mind. Don’t sacrifice readability for optimizations,
but your code should be efficient.
• If you use GenAI to help you implement this code, please add the
entire conversation you had with the AI to the files you submit!
Main Product
Implement a function that can parse a json string into a predicate. A predicate
always has one feature and one operation to test against it. A predicate will be
of the following format:
Predicate Format
A predicate will be of the format:
Predicate: {
"feature": <string>
"operation": <Operation>
}
Operation: {
"operator": <UnaryOperator>
}
OR {
"operator": <BinaryOperator>
"operand": Any
}
OR {
"operator": <GroupOperator>
"operations": list[<Operation>]
1
}
Supported Operations
Your module should support the following operators:
• isNone: Will return true if the feature is the singleton None.
• isNotNone: Will return true if the feature is not the singleton None.
• eqTo: Will return true if the feature is equal to the operand.
• notEqTo: Will return true if the feature is not equal to the operand.
• isLessThan: Will return true if the feature is strictly less than the operand.
• isGreaterThan: Will return true if the feature is strictly greater than the
operand.
• and: Will return true if all of the given operations return true for the
feature.
• or: Will return true if any of the given operations return true for the
feature.
If any operation raises an exception, a log should be sent, and the operation
2
should be treated as though it returned false.
Feature Extraction
A feature specification should always be of the form:
(.<attribute>)*
Where <attribute> is a non-empty combination of letters, digits, and underlines,
that does not begin with an underline or digit. Each attribute accessor operates on
the result of the previous accessor. The following are valid feature specifications:
• .x
• .x.y.z
• .a_b12_
• .x.z
• (an empty string, means to test the root directly)
If a feature does not exist, the predicate should fail (i.e. act as though its
operation returned False).
Implement a function that can test a predicate against a given object (called
the “root”). The function should fetch the value of the feature from the root
object, and return whether the predicate’s operation return true.
You should implement the following API:
class Predicate:
@classmethod
def from_json(cls, json_string: str)->Predicate:
...
# Example usage:
from types import SimpleNamespace
predicate = Predicate.from_json(j_str)
ns1 = SimpleNamespace(x=SimpleNamespace(y=5))
ns2 = SimpleNamespace(x=SimpleNamespace(y=3))
ns3 = SimpleNamespace(z=SimpleNamespace(y=5))
assert predicate.evaluate(ns1)
assert not predicate.evaluate(ns2)
assert not predicate.evaluate(ns3)
3
# Another Example
@dataclass
class User:
name: str
level: int
@dataclass
class Game:
user: User
g = Game(user=User(name="bob", level=6))
4
@property
def predicate(self) -> Predicate:
...
Note that:
• The endpoint supports the Etag header in its response, as well as the
If-None-Match header in its requests.
• The HTTP request should be asynchronous (with async/await).