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):
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, })
Wrap functions as ‘staticmethod’ or functions of module:
@staticmethod def __log10(a): return math.log10(a)
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
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
37f7c76f282130b58f540296d5c8824367f3b6b10b6c6a9b065e8bae5f560c79
|
|
| MD5 |
f7fa0778abde22e503c6149f757a8382
|
|
| BLAKE2b-256 |
acf466d379ea0f716ed714c26c6c9282942ec1d9f62ff30a5655b552bc845b35
|
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: Egg
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9eb5d499bdfb38bbe02ca74a384d5d4a28cc38af425db3f56b739bfeee176264
|
|
| MD5 |
f5d7360dffdb48c5fbcf7927084be7c8
|
|
| BLAKE2b-256 |
8311025ebdc8da5bc36f1ec9ffa4e1b31765ba5911f9e24510e058d1c53a0e9d
|