Package: j1bz.expression
SummaryDescriptionCrudity expressions, cool ! But… what is it ?InstallationHow-toQuickstartExamples of expressionsCREATEREADUPDATEDELETEAdvanced how-toUsing a custom grammarInterpreter vs interpretException handlingDescriptionExpression is a Domain Specific Language (DSL) designed to express CRUD queries
forb3j0f.crudity(https://github.com/b3j0f/requester/).In other words, you can write Crudity Expressions.Crudity expressions, cool ! But… what is it ?This package has been designed to work in strong coupling withb3j0f.requester, so you should know what it is before continuing.Inb3j0f.requester, everything is anExpression(E) or aFunction(F). If you want to compareaandbyou need to write
:>>>F('>',params=[E.a,E.b])A programmer can manipulate those nested function calls with no problem, but
people with less technical background will find difficulties doing it.j1bz.expressionaims to makeb3j0f.requestercalls more accessible. The
syntax is highly inspired from SQL, because any IT guy should have seen at
least a few SQL queries and should not be lost :>>>interpret("SELECT ALL WHERE (a > b);")The syntax is more restrictive than what nativeb3j0f.requesterallows you
to do (main needs are covered). Thus, users are incentived to write things that
make sense.j1bz.expression.intepreter.interpretreturn ab3j0f.requester.request.crud.{create.Create,read.Read,update.Update,delete.Delete}serialized object (depending on the query). The object can then be processed by
any driver.Installationaptitudeinstallgitvirtualenvvirtualenvvenvsourcevenv/bin/activategitclonehttps://github.com/b3j0f/requestercdrequesterpythonsetup.pyinstallpipinstallj1bz.expressionexpression-cliHow-toQuickstart>>>fromj1bz.expression.interpreterimportinterpret>>>res=interpret("SELECT a;")>>>print(repr(res))SELECTaNote:j1bz.expression.exceptions.ParseErrorshould be the only
exception you have to catch when invoking interpret function as is.Examples of expressionsCREATEINSERTVALUESk:v;INSERTVALUESk1:v1,k2:v2;INSERTINTOiVALUESk:v;INSERTVALUESk:v;ASiNote:CREATEis a synonym ofINSERT. It means every time you can
useINSERTyou could have usedCREATEinstead (for semantics in some
cases).CREATEVALUESk:v;READSELECTALL;SELECTs;SELECTsWHEREw;SELECTsGROUPBYg;SELECTsORDERBYo;SELECTsLIMIT10;SELECTs;ASmysSELECTsWHEREwhGROUPBYgORDERBYoLIMIT10;ASmysSELECTa,b,c;SELECTf();SELECTf(a,b,c);SELECTsWHERE(a);SELECTsWHERE(a=b);SELECTsWHERE(a!=b);SELECTsWHERE(a>b);SELECTsWHERE(a>=b);SELECTsWHERE(a<b);SELECTsWHERE(a<=b);SELECTsWHERE(aINb);SELECTsWHERE(aNINb);SELECTsWHERE(aLIKEb);SELECTsWHERE(aANDb);SELECTsWHERE(aORb);SELECTsWHERE(aOR(bANDc));SELECTsORDERBYo1,o2;SELECTsORDERBYo1DESC,o2,o3ASC;Note:READis a synonym ofSELECT.READALL;UPDATEUPDATEVALUESk:v;UPDATEVALUESk:vWHEREw;UPDATEVALUESk:v;ASmyuUPDATEINTOuVALUESk:v;UPDATEINTOuVALUESk:vWHEREw;UPDATEINTOuVALUESk1:v1,k2:v2;DELETEDELETEd;DELETEd1,d2,d3;DELETEdWHEREw;DELETEd1,d2,d3WHEREw;DELETEd;ASmydNote: Expression uses Grako (https://pypi.python.org/pypi/grako) to
generate a parser from a grammar defined in{{ PACKAGE}}/etc/j1bz/expression/grammar.bnf. You can read this bnf
description to check for all available possibilities.Advanced how-toIf you want to integratej1bz.expressionin your code to do more than justinterpret()calls, this part is for you.Using a custom grammarThe engine poweringj1bz.expressionisgrako. Grako (for Grammar
Kompiler) take a bnf-derivated grammar file in input and generates a parser in
python code.This package embeds a grammar file and a pre-compiled parser generated from
this grammar ({{ PACKAGE}}/etc/j1bz/expression/grammar.bnf).It is possible to change this grammar and use a parser generated at runtime for
yourinterpret()calls :>>>interpret("SELECT a;","/PATH/TO/grammar.bnf")Note: Once you’ve calledinterpret()for the first time (with or
without grammar file), the same parser used for the first time will be used
later (due to a singleton mechanic).Interpreter vs interpretinterpret()uses a singleton mechanic to provide a shortcut.If you want to do more complex things, you can use thej1bz.expression.interpreter.Interpreterclass.Examples :>>>i1=Interpreter(parser=mygrakoparser,walker=mycustomwalker)>>>i1.interpret("SELECT a;")>>>i2=Interpreter(pkwargs={'rule_name':'condition'})>>>i2.interpret("(a > b)")>>>i3=Interpreter(grammar_file='/PATH/TO/grammar.bnf')>>>i3.interpret("SELECT_ALIAS a;")Note:grammar_fileargument will be used only if you did not provide a
custom parser viaparserargument.Exception handlingj1bz.expression.exceptionscurrently defines 2 exceptions :ParserGenerationError: Raised when you provide a custom grammar in
order to generate a   parser, but generation failed (your grammar is not
correct !).ParseError: Raised when an expression given tointerpret()is not
parsable.Some other exceptions (IOError,OSError,FileNotFoundError) can
also be raised when trying to compile a custom parser if the grammar file is
not readable, does not exist, etc.If you’re only usinginterpret()shortcut, you should handle all named
exceptions above.If you’re usingInterpreterclass, you need to handleParserGenerationError(and file related exceptions) when you instantiate it
andParseErrorwhen you makeinterpret()calls.