Giter VIP home page Giter VIP logo

pya2l's Introduction

branch build coverage
master tests status Build status code coverage
develop tests status Build status code coverage

License Gitter

package description

the purpose of this package is to provide an easy way to access and navigate in a2l formatted file.
once the file has been loaded, a tree of Python objects is generated, allowing the user to access nodes.

installation

using pip

install the most recent version of the package (master branch) by running the following command: pip install git+https://github.com/Sauci/pya2l.git@master

or install the last released version of the package by running the following command: pip install pya2l

from source

this package uses ply package. if it is not already installed, install it first.
once the above prerequisite is installed:

  • download the pya2l package
  • unzip it
  • move to the directory containing the setup.py file
  • run the command python setup.py install

note: the above command might require privileged access to succeed.

example of usage

the bellow code snippet shows how properties of a node in an a2l string can be retrieved using this package.

from pya2l.parser import A2lParser as Parser

a2l_string = """
    /begin PROJECT project_name "example project"
        /begin MODULE first_module "first module long identifier"
            /begin CHARACTERISTIC
                example_of_characteristic
                "first characteristic long identifier"
                VALUE
                0
                DAMOS_SST
                0
                first_characteristic_conversion
                -4.5
                12.0
            /end CHARACTERISTIC
        /end MODULE
    /end PROJECT
"""

a2l = Parser(a2l_string)

# get a list of available properties for a specific node.
assert set(a2l.tree.project.get_properties()) == set(['name', 'module', 'header', 'long_identifier'])

# access nodes explicitly.
assert a2l.tree.project.module[0].characteristic[0].name == 'example_of_characteristic'
assert a2l.tree.project.module[0].characteristic[0].lower_limit == -4.5
assert a2l.tree.project.module[0].characteristic[0].upper_limit == 12.0

# access nodes by type.
assert a2l.get_node('CHARACTERISTIC')[0].name == 'example_of_characteristic'

# instantiate custom class for specified node.
from pya2l.parser.grammar.node import Characteristic


class CustomCharacteristic(Characteristic):
    def node(self):
        return 'my custom ' + super(CustomCharacteristic, self).node()


a2l = Parser(a2l_string, CHARACTERISTIC=CustomCharacteristic)

assert isinstance(a2l.tree.project.module[0].characteristic[0], CustomCharacteristic)
assert a2l.tree.project.module[0].characteristic[0].node() == 'my custom CHARACTERISTIC'

# convert node to json-formatted string.
from json import dumps as python_object_to_json_string

a2l = Parser("""
    /begin PROJECT project_name "example project"
        /begin MODULE first_module "first module long identifier"
        /end MODULE
    /end PROJECT
    """)

assert python_object_to_json_string(a2l.tree.project.json, sort_keys=True, indent=4) == """{
    "header": null,
    "long_identifier": "example project",
    "module": [
        {
            "a2ml": null,
            "axis_pts": [],
            "characteristic": [],
            "compu_method": [],
            "compu_tab": [],
            "compu_vtab": [],
            "compu_vtab_range": [],
            "frame": null,
            "function": [],
            "group": [],
            "if_data_module": [],
            "if_data_xcp": null,
            "long_identifier": "first module long identifier",
            "measurement": [],
            "mod_common": null,
            "mod_par": null,
            "name": "first_module",
            "node": "MODULE",
            "record_layout": [],
            "unit": [],
            "user_rights": [],
            "variant_coding": null
        }
    ],
    "name": "project_name",
    "node": "PROJECT"
}"""

limitations

currently, the a2ml-formatted content is only described in the grammar, but the content of the node cannot be accessed as described above.

HitCount

pya2l's People

Contributors

mergify[bot] avatar sauci avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.