Package: logics-py
LogicsA multi-platform, sandboxed and extendable expression language with a Python-like syntax.AboutLogics is a formula language with the aim of flexibly implementing programmable logics such as conditions, calculations or validations, so that developers as well as administrators and experienced users can use such expressions within a framework specified by the system to influence, calculate or otherwise dynamically adapt certain processes of a system.The language can be compared toExcel's formula language, where expressions can be defined based on variables and calculated dynamically. But Logics has a Python-like syntax at this point, but provides its own semantics, so that numbers and strings, for example, can be directly linked, None-values are assumed as 0, and Exceptions are generally not raised during execution.In addition, Logics provides a sandbox environment, which does not allow potential attackers to break out of the provided environment or slow down the system. There are appropriate security mechanisms integrated into the language for this purpose.Logics has been developed atMausbrand Informationssysteme GmbHas part of theViUR ecosystem, but it is a standalone project that can also be used outside the ViUR context.Previous uses of Logics includedevent-based state evaluationdynamic rule systemsprogrammable conditions for filterstemplate engine generating text modulesquestionnaires with queries that depend on previous answersUsageSince Logics is used on both the client and server side, the language has been implemented in two separate implementations:logics-jsis a pure (vanilla) JavaScript implementation of Logics provided as npm-package.logics-pyis a pure Python 3.10 implementation of Logics provided as PyPI-package, with no other dependencies.Both packages are under recent development and not stable right now. They are maintained in separate version numbers, which is planned to be changed soon, when they become almost feature-complete.Using Logics in JavaScript:// npm install logics-jsimportLogicsfrom"logics-js";letlogics=newLogics("a + 2 * 3 + b");console.log(logics.run({a:1,b:"-Logics"}).toString());// "7-Logics"Using Logics in Python:# pip install logics-pyfromlogicsimportLogicslogics=Logics("a + 2 * 3 + b")print(logics.run({"a":1,"b":"-Logics"}))# "7-Logics"FeaturesSecure, native, running in a sandboxed environment apart from the host languageDisallows variable assignment, except in comprehensionslogics-js: Implementation in JavaScriptlogics-py: Implementation in PythonPython-inspired syntax and semanticsMake use of all standard operatorsUnary+,-,~,notBinary+,*,-,/,//,**Comparison==,!=,<>,<,<=,>,>=,in,not inLogicaland,orConditionsy if x else zComprehensions[x for x in y if z]Slicesx[:]Attribute accessx[y]# commentsin separate linesDedicated Value object abstraction of native types forNoneTrue,Falseint,float,strlistfor lists (similar to arrays)dictfor dicts (similar to structured objects)Provides a set of functions that can be used in expressionsExtendable to custom functionsEmbeddable into other languagesLogicsvs.PythonLogics does look like Python, but it isn't Python!It differs both in syntax and semantics:Expressions can be used with arbitrary whitespace and line-breaksThere are no methods on objects, but functions that work on values, e.g.Python'sdict.keys()iskeys(dict)in LogicsPython's";".join(["a", "b", "c"])isjoin(["a", "b", "c"], ";")in LogicsNo exceptions during runtime, but a defined behaviorAccess to e.g. invalid index or key just returnsNoneAccess of undefined variables returnsNoneDivision by zero returns string"#ERR: Division by zero"Dynamic and automatic value conversione.g. the content of strings is automatically converted when used in calculations,
so"42" ** 3produces 74088, and not a TypeError.Building & PackagingLogics is built using theUniCC LALR(1) Parser Generator, which supports generating parsers in multiple target languages. UniCC should be compiled from source, as the latest version 1.8 is required.Whenever something is changed on the syntax, ensureuniccis installed properly and runmake, which regenerates the parser modules.Shared test casesLogics-related test cases are placed as*.lgx-files into thetests/folder. They are shared by both implementations.# In lgx-files, variables can be set using `#set:` as shown below.
# The part behind the second colon may contain any logics expression, including variables.
#set:x:6 * 7

#
# To verify if an expression produces an expected result, provide the expression followed by an `#expect:`.
#

x
#expect:42

x * x
#expect:1764logics-jsFormattingTo format the source code, useprettieras follows:cdlogics-js
npmrunfmtTestsTests are implemented usingMocha:cdlogics-js
npmtestPackagingPublish onnpmjs.com:cdlogics-js
npmpublishlogics-pyFormattingTo format the source code, useBlackas follows:cdlogics-py
pipenvrunfmtTestsTests are implemented usingpytest:cdlogics-py
pipenvruntestPackagingPublish onPyPI:cdlogics-py
pipenvinstall--dev
pipenvrunbuild
pipenvrunpublish
pipenvrunclean
pipenv--rmLicenseCopyright © 2023 by Mausbrand Informationssysteme GmbH.Mausbrand and ViUR are registered trademarks of Mausbrand Informationssysteme GmbH.Logics is free software under the MIT license.Please see the LICENSE file for details.