Giter VIP home page Giter VIP logo

py-openmath's Introduction

pyopenmath

Build Status Pypi

Python OpenMath 2.0 implementation.

Description

OpenMath is an extensible standard for representing the semantics of mathematical objects.

Installation

pip install openmath

Usage

This package provides an object implementation of OpenMath, and XML parsing/serialization.

See py-scscp for an example of use.

XML Serialization

The modules encoder and decoder provide XML de-serialization for OpenMath objects.

>>> from openmath import encoder, decoder, openmath as om
>>> xml = encoder.encode_xml(om.OMString('hello world')); xml
<Element {http://www.openmath.org/OpenMath}OMSTR at 0x7fcb3cd82708>
>>> b = encoder.encode_bytes(om.OMString('hello world')); b
b'<OMSTR xmlns="http://www.openmath.org/OpenMath">hello world</OMSTR>'
>>> decoder.decode_xml(xml)
OMString('hello world', id=None)
>>> decoder.decode_bytes(b, snippet=True)
OMString('hello world', id=None)

Conversions between Python and OpenMath

This package provides facilities for easy conversions from Python to OpenMath and back. The module convert contains a Converter class, which is used to implements this functionality. For convenienve, an instance of this class, DefaultConverter is provided.

The two functions to_python() and to_openmath() do the conversion as their names suggest, or raise a ValueError if no conversion is known.

By default, a Converter only implements conversions for basic Python types:

  • bools,
  • ints,
  • floats,
  • complex numbers,
  • strings,
  • bytes,
  • lists (recursively),
  • sets (recursively).

Furthermore, any object that defines an __openmath__(self) method will have that method called by to_python.

Finally, this class contains a mechanism for registering converters.

>>> from fractions import Fraction
>>> from openmath import openmath as om
>>> from openmath.convert import DefaultConverter as converter
>>> def to_om_rat(obj):
...     return om.OMApplication(om.OMSymbol('rational', cd='nums1'),
...                             list(map(converter.to_openmath, [obj.numerator, obj.denominator])))
...
>>> def to_py_rat(obj):
...     return Fraction(converter.to_python(obj.arguments[0]), converter.to_python(obj.arguments[1]))
...
>>> converter.register(Fraction, to_om_rat, 'nums1', 'rational', to_py_rat)
>>> omobj = converter.to_openmath(Fraction(5, 6)); omobj
OMApplication(OMSymbol('rational', 'nums1', id=None, cdbase=None), [OMInteger(5, id=None), OMInteger(6, id=None)], id=None, cdbase=None)
>>> converter.to_python(omobj)
Fraction(5, 6)

Contributing

The source code of this project can be found on GitHub. Please use GitHub issues and pull requests to contribute to this project.

Credits

This work is supported by OpenDreamKit.

License

This work is licensed under the MIT License, for details see the LICENSE file.

py-openmath's People

Contributors

tkw1536 avatar nthiery avatar florian-rabe avatar defeo avatar videlec avatar

Watchers

James Cloos avatar  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.