Giter VIP home page Giter VIP logo

pytopas's Introduction

pytopas

This is an early version of Bruker's TOPAS macro language parser, used by commercial and academic TOPAS code. We have compared two canonical parsing approaches for Python, lark vs. pyparsing, and ended up with pyparsing being more convenient for debugging.

Installation

pip install .

Install package with optional dependencies with pip install -e .[lint,test,release]

Usage

Parse a TOPAS macro language node with:

echo "xdd { 42 }" | topas2json - | json2topas -

More specifically, parse TOPAS input and convert it to JSON with:

import json
from pytopas import TOPASParser

src = "a(b,c)"

tree = TOPASParser.parse(src)
print(json.dumps(tree))

Convert parser's JSON-encoded TOPAS code back into the TOPAS input format:

import json
from pytopas import TOPASParseTree

input_json = """
["topas",
  ["formula",
    ["func_call", "a",
      ["formula", ["p", {"n": ["parameter_name", "b"]}]],
      ["formula", ["p", {"n": ["parameter_name", "c"]}]]]]]
"""
serialized = json.loads(input_json)
src = TOPASParser.reconstruct(serialized)
print(src)

CLI

After installing the package, two command line utilities will be available.

usage: topas2json [-h] [--ignore-warnings] file

Parse TOPAS input and output JSON

positional arguments:
  file               Path to TOPAS file or '-' for stdin input

options:
  -h, --help         show this help message and exit
  --ignore-warnings  Don't print parsing warnings
usage: json2topas [-h] file

Parse JSON input and output TOPAS

positional arguments:
  file        Path to JSON file or '-' for stdin input

options:
  -h, --help  show this help message and exit

License

Author Sergey Korolev, Tilde Materials Informatics

Copyright 2023 BASF SE

BSD 3-Clause

pytopas's People

Contributors

blokhin avatar dependabot[bot] avatar knopki avatar mend-bolt-for-github[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

knopki tilde-lab

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.