Package: mlalchemy
OverviewMLAlchemy is a Python-based utility library aimed at allowing relatively
safe conversion from YAML/JSON to SQLAlchemy read-only queries. One use
case here is to allow RESTful web applications (written in Python) to
receive YAML- or JSON-based queries for data, e.g. from a front-end
JavaScript-based application.The name “MLAlchemy” is an abbreviation for “Markup Language for
SQLAlchemy”.InstallationInstallation via PyPI:>pipinstallmlalchemyQuery ExamplesTo get a feel for what MLAlchemy queries look like, take a look at the
following.Note: All field names are converted fromcamelCaseorkebab-casetosnake_caseprior to query execution.Example YAML QueriesFetching all the entries from a table calledUsers:from:UsersLimiting the users to only those with the last name “Michaels”:from:Userswhere:last-name:MichaelsA more complex YAML query:from:Userswhere:$or:last-name:Michaelsfirst-name:Michael$gt:date-of-birth:1984-01-01The raw SQL query for the above would look like:SELECT*FROMusersWHERE(last_name="Michaels"ORfirst_name="Michael")AND(date_of_birth>"1984-01-01")Example JSON QueriesThe same queries as above, but in JSON format. To fetch all entries in
theUserstable:{"from":"Users"}Limiting the users to only those with the last name “Michaels”:{"from":"Users","where":{"lastName":"Michaels"}}And finally, the more complex query:{"from":"Users","where":{"$or":{"lastName":"Michaels","firstName":"Michael"},"$gt":{"dateOfBirth":"1984-01-01"}}}UsageA simple example of how to use MLAlchemy:fromsqlalchemyimportcreate_engine,Column,Integer,String,Datefromsqlalchemy.ext.declarativeimportdeclarative_basefromsqlalchemy.ormimportsessionmakerfrommlalchemyimportparse_yaml_query,parse_json_queryBase=declarative_base()classUser(Base):__tablename__="users"id=Column(Integer,primary_key=True)first_name=Column(String)last_name=Column(String)date_of_birth=Column(Date)# use an in-memory SQLite database for this exampleengine=create_engine("sqlite:///:memory:")Base.metadata.create_all(engine)Session=sessionmaker(bind=engine)session=Session()# add a couple of dummy usersuser1=User(first_name="Michael",last_name="Anderson",date_of_birth=date(1980,1,1))user2=User(first_name="James",last_name="Michaels",date_of_birth=date(1976,10,23))user3=User(first_name="Andrew",last_name="Michaels",date_of_birth=date(1988,8,12))session.add_all([user1,user2,user3])session.commit()# we need a lookup table for MLAlchemytables={"User":User}# try a simple YAML-based query firstall_users=parse_yaml_query("from: User").to_sqlalchemy(session,tables).all()print(all_users)# same query, but this time in JSONall_users=parse_json_query("""{"from": "User"}""").to_sqlalchemy(session,tables).all()print(all_users)# a slightly more complex queryyoung_users=parse_yaml_query("""from: User
where:
    $gt:
        date-of-birth: 1988-01-01
""").to_sqlalchemy(session,tables).all()print(young_users)Query Language SyntaxAs mentioned before, queries can either be supplied in YAML format or in
JSON format to one of the respective parsers.fromAt present, MLAlchemy can only support selecting data from a single
table (multi-table support is planned in future). Here, thefromparameter allows you to specify the name of the table from which to
select data.whereThewhereparameter defines, in hierarchical fashion, the structure
of the logical query to perform. There are 3 kinds of key types in the
JSON/YAML structures, as described in the following table.KindDescriptionOptionsOperatorsLogical (boolean)
operators for combining
sub-clauses$and,$or,$notComparatorsComparative operators for
comparing fields to values$eq,$gt,$gte,$lt,$lte,$like,$neq,$in,$nin,$isField NamesThe name of a field in thefromtable(Depends on table)order-by(YAML) ororderBy(JSON)Provides the ordering for the resulting query. Must either be a single
field name or a list of field names, with the direction specifier in
front of the field name. For example:# Order by "field2" in ascending orderorder-by:field2Another example:# Order by "field2" in *descending* orderorder-by:"-field2"A more complex example:# Order first by "field1" in ascending order, then by "field2" in# descending orderorder-by:-field1-"-field2"offsetSpecifies the number of results to skip before providing results. If not
specified, no results are skipped.limitSpecifies the maximum number of results to return. If not specified,
there will be no limit to the number of returned results.Query ExamplesExample 1: Simple QueryThe following is an example of a relatively simple query in YAML format:from:SomeTablewhere:-$gt:field1:5-$lt:field2:3order-by:-field1offset:2limit:10This would translate into the following SQLAlchemy query:fromsqlalchemy.sql.expressionimportand_session.query(SomeTable).filter(and_(SomeTable.field1>5,SomeTable.field2<3))\.order_by(SomeTable.field1)\.offset(2)\.limit(10)Example 2: Slightly More Complex QueryThe following is an example of a more complex query in YAML format:from:SomeTablewhere:-$or:field1:5field2:something-$not:$like:field3:"else%"This would translate into the following SQLAlchemy query:fromsqlalchemy.sql.expressionimportand_,or_,not_session.query(SomeTable)\.filter(and_(or_(SomeTable.field1==5,SomeTable.field2=="something"),not_(SomeTable.field3.like("else%"))))Example 3: Complex JSON QueryThe following is an example of a relatively complex query in JSON
format:{"from":"SomeTable","where":[{"$or":[{"field1":10},{"$gt":{"field2":5}}],"$and":[{"field3":"somevalue"},{"field4":"othervalue"},{"$or":{"field5":5,"field6":6}}]}],"orderBy":["field1","-field2"],"offset":2,"limit":10}This query would be translated into the following SQLAlchemy code:fromsqlalchemy.sql.expressionimportand_,or_,not_session.query(SomeTable)\.filter(and_(or_(SomeTable.field1==10,SomeTable.field2>5),and_(SomeTable.field3=="somevalue",SomeTable.field4=="othervalue",or_(SomeTable.field5==5,SomeTable.field6==6))))\.order_by(SomeTable.field1,SomeTable.field2.desc())\.offset(2)\.limit(10)LicenseThe MIT License (MIT)Copyright (c) 2017 Thane ThomsonPermission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
“Software”), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.