Parse an arythmic formula from a string
Project description
A formula parser for python.
Copyright (C) 2017 Lars van de Kerkhof
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
what it does
Parses simple arythmetic formula’s containing variables. Can then be used to compute values resolving the variables from a context object.
>>> from argparse import Namespace as KeyedObject
>>> from formulaparser import Formula
>>>
>>> context = KeyedObject()
>>> context.four = 4
>>> context.three = 3
>>> deepercontext = KeyedObject()
>>> deepercontext.ten = 10
>>> deepercontext.twelve = 12
>>> context.nextlevel = deepercontext
>>>
>>> Formula("((1 + 2 + 3) + 4 + (3 + 7)) + 5").calculate_value()
25
>>> Formula("4!").calculate_value()
24
>>> Formula("3.287 / 6").calculate_value()
0.5478333333333333
>>> Formula("2 ^ 8").calculate_value()
256
>>> Formula("4 - (-4)").calculate_value()
8
>>> Formula("(4 * 6) - 8 + 7 - 4 + 3").calculate_value()
22
>>> Formula("((1 + four + 3) + nextlevel.ten + (3 + 7)) + 5").calculate_value(context)
33
>>> Formula("((four!) - 6) / nextlevel.twelve").calculate_value(context)
1.5
>>> Formula("2 ^ three").calculate_value(context)
8
>>> Formula("nextlevel.twelve - (-four)").calculate_value(context)
16
>>> Formula("(four * 6) - nextlevel.ten + 7 - 4 + 3").calculate_value(context)
20
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
File details
Details for the file formulaparser-0.0.1.tar.gz
.
File metadata
- Download URL: formulaparser-0.0.1.tar.gz
- Upload date:
- Size: 15.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 090c9cbc3a91d914b2be59b5cd29e80f21ca170ff8272fc6da9c10af2abf5855 |
|
MD5 | 76d11c2344f62e7619f182549c9bfecb |
|
BLAKE2b-256 | ff4228b9be2ee5ed92fa2f6bb5169cc16ad21f929371410c038b89c8508b75a7 |