Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (pypi.python.org).
Help us improve Python packaging - Donate today!

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.

Release History

Release History

This version
History Node

1.0

Download Files

Download Files

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

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
ncalc-1.0-py3.4.egg (8.3 kB) Copy SHA256 Checksum SHA256 3.4 Egg Jan 31, 2016
ncalc-1.0.tar.gz (3.7 kB) Copy SHA256 Checksum SHA256 Source Jan 31, 2016

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting