up:: [[PynamoDB MOC]] # Filter Conditions in PynamoDB Filter conditions in PynamoDB are objects of the class pynamodb.expressions.condition.Comparison. For testing, you can get the string representation by casting it to a string, and I can use that for matching. ## Example ### Creating a condition ```python condition = None if source: condition &= Model.source == source if time_limit: time_limit_min = datetime.timestamp(datetime.now() - timedelta(days=time_limit)) condition &= time_limit_min <= Model.last_updated ``` In PynamoDB, filter objects overload common Python logic operators for translation into DynamoDB's conditional filter operators. You can find the translation table [here](https://pynamodb.readthedocs.io/en/latest/conditional.html). ### Testing condition objects by stringifying ```python filter_string = str( filter_condition ) if limit and filter_string == "source = {'S': 'test_source'}": do_something() elif filter_string == "source = {'S': 'test_source'}": do_something_else() elif filter_string.startswith( "(source = {'S': 'test_source'} AND last_updated >= {'N':" ): do_something_entirely_different() elif filter_string.startswith("last_updated >= {'N':"): do_more() ```