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 details)

Uploaded Source

Built Distribution

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

Uploaded Source

File details

Details for the file ncalc-1.0.tar.gz.

File metadata

  • Download URL: ncalc-1.0.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for ncalc-1.0.tar.gz
Algorithm Hash digest
SHA256 37f7c76f282130b58f540296d5c8824367f3b6b10b6c6a9b065e8bae5f560c79
MD5 f7fa0778abde22e503c6149f757a8382
BLAKE2b-256 acf466d379ea0f716ed714c26c6c9282942ec1d9f62ff30a5655b552bc845b35

See more details on using hashes here.

File details

Details for the file ncalc-1.0-py3.4.egg.

File metadata

  • Download URL: ncalc-1.0-py3.4.egg
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for ncalc-1.0-py3.4.egg
Algorithm Hash digest
SHA256 9eb5d499bdfb38bbe02ca74a384d5d4a28cc38af425db3f56b739bfeee176264
MD5 f5d7360dffdb48c5fbcf7927084be7c8
BLAKE2b-256 8311025ebdc8da5bc36f1ec9ffa4e1b31765ba5911f9e24510e058d1c53a0e9d

See more details on using hashes here.

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