Skip to main content

Allows evaluate math expression represented as string.

Project description

This module provides classes that can be used to evaluate math expression contained in string. Most common class is Ncalc. It allows you evaluate expression contains variables ‘x’, ‘y’ and most common functions such as sin, cos, tg, log2, log10, exp, mod, exp and factorial. Furthermore you can build your own ‘calculator’ with your own operator precedences (don’t know why it can be need), functions and variables. To build The Calculator see at Ncalc definition and do it like it: 1. Define new class inherited from Matheval:

class Ncalc(Matheval):

  1. Using __init__ set up given math expression and context namely dictionary of variables and functions:

def __init__(self, mexp): super().__init__(mexp, { ‘x’: 1, ‘y’: 1, ‘factorial’: Ncalc.__factorial, })

  1. Wrap functions as ‘staticmethod’ or functions of module:

@staticmethod def __log10(a): return math.log10(a)

  1. That’s it. :)

Using: >>> from ncalc.eval import Ncalc >>> e = Ncalc(‘2*factorial(5)’) >>> e.eval() 240 >>> e.expression = ‘1+mod(5, 2)+sin(0)’ >>> e.eval() 2.0

P.S. This version can work wrong cause I didn’t test it as it’s necessary.

Project details


Release history Release notifications | RSS feed

This version

1.0

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ncalc-1.0.tar.gz (3.7 kB view hashes)

Uploaded Source

Built Distribution

ncalc-1.0-py3.4.egg (8.3 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page