a calculator command in python - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: General (https://python-forum.io/forum-1.html) +--- Forum: News and Discussions (https://python-forum.io/forum-31.html) +--- Thread: a calculator command in python (/thread-10371.html) |
a calculator command in python - Skaperen - May-18-2018 a simple calculator command in python is rather easy to do. the eval() built-in function makes it nearly trivial. so what about one that does calculations like the decimal module does, but without requiring the expressions code any decimal functions (so a user can do: "calc 0.1+0.1+0.1-0.3" and get 0.0 or 0)? RE: a calculator command in python - wavic - May-18-2018 Hm! Eval? What if the user input is: import subprocess; subprocess.run(['rm', '-rf', '/'], check=True) ?There is a safe way to use eval but it is a bit more coding. You have to pass a dictionary with the allowed functions and methods. RE: a calculator command in python - Skaperen - May-19-2018 a "calc" command is not a "python script in one line" command. let them use "python -c" if that's what they need. i used to do a lot of commands like "python -c 'import math;print math.sqrt(57600)'" for my electrical calculations until i threw together a command named "sqrt". fyi, the "calc" command i made does "from math import *" so i can do a plain "sqrt()", and others like it. maybe i should do that with "cmath", too. and maybe "decimal". oh wait, i wanted "decalc" to just do everything in decimal. |