Package: rest-filter
rest-filterrest-filter aims to ba a light weight fast parser written in pure Python, meant for use in REST API servers.
It was inspired by theOData standard, but this query is a bit simpler and less feature rich.GoalsAn easily extendable querying syntaxBe able to query any data source by writing a simple translatorUsable in any Python REST frameworkInstallationpipinstallrest-filterExample usageHere is an example of the most basic usagefromrest_filter.encoder.commonimportencoderfromrest_filter.translatorimportget_mongo_translator# The raw input expressionexpression=r"((age gt 20) and (age ge 10))"# Generate the encoded expressionencoded_expression=encoder.encode_expression(expression)print(encoded_expression)# Translate into a mongo querymongo_query=get_mongo_translator().translate(encoded_expression)print(mongo_query)The resulting encoded expression looks like this:And(operands=[GraterThan(field=Field(name='age'),value=20.0),GraterEqualsTo(field=Field(name='age'),value=10.0)])And the resulting mongo query looks like this:{'$and':[{'age':{'$gt':20.0}},{'age':{'$ge':10.0}}]}This uses the default pre built expression encoding elements & the pre build
translation elements. you can add additional elements like this:importrefromrest_filter.encoder.commonimportencoder,Andfromrest_filter.encoderimportBinaryOperationfromrest_filter.translatorimportget_mongo_translatormongo_translator=get_mongo_translator()encoder.register_binary_logical_operator('&&')(And)@encoder.register_ratio('contains')classContains(BinaryOperation):pass@mongo_translator.register_binary_operation_translator(Contains)deftranslate_equals(operation:Contains):return{operation.field.name:f'/{re.escape(operation.value)}/'}# The raw input expressionexpression=r"((age gt 20) && (name contains 'a string with special **^^ chars'))"# Generate the encoded expressionencoded_expression=encoder.encode_expression(expression)print(encoded_expression)# Translate into a mongo querymongo_query=get_mongo_translator().translate(encoded_expression)print(mongo_query)If you want you can forgo the pre loaded encoder & translator objects and create ones of your one with completely custom elements:fromrest_filter.encoderimportEncoderfromrest_filter.encoderimportBooleanOperation,BinaryOperationfromrest_filter.translatorimportget_mongo_translator,Translatorencoder=Encoder()# Register encoders@encoder.register_binary_logical_operator('and')classAnd(BooleanOperation):pass@encoder.register_ratio('eq')classEquals(BinaryOperation):pass@encoder.register_type(r'-?\d+')defnumber(text:str):returnint(text)mongo_translator=get_mongo_translator()# Register translators@mongo_translator.register_boolean_operation_translator(And)deftranslate_and(operation:And,translator:Translator):return{'$and':[translator.translate(operand)foroperandinoperation.operands]}@mongo_translator.register_binary_operation_translator(Equals)deftranslate_equals(operation:Equals):return{operation.field.name:{'$eq':operation.value}}expression="((age eq 3) and (height eq 18))"# Generate the encoded expressionencoded_expression=encoder.encode_expression(expression)print(encoded_expression)# Translate into a mongo querymongo_query=mongo_translator.translate(encoded_expression)print(mongo_query)LimitationsExpressions can only be parentheses complete (for now).
For example, these expressions are not allowed:age gt 10 and name eq 'eric' and city eq 'tlv'(age gt 10 and name eq 'eric' and city eq 'tlv')(age gt 10) and (name eq 'eric') and (city eq 'tlv')But this expression is valid:(((age gt 10) and (name eq 'eric')) and (city eq 'tlv'))You cannot register new value types (for now), you must use the prebuilt boolean, string or number valuesEncoder Translator designThis utility is separated into two parts: Encoder & TranslatorEncoderThe encoder is responsible for parsing the input string into a structured expression object,
that can be easily read & parsed by codeThe encoder can encodes grammar elements that are registered to it. each registered grammar is a
ratio between a field & a value, or a logical operation between two logical values or expressionsTranslatorThe translator translates the generic expression format given from the encoder into a target language.There is a basic mongo translator built into this package (more yet to come), but you can easily write
a translator of your own.ContributorsSagiv Oulu